4f1a0b64cd6ea97d2f2bc3e02c5ddfeadb2cae75
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.cnfm.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.UUID;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.so.cnfm.lcm.Constants;
31 import org.onap.so.cnfm.lcm.TestApplication;
32 import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider;
33 import org.onap.so.cnfm.lcm.database.beans.AsInst;
34 import org.onap.so.cnfm.lcm.database.beans.AsLcmOpType;
35 import org.onap.so.cnfm.lcm.database.beans.OperationStateEnum;
36 import org.onap.so.cnfm.lcm.database.beans.State;
37 import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
38 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
39 import org.onap.so.cnfm.lcm.model.ErrorDetails;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.web.client.TestRestTemplate;
43 import org.springframework.boot.web.client.RestTemplateBuilder;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.http.HttpEntity;
46 import org.springframework.http.HttpHeaders;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.http.converter.json.GsonHttpMessageConverter;
51 import org.springframework.test.context.ActiveProfiles;
52 import org.springframework.test.context.junit4.SpringRunner;
53 import com.google.gson.Gson;
54
55 /**
56  *
57  * @author Waqas Ikram (waqas.ikram@est.tech)
58  *
59  */
60 @RunWith(SpringRunner.class)
61 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
62 @ActiveProfiles("test")
63 public class AsLcmOperationOccurrencesControllerTest {
64
65     private static final String RANDOM_UUID = UUID.randomUUID().toString();
66
67     private static final String AS_LCM_OP_OCCS = "/as_lcm_op_occs/";
68
69     @LocalServerPort
70     private int port;
71
72     @Autowired
73     private DatabaseServiceProvider databaseServiceProvider;
74
75     @Autowired
76     private GsonProvider gsonProvider;
77
78     private TestRestTemplate testRestTemplate;
79
80     @Before
81     public void setUp() {
82         final Gson gson = gsonProvider.getGson();
83         testRestTemplate = new TestRestTemplate(
84                 new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
85     }
86
87     @Test
88     public void testGetOperationStatus_validAsLcmOpOccId_returnsAsLcmOpOcc() {
89         final String asLcmOpOccId = addDummyAsLcmOpOccToDatabase();
90         final String baseUrl = getAsLcmBaseUrl() + AS_LCM_OP_OCCS + asLcmOpOccId;
91         final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
92         final ResponseEntity<AsLcmOpOcc> responseEntity =
93                 testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, AsLcmOpOcc.class);
94         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
95         assertTrue(responseEntity.hasBody());
96         assertNotNull(responseEntity.getBody());
97     }
98
99     @Test
100     public void testGetOperationStatus_invalidAsLcmOpOccId_returnsErrorDetails() {
101         final String baseUrl = getAsLcmBaseUrl() + AS_LCM_OP_OCCS + "123";
102         final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
103         final ResponseEntity<ErrorDetails> responseEntity =
104                 testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, ErrorDetails.class);
105         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
106         assertTrue(responseEntity.hasBody());
107         assertNotNull(responseEntity.getBody());
108     }
109
110     private String getAsLcmBaseUrl() {
111         return "http://localhost:" + port + Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
112     }
113
114     private String addDummyAsLcmOpOccToDatabase() {
115         final LocalDateTime now = LocalDateTime.now();
116         final AsInst asInst = new AsInst().name("name").asdId(RANDOM_UUID).status(State.NOT_INSTANTIATED)
117                 .asdInvariantId(RANDOM_UUID).statusUpdatedTime(now).asApplicationName("asApplicationName")
118                 .asApplicationVersion("asApplicationVersion").asProvider("asProvider").serviceInstanceId(RANDOM_UUID)
119                 .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion")
120                 .tenantId("tenantId");
121
122         databaseServiceProvider.saveAsInst(asInst);
123
124         final org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc databaseEntry =
125                 new org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc();
126
127         databaseEntry.asInst(asInst).operationState(OperationStateEnum.PROCESSING).isCancelPending(false)
128                 .isAutoInvocation(false).operation(AsLcmOpType.INSTANTIATE).startTime(now).stateEnteredTime(now)
129                 .operationParams("");
130
131         databaseServiceProvider.addAsLcmOpOcc(databaseEntry);
132
133         return databaseEntry.getId();
134     }
135
136 }