Add info types for producer and consumer in rApp package
[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  * ===============================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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========================================================================
17  */
18
19 package com.oransc.rappmanager.models.csar;
20
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;
26
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;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.util.Set;
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;
47
48 @SpringBootTest
49 @ContextConfiguration(classes = RappCsarConfigurationHandler.class)
50 class RappCsarConfigurationHandlerTest {
51
52     @Autowired
53     RappCsarConfigurationHandler rappCsarConfigurationHandler;
54
55     String validCsarFileLocation = "src/test/resources/";
56
57
58     private final String validRappFile = "valid-rapp-package.csar";
59
60     private final String invalidRappFile = "invalid-rapp-package.csar";
61
62     @Test
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));
69     }
70
71     @Test
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));
78     }
79
80     @Test
81     void testCsarPackageValidationFailureWithoutOrginalName() throws IOException {
82         MultipartFile multipartFile = mock(MultipartFile.class);
83         assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
84     }
85
86     @Test
87     void testInvalidCsarFileExist() {
88         MultipartFile multipartFile = mock(MultipartFile.class);
89         assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isFileExistsInCsar(multipartFile, "INVALID_LOCATION"));
90     }
91
92     @Test
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));
101     }
102
103     @Test
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);
108     }
109
110     @Test
111     void testInvalidFileListing() {
112         File file = new File(validCsarFileLocation);
113         Set<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
114         assertThat(fileListFromCsar).isEmpty();
115     }
116
117     @Test
118     void testInvalidFileListingFromCsar() {
119         File file = new File("InvalidFile");
120         ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(file, null);
121         assertThat(fileByteArray.size()).isZero();
122     }
123
124     @Test
125     void testListResources() {
126         UUID rappId = UUID.randomUUID();
127         Rapp rapp =
128                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
129                         .build();
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);
141     }
142
143     @Test
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());
152     }
153
154     @Test
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());
160         Rapp rapp =
161                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
162                         .rappResources(rappResources).build();
163         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
164         assertNotNull(acmCompositionPayload);
165     }
166
167     @Test
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());
173         Rapp rapp =
174                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
175                         .rappResources(rappResources).build();
176         String acmCompositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
177         assertEquals("", acmCompositionPayload);
178     }
179
180     @Test
181     void testGetSmeProviderDomainPayload() {
182         UUID rappId = UUID.randomUUID();
183         RappSMEInstance rappSMEInstance = new RappSMEInstance();
184         rappSMEInstance.setProviderFunction("aef-provider-function");
185         Rapp rapp =
186                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
187                         .build();
188         String smeProviderDomainPayload =
189                 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappSMEInstance);
190         assertNotNull(smeProviderDomainPayload);
191     }
192
193     @Test
194     void testGetSmeServiceApiPayload() {
195         UUID rappId = UUID.randomUUID();
196         RappSMEInstance rappSMEInstance = new RappSMEInstance();
197         rappSMEInstance.setServiceApis("api-set-1");
198         Rapp rapp =
199                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
200                         .build();
201         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappSMEInstance);
202         assertNotNull(smeProviderDomainPayload);
203     }
204
205     @Test
206     void testGetSmeInvokerPayload() {
207         UUID rappId = UUID.randomUUID();
208         RappSMEInstance rappSMEInstance = new RappSMEInstance();
209         rappSMEInstance.setServiceApis("invoker-app1");
210         Rapp rapp =
211                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
212                         .build();
213         String smeProviderDomainPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappSMEInstance);
214         assertNotNull(smeProviderDomainPayload);
215     }
216
217     @Test
218     void testGetDmeProducerInfoTypePayload() {
219         UUID rappId = UUID.randomUUID();
220         RappDMEInstance rappDMEInstance = new RappDMEInstance();
221         rappDMEInstance.setInfoTypesProducer(Set.of("json-file-data-from-filestore"));
222         Rapp rapp =
223                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
224                         .build();
225         String dmeInfoTypePayload = rappCsarConfigurationHandler.getDmeProducerInfoTypePayload(rapp,
226                 rappDMEInstance.getInfoTypesProducer().iterator().next());
227         assertNotNull(dmeInfoTypePayload);
228     }
229
230     @Test
231     void testGetDmeConsumerInfoTypePayload() {
232         UUID rappId = UUID.randomUUID();
233         RappDMEInstance rappDMEInstance = new RappDMEInstance();
234         rappDMEInstance.setInfoTypeConsumer("json-file-data-from-filestore");
235         Rapp rapp =
236                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
237                         .build();
238         String dmeInfoTypePayload =
239                 rappCsarConfigurationHandler.getDmeConsumerInfoTypePayload(rapp, rappDMEInstance.getInfoTypeConsumer());
240         assertNotNull(dmeInfoTypePayload);
241     }
242
243     @Test
244     void testGetDmeInfoProducerPayload() {
245         UUID rappId = UUID.randomUUID();
246         RappDMEInstance rappDMEInstance = new RappDMEInstance();
247         rappDMEInstance.setInfoProducer("json-file-data-producer");
248         Rapp rapp =
249                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
250                         .build();
251         String dmeInfoProducerPayload =
252                 rappCsarConfigurationHandler.getDmeInfoProducerPayload(rapp, rappDMEInstance.getInfoProducer());
253         assertNotNull(dmeInfoProducerPayload);
254     }
255
256     @Test
257     void testGetDmeInfoConsumerPayload() {
258         UUID rappId = UUID.randomUUID();
259         RappDMEInstance rappDMEInstance = new RappDMEInstance();
260         rappDMEInstance.setInfoConsumer("json-file-consumer");
261         Rapp rapp =
262                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
263                         .build();
264         String dmeInfoConsumerPayload =
265                 rappCsarConfigurationHandler.getDmeInfoConsumerPayload(rapp, rappDMEInstance.getInfoConsumer());
266         assertNotNull(dmeInfoConsumerPayload);
267     }
268
269 }