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
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.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;
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;
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;
54 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
56 class RappControllerTest {
59 private MockMvc mockMvc;
62 private RappCacheService rappCacheService;
65 AcmDeployer acmDeployer;
68 DmeDeployer dmeDeployer;
71 SmeLifecycleManager smeLifecycleManager;
73 private final String validRappFile = "valid-rapp-package.csar";
75 private final String invalidRappFile = "invalid-rapp-package.csar";
76 private final String validCsarFileLocation = "src/test/resources/";
78 private final ObjectMapper objectMapper = new ObjectMapper();
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)));
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()));
114 void testGetInvalidRapp() throws Exception {
115 mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
116 .andExpect(status().isNotFound());
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());
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());
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());
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());
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());
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());
203 void testDeleteRappFailure() throws Exception {
204 mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", UUID.randomUUID()))
205 .andExpect(status().isNotFound());