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.models.csar;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.google.gson.JsonSyntaxException;
35 import com.oransc.rappmanager.models.configuration.RappsEnvironmentConfiguration;
36 import com.oransc.rappmanager.models.rapp.Rapp;
37 import com.oransc.rappmanager.models.rapp.RappResources;
38 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
39 import com.oransc.rappmanager.models.rappinstance.RappDMEInstance;
40 import com.oransc.rappmanager.models.rappinstance.RappInstance;
41 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
42 import java.io.ByteArrayOutputStream;
44 import java.io.IOException;
46 import java.util.UUID;
47 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
48 import org.json.JSONArray;
49 import org.json.JSONException;
50 import org.json.JSONObject;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.params.ParameterizedTest;
53 import org.junit.jupiter.params.provider.ValueSource;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.context.properties.EnableConfigurationProperties;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.mock.mockito.SpyBean;
58 import org.springframework.test.context.ContextConfiguration;
61 @EnableConfigurationProperties
62 @ContextConfiguration(
63 classes = {ObjectMapper.class, RappsEnvironmentConfiguration.class, RappCsarConfigurationHandler.class})
64 class RappCsarConfigurationHandlerTest {
67 RappCsarConfigurationHandler rappCsarConfigurationHandler;
69 RappsEnvironmentConfiguration rappsEnvironmentConfiguration;
71 ObjectMapper objectMapper;
73 String validCsarFileLocation = "src/test/resources/";
75 private final String validRappFile = "valid-rapp-package.csar";
77 private final String invalidRappNoAsdFile = "invalid-rapp-package-no-asd-yaml.csar";
79 private final String invalidRappEmptyAsdFile = "invalid-rapp-package-empty-asd-yaml.csar";
82 void testCsarInstantiationPayload() throws JSONException, JsonProcessingException {
83 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
84 UUID compositionId = UUID.randomUUID();
85 RappInstance rappInstance = new RappInstance();
86 RappACMInstance rappKserveACMInstance = new RappACMInstance();
87 rappKserveACMInstance.setInstance("kserve-instance");
88 rappInstance.setAcm(rappKserveACMInstance);
89 JSONObject kserveInstanceJsonObject =
90 new JSONObject(rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappInstance, compositionId));
91 assertEquals(kserveInstanceJsonObject.get("compositionId"), String.valueOf(compositionId));
92 RappACMInstance rappK8sACMInstance = new RappACMInstance();
93 rappK8sACMInstance.setInstance("k8s-instance");
94 rappInstance.setAcm(rappK8sACMInstance);
95 JsonNode jsonNode = objectMapper.readTree(
96 rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappInstance, compositionId));
97 assertEquals(jsonNode.get("compositionId").asText(), String.valueOf(compositionId));
98 JsonNode overrideParamsNode = jsonNode.at("/elements").elements().next().at("/properties/chart/overrideParams");
99 System.out.println(overrideParamsNode);
100 assertEquals(overrideParamsNode.get("appId").asText(), rappInstance.getRappInstanceId().toString());
101 assertEquals(overrideParamsNode.get("smeDiscoveryEndpoint").asText(),
102 rappsEnvironmentConfiguration.getSmeDiscoveryEndpoint());
106 void testFileListing() {
107 File file = new File(validCsarFileLocation + validRappFile);
108 Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, "Files/Sme/serviceapis/");
109 assertThat(fileListFromCsar).hasSize(2);
113 void testInvalidFileListing() {
114 File file = new File(validCsarFileLocation);
115 Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
116 assertThat(fileListFromCsar).isEmpty();
120 void testInvalidFileListingFromCsar() {
121 File file = new File("InvalidFile");
122 ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
123 assertThat(fileByteArray.size()).isZero();
127 void testInvalidZipStreamGetFromCsar() throws IOException {
128 ZipArchiveInputStream zipArchiveInputStream = mock(ZipArchiveInputStream.class);
129 doThrow(new IOException()).when(zipArchiveInputStream).getNextEntry();
130 ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(zipArchiveInputStream, null);
131 assertThat(fileByteArray.size()).isZero();
135 void testListResources() {
136 UUID rappId = UUID.randomUUID();
138 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
140 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
141 assertThat(rappResources).isNotNull();
142 assertNotNull(rappResources.getAcm().getCompositionDefinitions());
143 assertThat(rappResources.getAcm().getCompositionInstances()).hasSize(4);
144 assertThat(rappResources.getSme().getProviderFunctions()).hasSize(3);
145 assertThat(rappResources.getSme().getServiceApis()).hasSize(2);
146 assertThat(rappResources.getSme().getInvokers()).hasSize(2);
147 assertThat(rappResources.getDme().getProducerInfoTypes()).hasSize(2);
148 assertThat(rappResources.getDme().getConsumerInfoTypes()).hasSize(2);
149 assertThat(rappResources.getDme().getInfoProducers()).hasSize(2);
150 assertThat(rappResources.getDme().getInfoConsumers()).hasSize(2);
154 void testListInvalidResources() {
155 UUID rappId = UUID.randomUUID();
156 Rapp rapp = Rapp.builder().rappId(rappId).name("").packageName("").packageLocation("").build();
157 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
158 assertThat(rappResources).isNotNull();
159 assertNull(rappResources.getAcm());
160 assertNull(rappResources.getSme());
161 assertNull(rappResources.getDme());
165 void testGetAcmCompositionPayload() {
166 UUID rappId = UUID.randomUUID();
167 RappResources rappResources = new RappResources();
168 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("compositions")
169 .compositionInstances(Set.of()).build());
171 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
172 .rappResources(rappResources).build();
173 String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
174 assertNotNull(acmCompositionPayload);
178 void testGetInvalidAcmCompositionPayload() {
179 UUID rappId = UUID.randomUUID();
180 RappResources rappResources = new RappResources();
181 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("invalidcomposition")
182 .compositionInstances(Set.of()).build());
184 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
185 .rappResources(rappResources).build();
186 String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
187 assertEquals("", acmCompositionPayload);
191 void testGetSmeProviderDomainPayload() {
192 UUID rappId = UUID.randomUUID();
193 RappSMEInstance rappSMEInstance = new RappSMEInstance();
194 rappSMEInstance.setProviderFunction("aef-provider-function");
196 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
198 String smeProviderDomainPayload =
199 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
200 assertNotNull(smeProviderDomainPayload);
204 void testGetSmeServiceApiPayload() {
205 UUID rappId = UUID.randomUUID();
206 RappSMEInstance rappSMEInstance = new RappSMEInstance();
207 rappSMEInstance.setServiceApis("api-set-1");
209 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
211 String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
212 assertNotNull(smeProviderDomainPayload);
216 void testGetSmeInvokerPayload() {
217 UUID rappId = UUID.randomUUID();
218 RappSMEInstance rappSMEInstance = new RappSMEInstance();
219 rappSMEInstance.setInvokers("invoker-app1");
220 RappInstance rappInstance = new RappInstance();
221 rappInstance.setSme(rappSMEInstance);
223 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
225 JSONArray smeInvokerPayload =
226 new JSONArray(rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappInstance));
227 assertNotNull(smeInvokerPayload);
228 assertEquals(smeInvokerPayload.getJSONObject(0).getString("apiInvokerInformation"),
229 rappInstance.getRappInstanceId().toString());
233 void testGetDmeProducerInfoTypePayload() {
234 UUID rappId = UUID.randomUUID();
235 RappDMEInstance rappDMEInstance = new RappDMEInstance();
236 rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
238 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
240 String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
241 rappDMEInstance.getInfoTypesProducer().iterator().next());
242 assertNotNull(dmeInfoTypePayload);
246 void testGetDmeConsumerInfoTypePayload() {
247 UUID rappId = UUID.randomUUID();
248 RappDMEInstance rappDMEInstance = new RappDMEInstance();
249 rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
251 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
253 String dmeInfoTypePayload =
254 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
255 assertNotNull(dmeInfoTypePayload);
259 void testGetDmeInfoProducerPayload() {
260 UUID rappId = UUID.randomUUID();
261 RappDMEInstance rappDMEInstance = new RappDMEInstance();
262 rappDMEInstance.setInfoProducer("json-file-data-producer");
264 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
266 String dmeInfoProducerPayload =
267 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
268 assertNotNull(dmeInfoProducerPayload);
272 void testGetDmeInfoConsumerPayload() {
273 UUID rappId = UUID.randomUUID();
274 RappDMEInstance rappDMEInstance = new RappDMEInstance();
275 rappDMEInstance.setInfoConsumer("json-file-consumer");
277 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
279 String dmeInfoConsumerPayload =
280 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
281 assertNotNull(dmeInfoConsumerPayload);
285 void testGetAsdMetadata() {
286 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
287 AsdMetadata asdMetadata = rappCsarConfigurationHandler.getAsdMetadata(rapp);
288 assertEquals("123e4567-e89b-12d3-a456-426614174000", asdMetadata.getDescriptorId());
289 assertEquals("040eff2a-eb1a-4aff-bd46-37ce38092985", asdMetadata.getDescriptorInvariantId());
290 assertEquals(2, asdMetadata.getDeploymentItems().size());
294 @ValueSource(strings = {invalidRappNoAsdFile, invalidRappEmptyAsdFile})
295 void testGetAsdMetadataNoAsd(String packageName) {
296 Rapp rapp = Rapp.builder().name("").packageName(packageName).packageLocation(validCsarFileLocation).build();
297 assertThat(rappCsarConfigurationHandler.getAsdMetadata(rapp)).isNotNull();
301 void testGetAsdMetadataException() throws JsonProcessingException {
302 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
303 doThrow(new JsonSyntaxException("")).when(rappCsarConfigurationHandler).getAsdContentNode(any());
304 assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorId());
305 assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorInvariantId());
306 assertThat(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDeploymentItems()).isNull();
310 void testGetAsdMetadataNullAsdContent() throws JsonProcessingException {
311 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
312 doReturn(null).when(rappCsarConfigurationHandler).getAsdContentNode(any());
313 assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorId());
314 assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorInvariantId());
315 assertThat(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDeploymentItems()).isNull();
319 void testGetArtifactPayload() {
320 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
321 assertNotNull(rappCsarConfigurationHandler.getArtifactPayload(rapp,
322 "Artifacts/Deployment/HELM/ransliceassurance-1.0.0.tgz"));