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
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========================================================================
20 package com.oransc.rappmanager.models.csar.validator;
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;
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.csar.AsdMetadata;
33 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
34 import com.oransc.rappmanager.models.exception.RappValidationException;
35 import com.oransc.rappmanager.models.rapp.Rapp;
36 import java.io.ByteArrayOutputStream;
38 import java.io.FileInputStream;
39 import java.io.IOException;
40 import java.util.UUID;
41 import org.apache.http.entity.ContentType;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.params.ParameterizedTest;
44 import org.junit.jupiter.params.provider.NullSource;
45 import org.junit.jupiter.params.provider.ValueSource;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.mock.mockito.SpyBean;
49 import org.springframework.cache.CacheManager;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.mock.web.MockMultipartFile;
52 import org.springframework.test.context.ContextConfiguration;
53 import org.springframework.web.multipart.MultipartFile;
56 @ContextConfiguration(classes = {BeanTestConfiguration.class, AsdDescriptorValidator.class, RappValidationUtils.class,
57 ObjectMapper.class, RappCsarConfigurationHandler.class, RappCacheService.class, CacheManager.class})
58 class AsdDescriptorValidatorTest {
61 AsdDescriptorValidator asdDescriptorValidator;
63 RappValidationUtils rappValidationUtils;
65 RappCacheService rappCacheService;
67 String validCsarFileLocation = "src/test/resources/";
68 String validRappFile = "valid-rapp-package.csar";
69 String invalidRappAsdEmptyFile = "invalid-rapp-package-empty-asd-yaml.csar";
72 void testCsarContainsValidAsdFile() throws IOException {
73 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
74 MultipartFile multipartFile =
75 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
76 new FileInputStream(rappCsarPath));
77 assertDoesNotThrow(() -> asdDescriptorValidator.validate(multipartFile, null));
81 void testCsarContainsDuplicateDescriptorId() throws IOException {
82 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
83 AsdMetadata asdMetadata = new AsdMetadata();
84 asdMetadata.setDescriptorId("123e4567-e89b-12d3-a456-426614174000");
85 asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
86 Rapp rapp = Rapp.builder().name("").asdMetadata(asdMetadata).build();
87 rappCacheService.putRapp(rapp);
88 MultipartFile multipartFile =
89 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
90 new FileInputStream(rappCsarPath));
91 RappValidationException exception =
92 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
93 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
94 assertEquals("ASD descriptor already exists.", exception.getMessage());
95 rappCacheService.deleteRapp(rapp);
99 void testCsarContainsDuplicateDescriptorInvariantId() throws IOException {
100 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
101 AsdMetadata asdMetadata = new AsdMetadata();
102 asdMetadata.setDescriptorId("");
103 asdMetadata.setDescriptorInvariantId("040eff2a-eb1a-4aff-bd46-37ce38092985");
104 Rapp rapp = Rapp.builder().name("").asdMetadata(asdMetadata).build();
105 rappCacheService.putRapp(rapp);
106 MultipartFile multipartFile =
107 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
108 new FileInputStream(rappCsarPath));
109 RappValidationException exception =
110 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
111 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
112 assertEquals("ASD descriptor invariant already exists.", exception.getMessage());
113 rappCacheService.deleteRapp(rapp);
117 void testCsarContainsValidAsdFileFailure() throws IOException {
118 String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
119 MultipartFile multipartFile =
120 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
121 new FileInputStream(rappCsarPath));
122 RappValidationException exception =
123 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
124 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
125 assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
130 @ValueSource(strings = {""})
131 void testCsarAsdLocationNullFailure(String asdLocation) throws IOException {
132 String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
133 MultipartFile multipartFile =
134 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
135 new FileInputStream(rappCsarPath));
136 doReturn(asdLocation).when(rappValidationUtils).getAsdDefinitionLocation(any());
137 RappValidationException exception =
138 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
139 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
140 assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
145 @ValueSource(strings = {"", "{asasdasd"})
146 void testCsarAsdContentInvalidFailure(String asdContent) throws IOException {
147 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
148 MultipartFile multipartFile =
149 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
150 new FileInputStream(rappCsarPath));
151 if (asdContent != null) {
152 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
153 byteArrayOutputStream.write(asdContent.getBytes(), 0, asdContent.getBytes().length);
154 doCallRealMethod().doReturn(byteArrayOutputStream).when(rappValidationUtils)
155 .getFileFromCsar(any(MultipartFile.class), any());
157 doCallRealMethod().doReturn(asdContent).when(rappValidationUtils)
158 .getFileFromCsar(any(MultipartFile.class), any());
160 RappValidationException exception =
161 assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
162 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
163 assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());