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 // InputStream inputStream = new FileInputStream(filename);
184 String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
185 String message = String.format("Unable to find the %s in src/test/resources", filename);
186 assertNotNull(message, inputStream);
188 JsonParser parser = new JsonParser();
189 JsonObject payload = parser.parse(result).getAsJsonObject();
194 public ResponseEntity<String> execute(String uri, HttpMethod method, Map<String, String> headers, String body) {
196 String url = "https://localhost:8447/aai/v14/" + uri;
199 * MockRestServiceServer server = MockRestServiceServer
200 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
202 * server.expect(MockRestRequestMatchers.requestTo(url))
203 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
206 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
207 // when(mockBuilder.build()).thenReturn(restTemplate);
210 * MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
211 * server.expect(requestTo(url))
212 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
213 * return new ResponseEntity("blah", HttpStatus.OK);
214 * server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl +
215 * aaiRequests.get(i).get("aai-uri").asText())))
216 * .andExpect(method(HttpMethod.GET))
217 * .andExpect(content().contentType(MediaType.APPLICATION_JSON))
218 * .andRespond(withStatus(HttpStatus.OK).body(aaiResponses.get(i).toString()).contentType(MediaType.
219 * APPLICATION_JSON));
222 HttpHeaders headersMap = new HttpHeaders();
224 headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
225 headersMap.setContentType(MediaType.APPLICATION_JSON);
226 headersMap.add("Real-Time", "true");
227 headersMap.add("X-FromAppId", "JUNIT");
228 headersMap.add("X-TransactionId", "JUNIT");
230 HttpEntity<String> httpEntity = new HttpEntity(headers);
232 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
234 // mockRestServiceServer.verify();
235 return responseEntity;
239 public ResponseEntity<Resource> executeResource(String uri, HttpMethod method, Map<String, String> headers,
242 String url = "https://localhost:8447/aai/v14/" + uri;
245 * MockRestServiceServer server = MockRestServiceServer
246 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
248 * server.expect(MockRestRequestMatchers.requestTo(url))
249 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
252 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
253 // when(mockBuilder.build()).thenReturn(restTemplate);
256 * MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
257 * server.expect(requestTo(url))
258 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
259 * return new ResponseEntity("blah", HttpStatus.OK);
260 * server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl +
261 * aaiRequests.get(i).get("aai-uri").asText())))
262 * .andExpect(method(HttpMethod.GET))
263 * .andExpect(content().contentType(MediaType.APPLICATION_JSON))
264 * .andRespond(withStatus(HttpStatus.OK).body(aaiResponses.get(i).toString()).contentType(MediaType.
265 * APPLICATION_JSON));
268 HttpHeaders headersMap = new HttpHeaders();
270 headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
271 headersMap.setContentType(MediaType.APPLICATION_JSON);
272 headersMap.add("Real-Time", "true");
273 headersMap.add("X-FromAppId", "JUNIT");
274 headersMap.add("X-TransactionId", "JUNIT");
276 HttpEntity<String> httpEntity = new HttpEntity(headers);
278 ResponseEntity<Resource> responseEntity =
279 restTemplate.exchange(url, HttpMethod.GET, httpEntity, Resource.class);
281 // mockRestServiceServer.verify();
282 return responseEntity;
286 public RestTemplate getRestTemplate() {
287 RestTemplate restTemplate = null;
291 public String getBaseUrl() {
295 protected MultiValueMap<String, String> getHeaders(Map<String, String> headers) {