6e7566d58dac43c787f7e1534e4b00059888aacb
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / rest / RappControllerTest.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023-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
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.hamcrest.Matchers.hasSize;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
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.fasterxml.jackson.databind.ObjectMapper;
28 import com.oransc.rappmanager.acm.service.AcmDeployer;
29 import com.oransc.rappmanager.dme.service.DmeDeployer;
30 import com.oransc.rappmanager.models.cache.RappCacheService;
31 import com.oransc.rappmanager.models.csar.AsdMetadata;
32 import com.oransc.rappmanager.models.rapp.PrimeOrder;
33 import com.oransc.rappmanager.models.rapp.Rapp;
34 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
35 import com.oransc.rappmanager.models.rapp.RappState;
36 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
37 import java.io.File;
38 import java.io.FileInputStream;
39 import java.util.List;
40 import java.util.UUID;
41 import org.apache.http.entity.ContentType;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.params.ParameterizedTest;
44 import org.junit.jupiter.params.provider.EnumSource;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.mock.mockito.MockBean;
49 import org.springframework.http.MediaType;
50 import org.springframework.mock.web.MockMultipartFile;
51 import org.springframework.test.web.servlet.MockMvc;
52 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
53
54 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
55 @AutoConfigureMockMvc
56 class RappControllerTest {
57
58     @Autowired
59     private MockMvc mockMvc;
60
61     @Autowired
62     private RappCacheService rappCacheService;
63
64     @MockBean
65     AcmDeployer acmDeployer;
66
67     @MockBean
68     DmeDeployer dmeDeployer;
69
70     @MockBean
71     SmeLifecycleManager smeLifecycleManager;
72
73     private final String validRappFile = "valid-rapp-package.csar";
74
75     private final String invalidRappFile = "invalid-rapp-package.csar";
76     private final String validCsarFileLocation = "src/test/resources/";
77
78     private final ObjectMapper objectMapper = new ObjectMapper();
79
80     @Test
81     void testGetRapps() throws Exception {
82         mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
83                 .andExpect(jsonPath("$", hasSize(0)));
84         UUID rappId = UUID.randomUUID();
85         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
86                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
87         AsdMetadata asdMetadata = new AsdMetadata();
88         asdMetadata.setDescriptorId(UUID.randomUUID().toString());
89         asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
90         asdMetadata.setDeploymentItems(List.of());
91         rapp.setAsdMetadata(asdMetadata);
92         rappCacheService.putRapp(rapp);
93         mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
94                 .andExpect(jsonPath("$", hasSize(1)));
95     }
96
97     @Test
98     void testGetRapp() throws Exception {
99         UUID rappId = UUID.randomUUID();
100         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
101                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
102         AsdMetadata asdMetadata = new AsdMetadata();
103         asdMetadata.setDescriptorId(UUID.randomUUID().toString());
104         asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
105         asdMetadata.setDeploymentItems(List.of());
106         rapp.setAsdMetadata(asdMetadata);
107         rappCacheService.putRapp(rapp);
108         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", rappId)).andExpect(status().isOk())
109                 .andExpect(jsonPath("$.rappId").value(rappId.toString()))
110                 .andExpect(jsonPath("$.state").value(RappState.COMMISSIONED.name()));
111     }
112
113     @Test
114     void testGetInvalidRapp() throws Exception {
115         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
116                 .andExpect(status().isNotFound());
117     }
118
119     @Test
120     void testCreateRapp() throws Exception {
121         String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
122         MockMultipartFile multipartFile =
123                 new MockMultipartFile("file", validRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
124                         new FileInputStream(rappCsarPath));
125         mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}", UUID.randomUUID()).file(multipartFile))
126                 .andExpect(status().isAccepted());
127     }
128
129     @Test
130     void testCreateInvalidRapp() throws Exception {
131         String rappCsarPath = validCsarFileLocation + File.separator + invalidRappFile;
132         MockMultipartFile multipartFile =
133                 new MockMultipartFile("file", invalidRappFile, ContentType.MULTIPART_FORM_DATA.getMimeType(),
134                         new FileInputStream(rappCsarPath));
135         mockMvc.perform(MockMvcRequestBuilders.multipart("/rapps/{rapp_id}", UUID.randomUUID()).file(multipartFile))
136                 .andExpect(status().isBadRequest());
137     }
138
139     @Test
140     void testPrimeRapp() throws Exception {
141         UUID rappId = UUID.randomUUID();
142         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
143                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
144         AsdMetadata asdMetadata = new AsdMetadata();
145         asdMetadata.setDescriptorId(UUID.randomUUID().toString());
146         asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
147         asdMetadata.setDeploymentItems(List.of());
148         rapp.setAsdMetadata(asdMetadata);
149         rappCacheService.putRapp(rapp);
150         when(acmDeployer.primeRapp(any())).thenReturn(true);
151         when(dmeDeployer.primeRapp(any())).thenReturn(true);
152         RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
153         rappPrimeOrder.setPrimeOrder(PrimeOrder.PRIME);
154         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", rappId).contentType(MediaType.APPLICATION_JSON)
155                                 .content(objectMapper.writeValueAsString(rappPrimeOrder))).andExpect(status().isOk());
156     }
157
158     @ParameterizedTest
159     @EnumSource(value = PrimeOrder.class, names = {"PRIME", "DEPRIME"})
160     void testPrimeRappFailure(PrimeOrder primeOrder) throws Exception {
161         RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
162         rappPrimeOrder.setPrimeOrder(primeOrder);
163         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", UUID.randomUUID())
164                                 .contentType(MediaType.APPLICATION_JSON)
165                                 .content(objectMapper.writeValueAsString(rappPrimeOrder)))
166                 .andExpect(status().isNotFound());
167     }
168
169     @Test
170     void testDeprimeRapp() throws Exception {
171         UUID rappId = UUID.randomUUID();
172         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
173                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
174         AsdMetadata asdMetadata = new AsdMetadata();
175         asdMetadata.setDescriptorId(UUID.randomUUID().toString());
176         asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
177         asdMetadata.setDeploymentItems(List.of());
178         rapp.setAsdMetadata(asdMetadata);
179         rappCacheService.putRapp(rapp);
180         when(acmDeployer.deprimeRapp(any())).thenReturn(true);
181         when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
182         RappPrimeOrder rappPrimeOrder = new RappPrimeOrder();
183         rappPrimeOrder.setPrimeOrder(PrimeOrder.DEPRIME);
184         mockMvc.perform(MockMvcRequestBuilders.put("/rapps/{rapp_id}", rappId).contentType(MediaType.APPLICATION_JSON)
185                                 .content(objectMapper.writeValueAsString(rappPrimeOrder))).andExpect(status().isOk());
186     }
187
188     @Test
189     void testDeleteRapp() throws Exception {
190         UUID rappId = UUID.randomUUID();
191         Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
192                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
193         AsdMetadata asdMetadata = new AsdMetadata();
194         asdMetadata.setDescriptorId(UUID.randomUUID().toString());
195         asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
196         asdMetadata.setDeploymentItems(List.of());
197         rapp.setAsdMetadata(asdMetadata);
198         rappCacheService.putRapp(rapp);
199         mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", rappId)).andExpect(status().isOk());
200     }
201
202     @Test
203     void testDeleteRappFailure() throws Exception {
204         mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", UUID.randomUUID()))
205                 .andExpect(status().isNotFound());
206     }
207 }