Fix sonar code smells
[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
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;
30 import java.io.File;
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;
44
45 @SpringBootTest
46 @ContextConfiguration(classes = RappCsarConfigurationHandler.class)
47 class RappCsarConfigurationHandlerTest {
48
49     @Autowired
50     RappCsarConfigurationHandler rappCsarConfigurationHandler;
51
52     String validCsarFileLocation = "src/test/resources/";
53
54
55     private final String validRappFile = "valid-rapp-package.csar";
56
57     private final String invalidRappFile = "invalid-rapp-package.csar";
58
59     @Test
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));
66     }
67
68     @Test
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));
75     }
76
77     @Test
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));
86     }
87
88     @Test
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);
94     }
95
96     @Test
97     void testInvalidFileListing() {
98         File file = new File(validCsarFileLocation);
99         List<String> fileListFromCsar = rappCsarConfigurationHandler.getFileListFromCsar(file, null);
100         assertThat(fileListFromCsar).isEmpty();
101     }
102
103     @Test
104     void testListResources() {
105         UUID rappId = UUID.randomUUID();
106         Rapp rapp =
107                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
108                         .build();
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);
116     }
117
118     @Test
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());
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(List.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(List.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 }