2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.etsi.nfvo.ns.lcm.rest;
23 * @author Waqas Ikram (waqas.ikram@est.tech)
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.when;
31 import java.net.URISyntaxException;
32 import java.time.LocalDateTime;
33 import java.util.List;
34 import java.util.UUID;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.so.etsi.nfvo.ns.lcm.Constants;
39 import org.onap.so.etsi.nfvo.ns.lcm.TestApplication;
40 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
41 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.NsRequestProcessingException;
42 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.service.JobExecutorService;
43 import org.onap.so.etsi.nfvo.ns.lcm.model.CreateNsRequest;
44 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
45 import org.onap.so.etsi.nfvo.ns.lcm.model.InstantiateNsRequest;
46 import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesNsInstance;
47 import org.onap.so.etsi.nfvo.ns.lcm.model.TerminateNsRequest;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.mock.mockito.MockBean;
51 import org.springframework.boot.test.web.client.TestRestTemplate;
52 import org.springframework.boot.web.client.RestTemplateBuilder;
53 import org.springframework.boot.web.server.LocalServerPort;
54 import org.springframework.http.HttpEntity;
55 import org.springframework.http.HttpHeaders;
56 import org.springframework.http.HttpMethod;
57 import org.springframework.http.HttpStatus;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.http.converter.json.GsonHttpMessageConverter;
60 import org.springframework.test.context.ActiveProfiles;
61 import org.springframework.test.context.junit4.SpringRunner;
62 import com.google.gson.Gson;
65 * @author Waqas Ikram (waqas.ikram@est.tech)
68 @RunWith(SpringRunner.class)
69 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
70 @ActiveProfiles("test")
71 public class NsLifecycleManagementControllerTest {
72 private static final String RANDOM_NS_INST_ID = UUID.randomUUID().toString();
73 private static final String SERVICE_TYPE = "NetworkService";
74 private static final String GLOBAL_CUSTOMER_ID = UUID.randomUUID().toString();
75 private static final String EXPECTED_LOCATION_URL = "http://etsi-so-ns-lcm-manager-service:9095"
76 + "/so/so-etsi-nfvo-ns-lcm/v1/" + "api/nslcm/v1/ns_instances/" + RANDOM_NS_INST_ID;
81 private TestRestTemplate testRestTemplate;
84 private GsonProvider gsonProvider;
87 private JobExecutorService mockedJobExecutorService;
91 final Gson gson = gsonProvider.getGson();
92 testRestTemplate = new TestRestTemplate(
93 new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
97 public void testCreateNs_ValidCreateNsRequest() throws URISyntaxException {
99 final CreateNsRequest createNsRequest = getCreateNsRequest();
101 when(mockedJobExecutorService.runCreateNsJob(eq(createNsRequest), eq(GLOBAL_CUSTOMER_ID), eq(SERVICE_TYPE)))
102 .thenReturn(new NsInstancesNsInstance().id(RANDOM_NS_INST_ID));
104 final String baseUrl = getNsLcmBaseUrl() + "/ns_instances";
105 final HttpHeaders headers = new HttpHeaders();
106 headers.add(Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME, GLOBAL_CUSTOMER_ID);
107 final HttpEntity<?> request = new HttpEntity<>(createNsRequest, headers);
108 final ResponseEntity<NsInstancesNsInstance> responseEntity =
109 testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, NsInstancesNsInstance.class);
110 assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
111 assertTrue(responseEntity.hasBody());
112 assertNotNull(responseEntity.getBody());
114 final HttpHeaders httpHeaders = responseEntity.getHeaders();
115 assertTrue(httpHeaders.containsKey(HttpHeaders.LOCATION));
116 final List<String> actual = httpHeaders.get(HttpHeaders.LOCATION);
117 assertEquals(1, actual.size());
118 assertEquals(EXPECTED_LOCATION_URL, actual.get(0));
122 public void testCreateNs_createNsRequest_nsRequestProcessingExceptionThrown_returnInlineResponse400()
123 throws URISyntaxException {
125 final CreateNsRequest createNsRequest = getCreateNsRequest();
127 final String message = "Unable to process request";
128 when(mockedJobExecutorService.runCreateNsJob(eq(createNsRequest), eq(GLOBAL_CUSTOMER_ID), eq(SERVICE_TYPE)))
129 .thenThrow(new NsRequestProcessingException(message, new InlineResponse400().detail(message)));
131 final String baseUrl = getNsLcmBaseUrl() + "/ns_instances";
132 final HttpHeaders headers = new HttpHeaders();
133 headers.add(Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME, GLOBAL_CUSTOMER_ID);
134 final HttpEntity<?> request = new HttpEntity<>(createNsRequest, headers);
135 final ResponseEntity<InlineResponse400> responseEntity =
136 testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, InlineResponse400.class);
137 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
138 assertTrue(responseEntity.hasBody());
139 assertNotNull(responseEntity.getBody());
141 final InlineResponse400 body = responseEntity.getBody();
142 assertEquals(message, body.getDetail());
147 public void testCreateNs_createNsRequest_exceptionThrown_returnInlineResponse400() throws URISyntaxException {
149 final CreateNsRequest createNsRequest = getCreateNsRequest();
151 final String message = "Unable to process request";
152 when(mockedJobExecutorService.runCreateNsJob(eq(createNsRequest), eq(GLOBAL_CUSTOMER_ID), eq(SERVICE_TYPE)))
153 .thenThrow(new RuntimeException(message));
155 final String baseUrl = getNsLcmBaseUrl() + "/ns_instances";
156 final HttpHeaders headers = new HttpHeaders();
157 headers.add(Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME, GLOBAL_CUSTOMER_ID);
158 final HttpEntity<?> request = new HttpEntity<>(createNsRequest, headers);
159 final ResponseEntity<InlineResponse400> responseEntity =
160 testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, InlineResponse400.class);
161 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
162 assertTrue(responseEntity.hasBody());
163 assertNotNull(responseEntity.getBody());
165 final InlineResponse400 body = responseEntity.getBody();
166 assertEquals(message, body.getDetail());
171 public void testCreateNs_ValidDeleteNsRequest() {
172 final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + UUID.randomUUID().toString();
173 final ResponseEntity<Void> responseEntity =
174 testRestTemplate.exchange(baseUrl, HttpMethod.DELETE, null, Void.class);
175 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
179 public void testInstantiateNs_ValidInstantiateNsRequest() {
180 final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + UUID.randomUUID().toString() + "/instantiate";
181 final HttpEntity<?> request = new HttpEntity<>(getInstantiateNsRequest());
182 final ResponseEntity<Void> responseEntity =
183 testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, Void.class);
184 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
188 public void testTerminateNs_ValidInstantiateNsRequest() {
189 final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + UUID.randomUUID().toString() + "/terminate";
190 final HttpEntity<?> request = new HttpEntity<>(getTerminateNsRequest());
191 final ResponseEntity<Void> responseEntity =
192 testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, Void.class);
193 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
197 private TerminateNsRequest getTerminateNsRequest() {
198 return new TerminateNsRequest().terminationTime(LocalDateTime.now());
201 private InstantiateNsRequest getInstantiateNsRequest() {
202 return new InstantiateNsRequest().nsFlavourId("FLAVOUR_ID");
205 private CreateNsRequest getCreateNsRequest() {
206 return new CreateNsRequest().nsdId(RANDOM_NS_INST_ID);
209 private String getNsLcmBaseUrl() {
210 return "http://localhost:" + port + Constants.NS_LIFE_CYCLE_MANAGEMENT_BASE_URL;