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;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import com.oransc.rappmanager.models.rapp.RappResources;
28 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
29 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.UUID;
35 import org.apache.http.entity.ContentType;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38 import org.junit.jupiter.api.Test;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.mock.web.MockMultipartFile;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.web.multipart.MultipartFile;
46 @ContextConfiguration(classes = RappCsarConfigurationHandler.class)
47 class RappCsarConfigurationHandlerTest {
50 RappCsarConfigurationHandler rappCsarConfigurationHandler;
52 String validCsarFileLocation = "src/test/resources/";
55 private final String validRappFile = "valid-rapp-package.csar";
57 private final String invalidRappFile = "invalid-rapp-package.csar";
60 void testCsarPackageValidationSuccess() throws IOException {
61 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
62 MultipartFile multipartFile =
63 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
64 new FileInputStream(rappCsarPath));
65 assertEquals(Boolean.TRUE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
69 void testCsarPackageValidationFailure() throws IOException {
70 String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
71 MultipartFile multipartFile =
72 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
73 new FileInputStream(rappCsarPath));
74 assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
78 void testCsarInstantiationPayload() throws JSONException {
79 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
80 UUID compositionId = UUID.randomUUID();
81 RappACMInstance rappACMInstance = new RappACMInstance();
82 rappACMInstance.setInstance("kserve-instance");
83 JSONObject jsonObject = new JSONObject(
84 rappCsarConfigurationHandler.getInstantiationPayload(rapp, rappACMInstance, compositionId));
85 assertEquals(jsonObject.get("compositionId"), String.valueOf(compositionId));
89 void testFileListing() {
90 File file = new File(validCsarFileLocation + validRappFile);
91 List<String> fileListFromCsar =
92 rappCsarConfigurationHandler.getFileListFromCsar(file, "Files/Sme/serviceapis/");
93 assertThat(fileListFromCsar).hasSize(2);
97 void testInvalidFileListing() {
98 File file = new File(validCsarFileLocation);
99 List<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
100 assertThat(fileListFromCsar).isEmpty();
104 void testListResources() {
105 UUID rappId = UUID.randomUUID();
107 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
109 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
110 assertThat(rappResources).isNotNull();
111 assertNotNull(rappResources.getAcm().getCompositionDefinitions());
112 assertThat(rappResources.getAcm().getCompositionInstances()).hasSize(3);
113 assertThat(rappResources.getSme().getProviderFunctions()).hasSize(4);
114 assertThat(rappResources.getSme().getServiceApis()).hasSize(2);
115 assertThat(rappResources.getSme().getInvokers()).hasSize(2);
119 void testListInvalidResources() {
120 UUID rappId = UUID.randomUUID();
121 Rapp rapp = Rapp.builder().rappId(rappId).name("").build();
122 RappResources rappResources = rappCsarConfigurationHandler.getRappResource(rapp);
123 assertThat(rappResources).isNotNull();
124 assertNull(rappResources.getAcm());
125 assertNull(rappResources.getSme());
129 void testGetAcmCompositionPayload() {
130 UUID rappId = UUID.randomUUID();
131 RappResources rappResources = new RappResources();
132 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions("compositions")
133 .compositionInstances(List.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(List.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);