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