Merge "Removing so-monitoring module"
[so.git] / 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 / NsLcmOperationOccurrencesControllerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.etsi.nfvo.ns.lcm.rest;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import java.time.LocalDateTime;
26 import java.util.Optional;
27 import java.util.UUID;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.so.etsi.nfvo.ns.lcm.Constants;
32 import org.onap.so.etsi.nfvo.ns.lcm.TestApplication;
33 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
34 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
35 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NsLcmOpOcc;
36 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NsLcmOpType;
37 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.OperationStateEnum;
38 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
39 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
40 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
41 import org.onap.so.etsi.nfvo.ns.lcm.model.NsLcmOpOccsNsLcmOpOcc;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.web.client.TestRestTemplate;
45 import org.springframework.boot.web.client.RestTemplateBuilder;
46 import org.springframework.boot.web.server.LocalServerPort;
47 import org.springframework.http.HttpEntity;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.http.converter.json.GsonHttpMessageConverter;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit4.SpringRunner;
55 import com.google.gson.Gson;
56
57 /**
58  * @author Waqas Ikram (waqas.ikram@est.tech)
59  * @author Andrew Lamb (andrew.a.lamb@est.tech)
60  *
61  */
62
63 @RunWith(SpringRunner.class)
64 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
65 @ActiveProfiles("test")
66 public class NsLcmOperationOccurrencesControllerTest {
67
68     private static final String NS_LCM_OP_OCCS = "/ns_lcm_op_occs/";
69
70     @LocalServerPort
71     private int port;
72
73     @Autowired
74     private DatabaseServiceProvider databaseServiceProvider;
75
76     @Autowired
77     private GsonProvider gsonProvider;
78
79     private TestRestTemplate testRestTemplate;
80
81     @Before
82     public void setUp() {
83         final Gson gson = gsonProvider.getGson();
84         testRestTemplate = new TestRestTemplate(
85                 new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
86     }
87
88     @Test
89     public void testGetOperationStatus_validNsLcmOpOccId_returnsNsLcmOpOcc() {
90         final String nsLcmOpOccId = addDummyNsLcmOpOccToDatabase();
91         final String baseUrl = getNsLcmBaseUrl() + NS_LCM_OP_OCCS + nsLcmOpOccId;
92         final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
93         final ResponseEntity<NsLcmOpOccsNsLcmOpOcc> responseEntity =
94                 testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, NsLcmOpOccsNsLcmOpOcc.class);
95         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
96         assertTrue(responseEntity.hasBody());
97         assertNotNull(responseEntity.getBody());
98     }
99
100     @Test
101     public void testGetOperationStatus_nsLcmOpOccIdNotFound_returnsInlineResponse400() {
102         final String nsLcmOpOccId = UUID.randomUUID().toString();
103         final Optional<NsLcmOpOcc> optionalNsLcmOpOcc = databaseServiceProvider.getNsLcmOpOcc(nsLcmOpOccId);
104         assertTrue(optionalNsLcmOpOcc.isEmpty());
105         final String baseUrl = getNsLcmBaseUrl() + NS_LCM_OP_OCCS + nsLcmOpOccId;
106         final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
107         final ResponseEntity<InlineResponse400> responseEntity =
108                 testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, InlineResponse400.class);
109         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
110         assertTrue(responseEntity.hasBody());
111         assertNotNull(responseEntity.getBody());
112     }
113
114     private String addDummyNsLcmOpOccToDatabase() {
115         final LocalDateTime currentDateTime = LocalDateTime.now();
116
117         final NfvoNsInst nsInst = new NfvoNsInst().name("name").nsdId("id").status(State.NOT_INSTANTIATED)
118                 .nsdInvariantId("id").statusUpdatedTime(currentDateTime);
119         databaseServiceProvider.saveNfvoNsInst(nsInst);
120
121         final NsLcmOpOcc nsLcmOpOcc = new NsLcmOpOcc().nfvoNsInst(nsInst).operationState(OperationStateEnum.PROCESSING)
122                 .isCancelPending(false).isAutoInvocation(false).operation(NsLcmOpType.INSTANTIATE)
123                 .startTime(currentDateTime).stateEnteredTime(currentDateTime).operationParams("");
124         databaseServiceProvider.addNSLcmOpOcc(nsLcmOpOcc);
125
126         return nsLcmOpOcc.getId();
127     }
128
129     private String getNsLcmBaseUrl() {
130         return "http://localhost:" + port + Constants.NS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
131     }
132 }
133