2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.so.apihandlerinfra;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.apache.http.HttpStatus;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.onap.so.apihandler.common.ErrorNumbers;
31 import org.onap.so.db.request.beans.InfraActiveRequests;
32 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
33 import org.onap.so.exceptions.ValidationException;
34 import org.onap.so.serviceinstancebeans.*;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpEntity;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.util.UriComponentsBuilder;
41 import javax.ws.rs.core.HttpHeaders;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
45 import java.io.IOException;
46 import java.nio.file.Files;
47 import java.nio.file.Paths;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
53 import static com.github.tomakehurst.wiremock.client.WireMock.*;
54 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
55 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
56 import static org.junit.Assert.assertEquals;
58 public class OrchestrationRequestsTest extends BaseTest {
60 private InfraActiveRequestsRepository iar;
63 private RequestsDbClient requestsDbClient;
65 private static final String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title></title></head><body></body></html>";
66 private static final GetOrchestrationListResponse ORCHESTRATION_LIST = generateOrchestrationList();
67 private static final String INVALID_REQUEST_ID = "invalid-request-id";
69 private static GetOrchestrationListResponse generateOrchestrationList() {
70 GetOrchestrationListResponse list = null;
72 ObjectMapper mapper = new ObjectMapper();
73 list = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationList.json"),
74 GetOrchestrationListResponse.class);
75 } catch (JsonParseException jpe) {
76 jpe.printStackTrace();
77 } catch (JsonMappingException jme) {
78 jme.printStackTrace();
79 } catch (IOException ioe) {
80 ioe.printStackTrace();
86 public void testGetOrchestrationRequest() throws Exception {
87 setupTestGetOrchestrationRequest();
89 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
91 Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
92 testResponse.setRequest(request);
93 String testRequestId = request.getRequestId();
95 headers.set("Accept", MediaType.APPLICATION_JSON);
96 headers.set("Content-Type", MediaType.APPLICATION_JSON);
97 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
99 UriComponentsBuilder builder = UriComponentsBuilder
100 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
102 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
103 entity, GetOrchestrationResponse.class);
105 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
106 assertThat(response.getBody(),
107 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
108 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
109 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
110 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
111 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
112 assertEquals("00032ab7-na18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
116 public void testGetOrchestrationRequestRequestDetails() throws Exception {
117 setupTestGetOrchestrationRequestRequestDetails("00032ab7-3fb3-42e5-965d-8ea592502017", "COMPLETED");
118 //Test request with modelInfo request body
119 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
121 Request request = ORCHESTRATION_LIST.getRequestList().get(0).getRequest();
122 testResponse.setRequest(request);
123 String testRequestId = request.getRequestId();
125 headers.set("Accept", MediaType.APPLICATION_JSON);
126 headers.set("Content-Type", MediaType.APPLICATION_JSON);
127 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
129 UriComponentsBuilder builder = UriComponentsBuilder
130 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
132 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
133 entity, GetOrchestrationResponse.class);
135 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
136 assertThat(response.getBody(),
137 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
138 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
139 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
140 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
141 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
142 assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
146 public void testGetOrchestrationRequestNoRequestID() {
147 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
148 headers.set("Accept", MediaType.APPLICATION_JSON);
150 UriComponentsBuilder builder = UriComponentsBuilder
151 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
153 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
154 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
155 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
159 public void testGetOrchestrationRequestFilter() throws Exception {
160 setupTestGetOrchestrationRequestFilter();
161 List<String> values = new ArrayList<>();
162 values.add("EQUALS");
163 values.add("vfModule");
165 Map<String, List<String>> orchestrationMap = new HashMap<>();
166 orchestrationMap.put("modelType", values);
168 List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
169 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
170 headers.set("Accept", MediaType.APPLICATION_JSON);
172 UriComponentsBuilder builder = UriComponentsBuilder
173 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
175 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
176 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
177 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
178 assertEquals(requests.size(), response.getBody().getRequestList().size());
182 public void testUnlockOrchestrationRequest() throws Exception {
183 setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
184 ObjectMapper mapper = new ObjectMapper();
185 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
186 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
188 headers.set("Accept", MediaType.APPLICATION_JSON);
189 headers.set("Content-Type", MediaType.APPLICATION_JSON);
190 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
192 UriComponentsBuilder builder;
193 ResponseEntity<String> response;
194 RequestError expectedRequestError;
195 RequestError actualRequestError;
199 expectedRequestError = new RequestError();
200 se = new ServiceException();
201 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
202 se.setText("Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
203 expectedRequestError.setServiceException(se);
205 builder = UriComponentsBuilder.fromHttpUrl(
206 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
208 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
209 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
211 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
212 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
216 public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
217 setupTestUnlockOrchestrationRequest_invalid_Json();
218 ObjectMapper mapper = new ObjectMapper();
219 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
220 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
222 headers.set("Accept", MediaType.APPLICATION_JSON);
223 headers.set("Content-Type", MediaType.APPLICATION_JSON);
224 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
226 UriComponentsBuilder builder;
227 ResponseEntity<String> response;
228 RequestError expectedRequestError;
229 RequestError actualRequestError;
232 // Test invalid requestId
233 expectedRequestError = new RequestError();
234 se = new ServiceException();
235 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
236 se.setText("Null response from RequestDB when searching by RequestId");
237 expectedRequestError.setServiceException(se);
239 builder = UriComponentsBuilder.fromHttpUrl(
240 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
242 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
243 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
245 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
246 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
250 public void testUnlockOrchestrationRequest_Valid_Status()
251 throws JsonParseException, JsonMappingException, IOException, ValidationException {
252 setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
253 ObjectMapper mapper = new ObjectMapper();
254 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
256 headers.set("Accept", MediaType.APPLICATION_JSON);
257 headers.set("Content-Type", MediaType.APPLICATION_JSON);
258 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
260 UriComponentsBuilder builder;
261 ResponseEntity<String> response;
265 request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
266 builder = UriComponentsBuilder.fromHttpUrl(
267 createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
269 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
270 //Cannot assert anything further here, already have a wiremock in place which ensures that the post was properly called to update.
273 @Ignore //What is this testing?
275 public void testGetOrchestrationRequestRequestDetailsWhiteSpace() throws Exception {
276 InfraActiveRequests requests = new InfraActiveRequests();
277 requests.setAction("create");
278 requests.setRequestBody(" ");
279 requests.setRequestId("requestId");
280 requests.setRequestScope("service");
281 requests.setRequestType("createInstance");
282 ObjectMapper mapper = new ObjectMapper();
283 String json = mapper.writeValueAsString(requests);
285 requestsDbClient.save(requests);
287 headers.set("Accept", MediaType.APPLICATION_JSON);
288 headers.set("Content-Type", MediaType.APPLICATION_JSON);
289 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
291 UriComponentsBuilder builder = UriComponentsBuilder
292 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
294 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
295 entity, GetOrchestrationResponse.class);
297 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
298 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
299 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
300 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
301 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
302 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
305 @Ignore //What is this testing?
307 public void testGetOrchestrationRequestRequestDetailsAlaCarte() throws IOException {
308 InfraActiveRequests requests = new InfraActiveRequests();
310 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/AlaCarteRequest.json")));
312 requests.setAction("create");
313 requests.setRequestBody(requestJSON);
314 requests.setRequestId("requestId");
315 requests.setRequestScope("service");
316 requests.setRequestType("createInstance");
319 headers.set("Accept", MediaType.APPLICATION_JSON);
320 headers.set("Content-Type", MediaType.APPLICATION_JSON);
321 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
323 UriComponentsBuilder builder = UriComponentsBuilder
324 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
326 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
327 entity, GetOrchestrationResponse.class);
329 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
330 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
331 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
332 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
333 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
334 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
337 public void setupTestGetOrchestrationRequest() throws Exception{
338 //For testGetOrchestrationRequest
339 stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-na18-42e5-965d-8ea592502018")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
340 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
341 .withStatus(HttpStatus.SC_OK)));
344 private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception{
345 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
346 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
347 .withStatus(HttpStatus.SC_OK)));
350 private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
351 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
352 .withBody(String.format(getResponseTemplate, requestId, status))
353 .withStatus(HttpStatus.SC_OK)));
359 private void setupTestUnlockOrchestrationRequest_invalid_Json() {
360 stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
361 .withStatus(HttpStatus.SC_NOT_FOUND)));
365 private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
366 stubFor(get(urlPathEqualTo(getTestUrl(requestID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
367 .withBody(String.format(getResponseTemplate, requestID, status))
368 .withStatus(HttpStatus.SC_OK)));
370 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
371 .withBody(String.format(infraActivePost, requestID))
372 .withStatus(HttpStatus.SC_OK)));
375 private void setupTestGetOrchestrationRequestFilter() throws Exception{
376 //for testGetOrchestrationRequestFilter();
377 stubFor(any(urlPathEqualTo("/infraActiveRequests/getOrchestrationFiltersFromInfraActive/")).withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
378 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
379 .withStatus(HttpStatus.SC_OK)));