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.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.Mockito.mock;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.oransc.rappmanager.models.BeanTestConfiguration;
28 import com.oransc.rappmanager.models.cache.RappCacheService;
29 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
30 import com.oransc.rappmanager.models.exception.RappValidationException;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.util.stream.Stream;
35 import org.apache.http.entity.ContentType;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.Arguments;
39 import org.junit.jupiter.params.provider.MethodSource;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.mock.web.MockMultipartFile;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.web.multipart.MultipartFile;
48 @ContextConfiguration(classes = {BeanTestConfiguration.class, RappValidationHandler.class, NamingValidator.class,
49 FileExistenceValidator.class, ArtifactDefinitionValidator.class, AsdDescriptorValidator.class,
50 RappValidationUtils.class, RappCsarConfigurationHandler.class, ObjectMapper.class, RappCacheService.class})
51 class RappValidationHandlerTest {
54 RappValidationHandler rappValidationHandler;
55 String validCsarFileLocation = "src/test/resources/";
56 private final String validRappFile = "valid-rapp-package.csar";
59 void testCsarPackageValidationSuccess() throws IOException {
60 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
61 MultipartFile multipartFile =
62 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
63 new FileInputStream(rappCsarPath));
64 assertEquals(Boolean.TRUE, rappValidationHandler.isValidRappPackage(multipartFile));
68 @MethodSource("getInvalidCsarPackage")
69 void testCsarPackageValidationFailure(MultipartFile multipartFile, String errorMessage) {
70 RappValidationException exception = assertThrows(RappValidationException.class,
71 () -> rappValidationHandler.isValidRappPackage(multipartFile));
72 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
73 assertEquals(errorMessage, exception.getMessage());
76 private static Stream<Arguments> getInvalidCsarPackage() throws IOException {
77 String validCsarFileLocation = "src/test/resources";
78 String errorMsgMissingAcmComposition = "rApp package missing a file Files/Acm/definition/compositions.json";
79 String rappCsarPath = validCsarFileLocation + File.separator + "invalid-rapp-package.csar";
80 MultipartFile multipartFile =
81 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
82 new FileInputStream(rappCsarPath));
83 String rappCsarPathNoTosca = validCsarFileLocation + File.separator + "invalid-rapp-package-no-tosca.csar";
84 MultipartFile multipartFileNoTosca = new MockMultipartFile(rappCsarPathNoTosca, rappCsarPathNoTosca,
85 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoTosca));
86 String rappCsarPathNoAsdYaml = validCsarFileLocation + File.separator + "invalid-rapp-package-no-asd-yaml.csar";
87 MultipartFile multipartFileNoAsdYaml = new MockMultipartFile(rappCsarPathNoAsdYaml, rappCsarPathNoAsdYaml,
88 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoAsdYaml));
89 String rappCsarPathMissingArtifact =
90 validCsarFileLocation + File.separator + "invalid-rapp-package-missing-artifact.csar";
91 MultipartFile multipartFileMissingArtifact =
92 new MockMultipartFile(rappCsarPathMissingArtifact, rappCsarPathMissingArtifact,
93 ContentType.MULTIPART_FORM_DATA.getMimeType(),
94 new FileInputStream(rappCsarPathMissingArtifact));
95 String rappCsarPathNoComposition =
96 validCsarFileLocation + File.separator + "invalid-rapp-package-no-acm-composition.csar";
97 MultipartFile multipartFileNoComposition =
98 new MockMultipartFile(rappCsarPathNoComposition, rappCsarPathNoComposition,
99 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoComposition));
100 return Stream.of(Arguments.of(multipartFile, errorMsgMissingAcmComposition),
101 Arguments.of(multipartFileNoTosca, "rApp package missing a file TOSCA-Metadata/TOSCA.meta"),
102 Arguments.of(multipartFileNoAsdYaml, "rApp package missing a file Definitions/asd.yaml"),
103 Arguments.of(multipartFileMissingArtifact,
104 "rApp package missing a file Artifacts/Deployment/HELM/orufhrecovery-1.0.0.tgz"),
105 Arguments.of(multipartFileNoComposition, errorMsgMissingAcmComposition));
109 void testCsarPackageValidationFailureWithoutOrginalName() {
110 MultipartFile multipartFile = mock(MultipartFile.class);
111 RappValidationException exception = assertThrows(RappValidationException.class,
112 () -> rappValidationHandler.isValidRappPackage(multipartFile));
113 assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
114 assertEquals("rApp package name should end with .csar", exception.getMessage());