com.microsoft.services.odata.unittests.WireMockTestClient.java Source code

Java tutorial

Introduction

Here is the source code for com.microsoft.services.odata.unittests.WireMockTestClient.java

Source

/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.microsoft.services.odata.unittests;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;

import static com.github.tomakehurst.wiremock.core.Options.DEFAULT_PORT;
import static com.github.tomakehurst.wiremock.http.MimeType.JSON;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_OK;

public class WireMockTestClient {

    private static final String LOCAL_WIREMOCK_ROOT = "http://%s:%d%s";
    private static final String LOCAL_WIREMOCK_NEW_RESPONSE_URL = "http://%s:%d/__admin/mappings/new";
    private static final String LOCAL_WIREMOCK_RESET_URL = "http://%s:%d/__admin/reset";
    private static final String LOCAL_WIREMOCK_RESET_DEFAULT_MAPPINS_URL = "http://%s:%d/__admin/mappings/reset";

    private int port;
    private String address;

    public WireMockTestClient(int port, String address) {
        this.port = port;
        this.address = address;
    }

    public WireMockTestClient(int port) {
        this(port, "localhost");
    }

    public WireMockTestClient() {
        this(8080);
    }

    private String mockServiceUrlFor(String path) {
        return String.format(LOCAL_WIREMOCK_ROOT, address, port, path);
    }

    private String newMappingUrl() {
        return String.format(LOCAL_WIREMOCK_NEW_RESPONSE_URL, address, port);
    }

    private String resetUrl() {
        return String.format(LOCAL_WIREMOCK_RESET_URL, address, port);
    }

    private String resetDefaultMappingsUrl() {
        return String.format(LOCAL_WIREMOCK_RESET_DEFAULT_MAPPINS_URL, address, port);
    }

    public WireMockResponse get(String url, TestHttpHeader... headers) {
        String actualUrl = URI.create(url).isAbsolute() ? url : mockServiceUrlFor(url);
        HttpUriRequest httpRequest = new HttpGet(actualUrl);
        return executeMethodAndCovertExceptions(httpRequest, headers);
    }

    public WireMockResponse getViaProxy(String url) {
        return getViaProxy(url, DEFAULT_PORT);
    }

    public WireMockResponse getViaProxy(String url, int proxyPort) {
        URI targetUri = URI.create(url);
        HttpHost proxy = new HttpHost(address, proxyPort, targetUri.getScheme());
        HttpClient httpClientUsingProxy = HttpClientBuilder.create().disableAuthCaching().disableAutomaticRetries()
                .disableCookieManagement().disableRedirectHandling().setProxy(proxy).build();

        try {
            HttpHost target = new HttpHost(targetUri.getHost(), targetUri.getPort(), targetUri.getScheme());
            HttpGet req = new HttpGet(
                    targetUri.getPath() + (isNullOrEmpty(targetUri.getQuery()) ? "" : "?" + targetUri.getQuery()));
            req.removeHeaders("Host");

            System.out.println("executing request to " + targetUri + "(" + target + ") via " + proxy);
            HttpResponse httpResponse = httpClientUsingProxy.execute(target, req);
            return new WireMockResponse(httpResponse);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    public WireMockResponse put(String url, TestHttpHeader... headers) {
        HttpUriRequest httpRequest = new HttpPut(mockServiceUrlFor(url));
        return executeMethodAndCovertExceptions(httpRequest, headers);
    }

    public WireMockResponse putWithBody(String url, String body, String contentType, TestHttpHeader... headers)
            throws UnsupportedEncodingException {
        HttpPut httpPut = new HttpPut(mockServiceUrlFor(url));
        return requestWithBody(httpPut, body, contentType, headers);
    }

    public WireMockResponse patchWithBody(String url, String body, String contentType, TestHttpHeader... headers)
            throws UnsupportedEncodingException {
        HttpPatch httpPatch = new HttpPatch(mockServiceUrlFor(url));
        return requestWithBody(httpPatch, body, contentType, headers);
    }

    private WireMockResponse requestWithBody(HttpEntityEnclosingRequestBase request, String body,
            String contentType, TestHttpHeader... headers) throws UnsupportedEncodingException {
        request.setEntity(new StringEntity(body, ContentType.create(contentType, "utf-8").toString()));
        return executeMethodAndCovertExceptions(request, headers);
    }

    public WireMockResponse postWithBody(String url, String body, String bodyMimeType, String bodyEncoding)
            throws UnsupportedEncodingException {
        return post(url, new StringEntity(body, ContentType.create(bodyMimeType, bodyEncoding).toString()));
    }

    public WireMockResponse postWithChunkedBody(String url, byte[] body) {
        return post(url, new InputStreamEntity(new ByteArrayInputStream(body), -1));
    }

    public WireMockResponse post(String url, HttpEntity entity) {
        HttpPost httpPost = new HttpPost(mockServiceUrlFor(url));
        httpPost.setEntity(entity);
        return executeMethodAndCovertExceptions(httpPost);
    }

    public WireMockResponse patchWithBody(String url, String body, String bodyMimeType, String bodyEncoding)
            throws UnsupportedEncodingException {
        return patch(url, new StringEntity(body, ContentType.create(bodyMimeType, bodyEncoding).toString()));
    }

    public WireMockResponse patch(String url, HttpEntity entity) {
        HttpPatch httpPatch = new HttpPatch(mockServiceUrlFor(url));
        httpPatch.setEntity(entity);
        return executeMethodAndCovertExceptions(httpPatch);
    }

    public void addResponse(String responseSpecJson) {
        int status = postJsonAndReturnStatus(newMappingUrl(), responseSpecJson);
        if (status != HTTP_CREATED) {
            throw new RuntimeException("Returned status code was " + status);
        }
    }

    public void resetMappings() {
        int status = postEmptyBodyAndReturnStatus(resetUrl());
        if (status != HTTP_OK) {
            throw new RuntimeException("Returned status code was " + status);
        }
    }

    public void resetDefaultMappings() {
        int status = postEmptyBodyAndReturnStatus(resetDefaultMappingsUrl());
        if (status != HTTP_OK) {
            throw new RuntimeException("Returned status code was " + status);
        }
    }

    private int postJsonAndReturnStatus(String url, String json) {
        HttpPost post = new HttpPost(url);
        try {
            if (json != null) {
                post.setEntity(new StringEntity(json, ContentType.create(JSON.toString(), "utf-8").toString()));
            }
            HttpResponse httpResponse = httpClient().execute(post);
            return httpResponse.getStatusLine().getStatusCode();
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private int postEmptyBodyAndReturnStatus(String url) {
        return postJsonAndReturnStatus(url, null);
    }

    private WireMockResponse executeMethodAndCovertExceptions(HttpUriRequest httpRequest,
            TestHttpHeader... headers) {
        try {
            for (TestHttpHeader header : headers) {
                httpRequest.addHeader(header.getName(), header.getValue());
            }
            HttpResponse httpResponse = httpClient().execute(httpRequest);
            return new WireMockResponse(httpResponse);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    private static HttpClient httpClient() {
        return HttpClientBuilder.create().disableAuthCaching().disableAutomaticRetries().disableCookieManagement()
                .disableRedirectHandling().build();
    }
}