2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. 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========================================================================
19 package com.oransc.rappmanager.service;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
27 import com.oransc.rappmanager.models.Rapp;
28 import com.oransc.rappmanager.models.RappCsarConfigurationHandler;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.util.UUID;
33 import org.apache.http.entity.ContentType;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.mock.web.MockMultipartFile;
39 import org.springframework.test.context.TestPropertySource;
40 import org.springframework.web.multipart.MultipartFile;
43 @TestPropertySource(properties = "rappmanager.csarlocation=src/test/resources")
44 public class RappCsarConfigurationHandlerTest {
47 RappCsarConfigurationHandler rappCsarConfigurationHandler;
50 RappManagerConfiguration rappManagerConfiguration;
52 ObjectMapper objectMapper = new ObjectMapper();
55 private final String validRappFile = "valid-rapp-package.csar";
57 private final String invalidRappFile = "invalid-rapp-package.csar";
60 void testCsarPackageValidationSuccess() throws IOException {
61 String rappCsarPath = rappManagerConfiguration.getCsarLocation() + File.separator + validRappFile;
62 MultipartFile multipartFile =
63 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
64 new FileInputStream(rappCsarPath));
65 assertEquals(Boolean.TRUE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
69 void testCsarPackageValidationFailure() throws IOException {
70 String rappCsarPath = rappManagerConfiguration.getCsarLocation() + File.separator + invalidRappFile;
71 MultipartFile multipartFile =
72 new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
73 new FileInputStream(rappCsarPath));
74 assertEquals(Boolean.FALSE, rappCsarConfigurationHandler.isValidRappPackage(multipartFile));
78 void testCsarInstantiationPayload() throws JsonProcessingException {
79 Rapp rapp = Rapp.builder().name("").packageName(validRappFile)
80 .packageLocation(rappManagerConfiguration.getCsarLocation()).build();
81 UUID compositionId = UUID.randomUUID();
82 AutomationComposition automationComposition =
83 objectMapper.readValue(rappCsarConfigurationHandler.getInstantiationPayload(rapp, compositionId),
84 AutomationComposition.class);
85 assertEquals(automationComposition.getCompositionId(), compositionId);