35da039a5aa9cdae253686a30971652f96f4445f
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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.onap.policy.clamp.acm.participant.intermediary.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.mockito.Mockito.mock;
28
29 import java.util.List;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
34 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
35 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.DeployState;
38 import org.onap.policy.clamp.models.acm.concepts.LockState;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
42 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
43 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.springframework.test.context.junit.jupiter.SpringExtension;
46
47 @ExtendWith(SpringExtension.class)
48 class AutomationCompositionHandlerTest {
49
50     private final CommonTestData commonTestData = new CommonTestData();
51
52     @Test
53     void automationCompositionHandlerTest() {
54         var ach = commonTestData.getMockAutomationCompositionHandler();
55         assertNotNull(ach.getAutomationCompositionMap());
56         assertNotNull(ach.getElementsOnThisParticipant());
57
58         var listener = mock(AutomationCompositionElementListener.class);
59         ach.registerAutomationCompositionElementListener(listener);
60         assertThat(ach.getListeners()).contains(listener);
61     }
62
63     @Test
64     void updateNullAutomationCompositionHandlerTest() {
65         var id = UUID.randomUUID();
66
67         var ach = commonTestData.getMockAutomationCompositionHandler();
68         assertDoesNotThrow(
69                 () -> ach.updateAutomationCompositionElementState(null, null, DeployState.UNDEPLOYED, LockState.NONE));
70
71         assertDoesNotThrow(
72                 () -> ach.updateAutomationCompositionElementState(null, id, DeployState.UNDEPLOYED, LockState.NONE));
73     }
74
75     @Test
76     void updateAutomationCompositionHandlerTest() {
77         var uuid = UUID.randomUUID();
78         var partecipantId = CommonTestData.getParticipantId();
79         var definition = CommonTestData.getDefinition();
80
81         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
82         var key = ach.getElementsOnThisParticipant().keySet().iterator().next();
83         var value = ach.getElementsOnThisParticipant().get(key);
84         assertEquals(DeployState.UNDEPLOYED, value.getDeployState());
85         assertEquals(LockState.LOCKED, value.getLockState());
86         ach.updateAutomationCompositionElementState(CommonTestData.AC_ID_1, uuid, DeployState.DEPLOYED,
87                 LockState.UNLOCKED);
88         assertEquals(DeployState.DEPLOYED, value.getDeployState());
89
90         ach.getAutomationCompositionMap().values().iterator().next().getElements().putIfAbsent(key, value);
91         ach.updateAutomationCompositionElementState(CommonTestData.AC_ID_1, key, DeployState.DEPLOYED,
92                 LockState.UNLOCKED);
93         assertEquals(DeployState.DEPLOYED, value.getDeployState());
94
95         ach.getElementsOnThisParticipant().remove(key, value);
96         ach.getAutomationCompositionMap().values().iterator().next().getElements().clear();
97         assertDoesNotThrow(() -> ach.updateAutomationCompositionElementState(CommonTestData.AC_ID_1, key,
98                 DeployState.DEPLOYED, LockState.UNLOCKED));
99     }
100
101     @Test
102     void handleAutomationCompositionStateChangeTest() {
103         var uuid = UUID.randomUUID();
104         var partecipantId = CommonTestData.getParticipantId();
105         var definition = CommonTestData.getDefinition();
106         var stateChange = commonTestData.getStateChange(partecipantId, uuid, DeployOrder.NONE, LockOrder.UNLOCK);
107         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
108         assertDoesNotThrow(() -> ach
109                 .handleAutomationCompositionStateChange(mock(AutomationCompositionStateChange.class), List.of()));
110
111         ach.handleAutomationCompositionStateChange(stateChange, List.of());
112         var newPartecipantId = CommonTestData.getRndParticipantId();
113         stateChange.setAutomationCompositionId(UUID.randomUUID());
114         stateChange.setParticipantId(newPartecipantId);
115         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChange, List.of()));
116     }
117
118     @Test
119     void handleAutomationCompositionDeployTest() {
120         var acd = new AutomationCompositionElementDefinition();
121         var definition = CommonTestData.getDefinition();
122         acd.setAcElementDefinitionId(definition);
123         var updateMsg = new AutomationCompositionDeploy();
124         updateMsg.setAutomationCompositionId(UUID.randomUUID());
125         var uuid = UUID.randomUUID();
126         updateMsg.setMessageId(uuid);
127         var partecipantId = CommonTestData.getParticipantId();
128         updateMsg.setParticipantId(partecipantId);
129         updateMsg.setFirstStartPhase(true);
130         updateMsg.setStartPhase(0);
131         var acElementDefinitions = List.of(acd);
132         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
133         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions));
134         updateMsg.setFirstStartPhase(false);
135         updateMsg.setStartPhase(1);
136         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions));
137
138         ach.getAutomationCompositionMap().clear();
139         updateMsg.setFirstStartPhase(true);
140         updateMsg.setStartPhase(0);
141         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions));
142
143         updateMsg.setAutomationCompositionId(UUID.randomUUID());
144         updateMsg.setParticipantUpdatesList(List.of(mock(ParticipantDeploy.class)));
145         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions));
146
147         updateMsg.setStartPhase(1);
148         var participantDeploy = new ParticipantDeploy();
149         participantDeploy.setParticipantId(partecipantId);
150         var element = new AcElementDeploy();
151         element.setDefinition(definition);
152         participantDeploy.setAcElementList(List.of(element));
153         updateMsg.setParticipantUpdatesList(List.of(participantDeploy));
154
155         var acd2 = new AutomationCompositionElementDefinition();
156         acd2.setAcElementDefinitionId(definition);
157         acd2.setAutomationCompositionElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
158         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(updateMsg, List.of(acd2)));
159
160     }
161
162     @Test
163     void acUndeployTest() {
164         var uuid = UUID.randomUUID();
165         var partecipantId = CommonTestData.getParticipantId();
166         var definition = CommonTestData.getDefinition();
167
168         var stateChangeUndeploy =
169                 commonTestData.getStateChange(partecipantId, uuid, DeployOrder.UNDEPLOY, LockOrder.NONE);
170
171         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
172         stateChangeUndeploy
173                 .setAutomationCompositionId(ach.getAutomationCompositionMap().entrySet().iterator().next().getKey());
174         ach.handleAutomationCompositionStateChange(stateChangeUndeploy, List.of());
175         stateChangeUndeploy.setAutomationCompositionId(UUID.randomUUID());
176         stateChangeUndeploy.setParticipantId(CommonTestData.getRndParticipantId());
177         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChangeUndeploy, List.of()));
178     }
179
180     @Test
181     void automationCompositionStateUnlock() {
182         var uuid = UUID.randomUUID();
183         var partecipantId = CommonTestData.getParticipantId();
184         var definition = CommonTestData.getDefinition();
185
186         var stateChangeUnlock =
187                 commonTestData.getStateChange(partecipantId, uuid, DeployOrder.NONE, LockOrder.UNLOCK);
188
189         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
190         stateChangeUnlock
191                 .setAutomationCompositionId(ach.getAutomationCompositionMap().entrySet().iterator().next().getKey());
192         ach.handleAutomationCompositionStateChange(stateChangeUnlock, List.of());
193         stateChangeUnlock.setAutomationCompositionId(UUID.randomUUID());
194         stateChangeUnlock.setParticipantId(CommonTestData.getRndParticipantId());
195         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChangeUnlock, List.of()));
196     }
197 }