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.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;
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;
47 @ContextConfiguration(classes = {RappValidationUtils.class, ObjectMapper.class, RappsEnvironmentConfiguration.class,
48 RappCsarConfigurationHandler.class})
49 class RappValidationUtilsTest {
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";
55 RappValidationUtils rappValidationUtils;
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));
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());
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));
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());
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));
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));