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.assertDoesNotThrow;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import com.oransc.rappmanager.models.exception.RappValidationException;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import org.apache.http.entity.ContentType;
33 import org.junit.jupiter.api.Test;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.boot.test.context.SpringBootTest;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.mock.web.MockMultipartFile;
38 import org.springframework.test.context.ContextConfiguration;
39 import org.springframework.web.multipart.MultipartFile;
42 @ContextConfiguration(classes = {NamingValidator.class})
43 class NamingValidatorTest {
46 NamingValidator namingValidator;
47 String validCsarFileLocation = "src/test/resources/";
48 String validRappFile = "valid-rapp-package.csar";
49 String expectedErrorMessage = "rApp package name should end with .csar";
52 void testNamingValidationSuccess() throws IOException {
53 String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
54 MultipartFile multipartFile =
55 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
56 new FileInputStream(rappCsarPath));
57 assertDoesNotThrow(() -> namingValidator.validate(multipartFile, null));
61 void testNamingValidationFailureWithNull() {
62 MultipartFile multipartFile = mock(MultipartFile.class);
63 RappValidationException rappValidationException =
64 assertThrows(RappValidationException.class, () -> namingValidator.validate(multipartFile, null));
65 assertEquals(HttpStatus.BAD_REQUEST, rappValidationException.getStatusCode());
66 assertEquals(expectedErrorMessage, rappValidationException.getMessage());
70 void testNamingValidationFailureWithEmptyName() {
71 MultipartFile multipartFile = mock(MultipartFile.class);
72 when(multipartFile.getOriginalFilename()).thenReturn("");
73 RappValidationException rappValidationException =
74 assertThrows(RappValidationException.class, () -> namingValidator.validate(multipartFile, null));
75 assertEquals(HttpStatus.BAD_REQUEST, rappValidationException.getStatusCode());
76 assertEquals(expectedErrorMessage, rappValidationException.getMessage());