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