Replaced all tabs with spaces in java and pom.xml
[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 import java.io.IOException;
31 import java.text.ParseException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import org.apache.http.HttpStatus;
37 import org.json.JSONException;
38 import org.junit.Test;
39 import org.onap.so.apihandlerinfra.tasksbeans.TaskList;
40 import org.onap.so.apihandlerinfra.tasksbeans.TasksGetResponse;
41 import org.onap.so.apihandlerinfra.tasksbeans.ValidResponses;
42 import org.springframework.http.HttpEntity;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.util.UriComponentsBuilder;
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 public class TasksHandlerTest extends BaseTest {
53
54     private final String basePath = "onap/so/infra/tasks/v1";
55
56     @Test
57     public void getTasksTestByOriginalRequestId()
58             throws ParseException, JSONException, JsonParseException, JsonMappingException, IOException {
59         wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task"))
60                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
61                         .withBodyFile("Camunda/GetTaskResponse.json").withStatus(HttpStatus.SC_OK)));
62
63         wireMockServer.stubFor(get(urlPathEqualTo("/sobpmnengine/task/b5fa707a-f55a-11e7-a796-005056856d52/variables"))
64                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
65                         .withBodyFile("Camunda/GetTaskVariablesResponse.json").withStatus(HttpStatus.SC_OK)));
66
67         String requestId = "4f6fe9ac-800c-4540-a93e-10d179fa1b0a";
68
69         // expected response
70         TasksGetResponse expectedResponse = new TasksGetResponse();
71
72         List<TaskList> taskList = new ArrayList<TaskList>();
73         TaskList taskList1 = new TaskList();
74         List<String> validEntries = new ArrayList<String>();
75         validEntries.add(ValidResponses.rollback.toString());
76         validEntries.add(ValidResponses.skip.toString());
77         validEntries.add(ValidResponses.manual.toString());
78         validEntries.add(ValidResponses.abort.toString());
79         taskList1.setBuildingBlockName("UpdateConfigurationState");
80         taskList1.setBuildingBlockStep("Configurationactivate SDNO Post-Check");
81         taskList1.setErrorCode("1002");
82         taskList1.setErrorSource("SDNO");
83         taskList1.setErrorMessage(
84                 "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 =
106                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
107
108         ObjectMapper mapper = new ObjectMapper();
109
110         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
111
112
113         // then
114         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
115         TasksGetResponse realResponse = mapper.readValue(response.getBody(), TasksGetResponse.class);
116         assertThat(realResponse, sameBeanAs(expectedResponse));
117         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
118         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
119         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
120         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
121     }
122
123 }