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.requestsdb.client.RequestsDbClient;
35 import org.onap.so.serviceinstancebeans.*;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.util.UriComponentsBuilder;
42 import javax.ws.rs.core.HttpHeaders;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
54 import static com.github.tomakehurst.wiremock.client.WireMock.*;
55 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
56 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
57 import static org.junit.Assert.assertEquals;
59 public class OrchestrationRequestsTest extends BaseTest {
61 private InfraActiveRequestsRepository iar;
64 private RequestsDbClient requestsDbClient;
66 private static final String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title></title></head><body></body></html>";
67 private static final GetOrchestrationListResponse ORCHESTRATION_LIST = generateOrchestrationList();
68 private static final String INVALID_REQUEST_ID = "invalid-request-id";
70 private static GetOrchestrationListResponse generateOrchestrationList() {
71 GetOrchestrationListResponse list = null;
73 ObjectMapper mapper = new ObjectMapper();
74 list = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationList.json"),
75 GetOrchestrationListResponse.class);
76 } catch (JsonParseException jpe) {
77 jpe.printStackTrace();
78 } catch (JsonMappingException jme) {
79 jme.printStackTrace();
80 } catch (IOException ioe) {
81 ioe.printStackTrace();
87 public void testGetOrchestrationRequest() throws Exception {
88 setupTestGetOrchestrationRequest();
90 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
92 Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
93 testResponse.setRequest(request);
94 String testRequestId = request.getRequestId();
96 headers.set("Accept", MediaType.APPLICATION_JSON);
97 headers.set("Content-Type", MediaType.APPLICATION_JSON);
98 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
100 UriComponentsBuilder builder = UriComponentsBuilder
101 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
103 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
104 entity, GetOrchestrationResponse.class);
106 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
107 assertThat(response.getBody(),
108 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
109 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
110 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
111 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
112 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
113 assertEquals("00032ab7-na18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
117 public void testGetOrchestrationRequestRequestDetails() throws Exception {
118 setupTestGetOrchestrationRequestRequestDetails("00032ab7-3fb3-42e5-965d-8ea592502017", "COMPLETED");
119 //Test request with modelInfo request body
120 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
122 Request request = ORCHESTRATION_LIST.getRequestList().get(0).getRequest();
123 testResponse.setRequest(request);
124 String testRequestId = request.getRequestId();
126 headers.set("Accept", MediaType.APPLICATION_JSON);
127 headers.set("Content-Type", MediaType.APPLICATION_JSON);
128 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
130 UriComponentsBuilder builder = UriComponentsBuilder
131 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
133 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
134 entity, GetOrchestrationResponse.class);
136 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
137 assertThat(response.getBody(),
138 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
139 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
140 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
141 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
142 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
143 assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
147 public void testGetOrchestrationRequestNoRequestID() {
148 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
149 headers.set("Accept", MediaType.APPLICATION_JSON);
151 UriComponentsBuilder builder = UriComponentsBuilder
152 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
154 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
155 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
156 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
160 public void testGetOrchestrationRequestFilter() throws Exception {
161 setupTestGetOrchestrationRequestFilter();
162 List<String> values = new ArrayList<>();
163 values.add("EQUALS");
164 values.add("vfModule");
166 Map<String, List<String>> orchestrationMap = new HashMap<>();
167 orchestrationMap.put("modelType", values);
169 List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
170 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
171 headers.set("Accept", MediaType.APPLICATION_JSON);
173 UriComponentsBuilder builder = UriComponentsBuilder
174 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
176 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
177 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
178 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
179 assertEquals(requests.size(), response.getBody().getRequestList().size());
183 public void testUnlockOrchestrationRequest() throws Exception {
184 setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
185 ObjectMapper mapper = new ObjectMapper();
186 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
187 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
189 headers.set("Accept", MediaType.APPLICATION_JSON);
190 headers.set("Content-Type", MediaType.APPLICATION_JSON);
191 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
193 UriComponentsBuilder builder;
194 ResponseEntity<String> response;
195 RequestError expectedRequestError;
196 RequestError actualRequestError;
200 expectedRequestError = new RequestError();
201 se = new ServiceException();
202 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
203 se.setText("Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
204 expectedRequestError.setServiceException(se);
206 builder = UriComponentsBuilder.fromHttpUrl(
207 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
209 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
210 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
212 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
213 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
217 public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
218 setupTestUnlockOrchestrationRequest_invalid_Json();
219 ObjectMapper mapper = new ObjectMapper();
220 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
221 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
223 headers.set("Accept", MediaType.APPLICATION_JSON);
224 headers.set("Content-Type", MediaType.APPLICATION_JSON);
225 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
227 UriComponentsBuilder builder;
228 ResponseEntity<String> response;
229 RequestError expectedRequestError;
230 RequestError actualRequestError;
233 // Test invalid requestId
234 expectedRequestError = new RequestError();
235 se = new ServiceException();
236 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
237 se.setText("Null response from RequestDB when searching by RequestId");
238 expectedRequestError.setServiceException(se);
240 builder = UriComponentsBuilder.fromHttpUrl(
241 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
243 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
244 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
246 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
247 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
251 public void testUnlockOrchestrationRequest_Valid_Status()
252 throws JsonParseException, JsonMappingException, IOException, ValidationException {
253 setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
254 ObjectMapper mapper = new ObjectMapper();
255 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
257 headers.set("Accept", MediaType.APPLICATION_JSON);
258 headers.set("Content-Type", MediaType.APPLICATION_JSON);
259 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
261 UriComponentsBuilder builder;
262 ResponseEntity<String> response;
266 request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
267 builder = UriComponentsBuilder.fromHttpUrl(
268 createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
270 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
271 //Cannot assert anything further here, already have a wiremock in place which ensures that the post was properly called to update.
274 @Ignore //What is this testing?
276 public void testGetOrchestrationRequestRequestDetailsWhiteSpace() throws Exception {
277 InfraActiveRequests requests = new InfraActiveRequests();
278 requests.setAction("create");
279 requests.setRequestBody(" ");
280 requests.setRequestId("requestId");
281 requests.setRequestScope("service");
282 requests.setRequestType("createInstance");
283 ObjectMapper mapper = new ObjectMapper();
284 String json = mapper.writeValueAsString(requests);
286 requestsDbClient.save(requests);
288 headers.set("Accept", MediaType.APPLICATION_JSON);
289 headers.set("Content-Type", MediaType.APPLICATION_JSON);
290 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
292 UriComponentsBuilder builder = UriComponentsBuilder
293 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
295 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
296 entity, GetOrchestrationResponse.class);
298 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
299 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
300 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
301 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
302 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
303 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
306 @Ignore //What is this testing?
308 public void testGetOrchestrationRequestRequestDetailsAlaCarte() throws IOException {
309 InfraActiveRequests requests = new InfraActiveRequests();
311 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/AlaCarteRequest.json")));
313 requests.setAction("create");
314 requests.setRequestBody(requestJSON);
315 requests.setRequestId("requestId");
316 requests.setRequestScope("service");
317 requests.setRequestType("createInstance");
320 headers.set("Accept", MediaType.APPLICATION_JSON);
321 headers.set("Content-Type", MediaType.APPLICATION_JSON);
322 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
324 UriComponentsBuilder builder = UriComponentsBuilder
325 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
327 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
328 entity, GetOrchestrationResponse.class);
330 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
331 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
332 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
333 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
334 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
335 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
338 public void setupTestGetOrchestrationRequest() throws Exception{
339 //For testGetOrchestrationRequest
340 stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-na18-42e5-965d-8ea592502018")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
341 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
342 .withStatus(HttpStatus.SC_OK)));
345 private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception{
346 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
347 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
348 .withStatus(HttpStatus.SC_OK)));
351 private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
352 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
353 .withBody(String.format(getResponseTemplate, requestId, status))
354 .withStatus(HttpStatus.SC_OK)));
360 private void setupTestUnlockOrchestrationRequest_invalid_Json() {
361 stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
362 .withStatus(HttpStatus.SC_NOT_FOUND)));
366 private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
367 stubFor(get(urlPathEqualTo(getTestUrl(requestID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
368 .withBody(String.format(getResponseTemplate, requestID, status))
369 .withStatus(HttpStatus.SC_OK)));
371 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
372 .withBody(String.format(infraActivePost, requestID))
373 .withStatus(HttpStatus.SC_OK)));
376 private void setupTestGetOrchestrationRequestFilter() throws Exception{
377 //for testGetOrchestrationRequestFilter();
378 stubFor(any(urlPathEqualTo("/infraActiveRequests/getOrchestrationFiltersFromInfraActive/")).withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
379 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
380 .withStatus(HttpStatus.SC_OK)));