001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.client;
007
008import static org.mockserver.model.HttpRequest.request;
009import static org.mockserver.model.HttpResponse.response;
010
011import java.net.URI;
012
013import org.apache.http.HttpStatus;
014import org.mockserver.client.MockServerClient;
015
016/**
017 * Expectations for the Mock Http Server
018 *
019 * @author esm
020 */
021public final class MockHttpExpectations {
022
023    /**
024     * The TCP host the mock HTTP server listens to.
025     */
026    final static String host = "localhost";
027
028    /**
029     * The TCP port the mock HTTP server listens to.
030     */
031    static String port;
032
033    final static class Uris {
034        int statusCode;
035        String suffix;
036        String path;
037
038        Uris(final int statusCode) {
039            this.statusCode = statusCode;
040            this.path = "/uri/" + statusCode;
041        }
042
043        Uris(final int statusCode, final String suffix) {
044            this.statusCode = statusCode;
045            this.suffix = suffix;
046            this.path = "/uri/" + statusCode + suffix;
047        }
048
049        URI asUri() {
050            return URI.create("http://" + host + ":" + port + path);
051        }
052
053        @Override
054        public String toString() {
055            return asUri().toString();
056        }
057    }
058
059    public final static class SupportedUris {
060
061        /**
062         * A request URI that will return a 500.
063         */
064        final Uris uri500 = new Uris(500);
065
066        /**
067         * A request URI that will return a 201.
068         */
069        final Uris uri201 = new Uris(201);
070
071        /**
072         * A request URI that will return a 200.
073         */
074        final Uris uri200 = new Uris(200);
075
076        /**
077         * A request URI that will return a 200 with a text response body.
078         */
079        final public Uris uri200RespBody = new Uris(200, "RespBody");
080    }
081
082    /**
083     *
084     * @param mockServerClient the mock HTTP server to be configured
085     * @param port the port the mock HTTP server is running on
086     */
087    public void initializeExpectations(final MockServerClient mockServerClient, final int port) {
088
089        MockHttpExpectations.port = String.valueOf(port);
090
091        mockServerClient.when(
092                request()
093                        .withPath("/uri/500")
094        ).respond(
095                response()
096                        .withStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
097        );
098
099        mockServerClient.when(
100                request()
101                        .withPath("/uri/201")
102        ).respond(
103                response()
104                        .withStatusCode(HttpStatus.SC_CREATED)
105        );
106
107
108        mockServerClient.when(
109                request()
110                        .withPath("/uri/200")
111        ).respond(
112                response()
113                        .withStatusCode(HttpStatus.SC_OK)
114        );
115
116        mockServerClient.when(
117                request()
118                        .withPath("/uri/200RespBody")
119        ).respond(
120                response("Response body")
121                        .withStatusCode(HttpStatus.SC_OK)
122        );
123    }
124
125}