aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-nfvo/so-etsi-nfvo-ns-lcm/so-etsi-nfvo-ns-lcm-service/src/test/java/org/onap/so/etsi/nfvo/ns/lcm/rest/NsLifecycleManagementControllerTest.java
blob: 4b1195244cbb230d227e0ae266cd844078ce8cb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.etsi.nfvo.ns.lcm.rest;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.so.etsi.nfvo.ns.lcm.Constants;
import org.onap.so.etsi.nfvo.ns.lcm.TestApplication;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.NsRequestProcessingException;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.service.JobExecutorService;
import org.onap.so.etsi.nfvo.ns.lcm.model.CreateNsRequest;
import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
import org.onap.so.etsi.nfvo.ns.lcm.model.InstantiateNsRequest;
import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesNsInstance;
import org.onap.so.etsi.nfvo.ns.lcm.model.TerminateNsRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.google.gson.Gson;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class NsLifecycleManagementControllerTest {
    private static final String EXPECTED_BASE_URL =
            "http://so-etsi-nfvo-ns-lcm.onap:9095/so/so-etsi-nfvo-ns-lcm/v1/api/nslcm/v1";
    private static final String RANDOM_NS_LCM_OP_OCC_ID = UUID.randomUUID().toString();
    private static final String RANDOM_NS_INST_ID = UUID.randomUUID().toString();
    private static final String SERVICE_TYPE = "NetworkService";
    private static final String GLOBAL_CUSTOMER_ID = UUID.randomUUID().toString();
    private static final String EXPECTED_CREATE_REQ_LOCATION_URL =
            EXPECTED_BASE_URL + "/ns_instances/" + RANDOM_NS_INST_ID;
    private static final String EXPECTED_NS_LCM_OP_OCC_REQ_LOCATION_URL =
            EXPECTED_BASE_URL + "/ns_lcm_op_occs/" + RANDOM_NS_LCM_OP_OCC_ID;

    @LocalServerPort
    private int port;

    private TestRestTemplate testRestTemplate;

    @Autowired
    private GsonProvider gsonProvider;

    @MockBean
    private JobExecutorService mockedJobExecutorService;

    @Before
    public void setUp() {
        final Gson gson = gsonProvider.getGson();
        testRestTemplate = new TestRestTemplate(
                new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
    }

    @Test
    public void testCreateNs_ValidCreateNsRequest() throws URISyntaxException {

        final CreateNsRequest createNsRequest = getCreateNsRequest();

        when(mockedJobExecutorService.runCreateNsJob(eq(createNsRequest), eq(GLOBAL_CUSTOMER_ID), eq(SERVICE_TYPE)))
                .thenReturn(new NsInstancesNsInstance().id(RANDOM_NS_INST_ID));

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances";
        final HttpHeaders headers = new HttpHeaders();
        headers.add(Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME, GLOBAL_CUSTOMER_ID);
        final HttpEntity<?> request = new HttpEntity<>(createNsRequest, headers);
        final ResponseEntity<NsInstancesNsInstance> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, NsInstancesNsInstance.class);
        assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());

        final HttpHeaders httpHeaders = responseEntity.getHeaders();
        assertTrue(httpHeaders.containsKey(HttpHeaders.LOCATION));
        final List<String> actual = httpHeaders.get(HttpHeaders.LOCATION);
        assertEquals(1, actual.size());
        assertEquals(EXPECTED_CREATE_REQ_LOCATION_URL, actual.get(0));
    }

    @Test
    public void testCreateNs_createNsRequest_nsRequestProcessingExceptionThrown_returnInlineResponse400()
            throws URISyntaxException {

        final CreateNsRequest createNsRequest = getCreateNsRequest();

        final String message = "Unable to process request";
        when(mockedJobExecutorService.runCreateNsJob(eq(createNsRequest), eq(GLOBAL_CUSTOMER_ID), eq(SERVICE_TYPE)))
                .thenThrow(new NsRequestProcessingException(message, new InlineResponse400().detail(message)));

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances";
        final HttpHeaders headers = new HttpHeaders();
        headers.add(Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME, GLOBAL_CUSTOMER_ID);
        final HttpEntity<?> request = new HttpEntity<>(createNsRequest, headers);
        final ResponseEntity<InlineResponse400> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, InlineResponse400.class);
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());

        final InlineResponse400 body = responseEntity.getBody();
        assertEquals(message, body.getDetail());

    }

    @Test
    public void testCreateNs_createNsRequest_exceptionThrown_returnInlineResponse400() throws URISyntaxException {

        final CreateNsRequest createNsRequest = getCreateNsRequest();

        final String message = "Unable to process request";
        when(mockedJobExecutorService.runCreateNsJob(eq(createNsRequest), eq(GLOBAL_CUSTOMER_ID), eq(SERVICE_TYPE)))
                .thenThrow(new RuntimeException(message));

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances";
        final HttpHeaders headers = new HttpHeaders();
        headers.add(Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME, GLOBAL_CUSTOMER_ID);
        final HttpEntity<?> request = new HttpEntity<>(createNsRequest, headers);
        final ResponseEntity<InlineResponse400> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, InlineResponse400.class);
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());

        final InlineResponse400 body = responseEntity.getBody();
        assertEquals(message, body.getDetail());

    }

    @Test
    public void testCreateNs_ValidDeleteNsRequest() {
        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + UUID.randomUUID().toString();
        final ResponseEntity<Void> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.DELETE, null, Void.class);
        assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
    }

    @Test
    public void testInstantiateNs_ValidInstantiateNsRequest() {

        final InstantiateNsRequest instantiateNsRequest = getInstantiateNsRequest();
        when(mockedJobExecutorService.runInstantiateNsJob(eq(RANDOM_NS_INST_ID), eq(instantiateNsRequest)))
                .thenReturn(RANDOM_NS_LCM_OP_OCC_ID);

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + RANDOM_NS_INST_ID + "/instantiate";
        final HttpEntity<?> request = new HttpEntity<>(instantiateNsRequest);
        final ResponseEntity<Void> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, Void.class);
        assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());

        final HttpHeaders httpHeaders = responseEntity.getHeaders();
        assertTrue(httpHeaders.containsKey(HttpHeaders.LOCATION));
        final List<String> actual = httpHeaders.get(HttpHeaders.LOCATION);
        assertEquals(1, actual.size());
        assertEquals(EXPECTED_NS_LCM_OP_OCC_REQ_LOCATION_URL, actual.get(0));
    }

    @Test
    public void testInstantiateNs_instantiateNsRequest_nsRequestProcessingExceptionThrown_returnInlineResponse400() {
        final String message = "Unable to process request";
        final InstantiateNsRequest instantiateNsRequest = getInstantiateNsRequest();
        when(mockedJobExecutorService.runInstantiateNsJob(eq(RANDOM_NS_INST_ID), eq(instantiateNsRequest)))
                .thenThrow(new NsRequestProcessingException(message, new InlineResponse400().detail(message)));

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + RANDOM_NS_INST_ID + "/instantiate";
        final HttpEntity<?> request = new HttpEntity<>(instantiateNsRequest);
        final ResponseEntity<InlineResponse400> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, InlineResponse400.class);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());
    }

    @Test
    public void testTerminateNs_ValidTerminateNsRequest_Success() {
        final TerminateNsRequest terminateNsRequest = getTerminateNsRequest();
        when(mockedJobExecutorService.runTerminateNsJob(eq(RANDOM_NS_INST_ID), eq(terminateNsRequest)))
                .thenReturn(RANDOM_NS_LCM_OP_OCC_ID);

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + RANDOM_NS_INST_ID + "/terminate";
        final HttpEntity<?> request = new HttpEntity<>(terminateNsRequest);
        final ResponseEntity<Void> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, Void.class);
        assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());

        final HttpHeaders httpHeaders = responseEntity.getHeaders();
        assertTrue(httpHeaders.containsKey(HttpHeaders.LOCATION));
        final List<String> actual = httpHeaders.get(HttpHeaders.LOCATION);
        assertEquals(1, actual.size());
        assertEquals(EXPECTED_NS_LCM_OP_OCC_REQ_LOCATION_URL, actual.get(0));
    }

    @Test
    public void testTerminateNs_ValidTerminateNsRequest_nsRequestProcessingExceptionThrown_returnInlineResponse400() {
        final String errorMessage = "ERROR MESSAGE";
        final TerminateNsRequest terminateNsRequest = getTerminateNsRequest();
        when(mockedJobExecutorService.runTerminateNsJob(eq(RANDOM_NS_INST_ID), eq(terminateNsRequest)))
                .thenThrow(new NsRequestProcessingException(errorMessage));

        final String baseUrl = getNsLcmBaseUrl() + "/ns_instances/" + RANDOM_NS_INST_ID + "/terminate";
        final HttpEntity<?> request = new HttpEntity<>(terminateNsRequest);
        final ResponseEntity<InlineResponse400> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.POST, request, InlineResponse400.class);
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());
    }

    private TerminateNsRequest getTerminateNsRequest() {
        // Only support for the immediate Terminate request; i.e., terminateTime field is empty (not set)
        return new TerminateNsRequest();
    }

    private InstantiateNsRequest getInstantiateNsRequest() {
        return new InstantiateNsRequest().nsFlavourId("FLAVOUR_ID");
    }

    private CreateNsRequest getCreateNsRequest() {
        return new CreateNsRequest().nsdId(RANDOM_NS_INST_ID);
    }

    private String getNsLcmBaseUrl() {
        return "http://localhost:" + port + Constants.NS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
    }
}