1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.hc.core5.testing.framework; 29 30 import java.util.Map; 31 32 public class ClientTestingAdapter { 33 /** 34 * This adapter will perform the HTTP request and return the response in the 35 * expected format. 36 */ 37 protected ClientPOJOAdapter adapter; 38 39 /* 40 * The following is not expected to be changed to true, but it is to highlight 41 * where the execute method can call the requestHandler's assertNothingThrown() 42 * method if desired. Since this adapter's execute method does not check 43 * the response, there is no need to call it. 44 */ 45 protected boolean callAssertNothingThrown; 46 47 public ClientTestingAdapter() { 48 } 49 50 public ClientTestingAdapter(final ClientPOJOAdapter adapter) { 51 this.adapter = adapter; 52 } 53 54 /** 55 * See the documentation for the same method in {@link ClientPOJOAdapter}. This 56 * method will typically call it. However, this method also has access to the 57 * test's response expectations if that is needed for some reason. Furthermore, 58 * this method also has access to the {@link TestingFrameworkRequestHandler} so 59 * it can optionally call assertNothingThrown() before checking the response 60 * further. It is optional because the test framework will call it later. 61 * 62 * @param defaultURI See execute method of {@link ClientPOJOAdapter}. 63 * @param request See execute method of {@link ClientPOJOAdapter}. 64 * @param requestHandler The request handler that checks the received HTTP request 65 * with the request that was intended. If there is a 66 * mismatch of expectations, then the requestHandler will 67 * throw an exception. If this execute method does not want 68 * to make further checks of the response in the case 69 * the responseHandler threw, then the assertNothingThrown() 70 * method should be called before doing further checks. 71 * @param responseExpectations The response expectations of the test. 72 * @return See return of the execute method of {@link ClientPOJOAdapter}. 73 * @throws TestingFrameworkException in the case of a problem. 74 */ 75 public Map<String, Object> execute(final String defaultURI, final Map<String, Object> request, 76 final TestingFrameworkRequestHandler requestHandler, 77 final Map<String, Object> responseExpectations) throws TestingFrameworkException { 78 79 try { 80 if (adapter == null) { 81 throw new TestingFrameworkException("adapter cannot be null"); 82 } 83 // Call the adapter's execute method to actually make the HTTP request. 84 final Map<String, Object> response = adapter.execute(defaultURI, request); 85 86 /* 87 * Adapters may call assertNothingThrown() if they would like. This would be to 88 * make sure the following code is not executed in the event there was something 89 * thrown in the request handler. 90 * 91 * Otherwise, the framework will call it when this method returns. So, it is 92 * optional. 93 */ 94 if (callAssertNothingThrown) { 95 if (requestHandler == null) { 96 throw new TestingFrameworkException("requestHandler cannot be null"); 97 } 98 requestHandler.assertNothingThrown(); 99 } 100 101 return response; 102 } catch (final TestingFrameworkException e) { 103 throw e; 104 } catch (final Exception ex) { 105 throw new TestingFrameworkException(ex); 106 } 107 } 108 109 /** 110 * See the documentation for the same method in {@link ClientPOJOAdapter}. 111 */ 112 public boolean isRequestSupported(final Map<String, Object> request) { 113 return (adapter == null) || adapter.checkRequestSupport(request) == null; 114 } 115 116 /** 117 * See the documentation for the same method in {@link ClientPOJOAdapter}. 118 */ 119 public Map<String, Object> modifyRequest(final Map<String, Object> request) { 120 return (adapter == null) ? request : adapter.modifyRequest(request); 121 } 122 123 /** 124 * Generally a test's response expectations should not need to be modified, but 125 * if a particular HTTP client (such as Groovy's RESTClient which uses HttpClient) 126 * needs to modify the response expectations, it should do so here. After this 127 * method returns, the {@link TestingFrameworkRequestHandler} is sent the 128 * expectations so the request handler will return a response that matches the 129 * expectations. When the HTTP response is obtained, the received response 130 * is matched against the expectations. 131 * 132 * @param request for the format, see the documentation for {@link ClientPOJOAdapter}. 133 * @param responseExpectations for the format, see the documentation for {@link ClientPOJOAdapter}. 134 * @return the same or modified response expectations. 135 */ 136 public Map<String, Object> modifyResponseExpectations(final Map<String, Object> request, 137 final Map<String, Object> responseExpectations) { 138 return responseExpectations; 139 } 140 141 /** 142 * Getter for the {@link ClientPOJOAdapter} that is actually used to make the 143 * HTTP request. 144 * 145 * @return the {@link ClientPOJOAdapter}. 146 */ 147 public ClientPOJOAdapter getClientPOJOAdapter() { 148 return adapter; 149 } 150 151 }