rApp package validation code refactor
[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.doThrow;
28
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;
38 import java.io.File;
39 import java.util.List;
40 import java.util.Set;
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;
50
51 @SpringBootTest
52 @ContextConfiguration(classes = {ObjectMapper.class, RappCsarConfigurationHandler.class})
53 class RappCsarConfigurationHandlerTest {
54
55     @SpyBean
56     RappCsarConfigurationHandler rappCsarConfigurationHandler;
57
58     String validCsarFileLocation = "src/test/resources/";
59
60     private final String validRappFile = "valid-rapp-package.csar";
61
62     private final String invalidRappNoAsdFile = "invalid-rapp-package-no-asd-yaml.csar";
63
64     private final String invalidRappEmptyAsdFile = "invalid-rapp-package-empty-asd-yaml.csar";
65
66     @Test
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));
75     }
76
77     @Test
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);
82     }
83
84     @Test
85     void testInvalidFileListing() {
86         File file = new File(validCsarFileLocation);
87         Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
88         assertThat(fileListFromCsar).isEmpty();
89     }
90
91     @Test
92     void testInvalidFileListingFromCsar() {
93         File file = new File("InvalidFile");
94         ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
95         assertThat(fileByteArray.size()).isZero();
96     }
97
98     @Test
99     void testListResources() {
100         UUID rappId = UUID.randomUUID();
101         Rapp rapp =
102                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
103                         .build();
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);
115     }
116
117     @Test
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());
126     }
127
128     @Test
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());
134         Rapp rapp =
135                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
136                         .rappResources(rappResources).build();
137         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
138         assertNotNull(acmCompositionPayload);
139     }
140
141     @Test
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());
147         Rapp rapp =
148                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
149                         .rappResources(rappResources).build();
150         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
151         assertEquals("", acmCompositionPayload);
152     }
153
154     @Test
155     void testGetSmeProviderDomainPayload() {
156         UUID rappId = UUID.randomUUID();
157         RappSMEInstance rappSMEInstance = new RappSMEInstance();
158         rappSMEInstance.setProviderFunction("aef-provider-function");
159         Rapp rapp =
160                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
161                         .build();
162         String smeProviderDomainPayload =
163                 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
164         assertNotNull(smeProviderDomainPayload);
165     }
166
167     @Test
168     void testGetSmeServiceApiPayload() {
169         UUID rappId = UUID.randomUUID();
170         RappSMEInstance rappSMEInstance = new RappSMEInstance();
171         rappSMEInstance.setServiceApis("api-set-1");
172         Rapp rapp =
173                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
174                         .build();
175         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
176         assertNotNull(smeProviderDomainPayload);
177     }
178
179     @Test
180     void testGetSmeInvokerPayload() {
181         UUID rappId = UUID.randomUUID();
182         RappSMEInstance rappSMEInstance = new RappSMEInstance();
183         rappSMEInstance.setServiceApis("invoker-app1");
184         Rapp rapp =
185                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
186                         .build();
187         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappSMEInstance);
188         assertNotNull(smeProviderDomainPayload);
189     }
190
191     @Test
192     void testGetDmeProducerInfoTypePayload() {
193         UUID rappId = UUID.randomUUID();
194         RappDMEInstance rappDMEInstance = new RappDMEInstance();
195         rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
196         Rapp rapp =
197                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
198                         .build();
199         String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
200                 rappDMEInstance.getInfoTypesProducer().iterator().next());
201         assertNotNull(dmeInfoTypePayload);
202     }
203
204     @Test
205     void testGetDmeConsumerInfoTypePayload() {
206         UUID rappId = UUID.randomUUID();
207         RappDMEInstance rappDMEInstance = new RappDMEInstance();
208         rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
209         Rapp rapp =
210                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
211                         .build();
212         String dmeInfoTypePayload =
213                 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
214         assertNotNull(dmeInfoTypePayload);
215     }
216
217     @Test
218     void testGetDmeInfoProducerPayload() {
219         UUID rappId = UUID.randomUUID();
220         RappDMEInstance rappDMEInstance = new RappDMEInstance();
221         rappDMEInstance.setInfoProducer("json-file-data-producer");
222         Rapp rapp =
223                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
224                         .build();
225         String dmeInfoProducerPayload =
226                 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
227         assertNotNull(dmeInfoProducerPayload);
228     }
229
230     @Test
231     void testGetDmeInfoConsumerPayload() {
232         UUID rappId = UUID.randomUUID();
233         RappDMEInstance rappDMEInstance = new RappDMEInstance();
234         rappDMEInstance.setInfoConsumer("json-file-consumer");
235         Rapp rapp =
236                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
237                         .build();
238         String dmeInfoConsumerPayload =
239                 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
240         assertNotNull(dmeInfoConsumerPayload);
241     }
242
243     @Test
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());
248     }
249
250     @ParameterizedTest
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();
255     }
256
257     @Test
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();
262     }
263
264     @Test
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"));
269
270     }
271 }