Roll versions after J-Relase master -> 0.2.0
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / test / java / com / oransc / rappmanager / models / csar / validator / RappValidationUtilsTest.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.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
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.csar.RappCsarPathProvider;
33 import com.oransc.rappmanager.models.exception.RappValidationException;
34 import java.io.File;
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.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.mock.web.MockMultipartFile;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.web.multipart.MultipartFile;
45
46 @SpringBootTest
47 @ContextConfiguration(classes = {RappValidationUtils.class, ObjectMapper.class, RappsEnvironmentConfiguration.class,
48         RappCsarConfigurationHandler.class})
49 class RappValidationUtilsTest {
50
51     String validCsarFileLocation = "src/test/resources/";
52     private final String validRappFile = "valid-rapp-package.csar";
53     private final String invalidRappNoToscaFile = "invalid-rapp-package-no-tosca.csar";
54     @Autowired
55     RappValidationUtils rappValidationUtils;
56
57     @Test
58     void testCsarFileExist() throws IOException {
59         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
60         MultipartFile multipartFile =
61                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
62                         new FileInputStream(rappCsarPath));
63         assertTrue(rappValidationUtils.isFileExistsInCsar(multipartFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION));
64
65     }
66
67     @Test
68     void testInvalidCsarFileExist() {
69         MultipartFile multipartFile = mock(MultipartFile.class);
70         RappValidationException exception = assertThrows(RappValidationException.class,
71                 () -> rappValidationUtils.isFileExistsInCsar(multipartFile, "INVALID_LOCATION"));
72         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
73         assertEquals("rApp package missing a file INVALID_LOCATION", exception.getMessage());
74     }
75
76     @Test
77     void testGetFileFromCsar() throws IOException {
78         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
79         MultipartFile multipartFile =
80                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
81                         new FileInputStream(rappCsarPath));
82         assertNotNull(rappValidationUtils.getFileFromCsar(multipartFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION));
83     }
84
85     @Test
86     void testGetFileFromCsarFailure() throws IOException {
87         MultipartFile multipartFile = mock(MultipartFile.class);
88         when(multipartFile.getInputStream()).thenThrow(new IOException());
89         RappValidationException exception = assertThrows(RappValidationException.class,
90                 () -> rappValidationUtils.getFileFromCsar(multipartFile, null));
91         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
92         assertEquals(String.format("Unable to get file %s from the multipart CSAR file", (Object) null),
93                 exception.getMessage());
94     }
95
96     @Test
97     void testGetAsdLocationCsar() throws IOException {
98         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
99         MultipartFile multipartFile =
100                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
101                         new FileInputStream(rappCsarPath));
102         assertEquals("Definitions/asd.yaml", rappValidationUtils.getAsdDefinitionLocation(multipartFile));
103     }
104
105     @Test
106     void testGetAsdLocationCsarFailure() throws IOException {
107         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappNoToscaFile;
108         MultipartFile multipartFile =
109                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
110                         new FileInputStream(rappCsarPath));
111         assertEquals("", rappValidationUtils.getAsdDefinitionLocation(multipartFile));
112     }
113 }