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