Update schema ingest library call schema service
[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 package org.onap.aai.restclient;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import org.apache.commons.io.IOUtils;
27 import org.springframework.core.io.Resource;
28 import org.springframework.http.*;
29 import org.springframework.stereotype.Component;
30 import org.springframework.test.web.client.ExpectedCount;
31 import org.springframework.test.web.client.MockRestServiceServer;
32 import org.springframework.util.MultiValueMap;
33 import org.springframework.web.client.RestTemplate;
34
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.charset.StandardCharsets;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.Map;
42
43 import static org.junit.Assert.assertNotNull;
44 import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
45 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
46
47 @Component
48 public class MockRestClient extends RestClient {
49
50 private RestTemplate restTemplate;
51         private MockRestServiceServer mockRestServiceServer;
52
53     String fileName = "mockrequests";
54
55     public MockRestClient(String fileName) {
56         /*
57         List<MockRestServiceServer> mockedAAIRequests = new ArrayList<>(aaiRequests.size());
58          */
59         List<MockRestServiceServer> mockedAAIRequests = new ArrayList<>();
60
61         restTemplate = new RestTemplate();
62            /* MockRestServiceServer server       = MockRestServiceServer
63                 .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
64                 .build();
65             server.expect(MockRestRequestMatchers.requestTo(url))
66                 .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));*/
67
68         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
69         //when(mockBuilder.build()).thenReturn(restTemplate);
70
71         JsonObject payload = null;
72         try {
73             payload = getPayload(fileName + ".json");
74         } catch (IOException e) {
75             e.printStackTrace();
76         }
77
78         JsonArray mockUris = payload.getAsJsonArray("mock-uri");
79
80
81         mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
82         String url = "https://localhost:8447/aai/v14";
83         /*mockRestServiceServer.expect(requestTo(url))
84             .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));*/
85
86
87         for (int i = 0; i < mockUris.size(); i++) {
88             String responseFile = mockUris.get(i).getAsJsonObject().get("response-file").getAsString();
89             String contentTypeValue = mockUris.get(i).getAsJsonObject().get("content").getAsString();
90
91
92             String uri = mockUris.get(i).getAsJsonObject().get("aai-uri").getAsString();
93
94             InputStream inputStream = getClass()
95                 .getClassLoader()
96                 .getResourceAsStream(responseFile);
97             String responseBody = null;
98             try {
99                 responseBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
100             } catch (IOException e) {
101                 e.printStackTrace();
102             }
103
104
105             mockRestServiceServer.expect(ExpectedCount.manyTimes(), requestTo(url + uri))
106                 .andExpect(method(HttpMethod.GET))
107                 .andExpect(content().contentType(contentTypeValue))
108                 .andRespond(withStatus(HttpStatus.OK).body(responseBody.toString()).contentType(MediaType.valueOf(contentTypeValue)));
109
110
111         }
112     }
113
114     public MockRestClient()  {
115
116          restTemplate = new RestTemplate();
117            /* MockRestServiceServer server       = MockRestServiceServer
118                 .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
119                 .build();
120             server.expect(MockRestRequestMatchers.requestTo(url))
121                 .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));*/
122
123         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
124         //when(mockBuilder.build()).thenReturn(restTemplate);
125
126         JsonObject payload = null;
127         try {
128             payload = getPayload( fileName + ".json");
129         } catch (IOException e) {
130             e.printStackTrace();
131         }
132
133         JsonArray mockUris = payload.getAsJsonArray("mock-uri");
134
135
136
137         mockRestServiceServer       = MockRestServiceServer.createServer(restTemplate);
138         String url="https://localhost:8447/aai/v14";
139         /*mockRestServiceServer.expect(requestTo(url))
140             .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));*/
141
142
143
144
145         for (int i = 0; i < mockUris.size(); i++) {
146             String responseFile = mockUris.get(i).getAsJsonObject().get("response-file").getAsString();
147             String contentTypeValue = mockUris.get(i).getAsJsonObject().get("content").getAsString();
148
149
150             String uri = mockUris.get(i).getAsJsonObject().get("aai-uri").getAsString();
151
152             InputStream inputStream = getClass()
153                 .getClassLoader()
154                 .getResourceAsStream(responseFile);
155             String responseBody = null;
156             try {
157                 responseBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
158             } catch (IOException e) {
159                 e.printStackTrace();
160             }
161
162
163             mockRestServiceServer.expect(ExpectedCount.manyTimes(), requestTo(url + uri))
164                 .andExpect(method(HttpMethod.GET))
165                 .andExpect(content().contentType(contentTypeValue))
166                 .andRespond(withStatus(HttpStatus.OK).body(responseBody.toString()).contentType(MediaType.valueOf(contentTypeValue)));
167
168
169         }
170
171
172     }
173
174     public JsonObject  getTestDetails(String fileName) throws IOException {
175
176         JsonObject payload = getPayload(fileName );
177
178         return payload;
179     }
180
181     public JsonObject getPayload(String filename) throws IOException {
182         InputStream inputStream = getClass()
183             .getClassLoader()
184             .getResourceAsStream(filename);
185
186         //InputStream inputStream = new FileInputStream(filename);
187
188         String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
189         String message = String.format("Unable to find the %s in src/test/resources", filename);
190         assertNotNull(message, inputStream);
191
192         JsonParser parser = new JsonParser();
193         JsonObject payload = parser.parse(result).getAsJsonObject();
194         return payload;
195     }
196
197     @Override
198         public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers, String body) {
199
200             String url="https://localhost:8447/aai/v14/"+ uri;
201
202            /* MockRestServiceServer server       = MockRestServiceServer
203                 .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
204                 .build();
205             server.expect(MockRestRequestMatchers.requestTo(url))
206                 .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));*/
207
208            // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
209             //when(mockBuilder.build()).thenReturn(restTemplate);
210
211         /*MockRestServiceServer server       = MockRestServiceServer.createServer(restTemplate);
212         server.expect(requestTo(url))
213             .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
214             return new ResponseEntity("blah", HttpStatus.OK);
215         server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl + 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.APPLICATION_JSON));*/
219
220
221         HttpHeaders headersMap = new HttpHeaders();
222
223         headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
224         headersMap.setContentType(MediaType.APPLICATION_JSON);
225         headersMap.add("Real-Time", "true");
226         headersMap.add("X-FromAppId", "JUNIT");
227         headersMap.add("X-TransactionId", "JUNIT");
228
229         HttpEntity httpEntity = new HttpEntity(headers);
230
231         ResponseEntity responseEntity = restTemplate.exchange(url , HttpMethod.GET, httpEntity, String.class);
232
233        // mockRestServiceServer.verify();
234        return responseEntity;
235     }
236
237     @Override
238     public ResponseEntity executeResource(String uri, HttpMethod method, Map<String,String> headers, String body) {
239
240         String url="https://localhost:8447/aai/v14/"+ uri;
241
242            /* MockRestServiceServer server       = MockRestServiceServer
243                 .bindTo(restClientFactory.getRestClient(ClientType.SchemaService).getRestTemplate())
244                 .build();
245             server.expect(MockRestRequestMatchers.requestTo(url))
246                 .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));*/
247
248         // RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
249         //when(mockBuilder.build()).thenReturn(restTemplate);
250
251         /*MockRestServiceServer server       = MockRestServiceServer.createServer(restTemplate);
252         server.expect(requestTo(url))
253             .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
254             return new ResponseEntity("blah", HttpStatus.OK);
255         server.expect(ExpectedCount.manyTimes(), requestTo(Matchers.startsWith(aaiBaseUrl + aaiRequests.get(i).get("aai-uri").asText())))
256             .andExpect(method(HttpMethod.GET))
257             .andExpect(content().contentType(MediaType.APPLICATION_JSON))
258             .andRespond(withStatus(HttpStatus.OK).body(aaiResponses.get(i).toString()).contentType(MediaType.APPLICATION_JSON));*/
259
260
261         HttpHeaders headersMap = new HttpHeaders();
262
263         headersMap.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
264         headersMap.setContentType(MediaType.APPLICATION_JSON);
265         headersMap.add("Real-Time", "true");
266         headersMap.add("X-FromAppId", "JUNIT");
267         headersMap.add("X-TransactionId", "JUNIT");
268
269         HttpEntity httpEntity = new HttpEntity(headers);
270
271         ResponseEntity responseEntity = restTemplate.exchange(url , HttpMethod.GET, httpEntity, Resource.class);
272
273         // mockRestServiceServer.verify();
274         return responseEntity;
275     }
276
277     @Override
278     public RestTemplate getRestTemplate() {
279         RestTemplate restTemplate = null;
280         return restTemplate;
281     }
282
283     public  String getBaseUrl(){
284             return "";
285     }
286
287     protected  MultiValueMap<String,String> getHeaders(Map<String,String> headers){
288             return null;
289     }
290
291     protected  EELFLogger getLogger(){
292             return null;
293     }
294
295 }