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