2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2023-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.acm.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.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
31 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
33 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.oransc.rappmanager.acm.configuration.ACMConfiguration;
38 import com.oransc.rappmanager.dme.service.DmeAcmInterceptor;
39 import com.oransc.rappmanager.models.cache.RappCacheService;
40 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
41 import com.oransc.rappmanager.models.rapp.Rapp;
42 import com.oransc.rappmanager.models.rapp.RappEvent;
43 import com.oransc.rappmanager.models.rapp.RappResourceBuilder;
44 import com.oransc.rappmanager.models.rapp.RappResources;
45 import com.oransc.rappmanager.models.rapp.RappState;
46 import com.oransc.rappmanager.models.rappinstance.RappInstance;
47 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
48 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachineConfig;
49 import java.io.IOException;
50 import java.util.UUID;
51 import java.util.stream.Stream;
52 import org.junit.jupiter.api.BeforeAll;
53 import org.junit.jupiter.api.BeforeEach;
54 import org.junit.jupiter.api.Test;
55 import org.junit.jupiter.api.TestInstance;
56 import org.junit.jupiter.params.ParameterizedTest;
57 import org.junit.jupiter.params.provider.Arguments;
58 import org.junit.jupiter.params.provider.MethodSource;
59 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
60 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
61 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
62 import org.onap.policy.clamp.models.acm.concepts.DeployState;
63 import org.onap.policy.clamp.models.acm.concepts.LockState;
64 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
65 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.PrimeOrder;
66 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
67 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
70 import org.springframework.boot.test.context.SpringBootTest;
71 import org.springframework.boot.test.mock.mockito.SpyBean;
72 import org.springframework.http.HttpMethod;
73 import org.springframework.http.HttpStatus;
74 import org.springframework.http.MediaType;
75 import org.springframework.test.web.client.ExpectedCount;
76 import org.springframework.test.web.client.MockRestServiceServer;
77 import org.springframework.web.client.RestTemplate;
80 classes = {BeanTestConfiguration.class, ACMConfiguration.class, AcmDeployer.class, DmeAcmInterceptor.class,
81 RappCsarConfigurationHandler.class, RappCacheService.class, RappInstanceStateMachineConfig.class,
82 RappInstanceStateMachine.class})
83 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
85 class AcmDeployerTest {
87 MockRestServiceServer mockServer;
89 AcmDeployer acmDeployer;
91 RestTemplate restTemplate;
93 ACMConfiguration acmConfiguration;
95 RappInstanceStateMachine rappInstanceStateMachine;
97 RappCsarConfigurationHandler rappCsarConfigurationHandler;
99 ObjectMapper objectMapper;
101 RappResourceBuilder rappResourceBuilder = new RappResourceBuilder();
102 private final String validRappFile = "valid-rapp-package.csar";
103 String validCsarFileLocation = "src/test/resources/";
104 String URI_ACM_COMPOSITIONS, URI_ACM_COMPOSITION, URI_ACM_INSTANCES, URI_ACM_INSTANCE;
108 URI_ACM_COMPOSITIONS = acmConfiguration.getBaseUrl() + "compositions";
109 URI_ACM_COMPOSITION = acmConfiguration.getBaseUrl() + "compositions/%s";
110 URI_ACM_INSTANCES = acmConfiguration.getBaseUrl() + "compositions/%s/instances";
111 URI_ACM_INSTANCE = acmConfiguration.getBaseUrl() + "compositions/%s/instances/%s";
116 mockServer = MockRestServiceServer.createServer(restTemplate);
120 void testCreateComposition() throws IOException {
121 RappResources rappResources = rappResourceBuilder.getResources();
122 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
123 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
124 .rappResources(rappResources).build();
126 String compositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
128 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
129 commissioningResponseExpected.setCompositionId(UUID.randomUUID());
130 mockServer.expect(ExpectedCount.once(), requestTo(URI_ACM_COMPOSITIONS)).andExpect(method(HttpMethod.POST))
131 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
132 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
133 CommissioningResponse commissioningResponseActual = acmDeployer.createComposition(compositionPayload);
135 assertEquals(commissioningResponseActual.getCompositionId(), commissioningResponseExpected.getCompositionId());
139 void testCreateCompositionFailure() {
140 RappResources rappResources = rappResourceBuilder.getResources();
141 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
142 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
143 .rappResources(rappResources).build();
144 String compositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
145 mockServer.expect(ExpectedCount.once(), requestTo(URI_ACM_COMPOSITIONS)).andExpect(method(HttpMethod.POST))
146 .andRespond(withStatus(HttpStatus.BAD_GATEWAY));
147 CommissioningResponse commissioningResponseActual = acmDeployer.createComposition(compositionPayload);
149 assertNull(commissioningResponseActual);
153 void testCompositionPriming() {
154 UUID compositionId = UUID.randomUUID();
155 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
156 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
158 acmDeployer.primeACMComposition(compositionId, PrimeOrder.PRIME);
163 void testDeployRappInstance() throws Exception {
165 UUID compositionId = UUID.randomUUID();
166 UUID instanceId = UUID.randomUUID();
167 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).compositionId(compositionId)
168 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
169 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
170 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
171 InstantiationResponse instantiationResponse = new InstantiationResponse();
172 instantiationResponse.setInstanceId(instanceId);
173 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCES, compositionId)))
174 .andExpect(method(HttpMethod.POST)).andRespond(
175 withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
176 .body(objectMapper.writeValueAsString(instantiationResponse)));
177 mockServer.expect(ExpectedCount.once(),
178 requestTo(acmConfiguration.getBaseUrl() + "compositions/" + compositionId + "/instances/" + instanceId))
179 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
180 boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
181 assertTrue(rappDeployStateActual);
186 void testDeployRappInstanceWithoutDmeInjection() throws Exception {
188 UUID compositionId = UUID.randomUUID();
189 UUID instanceId = UUID.randomUUID();
190 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).compositionId(compositionId)
191 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
192 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
193 rappInstance.setDme(null);
194 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
195 InstantiationResponse instantiationResponse = new InstantiationResponse();
196 instantiationResponse.setInstanceId(instanceId);
197 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCES, compositionId)))
198 .andExpect(method(HttpMethod.POST)).andRespond(
199 withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
200 .body(objectMapper.writeValueAsString(instantiationResponse)));
201 mockServer.expect(ExpectedCount.once(),
202 requestTo(acmConfiguration.getBaseUrl() + "compositions/" + compositionId + "/instances/" + instanceId))
203 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
204 boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
205 assertTrue(rappDeployStateActual);
210 void testDeployRappInstanceFailureWithNoInstanceId() throws JsonProcessingException {
211 UUID compositionId = UUID.randomUUID();
212 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).compositionId(compositionId)
213 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
214 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
215 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
216 InstantiationResponse instantiationResponse = new InstantiationResponse();
217 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCES, compositionId)))
218 .andExpect(method(HttpMethod.POST)).andRespond(
219 withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
220 .body(objectMapper.writeValueAsString(instantiationResponse)));
222 boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
224 assertFalse(rappDeployStateActual);
228 void testDeployRappInstanceFailure() {
229 UUID compositionId = UUID.randomUUID();
230 Rapp rapp = Rapp.builder().name("").packageName(validRappFile).compositionId(compositionId)
231 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
232 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
233 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
234 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCES, compositionId)))
235 .andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.BAD_GATEWAY));
237 boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
239 assertFalse(rappDeployStateActual);
243 void testUndeployRappInstance() throws JsonProcessingException {
244 UUID compositionId = UUID.randomUUID();
245 UUID rappId = UUID.randomUUID();
246 UUID instanceId = UUID.randomUUID();
247 Rapp rapp = Rapp.builder().rappId(rappId).name("").packageName(validRappFile).compositionId(compositionId)
248 .state(RappState.PRIMED).build();
250 expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.DEPLOYED, LockState.LOCKED,
251 ExpectedCount.once());
253 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
254 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
256 expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.UNDEPLOYED, LockState.UNLOCKED,
257 ExpectedCount.once());
259 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
260 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
262 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
263 rappInstance.getAcm().setAcmInstanceId(instanceId);
264 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
265 boolean rappUndeployStateActual = acmDeployer.undeployRappInstance(rapp, rappInstance);
267 assertTrue(rappUndeployStateActual);
271 void testUndeployRappInstanceFailure() throws JsonProcessingException {
272 UUID compositionId = UUID.randomUUID();
273 UUID rappId = UUID.randomUUID();
274 UUID instanceId = UUID.randomUUID();
275 Rapp rapp = Rapp.builder().name(rappId.toString()).packageName(validRappFile).compositionId(compositionId)
276 .state(RappState.PRIMED).build();
278 expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.DEPLOYED, LockState.LOCKED,
279 ExpectedCount.once());
281 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
282 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
284 expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.UNDEPLOYING, LockState.UNLOCKING,
285 ExpectedCount.manyTimes());
287 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
288 rappInstance.getAcm().setAcmInstanceId(instanceId);
289 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
290 boolean rappUndeployStateActual = acmDeployer.undeployRappInstance(rapp, rappInstance);
292 assertFalse(rappUndeployStateActual);
296 void testUndeployRappInstanceACMErrorFailure() throws JsonProcessingException {
297 UUID compositionId = UUID.randomUUID();
298 UUID rappId = UUID.randomUUID();
299 UUID instanceId = UUID.randomUUID();
300 Rapp rapp = Rapp.builder().name(rappId.toString()).packageName(validRappFile).compositionId(compositionId)
301 .state(RappState.PRIMED).build();
302 expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.DEPLOYED, LockState.LOCKED,
303 ExpectedCount.once());
304 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
305 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
306 mockServer.expect(ExpectedCount.manyTimes(),
307 requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId))).andExpect(method(HttpMethod.GET))
308 .andRespond(withServerError());
309 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
310 rappInstance.getAcm().setAcmInstanceId(instanceId);
311 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
312 boolean rappUndeployStateActual = acmDeployer.undeployRappInstance(rapp, rappInstance);
314 assertFalse(rappUndeployStateActual);
318 @MethodSource("getAcmStatusEventMap")
319 void testSyncRappInstanceStatus(DeployState deployState, LockState lockState, RappEvent rappEvent)
320 throws JsonProcessingException {
321 UUID compositionId = UUID.randomUUID();
322 UUID instanceId = UUID.randomUUID();
323 expectAcmGetInstanceToReturnState(compositionId, instanceId, deployState, lockState, ExpectedCount.once());
324 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
325 rappInstance.getAcm().setAcmInstanceId(instanceId);
326 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
327 acmDeployer.syncRappInstanceStatus(compositionId, rappInstance);
329 verify(rappInstanceStateMachine, times(1)).sendRappInstanceEvent(rappInstance, rappEvent);
332 private static Stream<Arguments> getAcmStatusEventMap() {
333 return Stream.of(Arguments.of(DeployState.UNDEPLOYING, LockState.UNLOCKING, RappEvent.UNDEPLOYING),
334 Arguments.of(DeployState.DEPLOYED, LockState.LOCKED, RappEvent.ACMDEPLOYED),
335 Arguments.of(DeployState.DEPLOYING, LockState.LOCKING, RappEvent.DEPLOYING),
336 Arguments.of(DeployState.UNDEPLOYED, LockState.UNLOCKED, RappEvent.ACMUNDEPLOYED));
340 void testSyncRappStatusFailure() {
341 UUID compositionId = UUID.randomUUID();
342 UUID instanceId = UUID.randomUUID();
344 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
345 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_GATEWAY));
347 RappInstance rappInstance = rappResourceBuilder.getRappInstance();
348 rappInstance.getAcm().setAcmInstanceId(instanceId);
349 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
350 acmDeployer.syncRappInstanceStatus(compositionId, rappInstance);
352 verify(rappInstanceStateMachine, never()).sendRappInstanceEvent(any(), any());
356 void testPrimeRapp() throws JsonProcessingException {
357 UUID compositionId = UUID.randomUUID();
358 RappResources rappResources = rappResourceBuilder.getResources();
359 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
360 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
361 .compositionId(compositionId).rappResources(rappResources).build();
363 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
364 commissioningResponseExpected.setCompositionId(compositionId);
365 mockServer.expect(ExpectedCount.once(), requestTo(URI_ACM_COMPOSITIONS)).andExpect(method(HttpMethod.POST))
366 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
367 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
368 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
369 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
370 boolean primeRapp = acmDeployer.primeRapp(rapp);
372 assertTrue(primeRapp);
376 void testPrimeRappFailure() throws JsonProcessingException {
377 UUID compositionId = UUID.randomUUID();
378 RappResources rappResources = rappResourceBuilder.getResources();
379 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
380 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
381 .compositionId(compositionId).rappResources(rappResources).build();
383 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
384 commissioningResponseExpected.setCompositionId(compositionId);
385 mockServer.expect(ExpectedCount.once(), requestTo(URI_ACM_COMPOSITIONS)).andExpect(method(HttpMethod.POST))
386 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
387 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
388 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
389 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
390 boolean primeRapp = acmDeployer.primeRapp(rapp);
392 assertFalse(primeRapp);
393 assertEquals(RappState.COMMISSIONED, rapp.getState());
397 void testPrimeRappFailureWithoutCompositionId() throws JsonProcessingException {
398 UUID compositionId = UUID.randomUUID();
399 RappResources rappResources = rappResourceBuilder.getResources();
400 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
401 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
402 .compositionId(compositionId).rappResources(rappResources).build();
404 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
405 mockServer.expect(ExpectedCount.once(), requestTo(URI_ACM_COMPOSITIONS)).andExpect(method(HttpMethod.POST))
406 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
407 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
408 boolean primeRapp = acmDeployer.primeRapp(rapp);
410 assertFalse(primeRapp);
414 void testDeprimeRapp() throws JsonProcessingException {
415 UUID compositionId = UUID.randomUUID();
416 RappResources rappResources = rappResourceBuilder.getResources();
417 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
418 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
419 .compositionId(compositionId).rappResources(rappResources).build();
421 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
422 commissioningResponseExpected.setCompositionId(compositionId);
423 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
424 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.OK));
425 AutomationCompositionDefinition automationCompositionDefinition =
426 getAutomationCompositionDefinition(compositionId, AcTypeState.COMMISSIONED);
427 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
428 .andExpect(method(HttpMethod.GET)).andRespond(
429 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
430 .body(objectMapper.writeValueAsString(automationCompositionDefinition)));
431 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
432 .andExpect(method(HttpMethod.DELETE)).andRespond(
433 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
434 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
436 boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
438 assertTrue(deprimeRapp);
442 void testDeprimeRappClientRetry() throws JsonProcessingException {
443 UUID compositionId = UUID.randomUUID();
444 RappResources rappResources = rappResourceBuilder.getResources();
445 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
446 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
447 .compositionId(compositionId).rappResources(rappResources).build();
449 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
450 commissioningResponseExpected.setCompositionId(compositionId);
451 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
452 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.OK));
453 AutomationCompositionDefinition automationCompositionDefinition =
454 getAutomationCompositionDefinition(compositionId, AcTypeState.DEPRIMING);
455 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
456 .andExpect(method(HttpMethod.GET)).andRespond(
457 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
458 .body(objectMapper.writeValueAsString(automationCompositionDefinition)));
459 automationCompositionDefinition = getAutomationCompositionDefinition(compositionId, AcTypeState.COMMISSIONED);
460 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
461 .andExpect(method(HttpMethod.GET)).andRespond(
462 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
463 .body(objectMapper.writeValueAsString(automationCompositionDefinition)));
464 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
465 .andExpect(method(HttpMethod.DELETE)).andRespond(
466 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
467 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
469 boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
471 assertTrue(deprimeRapp);
475 void testDeprimeFailureRapp() throws JsonProcessingException {
476 UUID compositionId = UUID.randomUUID();
477 RappResources rappResources = rappResourceBuilder.getResources();
478 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
479 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
480 .compositionId(compositionId).rappResources(rappResources).build();
482 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
483 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.OK));
484 AutomationCompositionDefinition automationCompositionDefinition =
485 getAutomationCompositionDefinition(compositionId, AcTypeState.COMMISSIONED);
486 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
487 .andExpect(method(HttpMethod.GET)).andRespond(
488 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
489 .body(objectMapper.writeValueAsString(automationCompositionDefinition)));
490 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
491 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
493 boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
495 assertFalse(deprimeRapp);
499 void testDeprimeACMStatusFailureRapp() throws JsonProcessingException {
500 UUID compositionId = UUID.randomUUID();
501 RappResources rappResources = rappResourceBuilder.getResources();
502 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
503 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
504 .compositionId(compositionId).rappResources(rappResources).build();
506 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
507 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.OK));
508 AutomationCompositionDefinition automationCompositionDefinition =
509 getAutomationCompositionDefinition(compositionId, AcTypeState.DEPRIMING);
510 mockServer.expect(ExpectedCount.manyTimes(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
511 .andExpect(method(HttpMethod.GET)).andRespond(
512 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
513 .body(objectMapper.writeValueAsString(automationCompositionDefinition)));
515 boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
517 assertFalse(deprimeRapp);
521 void testDeprimeACMStatusErrorRapp() {
522 UUID compositionId = UUID.randomUUID();
523 RappResources rappResources = rappResourceBuilder.getResources();
524 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
525 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
526 .compositionId(compositionId).rappResources(rappResources).build();
528 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
529 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.OK));
530 mockServer.expect(ExpectedCount.manyTimes(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
531 .andExpect(method(HttpMethod.GET)).andRespond(withServerError());
532 boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
534 assertFalse(deprimeRapp);
538 void testDeprimeExceptionRapp() {
539 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
540 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
541 boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
542 assertFalse(deprimeRapp);
546 void testDeleteComposition() throws JsonProcessingException {
547 UUID compositionId = UUID.randomUUID();
548 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
549 commissioningResponseExpected.setCompositionId(compositionId);
550 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
551 .andExpect(method(HttpMethod.DELETE)).andRespond(
552 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
553 .body(objectMapper.writeValueAsString(commissioningResponseExpected)));
554 CommissioningResponse commissioningResponse = acmDeployer.deleteComposition(compositionId);
556 assertEquals(commissioningResponse.getCompositionId(), compositionId);
560 void testDeleteCompositionFailure() {
561 UUID compositionId = UUID.randomUUID();
562 CommissioningResponse commissioningResponseExpected = new CommissioningResponse();
563 commissioningResponseExpected.setCompositionId(compositionId);
564 mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_COMPOSITION, compositionId)))
565 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
566 CommissioningResponse commissioningResponse = acmDeployer.deleteComposition(compositionId);
568 assertNull(commissioningResponse);
571 void expectAcmGetInstanceToReturnState(UUID compositionId, UUID instanceId, DeployState deployState,
572 LockState lockState, ExpectedCount expectedCount) throws JsonProcessingException {
573 AutomationComposition automationCompositionDeployed = new AutomationComposition();
574 automationCompositionDeployed.setCompositionId(compositionId);
575 automationCompositionDeployed.setInstanceId(instanceId);
576 automationCompositionDeployed.setDeployState(deployState);
577 automationCompositionDeployed.setLockState(lockState);
579 mockServer.expect(expectedCount, requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
580 .andExpect(method(HttpMethod.GET)).andRespond(
581 withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
582 .body(objectMapper.writeValueAsString(automationCompositionDeployed)));
585 AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId, AcTypeState acTypeState) {
586 AutomationCompositionDefinition automationCompositionDefinition = new AutomationCompositionDefinition();
587 automationCompositionDefinition.setCompositionId(compositionId);
588 automationCompositionDefinition.setState(acTypeState);
589 automationCompositionDefinition.setServiceTemplate(new ToscaServiceTemplate());
590 return automationCompositionDefinition;