Merge "Pass rAppInstanceId to k8s participant and create the invoker with the instanc...
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / csar / validator / AsdDescriptorValidatorTest.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2024 OpenInfra Foundation Europe. 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
20 package com.oransc.rappmanager.models.csar.validator;
21
22 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doCallRealMethod;
27 import static org.mockito.Mockito.doReturn;
28
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.oransc.rappmanager.models.BeanTestConfiguration;
31 import com.oransc.rappmanager.models.cache.RappCacheService;
32 import com.oransc.rappmanager.models.configuration.RappsEnvironmentConfiguration;
33 import com.oransc.rappmanager.models.csar.AsdMetadata;
34 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
35 import com.oransc.rappmanager.models.exception.RappValidationException;
36 import com.oransc.rappmanager.models.rapp.Rapp;
37 import java.io.ByteArrayOutputStream;
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.IOException;
41 import java.util.UUID;
42 import org.apache.http.entity.ContentType;
43 import org.junit.jupiter.api.Test;
44 import org.junit.jupiter.params.ParameterizedTest;
45 import org.junit.jupiter.params.provider.NullSource;
46 import org.junit.jupiter.params.provider.ValueSource;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.mock.mockito.SpyBean;
50 import org.springframework.cache.CacheManager;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.mock.web.MockMultipartFile;
53 import org.springframework.test.context.ContextConfiguration;
54 import org.springframework.web.multipart.MultipartFile;
55
56 @SpringBootTest
57 @ContextConfiguration(classes = {BeanTestConfiguration.class, AsdDescriptorValidator.class, RappValidationUtils.class,
58         ObjectMapper.class, RappsEnvironmentConfiguration.class, RappCsarConfigurationHandler.class,
59         RappCacheService.class, CacheManager.class})
60 class AsdDescriptorValidatorTest {
61
62     @Autowired
63     AsdDescriptorValidator asdDescriptorValidator;
64     @SpyBean
65     RappValidationUtils rappValidationUtils;
66     @Autowired
67     RappCacheService rappCacheService;
68
69     String validCsarFileLocation = "src/test/resources/";
70     String validRappFile = "valid-rapp-package.csar";
71     String invalidRappAsdEmptyFile = "invalid-rapp-package-empty-asd-yaml.csar";
72
73     @Test
74     void testCsarContainsValidAsdFile() throws IOException {
75         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
76         MultipartFile multipartFile =
77                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
78                         new FileInputStream(rappCsarPath));
79         assertDoesNotThrow(() -> asdDescriptorValidator.validate(multipartFile, null));
80     }
81
82     @Test
83     void testCsarContainsDuplicateDescriptorId() throws IOException {
84         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
85         AsdMetadata asdMetadata = new AsdMetadata();
86         asdMetadata.setDescriptorId("123e4567-e89b-12d3-a456-426614174000");
87         asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
88         Rapp rapp = Rapp.builder().name("").asdMetadata(asdMetadata).build();
89         rappCacheService.putRapp(rapp);
90         MultipartFile multipartFile =
91                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
92                         new FileInputStream(rappCsarPath));
93         RappValidationException exception =
94                 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
95         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
96         assertEquals("ASD descriptor already exists.", exception.getMessage());
97         rappCacheService.deleteRapp(rapp);
98     }
99
100     @Test
101     void testCsarContainsDuplicateDescriptorInvariantId() throws IOException {
102         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
103         AsdMetadata asdMetadata = new AsdMetadata();
104         asdMetadata.setDescriptorId("");
105         asdMetadata.setDescriptorInvariantId("040eff2a-eb1a-4aff-bd46-37ce38092985");
106         Rapp rapp = Rapp.builder().name("").asdMetadata(asdMetadata).build();
107         rappCacheService.putRapp(rapp);
108         MultipartFile multipartFile =
109                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
110                         new FileInputStream(rappCsarPath));
111         RappValidationException exception =
112                 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
113         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
114         assertEquals("ASD descriptor invariant already exists.", exception.getMessage());
115         rappCacheService.deleteRapp(rapp);
116     }
117
118     @Test
119     void testCsarContainsValidAsdFileFailure() throws IOException {
120         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
121         MultipartFile multipartFile =
122                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
123                         new FileInputStream(rappCsarPath));
124         RappValidationException exception =
125                 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
126         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
127         assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
128     }
129
130     @ParameterizedTest
131     @NullSource
132     @ValueSource(strings = {""})
133     void testCsarAsdLocationNullFailure(String asdLocation) throws IOException {
134         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
135         MultipartFile multipartFile =
136                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
137                         new FileInputStream(rappCsarPath));
138         doReturn(asdLocation).when(rappValidationUtils).getAsdDefinitionLocation(any());
139         RappValidationException exception =
140                 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
141         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
142         assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
143     }
144
145     @ParameterizedTest
146     @NullSource
147     @ValueSource(strings = {"", "{asasdasd"})
148     void testCsarAsdContentInvalidFailure(String asdContent) throws IOException {
149         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
150         MultipartFile multipartFile =
151                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
152                         new FileInputStream(rappCsarPath));
153         if (asdContent != null) {
154             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
155             byteArrayOutputStream.write(asdContent.getBytes(), 0, asdContent.getBytes().length);
156             doCallRealMethod().doReturn(byteArrayOutputStream).when(rappValidationUtils)
157                     .getFileFromCsar(any(MultipartFile.class), any());
158         } else {
159             doCallRealMethod().doReturn(asdContent).when(rappValidationUtils)
160                     .getFileFromCsar(any(MultipartFile.class), any());
161         }
162         RappValidationException exception =
163                 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
164         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
165         assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
166     }
167 }