eec68d8079d94a90b821c8c2e4194dbc8633cbfc
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / ManualTasksTest.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.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
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.assertNotNull;
30 import static org.junit.Assert.assertThat;
31
32 import java.io.IOException;
33 import java.util.Map;
34
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37
38 import org.apache.http.HttpStatus;
39 import org.junit.Test;
40 import org.onap.logging.ref.slf4j.ONAPLogConstants;
41 import org.onap.so.apihandlerinfra.tasksbeans.RequestDetails;
42 import org.onap.so.apihandlerinfra.tasksbeans.RequestInfo;
43 import org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference;
44 import org.onap.so.apihandlerinfra.tasksbeans.TasksRequest;
45 import org.onap.so.apihandlerinfra.tasksbeans.ValidResponses;
46 import org.onap.so.logger.MsoLogger;
47 import org.springframework.http.HttpEntity;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.web.util.UriComponentsBuilder;
52
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56 import ch.qos.logback.classic.spi.ILoggingEvent;
57
58
59 public class ManualTasksTest extends BaseTest{
60
61     private final String basePath = "/tasks/v1/";
62
63
64
65     @Test
66     public void testCreateOpEnvObjectMapperError() throws IOException {
67         TestAppender.events.clear();
68         stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete"))
69                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
70
71         String taskId = "55";
72         TasksRequest taskReq = new TasksRequest();
73         RequestDetails reqDetail = new RequestDetails();
74         RequestInfo reqInfo = new RequestInfo();
75         reqInfo.setRequestorId("testId");
76         reqInfo.setSource("testSource");        
77         reqInfo.setResponseValue(ValidResponses.skip);
78         reqDetail.setRequestInfo(reqInfo);
79         taskReq.setRequestDetails(reqDetail);
80
81         //expected response
82         TaskRequestReference expectedResponse = new TaskRequestReference();
83         expectedResponse.setTaskId(taskId);     
84         HttpHeaders headers = new HttpHeaders();
85         headers.set("Accept", MediaType.APPLICATION_JSON);
86         headers.set("Content-Type", MediaType.APPLICATION_JSON);
87         headers.set(MsoLogger.ECOMP_REQUEST_ID, "987654321");
88         headers.set(MsoLogger.CLIENT_ID, "VID");
89         HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
90
91         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");           
92         ResponseEntity<String> response = restTemplate.exchange(
93                 builder.toUriString(),
94                 HttpMethod.POST, entity, String.class);
95
96
97         ObjectMapper mapper = new ObjectMapper();
98         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
99         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
100
101         TaskRequestReference realResponse = mapper.readValue(response.getBody(), TaskRequestReference.class);
102
103
104         //then          
105         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());       
106         assertThat(realResponse, sameBeanAs(expectedResponse)); 
107         
108         for(ILoggingEvent logEvent : TestAppender.events)
109             if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.jersey.JaxRsFilterLogging") &&
110                     logEvent.getMarker().getName().equals("ENTRY")
111                     ){
112                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
113                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
114                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
115                 assertNotNull(mdc.get(MsoLogger.INVOCATION_ID));               
116                 assertEquals("UNKNOWN",mdc.get(MsoLogger.PARTNERNAME));
117                 assertEquals("tasks/v1/55/complete",mdc.get(MsoLogger.SERVICE_NAME));
118                 assertEquals("INPROGRESS",mdc.get(MsoLogger.STATUSCODE));
119             }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.jersey.JaxRsFilterLogging") &&
120                     logEvent.getMarker().getName().equals("EXIT")){
121                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
122                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
123                 assertNotNull(mdc.get(MsoLogger.ENDTIME));
124                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
125                 assertNotNull(mdc.get(MsoLogger.INVOCATION_ID));
126                 assertEquals("202",mdc.get(MsoLogger.RESPONSECODE));
127                 assertEquals("UNKNOWN",mdc.get(MsoLogger.PARTNERNAME));
128                 assertEquals("tasks/v1/55/complete",mdc.get(MsoLogger.SERVICE_NAME));
129                 assertEquals("COMPLETE",mdc.get(MsoLogger.STATUSCODE));
130                 assertNotNull(mdc.get(MsoLogger.RESPONSEDESC));
131                 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
132                 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
133                 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
134                 assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
135             }
136     }
137 }