Generate rApp packages for unit test
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / csar / RappCsarConfigurationHandlerTest.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.models.csar;
21
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;
30
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;
43 import java.io.File;
44 import java.io.IOException;
45 import java.util.Set;
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;
59
60 @SpringBootTest
61 @EnableConfigurationProperties
62 @ContextConfiguration(
63         classes = {ObjectMapper.class, RappsEnvironmentConfiguration.class, RappCsarConfigurationHandler.class})
64 class RappCsarConfigurationHandlerTest {
65
66     @SpyBean
67     RappCsarConfigurationHandler rappCsarConfigurationHandler;
68     @Autowired
69     RappsEnvironmentConfiguration rappsEnvironmentConfiguration;
70     @Autowired
71     ObjectMapper objectMapper;
72
73     String validCsarFileLocation = "src/test/resources/";
74
75     private final String validRappFile = "valid-rapp-package.csar";
76
77     private final String invalidRappNoAsdFile = "invalid-rapp-package-no-asd-yaml.csar";
78
79     private final String invalidRappEmptyAsdFile = "invalid-rapp-package-empty-asd-yaml.csar";
80
81     @Test
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());
103     }
104
105     @Test
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);
110     }
111
112     @Test
113     void testInvalidFileListing() {
114         File file = new File(validCsarFileLocation);
115         Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
116         assertThat(fileListFromCsar).isEmpty();
117     }
118
119     @Test
120     void testInvalidFileListingFromCsar() {
121         File file = new File("InvalidFile");
122         ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
123         assertThat(fileByteArray.size()).isZero();
124     }
125
126     @Test
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();
132     }
133
134     @Test
135     void testListResources() {
136         UUID rappId = UUID.randomUUID();
137         Rapp rapp =
138                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
139                         .build();
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);
151     }
152
153     @Test
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());
162     }
163
164     @Test
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());
170         Rapp rapp =
171                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
172                         .rappResources(rappResources).build();
173         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
174         assertNotNull(acmCompositionPayload);
175     }
176
177     @Test
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());
183         Rapp rapp =
184                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
185                         .rappResources(rappResources).build();
186         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
187         assertEquals("", acmCompositionPayload);
188     }
189
190     @Test
191     void testGetSmeProviderDomainPayload() {
192         UUID rappId = UUID.randomUUID();
193         RappSMEInstance rappSMEInstance = new RappSMEInstance();
194         rappSMEInstance.setProviderFunction("aef-provider-function");
195         Rapp rapp =
196                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
197                         .build();
198         String smeProviderDomainPayload =
199                 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
200         assertNotNull(smeProviderDomainPayload);
201     }
202
203     @Test
204     void testGetSmeServiceApiPayload() {
205         UUID rappId = UUID.randomUUID();
206         RappSMEInstance rappSMEInstance = new RappSMEInstance();
207         rappSMEInstance.setServiceApis("api-set-1");
208         Rapp rapp =
209                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
210                         .build();
211         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
212         assertNotNull(smeProviderDomainPayload);
213     }
214
215     @Test
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);
222         Rapp rapp =
223                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
224                         .build();
225         JSONArray smeInvokerPayload =
226                 new JSONArray(rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappInstance));
227         assertNotNull(smeInvokerPayload);
228         assertEquals(smeInvokerPayload.getJSONObject(0).getString("apiInvokerInformation"),
229                 rappInstance.getRappInstanceId().toString());
230     }
231
232     @Test
233     void testGetDmeProducerInfoTypePayload() {
234         UUID rappId = UUID.randomUUID();
235         RappDMEInstance rappDMEInstance = new RappDMEInstance();
236         rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
237         Rapp rapp =
238                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
239                         .build();
240         String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
241                 rappDMEInstance.getInfoTypesProducer().iterator().next());
242         assertNotNull(dmeInfoTypePayload);
243     }
244
245     @Test
246     void testGetDmeConsumerInfoTypePayload() {
247         UUID rappId = UUID.randomUUID();
248         RappDMEInstance rappDMEInstance = new RappDMEInstance();
249         rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
250         Rapp rapp =
251                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
252                         .build();
253         String dmeInfoTypePayload =
254                 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
255         assertNotNull(dmeInfoTypePayload);
256     }
257
258     @Test
259     void testGetDmeInfoProducerPayload() {
260         UUID rappId = UUID.randomUUID();
261         RappDMEInstance rappDMEInstance = new RappDMEInstance();
262         rappDMEInstance.setInfoProducer("json-file-data-producer");
263         Rapp rapp =
264                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
265                         .build();
266         String dmeInfoProducerPayload =
267                 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
268         assertNotNull(dmeInfoProducerPayload);
269     }
270
271     @Test
272     void testGetDmeInfoConsumerPayload() {
273         UUID rappId = UUID.randomUUID();
274         RappDMEInstance rappDMEInstance = new RappDMEInstance();
275         rappDMEInstance.setInfoConsumer("json-file-consumer");
276         Rapp rapp =
277                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
278                         .build();
279         String dmeInfoConsumerPayload =
280                 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
281         assertNotNull(dmeInfoConsumerPayload);
282     }
283
284     @Test
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());
291     }
292
293     @ParameterizedTest
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();
298     }
299
300     @Test
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();
307     }
308
309     @Test
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();
316     }
317
318     @Test
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"));
323
324     }
325 }