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.doThrow;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.google.gson.JsonSyntaxException;
32 import com.oransc.rappmanager.models.rapp.Rapp;
33 import com.oransc.rappmanager.models.rapp.RappResources;
34 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
35 import com.oransc.rappmanager.models.rappinstance.RappDMEInstance;
36 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
37 import java.io.ByteArrayOutputStream;
39 import java.util.List;
41 import java.util.UUID;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.params.ParameterizedTest;
46 import org.junit.jupiter.params.provider.ValueSource;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.mock.mockito.SpyBean;
49 import org.springframework.test.context.ContextConfiguration;
52 @ContextConfiguration(classes = {ObjectMapper.class, RappCsarConfigurationHandler.class})
53 class RappCsarConfigurationHandlerTest {
56 RappCsarConfigurationHandler rappCsarConfigurationHandler;
58 String validCsarFileLocation = "src/test/resources/";
60 private final String validRappFile = "valid-rapp-package.csar";
62 private final String invalidRappNoAsdFile = "invalid-rapp-package-no-asd-yaml.csar";
64 private final String invalidRappEmptyAsdFile = "invalid-rapp-package-empty-asd-yaml.csar";
67 void testCsarInstantiationPayload() throws JSONException {
68 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
69 UUID compositionId = UUID.randomUUID();
70 RappACMInstance rappACMInstance = new RappACMInstance();
71 rappACMInstance.setInstance("kserve-instance");
72 JSONObject jsonObject = new JSONObject(
73 rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappACMInstance, compositionId));
74 assertEquals(jsonObject.get("compositionId"), String.valueOf(compositionId));
78 void testFileListing() {
79 File file = new File(validCsarFileLocation + validRappFile);
80 Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, "Files/Sme/serviceapis/");
81 assertThat(fileListFromCsar).hasSize(2);
85 void testInvalidFileListing() {
86 File file = new File(validCsarFileLocation);
87 Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
88 assertThat(fileListFromCsar).isEmpty();
92 void testInvalidFileListingFromCsar() {
93 File file = new File("InvalidFile");
94 ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
95 assertThat(fileByteArray.size()).isZero();
99 void testListResources() {
100 UUID rappId = UUID.randomUUID();
102 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
104 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
105 assertThat(rappResources).isNotNull();
106 assertNotNull(rappResources.getAcm().getCompositionDefinitions());
107 assertThat(rappResources.getAcm().getCompositionInstances()).hasSize(4);
108 assertThat(rappResources.getSme().getProviderFunctions()).hasSize(4);
109 assertThat(rappResources.getSme().getServiceApis()).hasSize(2);
110 assertThat(rappResources.getSme().getInvokers()).hasSize(2);
111 assertThat(rappResources.getDme().getProducerInfoTypes()).hasSize(2);
112 assertThat(rappResources.getDme().getConsumerInfoTypes()).hasSize(2);
113 assertThat(rappResources.getDme().getInfoProducers()).hasSize(2);
114 assertThat(rappResources.getDme().getInfoConsumers()).hasSize(2);
118 void testListInvalidResources() {
119 UUID rappId = UUID.randomUUID();
120 Rapp rapp = Rapp.builder().rappId(rappId).name("").packageName("").packageLocation("").build();
121 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
122 assertThat(rappResources).isNotNull();
123 assertNull(rappResources.getAcm());
124 assertNull(rappResources.getSme());
125 assertNull(rappResources.getDme());
129 void testGetAcmCompositionPayload() {
130 UUID rappId = UUID.randomUUID();
131 RappResources rappResources = new RappResources();
132 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("compositions")
133 .compositionInstances(Set.of()).build());
135 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
136 .rappResources(rappResources).build();
137 String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
138 assertNotNull(acmCompositionPayload);
142 void testGetInvalidAcmCompositionPayload() {
143 UUID rappId = UUID.randomUUID();
144 RappResources rappResources = new RappResources();
145 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("invalidcomposition")
146 .compositionInstances(Set.of()).build());
148 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
149 .rappResources(rappResources).build();
150 String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
151 assertEquals("", acmCompositionPayload);
155 void testGetSmeProviderDomainPayload() {
156 UUID rappId = UUID.randomUUID();
157 RappSMEInstance rappSMEInstance = new RappSMEInstance();
158 rappSMEInstance.setProviderFunction("aef-provider-function");
160 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
162 String smeProviderDomainPayload =
163 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
164 assertNotNull(smeProviderDomainPayload);
168 void testGetSmeServiceApiPayload() {
169 UUID rappId = UUID.randomUUID();
170 RappSMEInstance rappSMEInstance = new RappSMEInstance();
171 rappSMEInstance.setServiceApis("api-set-1");
173 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
175 String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
176 assertNotNull(smeProviderDomainPayload);
180 void testGetSmeInvokerPayload() {
181 UUID rappId = UUID.randomUUID();
182 RappSMEInstance rappSMEInstance = new RappSMEInstance();
183 rappSMEInstance.setServiceApis("invoker-app1");
185 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
187 String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappSMEInstance);
188 assertNotNull(smeProviderDomainPayload);
192 void testGetDmeProducerInfoTypePayload() {
193 UUID rappId = UUID.randomUUID();
194 RappDMEInstance rappDMEInstance = new RappDMEInstance();
195 rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
197 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
199 String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
200 rappDMEInstance.getInfoTypesProducer().iterator().next());
201 assertNotNull(dmeInfoTypePayload);
205 void testGetDmeConsumerInfoTypePayload() {
206 UUID rappId = UUID.randomUUID();
207 RappDMEInstance rappDMEInstance = new RappDMEInstance();
208 rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
210 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
212 String dmeInfoTypePayload =
213 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
214 assertNotNull(dmeInfoTypePayload);
218 void testGetDmeInfoProducerPayload() {
219 UUID rappId = UUID.randomUUID();
220 RappDMEInstance rappDMEInstance = new RappDMEInstance();
221 rappDMEInstance.setInfoProducer("json-file-data-producer");
223 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
225 String dmeInfoProducerPayload =
226 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
227 assertNotNull(dmeInfoProducerPayload);
231 void testGetDmeInfoConsumerPayload() {
232 UUID rappId = UUID.randomUUID();
233 RappDMEInstance rappDMEInstance = new RappDMEInstance();
234 rappDMEInstance.setInfoConsumer("json-file-consumer");
236 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
238 String dmeInfoConsumerPayload =
239 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
240 assertNotNull(dmeInfoConsumerPayload);
244 void testListDeploymentItems() {
245 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
246 List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
247 assertEquals(2, deploymentItems.size());
251 @ValueSource(strings = {invalidRappNoAsdFile, invalidRappEmptyAsdFile})
252 void testListDeploymentItemsNoAsd(String packageName) {
253 Rapp rapp = Rapp.builder().name("").packageName(packageName).packageLocation(validCsarFileLocation).build();
254 assertThat(rappCsarConfigurationHandler.getDeploymentItems(rapp)).isEmpty();
258 void testListDeploymentItemsWithException() throws JsonProcessingException {
259 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
260 doThrow(new JsonSyntaxException("")).when(rappCsarConfigurationHandler).getAsdContentNode(any());
261 assertThat(rappCsarConfigurationHandler.getDeploymentItems(rapp)).isEmpty();
265 void testGetArtifactPayload() {
266 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
267 assertNotNull(rappCsarConfigurationHandler.getArtifactPayload(rapp,
268 "Artifacts/Deployment/HELM/ransliceassurance-1.0.0.tgz"));