Add validation for asd descriptor and invariant id
[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.csar.RappCsarConfigurationHandler;
31 import com.oransc.rappmanager.models.csar.RappCsarPathProvider;
32 import com.oransc.rappmanager.models.exception.RappValidationException;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import org.apache.http.entity.ContentType;
37 import org.junit.jupiter.api.Test;
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;
44
45 @SpringBootTest
46 @ContextConfiguration(classes = {RappValidationUtils.class, ObjectMapper.class, RappCsarConfigurationHandler.class})
47 class RappValidationUtilsTest {
48
49     String validCsarFileLocation = "src/test/resources/";
50     private final String validRappFile = "valid-rapp-package.csar";
51     private final String invalidRappNoToscaFile = "invalid-rapp-package-no-tosca.csar";
52     @Autowired
53     RappValidationUtils rappValidationUtils;
54
55     @Test
56     void testCsarFileExist() throws IOException {
57         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
58         MultipartFile multipartFile =
59                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
60                         new FileInputStream(rappCsarPath));
61         assertTrue(rappValidationUtils.isFileExistsInCsar(multipartFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION));
62
63     }
64
65     @Test
66     void testInvalidCsarFileExist() {
67         MultipartFile multipartFile = mock(MultipartFile.class);
68         RappValidationException exception = assertThrows(RappValidationException.class,
69                 () -> rappValidationUtils.isFileExistsInCsar(multipartFile, "INVALID_LOCATION"));
70         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
71         assertEquals("rApp package missing a file INVALID_LOCATION", exception.getMessage());
72     }
73
74     @Test
75     void testGetFileFromCsar() throws IOException {
76         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
77         MultipartFile multipartFile =
78                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
79                         new FileInputStream(rappCsarPath));
80         assertNotNull(rappValidationUtils.getFileFromCsar(multipartFile, RappCsarPathProvider.TOSCA_METADATA_LOCATION));
81     }
82
83     @Test
84     void testGetFileFromCsarFailure() throws IOException {
85         MultipartFile multipartFile = mock(MultipartFile.class);
86         when(multipartFile.getInputStream()).thenThrow(new IOException());
87         RappValidationException exception = assertThrows(RappValidationException.class,
88                 () -> rappValidationUtils.getFileFromCsar(multipartFile, null));
89         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
90         assertEquals(String.format("Unable to get file %s from the multipart CSAR file", (Object) null), exception.getMessage());
91     }
92
93     @Test
94     void testGetAsdLocationCsar() throws IOException {
95         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
96         MultipartFile multipartFile =
97                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
98                         new FileInputStream(rappCsarPath));
99         assertEquals("Definitions/asd.yaml", rappValidationUtils.getAsdDefinitionLocation(multipartFile));
100     }
101
102     @Test
103     void testGetAsdLocationCsarFailure() throws IOException {
104         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappNoToscaFile;
105         MultipartFile multipartFile =
106                 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
107                         new FileInputStream(rappCsarPath));
108         assertEquals("", rappValidationUtils.getAsdDefinitionLocation(multipartFile));
109     }
110 }