Remove the apache commons-lang dependency in aai-common
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / restclient / MockRestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.restclient;
22
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;
28
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;
35 import java.util.Map;
36
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;
50
51 import com.google.gson.JsonArray;
52 import com.google.gson.JsonObject;
53 import com.google.gson.JsonParser;
54
55 @Component
56 public class MockRestClient extends RestClient {
57
58     private RestTemplate restTemplate;
59     private MockRestServiceServer mockRestServiceServer;
60
61     String fileName = "mockrequests";
62
63     public MockRestClient(String fileName) {
64         /*
65          * List<MockRestServiceServer> mockedAAIRequests = new ArrayList<>(aaiRequests.size());
66          */
67         List<MockRestServiceServer> mockedAAIRequests = new ArrayList<>();
68
69         restTemplate = new RestTemplate();
70         /*
71          * MockRestServiceServer server = MockRestServiceServer
72          * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
73          * .build();
74          * server.expect(MockRestRequestMatchers.requestTo(url))
75          * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
76          */
77
78         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
79         // when(mockBuilder.build()).thenReturn(restTemplate);
80
81         JsonObject payload = null;
82         try {
83             payload = getPayload(fileName + ".json");
84         } catch (IOException e) {
85             e.printStackTrace();
86         }
87
88         JsonArray mockUris = payload.getAsJsonArray("mock-uri");
89
90         mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
91         String url = "https://localhost:8447/aai/v14";
92         /*
93          * mockRestServiceServer.expect(requestTo(url))
94          * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
95          */
96
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();
100
101             String uri = mockUris.get(i).getAsJsonObject().get("aai-uri").getAsString();
102
103             InputStream inputStream = getClass().getClassLoader().getResourceAsStream(responseFile);
104             String responseBody = null;
105             try {
106                 responseBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
107             } catch (IOException e) {
108                 e.printStackTrace();
109             }
110
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)));
115
116         }
117     }
118
119     public MockRestClient() {
120
121         restTemplate = new RestTemplate();
122         /*
123          * MockRestServiceServer server = MockRestServiceServer
124          * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
125          * .build();
126          * server.expect(MockRestRequestMatchers.requestTo(url))
127          * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
128          */
129
130         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
131         // when(mockBuilder.build()).thenReturn(restTemplate);
132
133         JsonObject payload = null;
134         try {
135             payload = getPayload(fileName + ".json");
136         } catch (IOException e) {
137             e.printStackTrace();
138         }
139
140         JsonArray mockUris = payload.getAsJsonArray("mock-uri");
141
142         mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
143         String url = "https://localhost:8447/aai/v14";
144         /*
145          * mockRestServiceServer.expect(requestTo(url))
146          * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
147          */
148
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();
152
153             String uri = mockUris.get(i).getAsJsonObject().get("aai-uri").getAsString();
154
155             InputStream inputStream = getClass().getClassLoader().getResourceAsStream(responseFile);
156             String responseBody = null;
157             try {
158                 responseBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
159             } catch (IOException e) {
160                 e.printStackTrace();
161             }
162
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)));
167
168         }
169
170     }
171
172     public JsonObject getTestDetails(String fileName) throws IOException {
173
174         JsonObject payload = getPayload(fileName);
175
176         return payload;
177     }
178
179     public JsonObject getPayload(String filename) throws IOException {
180         InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename);
181
182         // InputStream inputStream = new FileInputStream(filename);
183
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);
187
188         JsonParser parser = new JsonParser();
189         JsonObject payload = parser.parse(result).getAsJsonObject();
190         return payload;
191     }
192
193     @Override
194     public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body) {
195
196         String url = "https://localhost:8447/aai/v14/" + uri;
197
198         /*
199          * MockRestServiceServer server = MockRestServiceServer
200          * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
201          * .build();
202          * server.expect(MockRestRequestMatchers.requestTo(url))
203          * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
204          */
205
206         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
207         // when(mockBuilder.build()).thenReturn(restTemplate);
208
209         /*
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));
220          */
221
222         HttpHeaders headersMap = new HttpHeaders();
223
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");
229
230         HttpEntity httpEntity = new HttpEntity(headers);
231
232         ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
233
234         // mockRestServiceServer.verify();
235         return responseEntity;
236     }
237
238     @Override
239     public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body) {
240
241         String url = "https://localhost:8447/aai/v14/" + uri;
242
243         /*
244          * MockRestServiceServer server = MockRestServiceServer
245          * .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
246          * .build();
247          * server.expect(MockRestRequestMatchers.requestTo(url))
248          * .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
249          */
250
251         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
252         // when(mockBuilder.build()).thenReturn(restTemplate);
253
254         /*
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));
265          */
266
267         HttpHeaders headersMap = new HttpHeaders();
268
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");
274
275         HttpEntity httpEntity = new HttpEntity(headers);
276
277         ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Resource.class);
278
279         // mockRestServiceServer.verify();
280         return responseEntity;
281     }
282
283     @Override
284     public RestTemplate getRestTemplate() {
285         RestTemplate restTemplate = null;
286         return restTemplate;
287     }
288
289     public String getBaseUrl() {
290         return "";
291     }
292
293     protected MultiValueMap<String, String> getHeaders(Map<String, String> headers) {
294         return null;
295     }
296
297 }