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
10 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
20 package com.oransc.rappmanager.dme.service;
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;
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;
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;
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)
73 class DmeDeployerTest {
75 MockRestServiceServer mockServer;
77 DmeDeployer dmeDeployer;
79 RestTemplate restTemplate;
81 DmeConfiguration dmeConfiguration;
83 RappDmeResourceBuilder rappDmeResourceBuilder = new RappDmeResourceBuilder();
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();
90 String URI_INFO_TYPES, URI_INFO_TYPE, URI_INFO_PRODUCER, URI_INFO_CONSUMER;
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";
102 mockServer = MockRestServiceServer.createServer(restTemplate);
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)) {
122 assertNull(rapp.getReason());
124 assertNotNull(rapp.getReason());
128 private static Stream<Arguments> getSuccessParamsWithUnavailableInfoTypes() {
129 return Stream.of(Arguments.of(validRappFile, true), Arguments.of(validRappFileNewInfoType, false));
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)) {
147 assertNull(rapp.getReason());
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));
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());
175 void testDeprimeRapp() {
176 Rapp rapp = getRapp(Optional.empty());
177 assertTrue(dmeDeployer.deprimeRapp(rapp));
178 assertNull(rapp.getReason());
182 void testDeployrAppInstanceSuccess() {
183 Rapp rapp = getRapp(Optional.empty());
184 RappInstance rappInstance = rappDmeResourceBuilder.getRappInstance();
185 assertTrue(dmeDeployer.deployRappInstance(rapp, rappInstance));
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));
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();