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.rest;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.when;
23 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
24 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
27 import com.oransc.rappmanager.acm.service.AcmDeployer;
28 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
29 import com.oransc.rappmanager.models.RappState;
30 import com.oransc.rappmanager.sme.service.SmeDeployer;
32 import java.io.FileInputStream;
33 import java.util.UUID;
34 import org.apache.http.entity.ContentType;
35 import org.junit.jupiter.api.Test;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.mock.mockito.MockBean;
40 import org.springframework.mock.web.MockMultipartFile;
41 import org.springframework.test.context.TestPropertySource;
42 import org.springframework.test.web.servlet.MockMvc;
43 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
45 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
46 @TestPropertySource(properties = "rappmanager.csarlocation=src/test/resources")
48 public class OnboardingControllerTest {
51 private MockMvc mockMvc;
54 RappManagerConfiguration rappManagerConfiguration;
57 AcmDeployer acmDeployer;
60 SmeDeployer smeDeployer;
62 private final String validRappFile = "valid-rapp-package.csar";
64 private final String invalidRappFile = "invalid-rapp-package.csar";
67 void testOnboardCsarPackage() throws Exception {
68 String rappCsarPath = rappManagerConfiguration.getCsarLocation() + File.separator + validRappFile;
69 MockMultipartFile multipartFile =
70 new MockMultipartFile("file", validRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
71 new FileInputStream(rappCsarPath));
73 MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/onboard", UUID.randomUUID()).file(multipartFile))
74 .andExpect(status().isAccepted());
78 void testOnboardCsarPackageFailure() throws Exception {
79 String rappCsarPath = rappManagerConfiguration.getCsarLocation() + File.separator + invalidRappFile;
80 MockMultipartFile multipartFile =
81 new MockMultipartFile("file", invalidRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
82 new FileInputStream(rappCsarPath));
84 MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/onboard", UUID.randomUUID()).file(multipartFile))
85 .andExpect(status().isBadRequest());
89 void testGetRapp() throws Exception {
90 UUID rappId = UUID.randomUUID();
91 this.onBoardRappCsar(rappId);
92 this.mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", rappId)).andExpect(status().isOk())
93 .andExpect(jsonPath("$.name").value(rappId.toString()))
94 .andExpect(jsonPath("$.state").value(RappState.ONBOARDED.name()));
98 void testGetInvalidRapp() throws Exception {
99 UUID rappId = UUID.randomUUID();
100 this.mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", rappId)).andDo(print())
101 .andExpect(status().isBadRequest());
105 void testRappDeploy() throws Exception {
106 UUID rappId = UUID.randomUUID();
107 this.onBoardRappCsar(rappId);
108 when(acmDeployer.deployRapp(any())).thenReturn(true);
109 when(smeDeployer.deployRapp(any())).thenReturn(true);
110 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/deploy", rappId))
111 .andExpect(status().isAccepted());
115 void testInvalidRappDeploy() throws Exception {
116 UUID rappId = UUID.randomUUID();
117 when(acmDeployer.deployRapp(any())).thenReturn(false);
118 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/deploy", rappId))
119 .andExpect(status().isInternalServerError());
123 void testRappUndeploy() throws Exception {
124 UUID rappId = UUID.randomUUID();
125 this.onBoardRappCsar(rappId);
126 when(acmDeployer.undeployRapp(any())).thenReturn(true);
127 when(smeDeployer.undeployRapp(any())).thenReturn(true);
128 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/undeploy", rappId))
129 .andExpect(status().isAccepted());
133 void testInvalidRappUndeploy() throws Exception {
134 UUID rappId = UUID.randomUUID();
135 when(acmDeployer.undeployRapp(any())).thenReturn(false);
136 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/undeploy", rappId))
137 .andExpect(status().isInternalServerError());
140 void onBoardRappCsar(UUID rappId) throws Exception {
141 String rappCsarPath = rappManagerConfiguration.getCsarLocation() + File.separator + validRappFile;
142 MockMultipartFile multipartFile =
143 new MockMultipartFile("file", validRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
144 new FileInputStream(rappCsarPath));
145 mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/onboard", rappId).file(multipartFile))
146 .andExpect(status().isAccepted());