07c484744ee23317fc9011daf1cc5df2ffdaa269
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.so.apihandlerinfra;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertThat;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.apache.http.HttpStatus;
36 import org.json.JSONException;
37 import org.junit.Test;
38 import org.onap.so.apihandlerinfra.tasksbeans.TaskList;
39 import org.onap.so.apihandlerinfra.tasksbeans.TasksGetResponse;
40 import org.onap.so.apihandlerinfra.tasksbeans.ValidResponses;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.util.UriComponentsBuilder;
46 import com.fasterxml.jackson.databind.DeserializationFeature;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49
50 public class TasksHandlerTest extends BaseTest {
51
52     private final String basePath = "/onap/so/infra/tasks/v1";
53
54     @Test
55     public void getTasksTestByOriginalRequestId() throws JSONException, JsonMappingException, IOException {
56         wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task"))
57                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
58                         .withBodyFile("Camunda/GetTaskResponse.json").withStatus(HttpStatus.SC_OK)));
59
60         wireMockServer.stubFor(get(urlPathEqualTo("/sobpmnengine/task/b5fa707a-f55a-11e7-a796-005056856d52/variables"))
61                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
62                         .withBodyFile("Camunda/GetTaskVariablesResponse.json").withStatus(HttpStatus.SC_OK)));
63
64         String requestId = "4f6fe9ac-800c-4540-a93e-10d179fa1b0a";
65
66         // expected response
67         TasksGetResponse expectedResponse = new TasksGetResponse();
68
69         List<TaskList> taskList = new ArrayList<TaskList>();
70         TaskList taskList1 = new TaskList();
71         List<String> validEntries = new ArrayList<String>();
72         validEntries.add(ValidResponses.rollback.toString());
73         validEntries.add(ValidResponses.skip.toString());
74         validEntries.add(ValidResponses.manual.toString());
75         validEntries.add(ValidResponses.abort.toString());
76         taskList1.setBuildingBlockName("UpdateConfigurationState");
77         taskList1.setBuildingBlockStep("Configurationactivate SDNO Post-Check");
78         taskList1.setErrorCode("1002");
79         taskList1.setErrorSource("SDNO");
80         taskList1.setErrorMessage(
81                 "SDN-O exception: failed with message FAIL - AnsibleOperations exception: Failed : HTTP error code : 400 - Error Msg : no node list provided and no inventory file found");
82         taskList1.setNfRole("VPROBE");
83         taskList1.setType("fallout");
84         taskList1.setOriginalRequestId(requestId);
85         taskList1.setOriginalRequestorId("VID");
86         taskList1.setSubscriptionServiceType("PORT-MIRROR");
87         taskList1.setTaskId("b5fa707a-f55a-11e7-a796-005056856d52");
88         taskList1.setDescription("test task");
89         taskList1.setTimeout("PT3000S");
90         taskList1.setValidResponses(validEntries);
91         taskList.add(taskList1);
92
93         expectedResponse.setTaskList(taskList);
94         HttpHeaders headers = new HttpHeaders();
95         headers.set("Accept", MediaType.APPLICATION_JSON);
96         headers.set("Content-Type", MediaType.APPLICATION_JSON);
97         HttpEntity<String> entity = new HttpEntity<String>(null, headers);
98
99         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath))
100                 .queryParam("taskId", "b5fa707a-f55a-11e7-a796-005056856d52");
101
102         ResponseEntity<String> response =
103                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
104
105         ObjectMapper mapper = new ObjectMapper();
106
107         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
108
109
110         // then
111         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
112         TasksGetResponse realResponse = mapper.readValue(response.getBody(), TasksGetResponse.class);
113         assertThat(realResponse, sameBeanAs(expectedResponse));
114         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
115         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
116         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
117         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
118     }
119
120 }