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.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;
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;
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;
46 @ContextConfiguration(classes = {RappValidationUtils.class, ObjectMapper.class, RappCsarConfigurationHandler.class})
47 class RappValidationUtilsTest {
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";
53 RappValidationUtils rappValidationUtils;
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));
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());
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));
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", null), exception.getMessage());
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));
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));