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