7e0c3da7ff6d42615db94a161fc57dd7cbce622f
[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.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
35 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
36 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
43 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
44 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
45 import org.onap.policy.models.base.PfModelException;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 class AutomationCompositionHandlerTest {
50
51     @Test
52     void handleAutomationCompositionStateChangeNullTest() {
53         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
54         var cacheProvider = mock(CacheProvider.class);
55         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher,
56                 mock(AutomationCompositionElementListener.class));
57
58         var automationCompositionStateChange = new AutomationCompositionStateChange();
59         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(automationCompositionStateChange));
60
61         automationCompositionStateChange.setAutomationCompositionId(UUID.randomUUID());
62         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(automationCompositionStateChange));
63         verify(participantMessagePublisher).sendAutomationCompositionAck(any(AutomationCompositionDeployAck.class));
64
65         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
66         automationCompositionStateChange.setAutomationCompositionId(automationComposition.getInstanceId());
67         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
68                 .thenReturn(automationComposition);
69         automationCompositionStateChange.setDeployOrderedState(DeployOrder.UPDATE);
70         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(automationCompositionStateChange));
71     }
72
73     @Test
74     void handleAutomationCompositionStateChangeUndeployTest() throws PfModelException {
75         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
76         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
77                 automationComposition.getInstanceId(), DeployOrder.UNDEPLOY, LockOrder.NONE);
78         var cacheProvider = mock(CacheProvider.class);
79         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
80                 .thenReturn(automationComposition);
81         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
82
83         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
84         var listener = mock(AutomationCompositionElementListener.class);
85         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
86         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
87         verify(listener, times(automationComposition.getElements().size())).undeploy(any(), any());
88     }
89
90     @Test
91     void handleAutomationCompositionStateChangeLockTest() throws PfModelException {
92         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
93         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
94                 automationComposition.getInstanceId(), DeployOrder.NONE, LockOrder.LOCK);
95         var cacheProvider = mock(CacheProvider.class);
96         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
97                 .thenReturn(automationComposition);
98         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
99
100         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
101         var listener = mock(AutomationCompositionElementListener.class);
102         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
103         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
104         verify(listener, times(automationComposition.getElements().size())).lock(any(), any());
105     }
106
107     @Test
108     void handleAutomationCompositionStateChangeUnlockTest() throws PfModelException {
109         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
110         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
111                 automationComposition.getInstanceId(), DeployOrder.NONE, LockOrder.UNLOCK);
112         var cacheProvider = mock(CacheProvider.class);
113         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
114                 .thenReturn(automationComposition);
115         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
116
117         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
118         var listener = mock(AutomationCompositionElementListener.class);
119         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
120         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
121         verify(listener, times(automationComposition.getElements().size())).unlock(any(), any());
122     }
123
124     @Test
125     void handleAutomationCompositionStateChangeDeleteTest() throws PfModelException {
126         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
127         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
128                 automationComposition.getInstanceId(), DeployOrder.DELETE, LockOrder.NONE);
129         var cacheProvider = mock(CacheProvider.class);
130         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
131                 .thenReturn(automationComposition);
132         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
133
134         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
135         var listener = mock(AutomationCompositionElementListener.class);
136         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
137         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
138         verify(listener, times(automationComposition.getElements().size())).delete(any(), any());
139     }
140
141     @Test
142     void handleAcPropertyUpdateTest() throws PfModelException {
143         var cacheProvider = mock(CacheProvider.class);
144         var listener = mock(AutomationCompositionElementListener.class);
145         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
146         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
147
148         var updateMsg = new PropertiesUpdate();
149         assertDoesNotThrow(() -> ach.handleAcPropertyUpdate(updateMsg));
150
151         updateMsg.setParticipantId(CommonTestData.getParticipantId());
152         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
153         var participantDeploy = new ParticipantDeploy();
154         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
155         updateMsg.getParticipantUpdatesList().add(participantDeploy);
156
157         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
158         updateMsg.setAutomationCompositionId(automationComposition.getInstanceId());
159         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
160                 .thenReturn(automationComposition);
161         var acElementDeploy = new AcElementDeploy();
162         acElementDeploy.setProperties(Map.of());
163         acElementDeploy.setId(automationComposition.getElements().values().iterator().next().getId());
164         participantDeploy.getAcElementList().add(acElementDeploy);
165
166         ach.handleAcPropertyUpdate(updateMsg);
167         verify(listener).update(any(), any(), any());
168     }
169
170     @Test
171     void handleAutomationCompositionDeployTest() throws PfModelException {
172         var cacheProvider = mock(CacheProvider.class);
173         var listener = mock(AutomationCompositionElementListener.class);
174         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
175         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
176
177         var deployMsg = new AutomationCompositionDeploy();
178         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(deployMsg));
179
180         deployMsg.setParticipantId(CommonTestData.getParticipantId());
181         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
182         var participantDeploy = new ParticipantDeploy();
183         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
184         deployMsg.getParticipantUpdatesList().add(participantDeploy);
185
186         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
187         deployMsg.setAutomationCompositionId(automationComposition.getInstanceId());
188         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
189                 .thenReturn(automationComposition);
190         for (var element : automationComposition.getElements().values()) {
191             var acElementDeploy = new AcElementDeploy();
192             acElementDeploy.setProperties(Map.of());
193             acElementDeploy.setId(element.getId());
194             participantDeploy.getAcElementList().add(acElementDeploy);
195         }
196         ach.handleAutomationCompositionDeploy(deployMsg);
197         verify(listener, times(automationComposition.getElements().size())).deploy(any(), any(), any());
198     }
199 }