Add initial version of code
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / rest / OnboardingControllerTest.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.rest;
20
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;
26
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;
31 import java.io.File;
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;
44
45 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
46 @TestPropertySource(properties = "rappmanager.csarlocation=src/test/resources")
47 @AutoConfigureMockMvc
48 public class OnboardingControllerTest {
49
50     @Autowired
51     private MockMvc mockMvc;
52
53     @Autowired
54     RappManagerConfiguration rappManagerConfiguration;
55
56     @MockBean
57     AcmDeployer acmDeployer;
58
59     @MockBean
60     SmeDeployer smeDeployer;
61
62     private final String validRappFile = "valid-rapp-package.csar";
63
64     private final String invalidRappFile = "invalid-rapp-package.csar";
65
66     @Test
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));
72         mockMvc.perform(
73                         MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/onboard", UUID.randomUUID()).file(multipartFile))
74                 .andExpect(status().isAccepted());
75     }
76
77     @Test
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));
83         mockMvc.perform(
84                         MockMvcRequestBuilders.multipart("/rapps/{rapp_id}/onboard", UUID.randomUUID()).file(multipartFile))
85                 .andExpect(status().isBadRequest());
86     }
87
88     @Test
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()));
95     }
96
97     @Test
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());
102     }
103
104     @Test
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());
112     }
113
114     @Test
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());
120     }
121
122     @Test
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());
130     }
131
132     @Test
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());
138     }
139
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());
147     }
148
149 }