Add initial version of code
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / service / RappCsarConfigurationHandlerTest.java
1 /*-
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
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 package com.oransc.rappmanager.service;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
26
27 import com.oransc.rappmanager.models.Rapp;
28 import com.oransc.rappmanager.models.RappCsarConfigurationHandler;
29 import java.io.File;
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;
41
42 @SpringBootTest
43 @TestPropertySource(properties = "rappmanager.csarlocation=src/test/resources")
44 public class RappCsarConfigurationHandlerTest {
45
46     @Autowired
47     RappCsarConfigurationHandler rappCsarConfigurationHandler;
48
49     @Autowired
50     RappManagerConfiguration rappManagerConfiguration;
51
52     ObjectMapper objectMapper = new ObjectMapper();
53
54
55     private final String validRappFile = "valid-rapp-package.csar";
56
57     private final String invalidRappFile = "invalid-rapp-package.csar";
58
59     @Test
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));
66     }
67
68     @Test
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));
75     }
76
77     @Test
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);
86     }
87 }