2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.restclient;
23 import static org.junit.Assert.assertNotNull;
24 import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
25 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
27 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.charset.StandardCharsets;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
41 import org.apache.commons.io.IOUtils;
42 import org.springframework.core.io.Resource;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.stereotype.Component;
50 import org.springframework.test.web.client.ExpectedCount;
51 import org.springframework.test.web.client.MockRestServiceServer;
52 import org.springframework.util.MultiValueMap;
53 import org.springframework.web.client.RestTemplate;
56 public class MockRestClient extends RestClient {
58 private RestTemplate restTemplate;
59 private MockRestServiceServer mockRestServiceServer;
61 String fileName = "mockrequests";
63 public MockRestClient(String fileName) {
65 * List<MockRestServiceServer> mockedAAIRequests = new ArrayList<>(aaiRequests.size());
67 List<MockRestServiceServer> mockedAAIRequests = new ArrayList<>();
69 restTemplate = new RestTemplate();
71 * MockRestServiceServer server = MockRestServiceServer
72 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
74 * server.expect(MockRestRequestMatchers.requestTo(url))
75 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
78 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
79 // when(mockBuilder.build()).thenReturn(restTemplate);
81 JsonObject payload = null;
83 payload = getPayload(fileName + ".json");
84 } catch (IOException e) {
88 JsonArray mockUris = payload.getAsJsonArray("mock-uri");
90 mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
91 String url = "https://localhost:8447/aai/v14";
93 * mockRestServiceServer.expect(requestTo(url))
94 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
97 for (int i = 0; i < mockUris.size(); i++) {
98 String responseFile = mockUris.get(i).getAsJsonObject().get("response-file").getAsString();
99 String contentTypeValue = mockUris.get(i).getAsJsonObject().get("content").getAsString();
101 String uri = mockUris.get(i).getAsJsonObject().get("aai-uri").getAsString();
103 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(responseFile);
104 String responseBody = null;
106 responseBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
107 } catch (IOException e) {
111 mockRestServiceServer.expect(ExpectedCount.manyTimes(), requestTo(url + uri))
112 .andExpect(method(HttpMethod.GET)).andExpect(content().contentType(contentTypeValue))
113 .andRespond(withStatus(HttpStatus.OK).body(responseBody.toString())
114 .contentType(MediaType.valueOf(contentTypeValue)));
119 public MockRestClient() {
121 restTemplate = new RestTemplate();
123 * MockRestServiceServer server = MockRestServiceServer
124 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
126 * server.expect(MockRestRequestMatchers.requestTo(url))
127 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
130 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
131 // when(mockBuilder.build()).thenReturn(restTemplate);
133 JsonObject payload = null;
135 payload = getPayload(fileName + ".json");
136 } catch (IOException e) {
140 JsonArray mockUris = payload.getAsJsonArray("mock-uri");
142 mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
143 String url = "https://localhost:8447/aai/v14";
145 * mockRestServiceServer.expect(requestTo(url))
146 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
149 for (int i = 0; i < mockUris.size(); i++) {
150 String responseFile = mockUris.get(i).getAsJsonObject().get("response-file").getAsString();
151 String contentTypeValue = mockUris.get(i).getAsJsonObject().get("content").getAsString();
153 String uri = mockUris.get(i).getAsJsonObject().get("aai-uri").getAsString();
155 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(responseFile);
156 String responseBody = null;
158 responseBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
159 } catch (IOException e) {
163 mockRestServiceServer.expect(ExpectedCount.manyTimes(), requestTo(url + uri))
164 .andExpect(method(HttpMethod.GET)).andExpect(content().contentType(contentTypeValue))
165 .andRespond(withStatus(HttpStatus.OK).body(responseBody.toString())
166 .contentType(MediaType.valueOf(contentTypeValue)));
172 public JsonObject getTestDetails(String fileName) throws IOException {
174 JsonObject payload = getPayload(fileName);
179 public JsonObject getPayload(String filename) throws IOException {
180 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename);
182 String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
183 String message = String.format("Unable to find the %s in src/test/resources", filename);
184 assertNotNull(message, inputStream);
186 JsonObject payload = JsonParser.parseString(result).getAsJsonObject();
191 public ResponseEntity<String> execute(String uri, HttpMethod method, Map<String, String> headers, String body) {
193 String url = "https://localhost:8447/aai/v14/" + uri;
196 * MockRestServiceServer server = MockRestServiceServer
197 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
199 * server.expect(MockRestRequestMatchers.requestTo(url))
200 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
203 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
204 // when(mockBuilder.build()).thenReturn(restTemplate);
207 * MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
208 * server.expect(requestTo(url))
209 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
210 * return new ResponseEntity("blah", HttpStatus.OK);
211 * server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl +
212 * aaiRequests.get(i).get("aai-uri").asText())))
213 * .andExpect(method(HttpMethod.GET))
214 * .andExpect(content().contentType(MediaType.APPLICATION_JSON))
215 * .andRespond(withStatus(HttpStatus.OK).body(aaiResponses.get(i).toString()).contentType(MediaType.
216 * APPLICATION_JSON));
219 HttpHeaders headersMap = new HttpHeaders();
221 headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
222 headersMap.setContentType(MediaType.APPLICATION_JSON);
223 headersMap.add("Real-Time", "true");
224 headersMap.add("X-FromAppId", "JUNIT");
225 headersMap.add("X-TransactionId", "JUNIT");
227 HttpEntity<String> httpEntity = new HttpEntity(headers);
229 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
231 // mockRestServiceServer.verify();
232 return responseEntity;
236 public ResponseEntity<Resource> executeResource(String uri, HttpMethod method, Map<String, String> headers,
239 String url = "https://localhost:8447/aai/v14/" + uri;
242 * MockRestServiceServer server = MockRestServiceServer
243 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
245 * server.expect(MockRestRequestMatchers.requestTo(url))
246 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
249 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
250 // when(mockBuilder.build()).thenReturn(restTemplate);
253 * MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
254 * server.expect(requestTo(url))
255 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
256 * return new ResponseEntity("blah", HttpStatus.OK);
257 * server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl +
258 * aaiRequests.get(i).get("aai-uri").asText())))
259 * .andExpect(method(HttpMethod.GET))
260 * .andExpect(content().contentType(MediaType.APPLICATION_JSON))
261 * .andRespond(withStatus(HttpStatus.OK).body(aaiResponses.get(i).toString()).contentType(MediaType.
262 * APPLICATION_JSON));
265 HttpHeaders headersMap = new HttpHeaders();
267 headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
268 headersMap.setContentType(MediaType.APPLICATION_JSON);
269 headersMap.add("Real-Time", "true");
270 headersMap.add("X-FromAppId", "JUNIT");
271 headersMap.add("X-TransactionId", "JUNIT");
273 HttpEntity<String> httpEntity = new HttpEntity(headers);
275 ResponseEntity<Resource> responseEntity =
276 restTemplate.exchange(url, HttpMethod.GET, httpEntity, Resource.class);
278 // mockRestServiceServer.verify();
279 return responseEntity;
283 public RestTemplate getRestTemplate() {
284 RestTemplate restTemplate = null;
288 public String getBaseUrl() {
292 protected MultiValueMap<String, String> getHeaders(Map<String, String> headers) {