001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.client; 017 018import org.apache.http.HttpStatus; 019import org.mockserver.client.server.MockServerClient; 020 021import java.net.URI; 022 023import static org.mockserver.model.HttpRequest.request; 024import static org.mockserver.model.HttpResponse.response; 025 026/** 027 * Expectations for the Mock Http Server 028 * 029 * @author esm 030 */ 031public final class MockHttpExpectations { 032 033 /** 034 * The TCP host the mock HTTP server listens to. 035 */ 036 final static String host = "localhost"; 037 038 /** 039 * The TCP port the mock HTTP server listens to. 040 */ 041 static String port; 042 043 final static class Uris { 044 int statusCode; 045 String path; 046 047 Uris(final int statusCode) { 048 this.statusCode = statusCode; 049 this.path = "/uri/" + statusCode; 050 } 051 052 URI asUri() { 053 return URI.create("http://" + host + ":" + port + path); 054 } 055 056 @Override 057 public String toString() { 058 return asUri().toString(); 059 } 060 } 061 062 final static class SupportedUris { 063 064 /** 065 * A request URI that will return a 500. 066 */ 067 final Uris uri500 = new Uris(500); 068 069 /** 070 * A request URI that will return a 201. 071 */ 072 final Uris uri201 = new Uris(201); 073 074 /** 075 * A request URI that will return a 200. 076 */ 077 final Uris uri200 = new Uris(200); 078 079 } 080 081 /** 082 * 083 * @param mockServerClient the mock HTTP server to be configured 084 * @param port the port the mock HTTP server is running on 085 */ 086 public void initializeExpectations(final MockServerClient mockServerClient, final int port) { 087 088 MockHttpExpectations.port = String.valueOf(port); 089 090 mockServerClient.when( 091 request() 092 .withPath("/uri/500") 093 ).respond( 094 response() 095 .withStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) 096 ); 097 098 mockServerClient.when( 099 request() 100 .withPath("/uri/201") 101 ).respond( 102 response() 103 .withStatusCode(HttpStatus.SC_CREATED) 104 ); 105 106 107 mockServerClient.when( 108 request() 109 .withPath("/uri/200") 110 ).respond( 111 response() 112 .withStatusCode(HttpStatus.SC_OK) 113 ); 114 115 } 116 117}