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
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.runtime.supervision;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyBoolean;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
32 import java.util.Optional;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
36 import org.onap.policy.clamp.acm.runtime.supervision.comm.AcElementPropertiesPublisher;
37 import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionDeployPublisher;
38 import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionStateChangePublisher;
39 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
40 import org.onap.policy.clamp.models.acm.concepts.AcElementDeployAck;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
43 import org.onap.policy.clamp.models.acm.concepts.DeployState;
44 import org.onap.policy.clamp.models.acm.concepts.LockState;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
47 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
49 class SupervisionAcHandlerTest {
50 private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
51 private static final UUID IDENTIFIER = UUID.randomUUID();
54 void testHandleAutomationCompositionStateChangeAckMessage() {
55 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
56 var automationComposition =
57 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
58 when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
59 .thenReturn(Optional.of(automationComposition));
61 var handler = new SupervisionAcHandler(automationCompositionProvider,
62 mock(AutomationCompositionDeployPublisher.class),
63 mock(AutomationCompositionStateChangePublisher.class),
64 mock(AcElementPropertiesPublisher.class));
66 var automationCompositionAckMessage =
67 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
68 for (var elementEntry : automationComposition.getElements().entrySet()) {
69 var acElementDeployAck =
70 new AcElementDeployAck(DeployState.DEPLOYED, LockState.UNLOCKED, "", "", Map.of(), true, "");
71 automationCompositionAckMessage.getAutomationCompositionResultMap().put(elementEntry.getKey(),
74 automationCompositionAckMessage.setAutomationCompositionId(IDENTIFIER);
76 handler.handleAutomationCompositionStateChangeAckMessage(automationCompositionAckMessage);
78 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
82 void testHandleAutomationCompositionUpdateAckMessage() {
83 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
84 var automationComposition =
85 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
86 when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
87 .thenReturn(Optional.of(automationComposition));
89 var automationCompositionAckMessage =
90 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY_ACK);
91 for (var elementEntry : automationComposition.getElements().entrySet()) {
92 var acElementDeployAck =
93 new AcElementDeployAck(DeployState.DEPLOYED, LockState.LOCKED, "", "", Map.of(), true, "");
94 automationCompositionAckMessage
95 .setAutomationCompositionResultMap(Map.of(elementEntry.getKey(), acElementDeployAck));
97 automationCompositionAckMessage.setParticipantId(CommonTestData.getParticipantId());
98 automationCompositionAckMessage.setAutomationCompositionId(IDENTIFIER);
100 var handler = new SupervisionAcHandler(automationCompositionProvider,
101 mock(AutomationCompositionDeployPublisher.class),
102 mock(AutomationCompositionStateChangePublisher.class),
103 mock(AcElementPropertiesPublisher.class));
105 handler.handleAutomationCompositionUpdateAckMessage(automationCompositionAckMessage);
107 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
111 void testUndeploy() {
112 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
113 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
114 var handler = new SupervisionAcHandler(automationCompositionProvider,
115 mock(AutomationCompositionDeployPublisher.class),
116 acStateChangePublisher, mock(AcElementPropertiesPublisher.class));
117 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
118 var acDefinition = CommonTestData.createAcDefinition(serviceTemplate, AcTypeState.PRIMED);
119 var automationComposition =
120 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Undeploy");
121 handler.undeploy(automationComposition, acDefinition);
123 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
124 verify(acStateChangePublisher).send(any(AutomationComposition.class), anyInt(), anyBoolean());
129 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
130 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
131 var handler = new SupervisionAcHandler(automationCompositionProvider,
132 mock(AutomationCompositionDeployPublisher.class),
133 acStateChangePublisher, mock(AcElementPropertiesPublisher.class));
134 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
135 var acDefinition = CommonTestData.createAcDefinition(serviceTemplate, AcTypeState.PRIMED);
136 var automationComposition =
137 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "UnLock");
138 handler.unlock(automationComposition, acDefinition);
140 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
141 verify(acStateChangePublisher).send(any(AutomationComposition.class), anyInt(), anyBoolean());
146 var automationCompositionProvider = mock(AutomationCompositionProvider.class);
147 var acStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
148 var handler = new SupervisionAcHandler(automationCompositionProvider,
149 mock(AutomationCompositionDeployPublisher.class),
150 acStateChangePublisher, mock(AcElementPropertiesPublisher.class));
151 var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
152 var acDefinition = CommonTestData.createAcDefinition(serviceTemplate, AcTypeState.PRIMED);
153 var automationComposition =
154 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Lock");
155 handler.lock(automationComposition, acDefinition);
157 verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
158 verify(acStateChangePublisher).send(any(AutomationComposition.class), anyInt(), anyBoolean());