2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.api.impl;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
28 import java.util.UUID;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
34 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
35 import org.onap.policy.clamp.models.acm.concepts.DeployState;
36 import org.onap.policy.clamp.models.acm.concepts.LockState;
37 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 class AcElementListenerV2Test {
44 void lockTest() throws PfModelException {
45 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
46 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
47 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
49 var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
50 acElementListenerV2.lock(compositionElement, instanceElement);
51 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
52 instanceElement.elementId(), null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
56 void deleteTest() throws PfModelException {
57 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
58 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
59 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
61 var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
62 acElementListenerV2.delete(compositionElement, instanceElement);
63 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
64 instanceElement.elementId(), DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
68 void updateTest() throws PfModelException {
69 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
70 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
71 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
73 var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
74 acElementListenerV2.update(compositionElement, instanceElement, instanceElement);
75 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
76 instanceElement.elementId(), DeployState.DEPLOYED, null,
77 StateChangeResult.NO_ERROR, "Update not supported");
81 void unlockTest() throws PfModelException {
82 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
83 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
84 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
86 var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
87 acElementListenerV2.unlock(compositionElement, instanceElement);
88 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
89 instanceElement.elementId(), null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
93 void primeTest() throws PfModelException {
94 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
95 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
96 var compositionId = UUID.randomUUID();
97 var toscaConceptIdentifier = new ToscaConceptIdentifier();
98 var composition = new CompositionDto(compositionId, Map.of(toscaConceptIdentifier, Map.of()), Map.of());
99 acElementListenerV2.prime(composition);
100 verify(intermediaryApi)
101 .updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
105 void deprimeTest() throws PfModelException {
106 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
107 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
108 var compositionId = UUID.randomUUID();
109 var toscaConceptIdentifier = new ToscaConceptIdentifier();
110 var composition = new CompositionDto(compositionId, Map.of(toscaConceptIdentifier, Map.of()), Map.of());
111 acElementListenerV2.deprime(composition);
112 verify(intermediaryApi)
113 .updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, "Deprimed");
117 void handleRestartComposition() {
118 var acElementListenerV2 = createAcElementListenerV2(mock(ParticipantIntermediaryApi.class));
119 assertThatThrownBy(() -> acElementListenerV2.handleRestartComposition(null, null))
120 .isInstanceOf(PfModelException.class);
124 void handleRestartInstance() {
125 var acElementListenerV2 = createAcElementListenerV2(mock(ParticipantIntermediaryApi.class));
126 assertThatThrownBy(() -> acElementListenerV2.handleRestartInstance(null, null,
127 null, null)).isInstanceOf(PfModelException.class);
131 void migrateTest() throws PfModelException {
132 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
133 var acElementListenerV2 = createAcElementListenerV2(intermediaryApi);
134 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
136 var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
137 acElementListenerV2.migrate(compositionElement, compositionElement, instanceElement, instanceElement);
138 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
139 instanceElement.elementId(), DeployState.DEPLOYED, null,
140 StateChangeResult.NO_ERROR, "Migrated");
143 private AcElementListenerV2 createAcElementListenerV2(ParticipantIntermediaryApi intermediaryApi) {
144 return new AcElementListenerV2(intermediaryApi) {
146 public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
147 throws PfModelException {
151 public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
152 throws PfModelException {