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