2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager.models.csar;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.mockito.Mockito.mock;
27 import com.oransc.rappmanager.models.rapp.Rapp;
28 import com.oransc.rappmanager.models.rapp.RappResources;
29 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
30 import com.oransc.rappmanager.models.rappinstance.RappDMEInstance;
31 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
32 import java.io.ByteArrayOutputStream;
34 import java.io.FileInputStream;
35 import java.io.IOException;
37 import java.util.UUID;
38 import org.apache.http.entity.ContentType;
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import org.junit.jupiter.api.Test;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.mock.web.MockMultipartFile;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.web.multipart.MultipartFile;
49 @ContextConfiguration(classes = RappCsarConfigurationHandler.class)
50 class RappCsarConfigurationHandlerTest {
53 RappCsarConfigurationHandler rappCsarConfigurationHandler;
55 String validCsarFileLocation = "src/test/resources/";
58 private final String validRappFile = "valid-rapp-package.csar";
60 private final String invalidRappFile = "invalid-rapp-package.csar";
63 void testCsarPackageValidationSuccess() throws IOException {
64 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
65 MultipartFile multipartFile =
66 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
67 new FileInputStream(rappCsarPath));
68 assertEquals(Boolean.TRUE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
72 void testCsarPackageValidationFailure() throws IOException {
73 String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
74 MultipartFile multipartFile =
75 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
76 new FileInputStream(rappCsarPath));
77 assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
81 void testCsarPackageValidationFailureWithoutOrginalName() throws IOException {
82 MultipartFile multipartFile = mock(MultipartFile.class);
83 assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
87 void testInvalidCsarFileExist() {
88 MultipartFile multipartFile = mock(MultipartFile.class);
89 assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isFileExistsInCsar(multipartFile, "INVALID_LOCATION"));
93 void testCsarInstantiationPayload() throws JSONException {
94 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
95 UUID compositionId = UUID.randomUUID();
96 RappACMInstance rappACMInstance = new RappACMInstance();
97 rappACMInstance.setInstance("kserve-instance");
98 JSONObject jsonObject = new JSONObject(
99 rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappACMInstance, compositionId));
100 assertEquals(jsonObject.get("compositionId"), String.valueOf(compositionId));
104 void testFileListing() {
105 File file = new File(validCsarFileLocation + validRappFile);
106 Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, "Files/Sme/serviceapis/");
107 assertThat(fileListFromCsar).hasSize(2);
111 void testInvalidFileListing() {
112 File file = new File(validCsarFileLocation);
113 Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
114 assertThat(fileListFromCsar).isEmpty();
118 void testInvalidFileListingFromCsar() {
119 File file = new File("InvalidFile");
120 ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
121 assertThat(fileByteArray.size()).isZero();
125 void testListResources() {
126 UUID rappId = UUID.randomUUID();
128 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
130 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
131 assertThat(rappResources).isNotNull();
132 assertNotNull(rappResources.getAcm().getCompositionDefinitions());
133 assertThat(rappResources.getAcm().getCompositionInstances()).hasSize(4);
134 assertThat(rappResources.getSme().getProviderFunctions()).hasSize(4);
135 assertThat(rappResources.getSme().getServiceApis()).hasSize(2);
136 assertThat(rappResources.getSme().getInvokers()).hasSize(2);
137 assertThat(rappResources.getDme().getProducerInfoTypes()).hasSize(2);
138 assertThat(rappResources.getDme().getConsumerInfoTypes()).hasSize(2);
139 assertThat(rappResources.getDme().getInfoProducers()).hasSize(2);
140 assertThat(rappResources.getDme().getInfoConsumers()).hasSize(2);
144 void testListInvalidResources() {
145 UUID rappId = UUID.randomUUID();
146 Rapp rapp = Rapp.builder().rappId(rappId).name("").build();
147 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
148 assertThat(rappResources).isNotNull();
149 assertNull(rappResources.getAcm());
150 assertNull(rappResources.getSme());
151 assertNull(rappResources.getDme());
155 void testGetAcmCompositionPayload() {
156 UUID rappId = UUID.randomUUID();
157 RappResources rappResources = new RappResources();
158 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("compositions")
159 .compositionInstances(Set.of()).build());
161 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
162 .rappResources(rappResources).build();
163 String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
164 assertNotNull(acmCompositionPayload);
168 void testGetInvalidAcmCompositionPayload() {
169 UUID rappId = UUID.randomUUID();
170 RappResources rappResources = new RappResources();
171 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("invalidcomposition")
172 .compositionInstances(Set.of()).build());
174 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
175 .rappResources(rappResources).build();
176 String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
177 assertEquals("", acmCompositionPayload);
181 void testGetSmeProviderDomainPayload() {
182 UUID rappId = UUID.randomUUID();
183 RappSMEInstance rappSMEInstance = new RappSMEInstance();
184 rappSMEInstance.setProviderFunction("aef-provider-function");
186 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
188 String smeProviderDomainPayload =
189 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
190 assertNotNull(smeProviderDomainPayload);
194 void testGetSmeServiceApiPayload() {
195 UUID rappId = UUID.randomUUID();
196 RappSMEInstance rappSMEInstance = new RappSMEInstance();
197 rappSMEInstance.setServiceApis("api-set-1");
199 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
201 String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
202 assertNotNull(smeProviderDomainPayload);
206 void testGetSmeInvokerPayload() {
207 UUID rappId = UUID.randomUUID();
208 RappSMEInstance rappSMEInstance = new RappSMEInstance();
209 rappSMEInstance.setServiceApis("invoker-app1");
211 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
213 String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappSMEInstance);
214 assertNotNull(smeProviderDomainPayload);
218 void testGetDmeProducerInfoTypePayload() {
219 UUID rappId = UUID.randomUUID();
220 RappDMEInstance rappDMEInstance = new RappDMEInstance();
221 rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
223 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
225 String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
226 rappDMEInstance.getInfoTypesProducer().iterator().next());
227 assertNotNull(dmeInfoTypePayload);
231 void testGetDmeConsumerInfoTypePayload() {
232 UUID rappId = UUID.randomUUID();
233 RappDMEInstance rappDMEInstance = new RappDMEInstance();
234 rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
236 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
238 String dmeInfoTypePayload =
239 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
240 assertNotNull(dmeInfoTypePayload);
244 void testGetDmeInfoProducerPayload() {
245 UUID rappId = UUID.randomUUID();
246 RappDMEInstance rappDMEInstance = new RappDMEInstance();
247 rappDMEInstance.setInfoProducer("json-file-data-producer");
249 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
251 String dmeInfoProducerPayload =
252 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
253 assertNotNull(dmeInfoProducerPayload);
257 void testGetDmeInfoConsumerPayload() {
258 UUID rappId = UUID.randomUUID();
259 RappDMEInstance rappDMEInstance = new RappDMEInstance();
260 rappDMEInstance.setInfoConsumer("json-file-consumer");
262 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
264 String dmeInfoConsumerPayload =
265 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
266 assertNotNull(dmeInfoConsumerPayload);