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
10 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
20 package com.oransc.rappmanager.sme.service;
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;
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;
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;
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)
70 class SmeDeployerTest {
72 MockRestServiceServer mockServer;
75 SmeDeployer smeDeployer;
77 RestTemplate restTemplate;
79 SmeConfiguration smeConfiguration;
80 String validCsarFileLocation = "src/test/resources/";
82 RappInstanceStateMachine rappInstanceStateMachine;
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,
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";
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";
104 mockServer = MockRestServiceServer.createServer(restTemplate);
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();
117 assertEquals(apiProvDomId, apiProviderEnrolmentResponse.getApiProvDomId());
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();
126 assertNull(apiProviderEnrolmentResponse);
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();
145 void testCreateProviderDomain() throws Exception {
146 UUID rappId = UUID.randomUUID();
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);
158 assertTrue(createProviderDomain);
162 void testCreateProviderDomainFailure() {
163 UUID rappId = UUID.randomUUID();
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);
172 assertFalse(createProviderDomain);
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));
185 void testCreatePublishApi() throws Exception {
186 UUID rappId = UUID.randomUUID();
187 UUID apfId = UUID.randomUUID();
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);
200 assertTrue(publishApi);
205 void testCreatePublishApiFailure() {
206 UUID rappId = UUID.randomUUID();
207 UUID apfId = UUID.randomUUID();
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);
217 assertFalse(publishApi);
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);
231 void testCreateInvoker() throws Exception {
232 UUID rappId = UUID.randomUUID();
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);
243 assertTrue(createInvoker);
247 void testCreateInvokerFailure() {
248 UUID rappId = UUID.randomUUID();
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);
257 assertFalse(createInvoker);
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);
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))
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);
298 assertTrue(deployRapp);
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))
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);
327 assertTrue(deployRapp);
331 void testDeployRappInstanceNoProvider() throws Exception {
332 UUID rappId = UUID.randomUUID();
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);
346 assertTrue(deployRapp);
350 void testDeployRappInstanceWithoutSme() {
351 UUID rappId = UUID.randomUUID();
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));
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))
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);
388 assertFalse(deployRapp);
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);
423 assertTrue(undeployRapp);
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);
453 assertTrue(undeployRapp);
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);
475 assertTrue(undeployRapp);
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));
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);
520 assertFalse(undeployRapp);
524 void testPrimingAndDeprimingDoesNothing() {
525 assertTrue(smeDeployer.primeRapp(null));
526 assertTrue(smeDeployer.deprimeRapp(null));
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);
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;
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;
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;