be8275ca626980c02a5425518e8fd2a82d00525f
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.api.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26
27 import java.util.Map;
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;
40
41 class AcElementListenerV2Test {
42
43     @Test
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(),
48             Map.of(), Map.of());
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");
53     }
54
55     @Test
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(),
60             Map.of(), Map.of());
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");
65     }
66
67     @Test
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(),
72             Map.of(), Map.of());
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");
78     }
79
80     @Test
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(),
85             Map.of(), Map.of());
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");
90     }
91
92     @Test
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");
102     }
103
104     @Test
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");
114     }
115
116     @Test
117     void handleRestartComposition() {
118         var acElementListenerV2 = createAcElementListenerV2(mock(ParticipantIntermediaryApi.class));
119         assertThatThrownBy(() -> acElementListenerV2.handleRestartComposition(null, null))
120                 .isInstanceOf(PfModelException.class);
121     }
122
123     @Test
124     void handleRestartInstance() {
125         var acElementListenerV2 = createAcElementListenerV2(mock(ParticipantIntermediaryApi.class));
126         assertThatThrownBy(() -> acElementListenerV2.handleRestartInstance(null, null,
127                 null, null)).isInstanceOf(PfModelException.class);
128     }
129
130     @Test
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(),
135             Map.of(), Map.of());
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");
141     }
142
143     @Test
144     void migratePrecheckTest() throws PfModelException {
145         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
146         var acElementListenerV1 = createAcElementListenerV2(intermediaryApi);
147         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
148                 Map.of(), Map.of());
149         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
150         acElementListenerV1.migratePrecheck(compositionElement, compositionElement, instanceElement, instanceElement);
151         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
152                 instanceElement.elementId(), DeployState.DEPLOYED, null,
153                 StateChangeResult.NO_ERROR, "Migration Precheck completed");
154     }
155
156     @Test
157     void reviewTest() throws PfModelException {
158         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
159         var acElementListenerV1 = createAcElementListenerV2(intermediaryApi);
160         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
161                 Map.of(), Map.of());
162         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
163         acElementListenerV1.review(compositionElement, instanceElement);
164         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
165                 instanceElement.elementId(), DeployState.DEPLOYED, null,
166                 StateChangeResult.NO_ERROR, "Review completed");
167     }
168
169     @Test
170     void prepareTest() throws PfModelException {
171         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
172         var acElementListenerV1 = createAcElementListenerV2(intermediaryApi);
173         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
174                 Map.of(), Map.of());
175         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
176         acElementListenerV1.prepare(compositionElement, instanceElement);
177         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
178                 instanceElement.elementId(), DeployState.UNDEPLOYED, null,
179                 StateChangeResult.NO_ERROR, "Prepare completed");
180     }
181
182     private AcElementListenerV2 createAcElementListenerV2(ParticipantIntermediaryApi intermediaryApi) {
183         return new AcElementListenerV2(intermediaryApi) {
184             @Override
185             public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) {
186                 // dummy implementation
187             }
188
189             @Override
190             public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) {
191                 // dummy implementation
192             }
193         };
194     }
195 }