All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.hsac.fitnesse.fixture.slim.MockXmlServerSetup Maven / Gradle / Ivy

package nl.hsac.fitnesse.fixture.slim;

import nl.hsac.fitnesse.fixture.util.HttpServer;
import nl.hsac.fitnesse.fixture.util.MockXmlHttpResponseSequence;
import nl.hsac.fitnesse.fixture.util.XmlHttpResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Fixture allowing you to host a SOAP server inside FitNesse. This mock server can then be called by a system you
 * are testing so you can verify that (and what) calls are made.
 * The idea is that you first use this fixture to setup the responses the mock server will generated and obtain the
 * URL the service is listening on. This URL is then passed to the system being tested which is expected to make
 * one or more calls, in response to which it will receive the configured responses.
 * Afterwards you can use this fixture to shut down the mock server.
 */
public class MockXmlServerSetup extends SlimFixture {
    private static final Map> SERVERS = new HashMap>();
    public static final String DEFAULT_PATH = "/FitNesseMock";
    private final String path;
    private final HttpServer mockServer;

    public static HttpServer getMockServer(String aPath) {
        HttpServer server = SERVERS.get(aPath);
        if (server == null) {
            throw new SlimFixtureException(false, "No server created at path: " + aPath);
        }
        return server;
    }

    public static void removeMockServer(String aPath) {
        HttpServer server = SERVERS.remove(aPath);
        if (server != null) {
            server.stopServer();
        }
    }

    public static List getResponses(String aPath) {
        return getMockServer(aPath).getResponse().getResponseList();
    }

    public MockXmlServerSetup() {
        this(DEFAULT_PATH);
    }

    public MockXmlServerSetup(String aPath) {
        path = cleanupValue(aPath);
        if (SERVERS.containsKey(path)) {
            mockServer = getMockServer(path);
        } else {
            mockServer = createMockServer(path);
            SERVERS.put(path, mockServer);
        }
    }

    protected HttpServer createMockServer(String aPath) {
        return new HttpServer(aPath, new MockXmlHttpResponseSequence());
    }

    public void addResponseFor(String aResponse, String aRequest) {
        addResponseImpl(aResponse, aRequest);
    }

    public void addResponse(String aResponse) {
        addResponseImpl(aResponse, null);
    }

    public void addResponseWithStatus(String aResponse, int aStatusCode) {
        XmlHttpResponse response = addResponseImpl(aResponse, null);
        response.setStatusCode(aStatusCode);
    }

    protected XmlHttpResponse addResponseImpl(String aResponse, String aRequest) {
        String responseBody = cleanupBody(aResponse);
        String request = cleanupValue(aRequest);
        return getResponse().addResponse(responseBody, request);
    }

    protected String cleanupBody(String body) {
        return getEnvironment().getHtmlCleaner().cleanupPreFormatted(body);
    }

    /**
     * @return url this server is listening on.
     */
    public String getMockServerUrl() {
        return "http:/" + mockServer.getAddress() + path;
    }

    public boolean verifyAllResponsesServed() {
        String not = getResponse().getMissingRequestsMessage();
        if (not != null) {
            throw new SlimFixtureException(false, not);
        }
        return true;
    }

    public boolean verifyNoExtraRequests() {
        String extra = getResponse().getExtraRequestsMessage();
        if (extra != null) {
            throw new SlimFixtureException(false, extra);
        }
        return true;
    }

    public void stop() {
        removeMockServer(path);
    }

    protected List getResponseList() {
        return getResponse().getResponseList();
    }

    protected MockXmlHttpResponseSequence getResponse() {
        return mockServer.getResponse();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy