SO changes for Service Intent
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / ServiceIntentApiHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Wipro Limited.
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 com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.apache.http.HttpStatus;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.logging.ref.slf4j.ONAPLogConstants;
30 import org.onap.so.db.catalog.beans.Service;
31 import org.onap.so.db.catalog.beans.ServiceRecipe;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.util.UriComponentsBuilder;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import java.io.IOException;
41 import java.nio.file.Files;
42 import java.nio.file.Paths;
43 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
44 import static com.github.tomakehurst.wiremock.client.WireMock.get;
45 import static com.github.tomakehurst.wiremock.client.WireMock.post;
46 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
47 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
48 import static org.junit.Assert.assertEquals;
49 import static org.onap.logging.filter.base.Constants.HttpHeaders.ONAP_PARTNER_NAME;
50 import static org.onap.logging.filter.base.Constants.HttpHeaders.ONAP_REQUEST_ID;
51 import static org.onap.logging.filter.base.Constants.HttpHeaders.TRANSACTION_ID;
52 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
53
54 public class ServiceIntentApiHandlerTest extends BaseTest {
55
56     private static final String ROOT_URI = "/onap/so/infra/serviceIntent/";
57
58     private static final ObjectMapper MAPPER = new ObjectMapper();
59
60     @Autowired
61     private Onap3gppServiceInstances objUnderTest;
62
63     @Before
64     public void init() throws JsonProcessingException {
65
66         Service defaultService = new Service();
67         defaultService.setModelUUID("d88da85c-d9e8-4f73-b837-3a72a431622a");
68         ServiceRecipe serviceRecipe = new ServiceRecipe();
69         serviceRecipe.setServiceModelUUID(defaultService.getModelUUID());
70         serviceRecipe.setRecipeTimeout(180);
71         serviceRecipe.setOrchestrationUri("/mso/async/services/commonServiceIntentTest");
72
73         wireMockServer.stubFor(get(urlPathEqualTo("/service/search/findFirstByModelNameOrderByModelVersionDesc"))
74                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
75                         .withBody(MAPPER.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
76
77         wireMockServer.stubFor(get(urlPathEqualTo("/serviceRecipe/search/findFirstByServiceModelUUIDAndAction"))
78                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
79                         .withBody(MAPPER.writeValueAsString(serviceRecipe)).withStatus(HttpStatus.SC_OK)));
80         wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests/")).willReturn(aResponse()
81                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
82         Mockito.doReturn(null).when(requestsDbClient).getInfraActiveRequestbyRequestId(Mockito.any());
83     }
84
85     public String inputStream(String JsonInput) throws IOException {
86         JsonInput = "src/test/resources/ServiceIntentTest" + JsonInput;
87         return new String(Files.readAllBytes(Paths.get(JsonInput)));
88     }
89
90     public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
91         HttpHeaders headers = new HttpHeaders();
92         headers.set("Accept", MediaType.APPLICATION_JSON);
93         headers.set("Content-Type", MediaType.APPLICATION_JSON);
94         headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
95         headers.set(TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
96         headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
97         headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
98         headers.set(ONAP_PARTNER_NAME, "VID");
99         headers.set(REQUESTOR_ID, "xxxxxx");
100         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
101         HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
102
103         return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
104     }
105
106     @Test
107     public void createServiceInstanceTest() throws IOException {
108         String uri = ROOT_URI + "v1/create";
109         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonServiceIntentTest"))
110                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
111                         .withBodyFile("Camunda/BPMN_response.json").withStatus(HttpStatus.SC_ACCEPTED)));
112
113         String expectedResponse =
114                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
115         ResponseEntity<String> response = sendRequest(inputStream("/create-cll-payload.json"), uri, HttpMethod.POST);
116
117         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
118         String actualResponse = response.getBody();
119         assertEquals(expectedResponse, actualResponse);
120     }
121
122     @Test
123     public void updateServiceInstanceTest() throws IOException {
124         String uri = ROOT_URI + "v1/modify";
125         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonServiceIntentTest"))
126                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
127                         .withBodyFile("Camunda/BPMN_response.json").withStatus(HttpStatus.SC_ACCEPTED)));
128
129         String expectedResponse =
130                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
131         ResponseEntity<String> response = sendRequest(inputStream("/modify-cll-payload.json"), uri, HttpMethod.PUT);
132
133         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
134         String actualResponse = response.getBody();
135         assertEquals(expectedResponse, actualResponse);
136     }
137
138     @Test
139     public void deleteServiceInstanceTest() throws IOException {
140         String uri = ROOT_URI + "v1/delete";
141         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonServiceIntentTest"))
142                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
143                         .withBodyFile("Camunda/BPMN_response.json").withStatus(HttpStatus.SC_ACCEPTED)));
144         String expectedResponse =
145                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
146         ResponseEntity<String> response = sendRequest(inputStream("/delete-cll-payload.json"), uri, HttpMethod.DELETE);
147
148         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
149         String actualResponse = response.getBody();
150         assertEquals(expectedResponse, actualResponse);
151     }
152
153
154 }
155
156