30c77a144c40b6eff4be944934ea3ea1526a62b5
[nonrtric/plt/rappmanager.git] / rapp-manager-acm / src / test / java / com / oransc / rappmanager / acm / service / AcmDeployerTest.java
1 /*-
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
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.acm.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.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;
34
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;
78
79 @SpringBootTest(
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)
84 @AutoConfigureMockMvc
85 class AcmDeployerTest {
86
87     MockRestServiceServer mockServer;
88     @SpyBean
89     AcmDeployer acmDeployer;
90     @Autowired
91     RestTemplate restTemplate;
92     @Autowired
93     ACMConfiguration acmConfiguration;
94     @SpyBean
95     RappInstanceStateMachine rappInstanceStateMachine;
96     @Autowired
97     RappCsarConfigurationHandler rappCsarConfigurationHandler;
98     @Autowired
99     ObjectMapper objectMapper;
100
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;
105
106     @BeforeAll
107     void initACMURI() {
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";
112     }
113
114     @BeforeEach
115     public void init() {
116         mockServer = MockRestServiceServer.createServer(restTemplate);
117     }
118
119     @Test
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();
125
126         String compositionPayload = rappCsarConfigurationHandler.getAcmCompositionPayload(rapp);
127
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);
134         mockServer.verify();
135         assertEquals(commissioningResponseActual.getCompositionId(), commissioningResponseExpected.getCompositionId());
136     }
137
138     @Test
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);
148         mockServer.verify();
149         assertNull(commissioningResponseActual);
150     }
151
152     @Test
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));
157
158         acmDeployer.primeACMComposition(compositionId, PrimeOrder.PRIME);
159         mockServer.verify();
160     }
161
162     @Test
163     void testDeployRappInstance() throws Exception {
164
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);
182         mockServer.verify();
183     }
184
185     @Test
186     void testDeployRappInstanceWithoutDmeInjection() throws Exception {
187
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);
206         mockServer.verify();
207     }
208
209     @Test
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)));
221
222         boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
223         mockServer.verify();
224         assertFalse(rappDeployStateActual);
225     }
226
227     @Test
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));
236
237         boolean rappDeployStateActual = acmDeployer.deployRappInstance(rapp, rappInstance);
238         mockServer.verify();
239         assertFalse(rappDeployStateActual);
240     }
241
242     @Test
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();
249
250         expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.DEPLOYED, LockState.LOCKED,
251                 ExpectedCount.once());
252
253         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
254                 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
255
256         expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.UNDEPLOYED, LockState.UNLOCKED,
257                 ExpectedCount.once());
258
259         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
260                 .andExpect(method(HttpMethod.DELETE)).andRespond(withStatus(HttpStatus.NO_CONTENT));
261
262         RappInstance rappInstance = rappResourceBuilder.getRappInstance();
263         rappInstance.getAcm().setAcmInstanceId(instanceId);
264         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
265         boolean rappUndeployStateActual = acmDeployer.undeployRappInstance(rapp, rappInstance);
266         mockServer.verify();
267         assertTrue(rappUndeployStateActual);
268     }
269
270     @Test
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();
277
278         expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.DEPLOYED, LockState.LOCKED,
279                 ExpectedCount.once());
280
281         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
282                 .andExpect(method(HttpMethod.PUT)).andRespond(withStatus(HttpStatus.ACCEPTED));
283
284         expectAcmGetInstanceToReturnState(compositionId, instanceId, DeployState.UNDEPLOYING, LockState.UNLOCKING,
285                 ExpectedCount.manyTimes());
286
287         RappInstance rappInstance = rappResourceBuilder.getRappInstance();
288         rappInstance.getAcm().setAcmInstanceId(instanceId);
289         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
290         boolean rappUndeployStateActual = acmDeployer.undeployRappInstance(rapp, rappInstance);
291         mockServer.verify();
292         assertFalse(rappUndeployStateActual);
293     }
294
295     @Test
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);
313         mockServer.verify();
314         assertFalse(rappUndeployStateActual);
315     }
316
317     @ParameterizedTest
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);
328         mockServer.verify();
329         verify(rappInstanceStateMachine, times(1)).sendRappInstanceEvent(rappInstance, rappEvent);
330     }
331
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));
337     }
338
339     @Test
340     void testSyncRappStatusFailure() {
341         UUID compositionId = UUID.randomUUID();
342         UUID instanceId = UUID.randomUUID();
343
344         mockServer.expect(ExpectedCount.once(), requestTo(String.format(URI_ACM_INSTANCE, compositionId, instanceId)))
345                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_GATEWAY));
346
347         RappInstance rappInstance = rappResourceBuilder.getRappInstance();
348         rappInstance.getAcm().setAcmInstanceId(instanceId);
349         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
350         acmDeployer.syncRappInstanceStatus(compositionId, rappInstance);
351         mockServer.verify();
352         verify(rappInstanceStateMachine, never()).sendRappInstanceEvent(any(), any());
353     }
354
355     @Test
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();
362
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);
371         mockServer.verify();
372         assertTrue(primeRapp);
373     }
374
375     @Test
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();
382
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);
391         mockServer.verify();
392         assertFalse(primeRapp);
393         assertEquals(RappState.COMMISSIONED, rapp.getState());
394     }
395
396     @Test
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();
403
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);
409         mockServer.verify();
410         assertFalse(primeRapp);
411     }
412
413     @Test
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();
420
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)));
435
436         boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
437         mockServer.verify();
438         assertTrue(deprimeRapp);
439     }
440
441     @Test
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();
448
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)));
468
469         boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
470         mockServer.verify();
471         assertTrue(deprimeRapp);
472     }
473
474     @Test
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();
481
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));
492
493         boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
494         mockServer.verify();
495         assertFalse(deprimeRapp);
496     }
497
498     @Test
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();
505
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)));
514
515         boolean deprimeRapp = acmDeployer.deprimeRapp(rapp);
516         mockServer.verify();
517         assertFalse(deprimeRapp);
518     }
519
520     @Test
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();
527
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);
533         mockServer.verify();
534         assertFalse(deprimeRapp);
535     }
536
537     @Test
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);
543     }
544
545     @Test
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);
555         mockServer.verify();
556         assertEquals(commissioningResponse.getCompositionId(), compositionId);
557     }
558
559     @Test
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);
567         mockServer.verify();
568         assertNull(commissioningResponse);
569     }
570
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);
578
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)));
583     }
584
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;
591     }
592 }