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.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.doCallRealMethod;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
33 import com.oransc.rappmanager.models.exception.RappValidationException;
34 import java.io.ByteArrayOutputStream;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import org.apache.http.entity.ContentType;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.params.ParameterizedTest;
41 import org.junit.jupiter.params.provider.NullSource;
42 import org.junit.jupiter.params.provider.ValueSource;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.mock.mockito.SpyBean;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.mock.web.MockMultipartFile;
48 import org.springframework.test.context.ContextConfiguration;
49 import org.springframework.web.multipart.MultipartFile;
52 @ContextConfiguration(classes = {ArtifactDefinitionValidator.class, RappValidationUtils.class, ObjectMapper.class,
53 RappCsarConfigurationHandler.class})
54 class ArtifactDefinitionValidatorTest {
57 ArtifactDefinitionValidator artifactDefinitionValidator;
59 RappValidationUtils rappValidationUtils;
60 String validCsarFileLocation = "src/test/resources/";
61 String validRappFile = "valid-rapp-package.csar";
62 String invalidRappFile = "invalid-rapp-package.csar";
63 String invalidRappAsdEmptyFile = "invalid-rapp-package-empty-asd-yaml.csar";
66 void testCsarContainsValidAsdFile() throws IOException {
67 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
68 MultipartFile multipartFile =
69 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
70 new FileInputStream(rappCsarPath));
71 assertDoesNotThrow(() -> artifactDefinitionValidator.validate(multipartFile, null));
75 void testCsarContainsValidAsdFileFailure() throws IOException {
76 String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
77 MultipartFile multipartFile =
78 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
79 new FileInputStream(rappCsarPath));
80 RappValidationException exception = assertThrows(RappValidationException.class,
81 () -> artifactDefinitionValidator.validate(multipartFile, null));
82 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
83 assertTrue(exception.getMessage().startsWith("rApp package missing a file"));
90 @ValueSource(strings = {"{asasdasd"})
91 void testCsarAsdContentInvalidFailure(String asdContent) throws IOException {
92 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
93 MultipartFile multipartFile =
94 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
95 new FileInputStream(rappCsarPath));
96 if (asdContent != null) {
97 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
98 byteArrayOutputStream.write(asdContent.getBytes(), 0, asdContent.getBytes().length);
99 doCallRealMethod().doReturn(byteArrayOutputStream).when(rappValidationUtils)
100 .getFileFromCsar(any(MultipartFile.class), any());
102 doCallRealMethod().doReturn(asdContent).when(rappValidationUtils)
103 .getFileFromCsar(any(MultipartFile.class), any());
105 RappValidationException exception = assertThrows(RappValidationException.class,
106 () -> artifactDefinitionValidator.validate(multipartFile, null));
107 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
108 assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());