Merge "Adding new Simple consumer producer rapp packages"
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / csar / validator / RappValidationHandlerTest.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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========================================================================
17  *
18  */
19
20 package com.oransc.rappmanager.models.csar.validator;
21
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;
25
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.configuration.RappsEnvironmentConfiguration;
30 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
31 import com.oransc.rappmanager.models.exception.RappValidationException;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.util.stream.Stream;
36 import org.apache.http.entity.ContentType;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.params.ParameterizedTest;
39 import org.junit.jupiter.params.provider.Arguments;
40 import org.junit.jupiter.params.provider.MethodSource;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.mock.web.MockMultipartFile;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.web.multipart.MultipartFile;
47
48 @SpringBootTest
49 @ContextConfiguration(classes = {BeanTestConfiguration.class, RappValidationHandler.class, NamingValidator.class,
50         FileExistenceValidator.class, ArtifactDefinitionValidator.class, AsdDescriptorValidator.class,
51         RappValidationUtils.class, RappsEnvironmentConfiguration.class, RappCsarConfigurationHandler.class,
52         ObjectMapper.class, RappCacheService.class})
53 class RappValidationHandlerTest {
54
55     @Autowired
56     RappValidationHandler rappValidationHandler;
57     String validCsarFileLocation = "src/test/resources/";
58     private final String validRappFile = "valid-rapp-package.csar";
59
60     @Test
61     void testCsarPackageValidationSuccess() throws IOException {
62         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
63         MultipartFile multipartFile =
64                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
65                         new FileInputStream(rappCsarPath));
66         assertEquals(Boolean.TRUE, rappValidationHandler.isValidRappPackage(multipartFile));
67     }
68
69     @ParameterizedTest
70     @MethodSource("getInvalidCsarPackage")
71     void testCsarPackageValidationFailure(MultipartFile multipartFile, String errorMessage) {
72         RappValidationException exception = assertThrows(RappValidationException.class,
73                 () -> rappValidationHandler.isValidRappPackage(multipartFile));
74         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
75         assertEquals(errorMessage, exception.getMessage());
76     }
77
78     private static Stream<Arguments> getInvalidCsarPackage() throws IOException {
79         String validCsarFileLocation = "src/test/resources";
80         String errorMsgMissingAcmComposition = "rApp package missing a file Files/Acm/definition/compositions.json";
81         String rappCsarPath = validCsarFileLocation + File.separator + "invalid-rapp-package.csar";
82         MultipartFile multipartFile =
83                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
84                         new FileInputStream(rappCsarPath));
85         String rappCsarPathNoTosca = validCsarFileLocation + File.separator + "invalid-rapp-package-no-tosca.csar";
86         MultipartFile multipartFileNoTosca = new MockMultipartFile(rappCsarPathNoTosca, rappCsarPathNoTosca,
87                 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoTosca));
88         String rappCsarPathNoAsdYaml = validCsarFileLocation + File.separator + "invalid-rapp-package-no-asd-yaml.csar";
89         MultipartFile multipartFileNoAsdYaml = new MockMultipartFile(rappCsarPathNoAsdYaml, rappCsarPathNoAsdYaml,
90                 ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoAsdYaml));
91         String rappCsarPathMissingArtifact =
92                 validCsarFileLocation + File.separator + "invalid-rapp-package-missing-artifact.csar";
93         MultipartFile multipartFileMissingArtifact =
94                 new MockMultipartFile(rappCsarPathMissingArtifact, rappCsarPathMissingArtifact,
95                         ContentType.MULTIPART_FORM_DATA.getMimeType(),
96                         new FileInputStream(rappCsarPathMissingArtifact));
97         String rappCsarPathNoComposition =
98                 validCsarFileLocation + File.separator + "invalid-rapp-package-no-acm-composition.csar";
99         MultipartFile multipartFileNoComposition =
100                 new MockMultipartFile(rappCsarPathNoComposition, rappCsarPathNoComposition,
101                         ContentType.MULTIPART_FORM_DATA.getMimeType(), new FileInputStream(rappCsarPathNoComposition));
102         return Stream.of(Arguments.of(multipartFile, errorMsgMissingAcmComposition),
103                 Arguments.of(multipartFileNoTosca, "rApp package missing a file TOSCA-Metadata/TOSCA.meta"),
104                 Arguments.of(multipartFileNoAsdYaml, "rApp package missing a file Definitions/asd.yaml"),
105                 Arguments.of(multipartFileMissingArtifact,
106                         "rApp package missing a file Artifacts/Deployment/HELM/hello-world-chart-0.1.0.tgz"),
107                 Arguments.of(multipartFileNoComposition, errorMsgMissingAcmComposition));
108     }
109
110     @Test
111     void testCsarPackageValidationFailureWithoutOrginalName() {
112         MultipartFile multipartFile = mock(MultipartFile.class);
113         RappValidationException exception = assertThrows(RappValidationException.class,
114                 () -> rappValidationHandler.isValidRappPackage(multipartFile));
115         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
116         assertEquals("rApp package name should end with .csar", exception.getMessage());
117     }
118 }