Read subnetCapabilities configuration file from SO pod
[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 import org.onap.so.apihandlerinfra.exceptions.ApiException;
53 import org.onap.so.apihandlerinfra.onap3gppserviceinstancebeans.QuerySubnetCapability;
54 import org.springframework.beans.factory.annotation.Autowired;
55
56 public class Onap3gppServiceInstancesTest extends BaseTest {
57
58     private static final String ONAP3GPPSERVICES_URI = "/onap/so/infra/3gppservices/";
59
60     private static final ObjectMapper MAPPER = new ObjectMapper();
61
62     @Autowired
63     private Onap3gppServiceInstances objUnderTest;
64
65     @Before
66     public void init() throws JsonProcessingException {
67
68         Service defaultService = new Service();
69         defaultService.setModelUUID("d88da85c-d9e8-4f73-b837-3a72a431622a");
70         ServiceRecipe serviceRecipe = new ServiceRecipe();
71         serviceRecipe.setServiceModelUUID(defaultService.getModelUUID());
72         serviceRecipe.setRecipeTimeout(180);
73         serviceRecipe.setOrchestrationUri("/mso/async/services/commonNssmfTest");
74
75         wireMockServer.stubFor(get(urlPathEqualTo("/service/search/findFirstByModelNameOrderByModelVersionDesc"))
76                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
77                         .withBody(MAPPER.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
78
79         wireMockServer.stubFor(get(urlPathEqualTo("/serviceRecipe/search/findFirstByServiceModelUUIDAndAction"))
80                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
81                         .withBody(MAPPER.writeValueAsString(serviceRecipe)).withStatus(HttpStatus.SC_OK)));
82         wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests/")).willReturn(aResponse()
83                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
84         Mockito.doReturn(null).when(requestsDbClient).getInfraActiveRequestbyRequestId(Mockito.any());
85     }
86
87     public String inputStream(String JsonInput) throws IOException {
88         JsonInput = "src/test/resources/Onap3gppServiceInstancesTest" + JsonInput;
89         return new String(Files.readAllBytes(Paths.get(JsonInput)));
90     }
91
92     public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
93         HttpHeaders headers = new HttpHeaders();
94         headers.set("Accept", MediaType.APPLICATION_JSON);
95         headers.set("Content-Type", MediaType.APPLICATION_JSON);
96         headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
97         headers.set(TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
98         headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
99         headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
100         headers.set(ONAP_PARTNER_NAME, "VID");
101         headers.set(REQUESTOR_ID, "xxxxxx");
102         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
103         HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
104
105         return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
106     }
107
108     @Test
109     public void createServiceInstanceTest() throws IOException {
110         String uri = ONAP3GPPSERVICES_URI + "v1/allocate";
111         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonNssmfTest")).willReturn(
112                 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/BPMN_response.json")
113                         .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
114
115         String expectedResponse =
116                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
117         ResponseEntity<String> response = sendRequest(inputStream("/allocateRequest.json"), uri, HttpMethod.POST);
118
119         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
120         String actualResponse = response.getBody();
121         assertEquals(expectedResponse, actualResponse);
122     }
123
124     @Test
125     public void updateServiceInstanceTest() throws IOException {
126         String uri = ONAP3GPPSERVICES_URI + "v1/modify";
127         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonNssmfTest")).willReturn(
128                 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/BPMN_response.json")
129                         .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
130
131         String expectedResponse =
132                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
133         ResponseEntity<String> response = sendRequest(inputStream("/modifyRequest.json"), uri, HttpMethod.PUT);
134
135         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
136         String actualResponse = response.getBody();
137         assertEquals(expectedResponse, actualResponse);
138     }
139
140     @Test
141     public void deleteServiceInstanceTest() throws IOException {
142         String uri = ONAP3GPPSERVICES_URI + "v1/deAllocate";
143         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonNssmfTest")).willReturn(
144                 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/BPMN_response.json")
145                         .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
146         String expectedResponse =
147                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
148         ResponseEntity<String> response = sendRequest(inputStream("/deAllocate.json"), uri, HttpMethod.DELETE);
149
150         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
151         String actualResponse = response.getBody();
152         assertEquals(expectedResponse, actualResponse);
153     }
154
155     @Test
156     public void activateServiceInstanceTest() throws IOException {
157         String uri = ONAP3GPPSERVICES_URI + "v1/activate";
158         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonNssmfTest")).willReturn(
159                 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/BPMN_response.json")
160                         .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
161         String expectedResponse =
162                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
163         ResponseEntity<String> response = sendRequest(inputStream("/activateRequest.json"), uri, HttpMethod.POST);
164
165         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
166         String actualResponse = response.getBody();
167         assertEquals(expectedResponse, actualResponse);
168     }
169
170     @Test
171     public void deActivateServiceInstance() throws IOException {
172         String uri = ONAP3GPPSERVICES_URI + "v1/deActivate";
173         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/commonNssmfTest")).willReturn(
174                 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/BPMN_response.json")
175                         .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
176         String expectedResponse =
177                 "{\"jobId\":\"db245365e79c47ed88fcd60caa8f6549\",\"status\":\"\",\"statusDescription\":{}}";
178         ResponseEntity<String> response = sendRequest(inputStream("/deActivateRequest.json"), uri, HttpMethod.POST);
179
180         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
181         String actualResponse = response.getBody();
182         assertEquals(expectedResponse, actualResponse);
183     }
184
185     @Test
186     public void getSliceSubnetCapabilitiesTest() throws IOException, ApiException {
187         String request = "{\"subnetTypes\":[\"AN\"]}";
188         QuerySubnetCapability subnetCapabilityRequest = MAPPER.readValue(request, QuerySubnetCapability.class);
189         String expectedResponse =
190                 "{\"AN\":{\"latency\":5,\"maxNumberofUEs\":\"100\",\"maxThroughput\":\"150\",\"terminalDensity\":\"50\"}}";
191         Response response = objUnderTest.getSliceSubnetCapabilities(subnetCapabilityRequest, "v1");
192         String actualResponse = (String) response.getEntity();
193         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
194         assertEquals(expectedResponse, actualResponse);
195     }
196 }
197
198