My first commit
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / csar / validator / FileExistenceValidatorTest.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
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
29 import com.oransc.rappmanager.models.exception.RappValidationException;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import org.apache.http.entity.ContentType;
34 import org.junit.jupiter.api.Test;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.mock.web.MockMultipartFile;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.web.multipart.MultipartFile;
41
42 @SpringBootTest
43 @ContextConfiguration(classes = {FileExistenceValidator.class, RappValidationUtils.class, ObjectMapper.class,
44         RappCsarConfigurationHandler.class})
45 class FileExistenceValidatorTest {
46
47     @Autowired
48     FileExistenceValidator fileExistenceValidator;
49     String validCsarFileLocation = "src/test/resources/";
50     String validRappFile = "valid-rapp-package.csar";
51     String invalidRappFile = "invalid-rapp-package.csar";
52     String invalidRappFileNoTosca = "invalid-rapp-package-no-tosca.csar";
53
54     @Test
55     void testFileExistenceValidationSuccess() throws IOException {
56         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
57         MultipartFile multipartFile =
58                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
59                         new FileInputStream(rappCsarPath));
60         assertDoesNotThrow(() -> fileExistenceValidator.validate(multipartFile, null));
61     }
62
63     @Test
64     void testFileExistenceNoCompositionValidation() throws IOException {
65         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
66         MultipartFile multipartFile =
67                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
68                         new FileInputStream(rappCsarPath));
69         RappValidationException rappValidationException =
70                 assertThrows(RappValidationException.class, () -> fileExistenceValidator.validate(multipartFile, null));
71         assertEquals(HttpStatus.BAD_REQUEST, rappValidationException.getStatusCode());
72         assertEquals("rApp package missing a file Files/Acm/definition/compositions.json",
73                 rappValidationException.getMessage());
74     }
75
76     @Test
77     void testFileExistenceNoToscaValidation() throws IOException {
78         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFileNoTosca;
79         MultipartFile multipartFile =
80                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
81                         new FileInputStream(rappCsarPath));
82         RappValidationException rappValidationException =
83                 assertThrows(RappValidationException.class, () -> fileExistenceValidator.validate(multipartFile, null));
84         assertEquals(HttpStatus.BAD_REQUEST, rappValidationException.getStatusCode());
85         assertEquals("rApp package missing a file TOSCA-Metadata/TOSCA.meta", rappValidationException.getMessage());
86     }
87 }