Kserve participant for simple rApp
[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.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.doCallRealMethod;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.oransc.rappmanager.models.exception.RappHandlerException;
35 import com.oransc.rappmanager.models.rapp.Rapp;
36 import com.oransc.rappmanager.models.rapp.RappResources;
37 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
38 import com.oransc.rappmanager.models.rappinstance.RappDMEInstance;
39 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
40 import java.io.ByteArrayOutputStream;
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.IOException;
44 import java.util.Set;
45 import java.util.UUID;
46 import java.util.stream.Stream;
47 import org.apache.http.entity.ContentType;
48 import org.json.JSONException;
49 import org.json.JSONObject;
50 import org.junit.jupiter.api.Test;
51 import org.junit.jupiter.params.ParameterizedTest;
52 import org.junit.jupiter.params.provider.Arguments;
53 import org.junit.jupiter.params.provider.MethodSource;
54 import org.springframework.boot.test.context.SpringBootTest;
55 import org.springframework.boot.test.mock.mockito.SpyBean;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.mock.web.MockMultipartFile;
58 import org.springframework.test.context.ContextConfiguration;
59 import org.springframework.web.multipart.MultipartFile;
60
61 @SpringBootTest
62 @ContextConfiguration(classes = {ObjectMapper.class, RappCsarConfigurationHandler.class})
63 class RappCsarConfigurationHandlerTest {
64
65     @SpyBean
66     RappCsarConfigurationHandler rappCsarConfigurationHandler;
67
68     String validCsarFileLocation = "src/test/resources/";
69
70
71     private final String validRappFile = "valid-rapp-package.csar";
72
73     private final String invalidRappFile = "invalid-rapp-package.csar";
74
75     @Test
76     void testCsarPackageValidationSuccess() throws IOException {
77         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
78         MultipartFile multipartFile =
79                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
80                         new FileInputStream(rappCsarPath));
81         assertEquals(Boolean.TRUE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
82     }
83
84     @ParameterizedTest
85     @MethodSource("getInvalidCsarPackage")
86     void testCsarPackageValidationFailure(MultipartFile multipartFile) {
87         System.out.println(multipartFile.getOriginalFilename());
88         RappHandlerException exception = assertThrows(RappHandlerException.class,
89                 () -> rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
90         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
91     }
92
93
94     private static Stream<Arguments> getInvalidCsarPackage() throws IOException {
95         String validCsarFileLocation = "src/test/resources";
96         String rappCsarPath = validCsarFileLocation + File.separator + "invalid-rapp-package.csar";
97         MultipartFile multipartFile =
98                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
99                         new FileInputStream(rappCsarPath));
100         String rappCsarPathNoTosca = validCsarFileLocation + File.separator + "invalid-rapp-package-no-tosca.csar";
101         MultipartFile multipartFileNoTosca = new MockMultipartFile(rappCsarPathNoTosca, rappCsarPathNoTosca,
102                 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoTosca));
103         String rappCsarPathNoAsdYaml = validCsarFileLocation + File.separator + "invalid-rapp-package-no-asd-yaml.csar";
104         MultipartFile multipartFileNoAsdYaml = new MockMultipartFile(rappCsarPathNoAsdYaml, rappCsarPathNoAsdYaml,
105                 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoAsdYaml));
106         String rappCsarPathMissingArtifact =
107                 validCsarFileLocation + File.separator + "invalid-rapp-package-missing-artifact.csar";
108         MultipartFile multipartFileMissingArtifact =
109                 new MockMultipartFile(rappCsarPathMissingArtifact, rappCsarPathMissingArtifact,
110                         ContentType.MULTIPART_FORM_DATA.getMimeType(),
111                         new FileInputStream(rappCsarPathMissingArtifact));
112         String rappCsarPathNoComposition =
113                 validCsarFileLocation + File.separator + "invalid-rapp-package-no-acm-composition.csar";
114         MultipartFile multipartFileNoComposition =
115                 new MockMultipartFile(rappCsarPathNoComposition, rappCsarPathNoComposition,
116                         ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoComposition));
117         return Stream.of(Arguments.of(multipartFile), Arguments.of(multipartFileNoTosca),
118                 Arguments.of(multipartFileNoAsdYaml), Arguments.of(multipartFileMissingArtifact),
119                 Arguments.of(multipartFileNoComposition));
120     }
121
122     @Test
123     void testCsarPackageValidationFailureWithoutOrginalName() {
124         MultipartFile multipartFile = mock(MultipartFile.class);
125         assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
126     }
127
128     @Test
129     void testInvalidCsarFileExist() {
130         MultipartFile multipartFile = mock(MultipartFile.class);
131         RappHandlerException exception = assertThrows(RappHandlerException.class,
132                 () -> rappCsarConfigurationHandler.isFileExistsInCsar(multipartFile, "INVALID_LOCATION"));
133         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
134     }
135
136     @Test
137     void testCsarContainsValidAsdFile() throws IOException {
138         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
139         MultipartFile multipartFile =
140                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
141                         new FileInputStream(rappCsarPath));
142         assertTrue(rappCsarConfigurationHandler.containsValidArtifactDefinition(multipartFile));
143     }
144
145     @Test
146     void testCsarContainsValidAsdFileFailure() throws IOException {
147         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
148         MultipartFile multipartFile =
149                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
150                         new FileInputStream(rappCsarPath));
151         RappHandlerException exception = assertThrows(RappHandlerException.class,
152                 () -> rappCsarConfigurationHandler.containsValidArtifactDefinition(multipartFile));
153         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
154     }
155
156     @Test
157     void testCsarNoAsdFailure() {
158         MultipartFile multipartFile = mock(MultipartFile.class);
159         RappHandlerException exception = assertThrows(RappHandlerException.class,
160                 () -> rappCsarConfigurationHandler.containsValidArtifactDefinition(multipartFile));
161         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
162     }
163
164     @Test
165     void testCsarAsdContentInvalidFailure() throws IOException {
166         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
167         MultipartFile multipartFile =
168                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
169                         new FileInputStream(rappCsarPath));
170         String invalidJson = "{asasdasd";
171         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
172         byteArrayOutputStream.write(invalidJson.getBytes(), 0, invalidJson.getBytes().length);
173         doCallRealMethod().doReturn(byteArrayOutputStream).when(rappCsarConfigurationHandler)
174                 .getFileFromCsar(any(MultipartFile.class), any());
175         RappHandlerException exception = assertThrows(RappHandlerException.class,
176                 () -> rappCsarConfigurationHandler.containsValidArtifactDefinition(multipartFile));
177         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
178     }
179
180     @Test
181     void testgetFileFromCsarFailure() throws IOException {
182         MultipartFile multipartFile = mock(MultipartFile.class);
183         when(multipartFile.getInputStream()).thenThrow(new IOException());
184         assertThat(rappCsarConfigurationHandler.getFileFromCsar(multipartFile, null).size()).isZero();
185     }
186
187     @Test
188     void testCsarInstantiationPayload() throws JSONException {
189         Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
190         UUID compositionId = UUID.randomUUID();
191         RappACMInstance rappACMInstance = new RappACMInstance();
192         rappACMInstance.setInstance("kserve-instance");
193         JSONObject jsonObject = new JSONObject(
194                 rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappACMInstance, compositionId));
195         assertEquals(jsonObject.get("compositionId"), String.valueOf(compositionId));
196     }
197
198     @Test
199     void testFileListing() {
200         File file = new File(validCsarFileLocation + validRappFile);
201         Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, "Files/Sme/serviceapis/");
202         assertThat(fileListFromCsar).hasSize(2);
203     }
204
205     @Test
206     void testInvalidFileListing() {
207         File file = new File(validCsarFileLocation);
208         Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
209         assertThat(fileListFromCsar).isEmpty();
210     }
211
212     @Test
213     void testInvalidFileListingFromCsar() {
214         File file = new File("InvalidFile");
215         ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
216         assertThat(fileByteArray.size()).isZero();
217     }
218
219     @Test
220     void testListResources() {
221         UUID rappId = UUID.randomUUID();
222         Rapp rapp =
223                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
224                         .build();
225         RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
226         assertThat(rappResources).isNotNull();
227         assertNotNull(rappResources.getAcm().getCompositionDefinitions());
228         assertThat(rappResources.getAcm().getCompositionInstances()).hasSize(4);
229         assertThat(rappResources.getSme().getProviderFunctions()).hasSize(4);
230         assertThat(rappResources.getSme().getServiceApis()).hasSize(2);
231         assertThat(rappResources.getSme().getInvokers()).hasSize(2);
232         assertThat(rappResources.getDme().getProducerInfoTypes()).hasSize(2);
233         assertThat(rappResources.getDme().getConsumerInfoTypes()).hasSize(2);
234         assertThat(rappResources.getDme().getInfoProducers()).hasSize(2);
235         assertThat(rappResources.getDme().getInfoConsumers()).hasSize(2);
236     }
237
238     @Test
239     void testListInvalidResources() {
240         UUID rappId = UUID.randomUUID();
241         Rapp rapp = Rapp.builder().rappId(rappId).name("").packageName("").packageLocation("").build();
242         RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
243         assertThat(rappResources).isNotNull();
244         assertNull(rappResources.getAcm());
245         assertNull(rappResources.getSme());
246         assertNull(rappResources.getDme());
247     }
248
249     @Test
250     void testGetAcmCompositionPayload() {
251         UUID rappId = UUID.randomUUID();
252         RappResources rappResources = new RappResources();
253         rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("compositions")
254                                      .compositionInstances(Set.of()).build());
255         Rapp rapp =
256                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
257                         .rappResources(rappResources).build();
258         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
259         assertNotNull(acmCompositionPayload);
260     }
261
262     @Test
263     void testGetInvalidAcmCompositionPayload() {
264         UUID rappId = UUID.randomUUID();
265         RappResources rappResources = new RappResources();
266         rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("invalidcomposition")
267                                      .compositionInstances(Set.of()).build());
268         Rapp rapp =
269                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
270                         .rappResources(rappResources).build();
271         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
272         assertEquals("", acmCompositionPayload);
273     }
274
275     @Test
276     void testGetSmeProviderDomainPayload() {
277         UUID rappId = UUID.randomUUID();
278         RappSMEInstance rappSMEInstance = new RappSMEInstance();
279         rappSMEInstance.setProviderFunction("aef-provider-function");
280         Rapp rapp =
281                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
282                         .build();
283         String smeProviderDomainPayload =
284                 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
285         assertNotNull(smeProviderDomainPayload);
286     }
287
288     @Test
289     void testGetSmeServiceApiPayload() {
290         UUID rappId = UUID.randomUUID();
291         RappSMEInstance rappSMEInstance = new RappSMEInstance();
292         rappSMEInstance.setServiceApis("api-set-1");
293         Rapp rapp =
294                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
295                         .build();
296         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
297         assertNotNull(smeProviderDomainPayload);
298     }
299
300     @Test
301     void testGetSmeInvokerPayload() {
302         UUID rappId = UUID.randomUUID();
303         RappSMEInstance rappSMEInstance = new RappSMEInstance();
304         rappSMEInstance.setServiceApis("invoker-app1");
305         Rapp rapp =
306                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
307                         .build();
308         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappSMEInstance);
309         assertNotNull(smeProviderDomainPayload);
310     }
311
312     @Test
313     void testGetDmeProducerInfoTypePayload() {
314         UUID rappId = UUID.randomUUID();
315         RappDMEInstance rappDMEInstance = new RappDMEInstance();
316         rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
317         Rapp rapp =
318                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
319                         .build();
320         String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
321                 rappDMEInstance.getInfoTypesProducer().iterator().next());
322         assertNotNull(dmeInfoTypePayload);
323     }
324
325     @Test
326     void testGetDmeConsumerInfoTypePayload() {
327         UUID rappId = UUID.randomUUID();
328         RappDMEInstance rappDMEInstance = new RappDMEInstance();
329         rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
330         Rapp rapp =
331                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
332                         .build();
333         String dmeInfoTypePayload =
334                 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
335         assertNotNull(dmeInfoTypePayload);
336     }
337
338     @Test
339     void testGetDmeInfoProducerPayload() {
340         UUID rappId = UUID.randomUUID();
341         RappDMEInstance rappDMEInstance = new RappDMEInstance();
342         rappDMEInstance.setInfoProducer("json-file-data-producer");
343         Rapp rapp =
344                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
345                         .build();
346         String dmeInfoProducerPayload =
347                 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
348         assertNotNull(dmeInfoProducerPayload);
349     }
350
351     @Test
352     void testGetDmeInfoConsumerPayload() {
353         UUID rappId = UUID.randomUUID();
354         RappDMEInstance rappDMEInstance = new RappDMEInstance();
355         rappDMEInstance.setInfoConsumer("json-file-consumer");
356         Rapp rapp =
357                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
358                         .build();
359         String dmeInfoConsumerPayload =
360                 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
361         assertNotNull(dmeInfoConsumerPayload);
362     }
363
364 }