Add DME Participant
[nonrtric/plt/rappmanager.git] / participants / participant-impl-dme / src / test / java / org / oransc / participant / dme / handler / AcElementHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.oransc.participant.dme.handler;
22
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.oransc.participant.dme.exception.DmeException;
45 import org.oransc.participant.dme.restclient.AcDmeClient;
46 import org.oransc.participant.dme.utils.CommonTestData;
47 import org.oransc.participant.dme.utils.ToscaUtils;
48
49 class AcElementHandlerTest {
50
51     private final AcDmeClient acDmeClient = mock(AcDmeClient.class);
52
53     private final CommonTestData commonTestData = new CommonTestData();
54
55     private static ToscaServiceTemplate serviceTemplate;
56     private static final String DME_AUTOMATION_COMPOSITION_ELEMENT =
57             "onap.policy.clamp.ac.element.DMEAutomationCompositionElement";
58
59     @BeforeAll
60     static void init() {
61         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
62     }
63
64     @BeforeEach
65     void startMocks() throws DmeException, JsonProcessingException {
66         when(acDmeClient.isDmeHealthy()).thenReturn(Boolean.TRUE);
67         doNothing().when(acDmeClient).createInfoType(any());
68     }
69
70     @Test
71     void test_automationCompositionElementStateChange() throws DmeException {
72         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
73         var automationCompositionElementHandler =
74                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
75
76         var automationCompositionId = commonTestData.getAutomationCompositionId();
77         var element = commonTestData.getAutomationCompositionElement();
78         var automationCompositionElementId = element.getId();
79
80         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
81         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
82                 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
83         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
84                 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
85
86         automationCompositionElementHandler.undeploy(automationCompositionId, automationCompositionElementId);
87         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
88                 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
89
90         when(acDmeClient.isDmeHealthy()).thenReturn(Boolean.FALSE);
91         assertThrows(DmeException.class, () -> automationCompositionElementHandler
92                 .undeploy(automationCompositionId, automationCompositionElementId));
93     }
94
95     @Test
96     void test_AutomationCompositionElementUpdate() throws DmeException {
97         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
98         var automationCompositionElementHandler =
99                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
100
101         var element = commonTestData.getAutomationCompositionElement();
102         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
103         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
104                 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
105         verify(participantIntermediaryApi).updateAutomationCompositionElementState(
106                 commonTestData.getAutomationCompositionId(), element.getId(), DeployState.DEPLOYED, null,
107                 StateChangeResult.NO_ERROR, "Deployed");
108     }
109
110     @Test
111     void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
112         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
113         var automationCompositionElementHandler =
114                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
115
116         var element = commonTestData.getAutomationCompositionElement();
117         when(acDmeClient.isDmeHealthy()).thenReturn(Boolean.FALSE);
118
119         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
120         assertThrows(DmeException.class,
121                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
122                         nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
123     }
124
125     @Test
126     void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
127         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
128         var automationCompositionElementHandler =
129                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
130
131         var element = commonTestData.getAutomationCompositionElement();
132         assertThrows(DmeException.class, () -> automationCompositionElementHandler
133                 .deploy(commonTestData.getAutomationCompositionId(), element, Map.of()));
134     }
135
136     @Test
137     void testLock() {
138         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
139         var automationCompositionElementHandler =
140                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
141
142         var automationCompositionId = UUID.randomUUID();
143         var elementId = UUID.randomUUID();
144         automationCompositionElementHandler.lock(automationCompositionId, elementId);
145
146         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
147                 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
148     }
149
150     @Test
151     void testUnlock() {
152         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
153         var automationCompositionElementHandler =
154                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
155
156         var automationCompositionId = UUID.randomUUID();
157         var elementId = UUID.randomUUID();
158         automationCompositionElementHandler.unlock(automationCompositionId, elementId);
159
160         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
161                 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
162     }
163
164     @Test
165     void testUpdate() {
166         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
167         var automationCompositionElementHandler =
168                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
169
170         var automationCompositionId = UUID.randomUUID();
171         var element = commonTestData.getAutomationCompositionElement();
172         automationCompositionElementHandler.update(automationCompositionId, element, Map.of());
173
174         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
175                 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
176     }
177
178     @Test
179     void testDelete() {
180         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
181         var automationCompositionElementHandler =
182                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
183
184         var automationCompositionId = UUID.randomUUID();
185         var elementId = UUID.randomUUID();
186         automationCompositionElementHandler.delete(automationCompositionId, elementId);
187
188         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
189                 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
190     }
191
192     @Test
193     void testPrime() {
194         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
195         var automationCompositionElementHandler =
196                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
197
198         var compositionId = UUID.randomUUID();
199         automationCompositionElementHandler.prime(compositionId, List.of());
200
201         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
202                 StateChangeResult.NO_ERROR, "Primed");
203     }
204
205     @Test
206     void testDeprime() {
207         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
208         var automationCompositionElementHandler =
209                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
210
211         var compositionId = UUID.randomUUID();
212         automationCompositionElementHandler.deprime(compositionId);
213
214         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
215                 StateChangeResult.NO_ERROR, "Deprimed");
216     }
217
218     @Test
219     void testHandleRestartComposition() {
220         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
221         var automationCompositionElementHandler =
222                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
223
224         var compositionId = UUID.randomUUID();
225         automationCompositionElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED);
226
227         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
228                 StateChangeResult.NO_ERROR, "Restarted");
229     }
230
231     @Test
232     void testHandleRestartInstanceDeploying() throws PfModelException {
233         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
234         var automationCompositionElementHandler =
235                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
236
237         var automationCompositionId = UUID.randomUUID();
238         var element = commonTestData.getAutomationCompositionElement();
239         var automationCompositionElementId = element.getId();
240         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
241         automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
242                 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYING,
243                 LockState.NONE);
244         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
245                 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
246     }
247
248     @Test
249     void testHandleRestartInstanceDeployed() throws PfModelException {
250         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
251         var automationCompositionElementHandler =
252                 new AutomationCompositionElementHandler(intermediaryApi, acDmeClient);
253
254         var automationCompositionId = UUID.randomUUID();
255         var element = commonTestData.getAutomationCompositionElement();
256         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
257         automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
258                 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYED,
259                 LockState.LOCKED);
260         verify(intermediaryApi).updateAutomationCompositionElementState(automationCompositionId, element.getId(),
261                 DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted");
262     }
263
264     @Test
265     void testHandleRestartInstanceUndeployed() throws PfModelException {
266         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
267         var automationCompositionElementHandler =
268                 new AutomationCompositionElementHandler(intermediaryApi, acDmeClient);
269
270         var automationCompositionId = UUID.randomUUID();
271         var element = commonTestData.getAutomationCompositionElement();
272         var automationCompositionElementId = element.getId();
273         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
274         automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
275                 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.UNDEPLOYING,
276                 LockState.LOCKED);
277         verify(intermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
278                 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
279     }
280
281     @Test
282     void testMigrate() {
283         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
284         var automationCompositionElementHandler =
285                 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
286
287         var automationCompositionId = UUID.randomUUID();
288         var element = commonTestData.getAutomationCompositionElement();
289         automationCompositionElementHandler.migrate(automationCompositionId, element, UUID.randomUUID(), Map.of());
290
291         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
292                 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
293     }
294 }