Add support for optional parameters in SME
[nonrtric/plt/rappmanager.git] / rapp-manager-sme / src / test / java / com / oransc / rappmanager / sme / service / SmeDeployerTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END========================================================================
18  */
19
20 package com.oransc.rappmanager.sme.service;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
27 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
28 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
29
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.oransc.rappmanager.models.cache.RappCacheService;
33 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
34 import com.oransc.rappmanager.models.rapp.Rapp;
35 import com.oransc.rappmanager.models.rapp.RappState;
36 import com.oransc.rappmanager.models.rappinstance.RappInstance;
37 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
38 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
39 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachineConfig;
40 import com.oransc.rappmanager.sme.configuration.SmeConfiguration;
41 import com.oransc.rappmanager.sme.invoker.data.APIInvokerEnrolmentDetails;
42 import com.oransc.rappmanager.sme.provider.data.APIProviderEnrolmentDetails;
43 import com.oransc.rappmanager.sme.provider.data.APIProviderFunctionDetails;
44 import com.oransc.rappmanager.sme.provider.data.ApiProviderFuncRole;
45 import com.oransc.rappmanager.sme.publishservice.data.AefProfile;
46 import com.oransc.rappmanager.sme.publishservice.data.ServiceAPIDescription;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.UUID;
50 import org.junit.jupiter.api.BeforeAll;
51 import org.junit.jupiter.api.BeforeEach;
52 import org.junit.jupiter.api.Test;
53 import org.junit.jupiter.api.TestInstance;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.mock.mockito.SpyBean;
58 import org.springframework.http.HttpMethod;
59 import org.springframework.http.HttpStatus;
60 import org.springframework.http.MediaType;
61 import org.springframework.test.web.client.ExpectedCount;
62 import org.springframework.test.web.client.MockRestServiceServer;
63 import org.springframework.web.client.RestTemplate;
64
65 @SpringBootTest(classes = {SmeConfiguration.class, SmeDeployer.class, BeanTestConfiguration.class,
66         RappCsarConfigurationHandler.class, RappCacheService.class, RappInstanceStateMachineConfig.class,
67         RappInstanceStateMachine.class})
68 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
69 @AutoConfigureMockMvc
70 class SmeDeployerTest {
71
72     MockRestServiceServer mockServer;
73     @Autowired
74     @SpyBean
75     SmeDeployer smeDeployer;
76     @Autowired
77     RestTemplate restTemplate;
78     @Autowired
79     SmeConfiguration smeConfiguration;
80     String validCsarFileLocation = "src/test/resources/";
81     @Autowired
82     RappInstanceStateMachine rappInstanceStateMachine;
83     @Autowired
84     ObjectMapper objectMapper;
85     private final String validRappFile = "valid-rapp-package.csar";
86     String URI_PROVIDER_REGISTRATIONS, URI_PROVIDER_REGISTRATION, URI_PUBLISH_APIS, URI_PUBLISH_API, URI_INVOKERS,
87             URI_INVOKER;
88
89     @BeforeAll
90     void initSmeUri() {
91         URI_PROVIDER_REGISTRATIONS =
92                 smeConfiguration.getBaseUrl() + smeConfiguration.getProviderBasePath() + "registrations";
93         URI_PROVIDER_REGISTRATION =
94                 smeConfiguration.getBaseUrl() + smeConfiguration.getProviderBasePath() + "registrations/%s";
95         URI_PUBLISH_APIS = smeConfiguration.getBaseUrl() + smeConfiguration.getPublishApiBasePath() + "%s/service-apis";
96         URI_PUBLISH_API =
97                 smeConfiguration.getBaseUrl() + smeConfiguration.getPublishApiBasePath() + "%s/service-apis/%s";
98         URI_INVOKERS = smeConfiguration.getBaseUrl() + smeConfiguration.getInvokerBasePath() + "onboardedInvokers";
99         URI_INVOKER = smeConfiguration.getBaseUrl() + smeConfiguration.getInvokerBasePath() + "onboardedInvokers/%s";
100     }
101
102     @BeforeEach
103     public void init() {
104         mockServer = MockRestServiceServer.createServer(restTemplate);
105     }
106
107     @Test
108     void testCreateAMF() throws JsonProcessingException {
109         String apiProvDomId = UUID.randomUUID().toString();
110         APIProviderEnrolmentDetails apiProviderEnrolmentDetails = new APIProviderEnrolmentDetails(apiProvDomId);
111         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
112                 .andExpect(method(HttpMethod.POST)).andRespond(
113                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
114                                 .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
115         APIProviderEnrolmentDetails apiProviderEnrolmentResponse = smeDeployer.createAMF();
116         mockServer.verify();
117         assertEquals(apiProvDomId, apiProviderEnrolmentResponse.getApiProvDomId());
118     }
119
120     @Test
121     void testCreateAMFFailure() {
122         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
123                 .andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
124         APIProviderEnrolmentDetails apiProviderEnrolmentResponse = smeDeployer.createAMF();
125         mockServer.verify();
126         assertNull(apiProviderEnrolmentResponse);
127     }
128
129     @Test
130     void testDeleteAMF() throws JsonProcessingException {
131         String apiProvDomId = UUID.randomUUID().toString();
132         APIProviderEnrolmentDetails apiProviderEnrolmentDetails = new APIProviderEnrolmentDetails(apiProvDomId);
133         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
134                 .andExpect(method(HttpMethod.POST)).andRespond(
135                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
136                                 .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
137         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PROVIDER_REGISTRATION, apiProvDomId)))
138                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
139         smeDeployer.createAMF();
140         smeDeployer.deleteAMF();
141         mockServer.verify();
142     }
143
144     @Test
145     void testCreateProviderDomain() throws Exception {
146         UUID rappId = UUID.randomUUID();
147         Rapp rapp =
148                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
149                         .state(RappState.COMMISSIONED).build();
150         APIProviderEnrolmentDetails apiProviderEnrolmentDetails = getProviderDomainApiEnrollmentDetails();
151         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
152                 .andExpect(method(HttpMethod.POST)).andRespond(
153                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
154                                 .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
155         RappInstance rappInstance = getRappInstance();
156         boolean createProviderDomain = smeDeployer.createProviderDomain(rapp, rappInstance);
157         mockServer.verify();
158         assertTrue(createProviderDomain);
159     }
160
161     @Test
162     void testCreateProviderDomainFailure() {
163         UUID rappId = UUID.randomUUID();
164         Rapp rapp =
165                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
166                         .state(RappState.COMMISSIONED).build();
167         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
168                 .andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
169         RappInstance rappInstance = getRappInstance();
170         boolean createProviderDomain = smeDeployer.createProviderDomain(rapp, rappInstance);
171         mockServer.verify();
172         assertFalse(createProviderDomain);
173     }
174
175     @Test
176     void testDeleteProviderFunc() {
177         UUID registrationId = UUID.randomUUID();
178         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PROVIDER_REGISTRATION, registrationId)))
179                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
180         smeDeployer.deleteProviderFunc(String.valueOf(registrationId));
181         mockServer.verify();
182     }
183
184     @Test
185     void testCreatePublishApi() throws Exception {
186         UUID rappId = UUID.randomUUID();
187         UUID apfId = UUID.randomUUID();
188         Rapp rapp =
189                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
190                         .state(RappState.COMMISSIONED).build();
191         ServiceAPIDescription serviceAPIDescription = getServiceApiDescription();
192         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_APIS, apfId)))
193                 .andExpect(method(HttpMethod.POST)).andRespond(
194                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
195                                 .body(objectMapper.writeValueAsString(serviceAPIDescription)));
196         RappInstance rappInstance = getRappInstance();
197         rappInstance.getSme().setApfId(String.valueOf(apfId));
198         boolean publishApi = smeDeployer.createPublishApi(rapp, rappInstance);
199         mockServer.verify();
200         assertTrue(publishApi);
201     }
202
203
204     @Test
205     void testCreatePublishApiFailure() {
206         UUID rappId = UUID.randomUUID();
207         UUID apfId = UUID.randomUUID();
208         Rapp rapp =
209                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
210                         .state(RappState.COMMISSIONED).build();
211         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_APIS, apfId)))
212                 .andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
213         RappInstance rappInstance = getRappInstance();
214         rappInstance.getSme().setApfId(String.valueOf(apfId));
215         boolean publishApi = smeDeployer.createPublishApi(rapp, rappInstance);
216         mockServer.verify();
217         assertFalse(publishApi);
218     }
219
220     @Test
221     void testDeletePublishApi() {
222         String serviceApiId = String.valueOf(UUID.randomUUID());
223         String apfId = String.valueOf(UUID.randomUUID());
224         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApiId)))
225                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
226         smeDeployer.deletePublishApi(serviceApiId, apfId);
227         mockServer.verify();
228     }
229
230     @Test
231     void testCreateInvoker() throws Exception {
232         UUID rappId = UUID.randomUUID();
233         Rapp rapp =
234                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
235                         .state(RappState.COMMISSIONED).build();
236         APIInvokerEnrolmentDetails apiInvokerEnrolmentDetails = getApiInvokerEnrollmentDetails();
237         mockServer.expect(ExpectedCount.once(), requestTo(URI_INVOKERS)).andExpect(method(HttpMethod.POST)).andRespond(
238                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
239                         .body(objectMapper.writeValueAsString(apiInvokerEnrolmentDetails)));
240         RappInstance rappInstance = getRappInstance();
241         boolean createInvoker = smeDeployer.createInvoker(rapp, rappInstance);
242         mockServer.verify();
243         assertTrue(createInvoker);
244     }
245
246     @Test
247     void testCreateInvokerFailure() {
248         UUID rappId = UUID.randomUUID();
249         Rapp rapp =
250                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
251                         .state(RappState.COMMISSIONED).build();
252         mockServer.expect(ExpectedCount.once(), requestTo(URI_INVOKERS)).andExpect(method(HttpMethod.POST))
253                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
254         RappInstance rappInstance = getRappInstance();
255         boolean createInvoker = smeDeployer.createInvoker(rapp, rappInstance);
256         mockServer.verify();
257         assertFalse(createInvoker);
258     }
259
260     @Test
261     void testDeleteInvoker() {
262         String invokerId = String.valueOf(UUID.randomUUID());
263         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokerId)))
264                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
265         smeDeployer.deleteInvoker(invokerId);
266         mockServer.verify();
267     }
268
269     @Test
270     void testDeployRappInstance() throws Exception {
271         UUID rappId = UUID.randomUUID();
272         APIProviderEnrolmentDetails apiProviderEnrolmentDetails = getProviderDomainApiEnrollmentDetails();
273         APIProviderFunctionDetails apfProviderFunctionDetails = apiProviderEnrolmentDetails.getApiProvFuncs().stream()
274                                                                         .filter(apiProviderFunctionDetails -> apiProviderFunctionDetails.getApiProvFuncRole()
275                                                                                                                       .equals(ApiProviderFuncRole.APF))
276                                                                         .findFirst().get();
277         Rapp rapp =
278                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
279                         .state(RappState.COMMISSIONED).build();
280         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
281                 .andExpect(method(HttpMethod.POST)).andRespond(
282                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
283                                 .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
284         ServiceAPIDescription serviceAPIDescription = getServiceApiDescription();
285         mockServer.expect(ExpectedCount.once(),
286                         requestTo(String.format(URI_PUBLISH_APIS, apfProviderFunctionDetails.getApiProvFuncId())))
287                 .andExpect(method(HttpMethod.POST)).andRespond(
288                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
289                                 .body(objectMapper.writeValueAsString(serviceAPIDescription)));
290         APIInvokerEnrolmentDetails apiInvokerEnrolmentDetails = getApiInvokerEnrollmentDetails();
291         mockServer.expect(ExpectedCount.once(), requestTo(URI_INVOKERS)).andExpect(method(HttpMethod.POST)).andRespond(
292                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
293                         .body(objectMapper.writeValueAsString(apiInvokerEnrolmentDetails)));
294         RappInstance rappInstance = getRappInstance();
295         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
296         boolean deployRapp = smeDeployer.deployRappInstance(rapp, rappInstance);
297         mockServer.verify();
298         assertTrue(deployRapp);
299     }
300
301     @Test
302     void testDeployRappInstanceNoInvoker() throws Exception {
303         UUID rappId = UUID.randomUUID();
304         APIProviderEnrolmentDetails apiProviderEnrolmentDetails = getProviderDomainApiEnrollmentDetails();
305         APIProviderFunctionDetails apfProviderFunctionDetails = apiProviderEnrolmentDetails.getApiProvFuncs().stream()
306                                                                         .filter(apiProviderFunctionDetails -> apiProviderFunctionDetails.getApiProvFuncRole()
307                                                                                                                       .equals(ApiProviderFuncRole.APF))
308                                                                         .findFirst().get();
309         Rapp rapp =
310                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
311                         .state(RappState.COMMISSIONED).build();
312         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
313                 .andExpect(method(HttpMethod.POST)).andRespond(
314                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
315                                 .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
316         ServiceAPIDescription serviceAPIDescription = getServiceApiDescription();
317         mockServer.expect(ExpectedCount.once(),
318                         requestTo(String.format(URI_PUBLISH_APIS, apfProviderFunctionDetails.getApiProvFuncId())))
319                 .andExpect(method(HttpMethod.POST)).andRespond(
320                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
321                                 .body(objectMapper.writeValueAsString(serviceAPIDescription)));
322         RappInstance rappInstance = getRappInstance();
323         rappInstance.getSme().setInvokers(null);
324         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
325         boolean deployRapp = smeDeployer.deployRappInstance(rapp, rappInstance);
326         mockServer.verify();
327         assertTrue(deployRapp);
328     }
329
330     @Test
331     void testDeployRappInstanceNoProvider() throws Exception {
332         UUID rappId = UUID.randomUUID();
333         Rapp rapp =
334                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
335                         .state(RappState.COMMISSIONED).build();
336         APIInvokerEnrolmentDetails apiInvokerEnrolmentDetails = getApiInvokerEnrollmentDetails();
337         mockServer.expect(ExpectedCount.once(), requestTo(URI_INVOKERS)).andExpect(method(HttpMethod.POST)).andRespond(
338                 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
339                         .body(objectMapper.writeValueAsString(apiInvokerEnrolmentDetails)));
340         RappInstance rappInstance = getRappInstance();
341         rappInstance.getSme().setProviderFunction(null);
342         rappInstance.getSme().setServiceApis(null);
343         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
344         boolean deployRapp = smeDeployer.deployRappInstance(rapp, rappInstance);
345         mockServer.verify();
346         assertTrue(deployRapp);
347     }
348
349     @Test
350     void testDeployRappInstanceWithoutSme() {
351         UUID rappId = UUID.randomUUID();
352         Rapp rapp =
353                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
354                         .state(RappState.PRIMED).build();
355         RappInstance rappInstance = getRappInstance();
356         rappInstance.setSme(null);
357         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
358         assertTrue(smeDeployer.deployRappInstance(rapp, rappInstance));
359     }
360
361     @Test
362     void testDeployRappFailure() throws Exception {
363         UUID rappId = UUID.randomUUID();
364         APIProviderEnrolmentDetails apiProviderEnrolmentDetails = getProviderDomainApiEnrollmentDetails();
365         APIProviderFunctionDetails apfProviderFunctionDetails = apiProviderEnrolmentDetails.getApiProvFuncs().stream()
366                                                                         .filter(apiProviderFunctionDetails -> apiProviderFunctionDetails.getApiProvFuncRole()
367                                                                                                                       .equals(ApiProviderFuncRole.APF))
368                                                                         .findFirst().get();
369         Rapp rapp =
370                 Rapp.builder().rappId(rappId).name("").packageName(validRappFile).packageLocation(validCsarFileLocation)
371                         .state(RappState.COMMISSIONED).build();
372         mockServer.expect(ExpectedCount.once(), requestTo(URI_PROVIDER_REGISTRATIONS))
373                 .andExpect(method(HttpMethod.POST)).andRespond(
374                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
375                                 .body(objectMapper.writeValueAsString(apiProviderEnrolmentDetails)));
376         ServiceAPIDescription serviceAPIDescription = getServiceApiDescription();
377         mockServer.expect(ExpectedCount.once(),
378                         requestTo(String.format(URI_PUBLISH_APIS, apfProviderFunctionDetails.getApiProvFuncId())))
379                 .andExpect(method(HttpMethod.POST)).andRespond(
380                         withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
381                                 .body(objectMapper.writeValueAsString(serviceAPIDescription)));
382         mockServer.expect(ExpectedCount.once(), requestTo(URI_INVOKERS)).andExpect(method(HttpMethod.POST))
383                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
384         RappInstance rappInstance = getRappInstance();
385         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
386         boolean deployRapp = smeDeployer.deployRappInstance(rapp, rappInstance);
387         mockServer.verify();
388         assertFalse(deployRapp);
389     }
390
391     @Test
392     void testUndeployRappInstance() {
393         UUID rappId = UUID.randomUUID();
394         UUID apfId = UUID.randomUUID();
395         List<String> invokers = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
396         List<String> serviceApis = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
397         Map<String, String> providerFuncs = Map.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()),
398                 String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
399         Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
400                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
401         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(0))))
402                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
403         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(1))))
404                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
405         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(0))))
406                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
407         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(1))))
408                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
409         mockServer.expect(ExpectedCount.once(),
410                         requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[0])))
411                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
412         mockServer.expect(ExpectedCount.once(),
413                         requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[1])))
414                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
415         RappInstance rappInstance = getRappInstance();
416         rappInstance.getSme().setApfId(String.valueOf(apfId));
417         rappInstance.getSme().setProviderFunctionIds(providerFuncs.values().stream().toList());
418         rappInstance.getSme().setServiceApiIds(serviceApis);
419         rappInstance.getSme().setInvokerIds(invokers);
420         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
421         boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
422         mockServer.verify();
423         assertTrue(undeployRapp);
424     }
425
426     @Test
427     void testUndeployRappInstanceNoInvokers() {
428         UUID rappId = UUID.randomUUID();
429         UUID apfId = UUID.randomUUID();
430         List<String> serviceApis = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
431         Map<String, String> providerFuncs = Map.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()),
432                 String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
433         Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
434                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
435         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(0))))
436                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
437         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(1))))
438                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
439         mockServer.expect(ExpectedCount.once(),
440                         requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[0])))
441                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
442         mockServer.expect(ExpectedCount.once(),
443                         requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[1])))
444                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
445         RappInstance rappInstance = getRappInstance();
446         rappInstance.getSme().setApfId(String.valueOf(apfId));
447         rappInstance.getSme().setProviderFunctionIds(providerFuncs.values().stream().toList());
448         rappInstance.getSme().setServiceApiIds(serviceApis);
449         rappInstance.getSme().setInvokerIds(null);
450         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
451         boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
452         mockServer.verify();
453         assertTrue(undeployRapp);
454     }
455
456     @Test
457     void testUndeployRappInstanceNoProviders() {
458         UUID rappId = UUID.randomUUID();
459         UUID apfId = UUID.randomUUID();
460         List<String> invokers = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
461         Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
462                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
463         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(0))))
464                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
465         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(1))))
466                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
467         RappInstance rappInstance = getRappInstance();
468         rappInstance.getSme().setApfId(String.valueOf(apfId));
469         rappInstance.getSme().setProviderFunctionIds(null);
470         rappInstance.getSme().setServiceApiIds(null);
471         rappInstance.getSme().setInvokerIds(invokers);
472         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
473         boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
474         mockServer.verify();
475         assertTrue(undeployRapp);
476     }
477
478     @Test
479     void testUndeployRappInstanceWithoutSme() {
480         UUID rappId = UUID.randomUUID();
481         Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
482                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
483         RappInstance rappInstance = getRappInstance();
484         rappInstance.setSme(null);
485         assertTrue(smeDeployer.undeployRappInstance(rapp, rappInstance));
486     }
487
488     @Test
489     void testUndeployRappInstanceFailure() {
490         UUID rappId = UUID.randomUUID();
491         UUID apfId = UUID.randomUUID();
492         List<String> invokers = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
493         List<String> serviceApis = List.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
494         Map<String, String> providerFuncs = Map.of(String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()),
495                 String.valueOf(UUID.randomUUID()), String.valueOf(UUID.randomUUID()));
496         Rapp rapp = Rapp.builder().rappId(rappId).name(rappId.toString()).packageName(validRappFile)
497                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
498         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(0))))
499                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
500         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_INVOKER, invokers.get(1))))
501                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
502         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(0))))
503                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
504         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_PUBLISH_API, apfId, serviceApis.get(1))))
505                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
506         mockServer.expect(ExpectedCount.once(),
507                         requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[0])))
508                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
509         mockServer.expect(ExpectedCount.once(),
510                         requestTo(String.format(URI_PROVIDER_REGISTRATION, providerFuncs.values().toArray()[1])))
511                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
512         RappInstance rappInstance = getRappInstance();
513         rappInstance.getSme().setApfId(String.valueOf(apfId));
514         rappInstance.getSme().setProviderFunctionIds(providerFuncs.values().stream().toList());
515         rappInstance.getSme().setServiceApiIds(serviceApis);
516         rappInstance.getSme().setInvokerIds(invokers);
517         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
518         boolean undeployRapp = smeDeployer.undeployRappInstance(rapp, rappInstance);
519         mockServer.verify();
520         assertFalse(undeployRapp);
521     }
522
523     @Test
524     void testPrimingAndDeprimingDoesNothing() {
525         assertTrue(smeDeployer.primeRapp(null));
526         assertTrue(smeDeployer.deprimeRapp(null));
527     }
528
529     RappInstance getRappInstance() {
530         RappSMEInstance rappSMEInstance = new RappSMEInstance();
531         rappSMEInstance.setInvokers("invoker-app1");
532         rappSMEInstance.setServiceApis("api-set-1");
533         rappSMEInstance.setAefId("TEST_AEF_ID");
534         rappSMEInstance.setApfId(String.valueOf(UUID.randomUUID()));
535         rappSMEInstance.setProviderFunction("aef-provider-function");
536         RappInstance rappInstance = new RappInstance();
537         rappInstance.setSme(rappSMEInstance);
538         return rappInstance;
539     }
540
541     APIProviderEnrolmentDetails getProviderDomainApiEnrollmentDetails() {
542         APIProviderEnrolmentDetails apiProviderEnrolmentDetails =
543                 new APIProviderEnrolmentDetails(UUID.randomUUID().toString());
544         APIProviderFunctionDetails apiProviderFunctionDetailsAEF = new APIProviderFunctionDetails();
545         apiProviderFunctionDetailsAEF.setApiProvFuncInfo("AEF");
546         apiProviderFunctionDetailsAEF.setApiProvFuncRole(ApiProviderFuncRole.AEF);
547         apiProviderFunctionDetailsAEF.setApiProvFuncId(String.valueOf(UUID.randomUUID()));
548         APIProviderFunctionDetails apiProviderFunctionDetailsAPF = new APIProviderFunctionDetails();
549         apiProviderFunctionDetailsAPF.setApiProvFuncInfo("APF");
550         apiProviderFunctionDetailsAPF.setApiProvFuncRole(ApiProviderFuncRole.APF);
551         apiProviderFunctionDetailsAPF.setApiProvFuncId(String.valueOf(UUID.randomUUID()));
552         apiProviderEnrolmentDetails.setApiProvFuncs(
553                 List.of(apiProviderFunctionDetailsAEF, apiProviderFunctionDetailsAPF));
554         return apiProviderEnrolmentDetails;
555     }
556
557
558     ServiceAPIDescription getServiceApiDescription() {
559         ServiceAPIDescription serviceAPIDescription = new ServiceAPIDescription();
560         serviceAPIDescription.setApiId(String.valueOf(UUID.randomUUID()));
561         AefProfile aefProfile = new AefProfile();
562         aefProfile.setAefId(String.valueOf(UUID.randomUUID()));
563         serviceAPIDescription.setAefProfiles(List.of(aefProfile));
564         return serviceAPIDescription;
565     }
566
567     APIInvokerEnrolmentDetails getApiInvokerEnrollmentDetails() {
568         APIInvokerEnrolmentDetails apiInvokerEnrolmentDetails =
569                 new APIInvokerEnrolmentDetails(String.valueOf(UUID.randomUUID()));
570         com.oransc.rappmanager.sme.invoker.data.ServiceAPIDescription serviceAPIDescription =
571                 new com.oransc.rappmanager.sme.invoker.data.ServiceAPIDescription();
572         serviceAPIDescription.setApiId(String.valueOf(UUID.randomUUID()));
573         apiInvokerEnrolmentDetails.setApiList(List.of(serviceAPIDescription));
574         return apiInvokerEnrolmentDetails;
575     }
576 }