Fix sonar issue
[nonrtric/plt/rappmanager.git] / rapp-manager-dme / src / test / java / com / oransc / rappmanager / dme / service / DmeDeployerTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END========================================================================
18  */
19
20 package com.oransc.rappmanager.dme.service;
21
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
27 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
28 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
29
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.oransc.rappmanager.dme.configuration.DmeConfiguration;
33 import com.oransc.rappmanager.models.cache.RappCacheService;
34 import com.oransc.rappmanager.models.configuration.RappsEnvironmentConfiguration;
35 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
36 import com.oransc.rappmanager.models.rapp.Rapp;
37 import com.oransc.rappmanager.models.rapp.RappDmeResourceBuilder;
38 import com.oransc.rappmanager.models.rapp.RappResources;
39 import com.oransc.rappmanager.models.rapp.RappState;
40 import com.oransc.rappmanager.models.rappinstance.RappInstance;
41 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
42 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachineConfig;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Optional;
46 import java.util.Set;
47 import java.util.UUID;
48 import java.util.stream.Stream;
49 import org.junit.jupiter.api.BeforeAll;
50 import org.junit.jupiter.api.BeforeEach;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.api.TestInstance;
53 import org.junit.jupiter.params.ParameterizedTest;
54 import org.junit.jupiter.params.provider.Arguments;
55 import org.junit.jupiter.params.provider.MethodSource;
56 import org.junit.jupiter.params.provider.ValueSource;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
59 import org.springframework.boot.test.context.SpringBootTest;
60 import org.springframework.boot.test.mock.mockito.SpyBean;
61 import org.springframework.http.HttpMethod;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.http.MediaType;
64 import org.springframework.test.web.client.ExpectedCount;
65 import org.springframework.test.web.client.MockRestServiceServer;
66 import org.springframework.web.client.RestTemplate;
67
68 @SpringBootTest(classes = {DmeConfiguration.class, DmeDeployer.class, BeanTestConfiguration.class,
69         RappsEnvironmentConfiguration.class, RappCsarConfigurationHandler.class, RappCacheService.class,
70         RappInstanceStateMachineConfig.class, RappInstanceStateMachine.class})
71 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
72 @AutoConfigureMockMvc
73 class DmeDeployerTest {
74
75     MockRestServiceServer mockServer;
76     @SpyBean
77     DmeDeployer dmeDeployer;
78     @Autowired
79     RestTemplate restTemplate;
80     @Autowired
81     DmeConfiguration dmeConfiguration;
82
83     RappDmeResourceBuilder rappDmeResourceBuilder = new RappDmeResourceBuilder();
84
85     private static final String validRappFile = "valid-rapp-package.csar";
86     private static final String validRappFileNewInfoType = "valid-rapp-package-new-info-type.csar";
87     String validCsarFileLocation = "src/test/resources/";
88     ObjectMapper objectMapper = new ObjectMapper();
89
90     String URI_INFO_TYPES, URI_INFO_TYPE, URI_INFO_PRODUCER, URI_INFO_CONSUMER;
91
92     @BeforeAll
93     void initACMURI() {
94         URI_INFO_TYPES = dmeConfiguration.getBaseUrl() + "/data-producer/v1/info-types";
95         URI_INFO_TYPE = dmeConfiguration.getBaseUrl() + "/data-producer/v1/info-types/%s";
96         URI_INFO_PRODUCER = dmeConfiguration.getBaseUrl() + "/data-producer/v1/info-producers/%s";
97         URI_INFO_CONSUMER = dmeConfiguration.getBaseUrl() + "/data-consumer/v1/info-jobs/%s";
98     }
99
100     @BeforeEach
101     public void init() {
102         mockServer = MockRestServiceServer.createServer(restTemplate);
103     }
104
105     @ParameterizedTest
106     @MethodSource("getSuccessParamsWithUnavailableInfoTypes")
107     void testPrimeRappSuccessWithUnavailableInfoType(String rappFile, boolean isSuccess)
108             throws JsonProcessingException {
109         RappResources rappResources = rappDmeResourceBuilder.getResources();
110         Rapp rapp = getRapp(Optional.empty());
111         rapp.setPackageName(rappFile);
112         rapp.setRappResources(rappResources);
113         List<String> infoTypes = List.of();
114         mockServer.expect(ExpectedCount.once(), requestTo(URI_INFO_TYPES)).andExpect(method(HttpMethod.GET)).andRespond(
115                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
116                         .body(objectMapper.writeValueAsString(infoTypes)));
117         assertTrue(dmeDeployer.primeRapp(rapp));
118         if (rappFile.equals(validRappFileNewInfoType)) {
119             mockServer.verify();
120         }
121         if (isSuccess) {
122             assertNull(rapp.getReason());
123         } else {
124             assertNotNull(rapp.getReason());
125         }
126     }
127
128     private static Stream<Arguments> getSuccessParamsWithUnavailableInfoTypes() {
129         return Stream.of(Arguments.of(validRappFile, true), Arguments.of(validRappFileNewInfoType, false));
130     }
131
132     @ParameterizedTest
133     @ValueSource(strings = {validRappFile, validRappFileNewInfoType})
134     void testPrimeRappSuccessWithValidInfoType(String rappFile) throws JsonProcessingException {
135         RappResources rappResources = rappDmeResourceBuilder.getResources();
136         Rapp rapp = getRapp(Optional.empty());
137         rapp.setPackageName(rappFile);
138         rapp.setRappResources(rappResources);
139         List<String> infoTypes = List.of("new-info-type-not-available");
140         mockServer.expect(ExpectedCount.once(), requestTo(URI_INFO_TYPES)).andExpect(method(HttpMethod.GET)).andRespond(
141                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
142                         .body(objectMapper.writeValueAsString(infoTypes)));
143         assertTrue(dmeDeployer.primeRapp(rapp));
144         if (rappFile.equals(validRappFileNewInfoType)) {
145             mockServer.verify();
146         }
147         assertNull(rapp.getReason());
148     }
149
150     @Test
151     void testPrimeRappWithoutDme() {
152         RappResources rappResources = rappDmeResourceBuilder.getResources();
153         rappResources.setDme(null);
154         Rapp rapp = getRapp(Optional.empty());
155         rapp.setPackageName(validRappFile);
156         rapp.setRappResources(rappResources);
157         assertTrue(dmeDeployer.primeRapp(rapp));
158     }
159
160     @Test
161     void testPrimeRappFailure() {
162         RappResources rappResources = rappDmeResourceBuilder.getResources();
163         RappResources.DMEResources dme = rappResources.getDme();
164         Set<String> infoProducers = new HashSet<>(rappResources.getDme().getInfoProducers());
165         infoProducers.add("invalid-producer-not-available-in-rapp");
166         dme.setInfoProducers(infoProducers);
167         rappResources.setDme(dme);
168         Rapp rapp = getRapp(Optional.empty());
169         rapp.setRappResources(rappResources);
170         assertFalse(dmeDeployer.primeRapp(rapp));
171         assertFalse(rapp.getReason().isEmpty());
172     }
173
174     @Test
175     void testDeprimeRapp() {
176         Rapp rapp = getRapp(Optional.empty());
177         assertTrue(dmeDeployer.deprimeRapp(rapp));
178         assertNull(rapp.getReason());
179     }
180
181     @Test
182     void testDeployrAppInstanceSuccess() {
183         Rapp rapp = getRapp(Optional.empty());
184         RappInstance rappInstance = rappDmeResourceBuilder.getRappInstance();
185         assertTrue(dmeDeployer.deployRappInstance(rapp, rappInstance));
186     }
187
188     @Test
189     void testUndeployrAppInstanceSuccess() {
190         Rapp rapp = getRapp(Optional.empty());
191         rapp.setState(RappState.PRIMED);
192         RappInstance rappInstance = rappDmeResourceBuilder.getRappInstance();
193         assertTrue(dmeDeployer.undeployRappInstance(rapp, rappInstance));
194     }
195
196     Rapp getRapp(Optional<UUID> rappOptional) {
197         return Rapp.builder().rappId(rappOptional.orElse(UUID.randomUUID())).name("").packageName(validRappFile)
198                        .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
199     }
200 }