7355b03e91fa457a9d88dbc48d4bc75d68dbf282
[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.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27
28 import java.util.Map;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.mockito.Answers;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
39 import org.onap.policy.clamp.models.acm.concepts.LockState;
40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43
44 class AcElementListenerV1Test {
45
46     @Test
47     void deployTest() throws PfModelException {
48         var acElementListenerV1 = mock(AcElementListenerV1.class, Answers.CALLS_REAL_METHODS);
49         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
50             Map.of(), Map.of());
51         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
52         acElementListenerV1.deploy(compositionElement, instanceElement);
53         verify(acElementListenerV1).deploy(any(), any(), any());
54     }
55
56     @Test
57     void undeployTest() throws PfModelException {
58         var acElementListenerV1 = mock(AcElementListenerV1.class, Answers.CALLS_REAL_METHODS);
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         acElementListenerV1.undeploy(compositionElement, instanceElement);
63         verify(acElementListenerV1).undeploy(instanceElement.instanceId(), instanceElement.elementId());
64     }
65
66     @Test
67     void lockTest() throws PfModelException {
68         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
69         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
70         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
71             Map.of(), Map.of());
72         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
73         acElementListenerV1.lock(compositionElement, instanceElement);
74         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
75             instanceElement.elementId(), null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
76     }
77
78     @Test
79     void deleteTest() throws PfModelException {
80         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
81         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
82         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
83             Map.of(), Map.of());
84         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
85         acElementListenerV1.delete(compositionElement, instanceElement);
86         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
87             instanceElement.elementId(), DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
88     }
89
90     @Test
91     void updateTest() throws PfModelException {
92         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
94         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
95             Map.of(), Map.of());
96         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
97         acElementListenerV1.update(compositionElement, instanceElement, instanceElement);
98         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
99             instanceElement.elementId(), DeployState.DEPLOYED, null,
100             StateChangeResult.NO_ERROR, "Update not supported");
101     }
102
103     @Test
104     void unlockTest() throws PfModelException {
105         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
106         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
107         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
108             Map.of(), Map.of());
109         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
110         acElementListenerV1.unlock(compositionElement, instanceElement);
111         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
112             instanceElement.elementId(), null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
113     }
114
115     @Test
116     void primeTest() throws PfModelException {
117         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
118         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
119         var compositionId = UUID.randomUUID();
120         var toscaConceptIdentifier = new ToscaConceptIdentifier();
121         var composition = new CompositionDto(compositionId, Map.of(toscaConceptIdentifier, Map.of()), Map.of());
122         acElementListenerV1.prime(composition);
123         verify(intermediaryApi)
124             .updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
125     }
126
127     @Test
128     void deprimeTest() throws PfModelException {
129         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
130         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
131         var compositionId = UUID.randomUUID();
132         var toscaConceptIdentifier = new ToscaConceptIdentifier();
133         var composition = new CompositionDto(compositionId, Map.of(toscaConceptIdentifier, Map.of()), Map.of());
134         acElementListenerV1.deprime(composition);
135         verify(intermediaryApi)
136             .updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, "Deprimed");
137     }
138
139     @Test
140     void handleRestartComposition() {
141         var acElementListenerV1 = createAcElementListenerV1(mock(ParticipantIntermediaryApi.class));
142         assertThatThrownBy(() -> acElementListenerV1.handleRestartComposition(null, null))
143                 .isInstanceOf(PfModelException.class);
144     }
145
146     @Test
147     void handleRestartInstance() {
148         var acElementListenerV1 = createAcElementListenerV1(mock(ParticipantIntermediaryApi.class));
149         assertThatThrownBy(() -> acElementListenerV1.handleRestartInstance(null, null,
150                 null, null)).isInstanceOf(PfModelException.class);
151     }
152
153     @Test
154     void migrateTest() throws PfModelException {
155         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
156         var acElementListenerV1 = createAcElementListenerV1(intermediaryApi);
157         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
158             Map.of(), Map.of());
159         var instanceElement = new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of());
160         acElementListenerV1.migrate(compositionElement, compositionElement, instanceElement, instanceElement);
161         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
162             instanceElement.elementId(), DeployState.DEPLOYED, null,
163             StateChangeResult.NO_ERROR, "Migrated");
164     }
165
166     private AcElementListenerV1 createAcElementListenerV1(ParticipantIntermediaryApi intermediaryApi) {
167         return new AcElementListenerV1(intermediaryApi) {
168             @Override
169             public void deploy(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
170                 throws PfModelException {
171
172             }
173
174             @Override
175             public void undeploy(UUID instanceId, UUID elementId) throws PfModelException {
176
177             }
178         };
179     }
180 }