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