2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-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
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.a1pms.handler;
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;
30 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
37 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
38 import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils;
39 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 class AcElementHandlerTest {
50 private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
52 private final CommonTestData commonTestData = new CommonTestData();
54 private static ToscaServiceTemplate serviceTemplate;
55 private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
56 "org.onap.domain.database.A1PMSAutomationCompositionElement";
60 serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
64 void startMocks() throws A1PolicyServiceException {
65 when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
66 doNothing().when(acA1PmsClient).createService(any());
70 void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
71 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
72 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
73 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
75 var automationCompositionId = commonTestData.getAutomationCompositionId();
76 var element = commonTestData.getAutomationCompositionElement();
77 var automationCompositionElementId = element.getId();
79 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
80 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
81 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
82 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
83 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
85 automationCompositionElementHandler.undeploy(automationCompositionId, automationCompositionElementId);
86 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
87 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
89 when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
90 assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
91 .undeploy(automationCompositionId, automationCompositionElementId));
95 void test_AutomationCompositionElementUpdate() throws A1PolicyServiceException {
96 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
97 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
98 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
99 var element = commonTestData.getAutomationCompositionElement();
101 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
102 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
103 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
104 verify(participantIntermediaryApi).updateAutomationCompositionElementState(
105 commonTestData.getAutomationCompositionId(), element.getId(), DeployState.DEPLOYED, null,
106 StateChangeResult.NO_ERROR, "Deployed");
110 void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
111 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
112 automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
113 var element = commonTestData.getAutomationCompositionElement();
114 when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
116 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
117 assertThrows(A1PolicyServiceException.class,
118 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
119 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
123 void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
124 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
125 automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
126 var element = commonTestData.getAutomationCompositionElement();
127 assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
128 .deploy(commonTestData.getAutomationCompositionId(), element, Map.of()));
132 void testLock() throws PfModelException {
133 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
134 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
135 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
136 var automationCompositionId = UUID.randomUUID();
137 var elementId = UUID.randomUUID();
138 automationCompositionElementHandler.lock(automationCompositionId, elementId);
140 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
141 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
145 void testUnlock() throws PfModelException {
146 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
147 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
148 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
149 var automationCompositionId = UUID.randomUUID();
150 var elementId = UUID.randomUUID();
151 automationCompositionElementHandler.unlock(automationCompositionId, elementId);
153 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
154 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
158 void testUpdate() throws PfModelException {
159 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
160 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
161 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
162 var automationCompositionId = UUID.randomUUID();
163 var element = commonTestData.getAutomationCompositionElement();
164 automationCompositionElementHandler.update(automationCompositionId, element, Map.of());
166 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
167 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
171 void testDelete() throws PfModelException {
172 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
173 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
174 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
175 var automationCompositionId = UUID.randomUUID();
176 var elementId = UUID.randomUUID();
177 automationCompositionElementHandler.delete(automationCompositionId, elementId);
179 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
180 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
184 void testPrime() throws PfModelException {
185 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
186 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
187 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
188 var compositionId = UUID.randomUUID();
189 automationCompositionElementHandler.prime(compositionId, List.of());
191 verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
192 StateChangeResult.NO_ERROR, "Primed");
196 void testDeprime() throws PfModelException {
197 var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
198 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
199 automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
200 var compositionId = UUID.randomUUID();
201 automationCompositionElementHandler.deprime(compositionId);
203 verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
204 StateChangeResult.NO_ERROR, "Deprimed");