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 java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
37 import org.apache.commons.io.IOUtils;
38 import org.springframework.core.io.Resource;
39 import org.springframework.http.HttpEntity;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.stereotype.Component;
46 import org.springframework.test.web.client.ExpectedCount;
47 import org.springframework.test.web.client.MockRestServiceServer;
48 import org.springframework.util.MultiValueMap;
49 import org.springframework.web.client.RestTemplate;
51 import com.google.gson.JsonArray;
52 import com.google.gson.JsonObject;
53 import com.google.gson.JsonParser;
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 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 httpEntity = new HttpEntity(headers);
232 ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
234 // mockRestServiceServer.verify();
235 return responseEntity;
239 public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body) {
241 String url = "https://localhost:8447/aai/v14/" + uri;
244 * MockRestServiceServer server = MockRestServiceServer
245 * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
247 * server.expect(MockRestRequestMatchers.requestTo(url))
248 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
251 // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
252 // when(mockBuilder.build()).thenReturn(restTemplate);
255 * MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
256 * server.expect(requestTo(url))
257 * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
258 * return new ResponseEntity("blah", HttpStatus.OK);
259 * server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl +
260 * aaiRequests.get(i).get("aai-uri").asText())))
261 * .andExpect(method(HttpMethod.GET))
262 * .andExpect(content().contentType(MediaType.APPLICATION_JSON))
263 * .andRespond(withStatus(HttpStatus.OK).body(aaiResponses.get(i).toString()).contentType(MediaType.
264 * APPLICATION_JSON));
267 HttpHeaders headersMap = new HttpHeaders();
269 headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
270 headersMap.setContentType(MediaType.APPLICATION_JSON);
271 headersMap.add("Real-Time", "true");
272 headersMap.add("X-FromAppId", "JUNIT");
273 headersMap.add("X-TransactionId", "JUNIT");
275 HttpEntity httpEntity = new HttpEntity(headers);
277 ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Resource.class);
279 // mockRestServiceServer.verify();
280 return responseEntity;
284 public RestTemplate getRestTemplate() {
285 RestTemplate restTemplate = null;
289 public String getBaseUrl() {
293 protected MultiValueMap<String, String> getHeaders(Map<String, String> headers) {