Merge "Add installation mode to use snapshot image"
[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.csar.RappCsarConfigurationHandler;
35 import com.oransc.rappmanager.models.rapp.Rapp;
36 import com.oransc.rappmanager.models.rapp.RappDmeResourceBuilder;
37 import com.oransc.rappmanager.models.rapp.RappResources;
38 import com.oransc.rappmanager.models.rapp.RappState;
39 import com.oransc.rappmanager.models.rappinstance.RappInstance;
40 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
41 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachineConfig;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Optional;
45 import java.util.Set;
46 import java.util.UUID;
47 import java.util.stream.Stream;
48 import org.junit.jupiter.api.BeforeAll;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.Test;
51 import org.junit.jupiter.api.TestInstance;
52 import org.junit.jupiter.params.ParameterizedTest;
53 import org.junit.jupiter.params.provider.Arguments;
54 import org.junit.jupiter.params.provider.MethodSource;
55 import org.junit.jupiter.params.provider.ValueSource;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
58 import org.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.boot.test.mock.mockito.SpyBean;
60 import org.springframework.http.HttpMethod;
61 import org.springframework.http.HttpStatus;
62 import org.springframework.http.MediaType;
63 import org.springframework.test.web.client.ExpectedCount;
64 import org.springframework.test.web.client.MockRestServiceServer;
65 import org.springframework.web.client.RestTemplate;
66
67 @SpringBootTest(classes = {DmeConfiguration.class, DmeDeployer.class, BeanTestConfiguration.class,
68         RappCsarConfigurationHandler.class, RappCacheService.class, RappInstanceStateMachineConfig.class,
69         RappInstanceStateMachine.class})
70 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
71 @AutoConfigureMockMvc
72 class DmeDeployerTest {
73
74     MockRestServiceServer mockServer;
75     @SpyBean
76     DmeDeployer dmeDeployer;
77     @Autowired
78     RestTemplate restTemplate;
79     @Autowired
80     DmeConfiguration dmeConfiguration;
81
82     RappDmeResourceBuilder rappDmeResourceBuilder = new RappDmeResourceBuilder();
83
84     private static final String validRappFile = "valid-rapp-package.csar";
85     private static final String validRappFileNewInfoType = "valid-rapp-package-new-info-type.csar";
86     String validCsarFileLocation = "src/test/resources/";
87     ObjectMapper objectMapper = new ObjectMapper();
88
89     String URI_INFO_TYPES, URI_INFO_TYPE, URI_INFO_PRODUCER, URI_INFO_CONSUMER;
90
91     @BeforeAll
92     void initACMURI() {
93         URI_INFO_TYPES = dmeConfiguration.getBaseUrl() + "/data-producer/v1/info-types";
94         URI_INFO_TYPE = dmeConfiguration.getBaseUrl() + "/data-producer/v1/info-types/%s";
95         URI_INFO_PRODUCER = dmeConfiguration.getBaseUrl() + "/data-producer/v1/info-producers/%s";
96         URI_INFO_CONSUMER = dmeConfiguration.getBaseUrl() + "/data-consumer/v1/info-jobs/%s";
97     }
98
99     @BeforeEach
100     public void init() {
101         mockServer = MockRestServiceServer.createServer(restTemplate);
102     }
103
104     @ParameterizedTest
105     @MethodSource("getSuccessParamsWithUnavailableInfoTypes")
106     void testPrimeRappSuccessWithUnavailableInfoType(String rappFile, boolean isSuccess)
107             throws JsonProcessingException {
108         RappResources rappResources = rappDmeResourceBuilder.getResources();
109         Rapp rapp = getRapp(Optional.empty());
110         rapp.setPackageName(rappFile);
111         rapp.setRappResources(rappResources);
112         List<String> infoTypes = List.of();
113         mockServer.expect(ExpectedCount.once(), requestTo(URI_INFO_TYPES)).andExpect(method(HttpMethod.GET)).andRespond(
114                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
115                         .body(objectMapper.writeValueAsString(infoTypes)));
116         assertTrue(dmeDeployer.primeRapp(rapp));
117         if (rappFile.equals(validRappFileNewInfoType)) {
118             mockServer.verify();
119         }
120         if (isSuccess) {
121             assertNull(rapp.getReason());
122         } else {
123             assertNotNull(rapp.getReason());
124         }
125     }
126
127     private static Stream<Arguments> getSuccessParamsWithUnavailableInfoTypes() {
128         return Stream.of(Arguments.of(validRappFile, true), Arguments.of(validRappFileNewInfoType, false));
129     }
130
131     @ParameterizedTest
132     @ValueSource(strings = {validRappFile, validRappFileNewInfoType})
133     void testPrimeRappSuccessWithValidInfoType(String rappFile) throws JsonProcessingException {
134         RappResources rappResources = rappDmeResourceBuilder.getResources();
135         Rapp rapp = getRapp(Optional.empty());
136         rapp.setPackageName(rappFile);
137         rapp.setRappResources(rappResources);
138         List<String> infoTypes = List.of("new-info-type-not-available");
139         mockServer.expect(ExpectedCount.once(), requestTo(URI_INFO_TYPES)).andExpect(method(HttpMethod.GET)).andRespond(
140                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
141                         .body(objectMapper.writeValueAsString(infoTypes)));
142         assertTrue(dmeDeployer.primeRapp(rapp));
143         if (rappFile.equals(validRappFileNewInfoType)) {
144             mockServer.verify();
145         }
146         assertNull(rapp.getReason());
147     }
148
149     @Test
150     void testPrimeRappWithoutDme() throws JsonProcessingException {
151         RappResources rappResources = rappDmeResourceBuilder.getResources();
152         rappResources.setDme(null);
153         Rapp rapp = getRapp(Optional.empty());
154         rapp.setPackageName(validRappFile);
155         rapp.setRappResources(rappResources);
156         assertTrue(dmeDeployer.primeRapp(rapp));
157     }
158
159     @Test
160     void testPrimeRappFailure() {
161         RappResources rappResources = rappDmeResourceBuilder.getResources();
162         RappResources.DMEResources dme = rappResources.getDme();
163         Set<String> infoProducers = new HashSet<>(rappResources.getDme().getInfoProducers());
164         infoProducers.add("invalid-producer-not-available-in-rapp");
165         dme.setInfoProducers(infoProducers);
166         rappResources.setDme(dme);
167         Rapp rapp = getRapp(Optional.empty());
168         rapp.setRappResources(rappResources);
169         assertFalse(dmeDeployer.primeRapp(rapp));
170         assertFalse(rapp.getReason().isEmpty());
171     }
172
173     @Test
174     void testDeprimeRapp() {
175         Rapp rapp = getRapp(Optional.empty());
176         assertTrue(dmeDeployer.deprimeRapp(rapp));
177         assertNull(rapp.getReason());
178     }
179
180     @Test
181     void testDeployrAppInstanceSuccess() {
182         Rapp rapp = getRapp(Optional.empty());
183         RappInstance rappInstance = rappDmeResourceBuilder.getRappInstance();
184         assertTrue(dmeDeployer.deployRappInstance(rapp, rappInstance));
185     }
186
187     @Test
188     void testUndeployrAppInstanceSuccess() {
189         Rapp rapp = getRapp(Optional.empty());
190         rapp.setState(RappState.PRIMED);
191         RappInstance rappInstance = rappDmeResourceBuilder.getRappInstance();
192         assertTrue(dmeDeployer.undeployRappInstance(rapp, rappInstance));
193     }
194
195     Rapp getRapp(Optional<UUID> rappOptional) {
196         return Rapp.builder().rappId(rappOptional.orElse(UUID.randomUUID())).name("").packageName(validRappFile)
197                        .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
198     }
199 }