cfef1a5ee24be844f8f1fa8a6ffd1cf29a2877dd
[policy/clamp.git] /
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.onap.policy.clamp.acm.runtime.supervision;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
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.main.parameters.AcRuntimeParameterGroup;
37 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
38 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
39 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRestartPublisher;
40 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
45 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
46 import org.onap.policy.clamp.models.acm.concepts.Participant;
47 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
48 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
50 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
51 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
52 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
53 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
54 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56
57 class SupervisionParticipantHandlerTest {
58
59     private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
60
61     @Test
62     void testHandleParticipantDeregister() {
63         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
64
65         var participantProvider = mock(ParticipantProvider.class);
66         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
67                 .thenReturn(Optional.of(participant));
68
69         var participantDeregisterMessage = new ParticipantDeregister();
70         participantDeregisterMessage.setMessageId(UUID.randomUUID());
71         participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
72         var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
73         var handler =
74                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
75                         participantDeregisterAckPublisher, mock(AutomationCompositionProvider.class),
76                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
77                         mock(AcRuntimeParameterGroup.class));
78
79         handler.handleParticipantMessage(participantDeregisterMessage);
80
81         verify(participantProvider).updateParticipant(any());
82         verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
83     }
84
85     @Test
86     void testHandleParticipantRegister() {
87         var participantRegisterMessage = new ParticipantRegister();
88         participantRegisterMessage.setMessageId(UUID.randomUUID());
89         participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
90         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
91         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
92
93         var participantProvider = mock(ParticipantProvider.class);
94         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
95         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
96                 mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
97                 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
98                 mock(AcRuntimeParameterGroup.class));
99         handler.handleParticipantMessage(participantRegisterMessage);
100
101         verify(participantProvider).saveParticipant(any());
102         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
103                 CommonTestData.getParticipantId());
104     }
105
106     @Test
107     void testHandleParticipantRestart() {
108         var participantRegisterMessage = new ParticipantRegister();
109         participantRegisterMessage.setMessageId(UUID.randomUUID());
110         var participantId = CommonTestData.getParticipantId();
111         participantRegisterMessage.setParticipantId(participantId);
112         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
113         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
114
115         var participant = new Participant();
116         participant.setParticipantId(participantId);
117         var participantProvider = mock(ParticipantProvider.class);
118         when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
119         var compositionId = UUID.randomUUID();
120         var composition2Id = UUID.randomUUID();
121         when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));
122
123         var acDefinitionProvider = mock(AcDefinitionProvider.class);
124         var acDefinition = new AutomationCompositionDefinition();
125         acDefinition.setState(AcTypeState.COMMISSIONED);
126         acDefinition.setCompositionId(composition2Id);
127         when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);
128
129         acDefinition = new AutomationCompositionDefinition();
130         acDefinition.setCompositionId(compositionId);
131         acDefinition.setState(AcTypeState.PRIMED);
132         var nodeTemplateState = new NodeTemplateState();
133         nodeTemplateState.setParticipantId(participantId);
134         acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
135         when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);
136
137         var automationComposition =
138                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
139         automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
140         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
141         when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
142                 .thenReturn(List.of(automationComposition));
143
144         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
145         var participantRestartPublisher = mock(ParticipantRestartPublisher.class);
146         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
147                 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
148                 participantRestartPublisher, CommonTestData.getTestParamaterGroup());
149         handler.handleParticipantMessage(participantRegisterMessage);
150
151         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId);
152         verify(acDefinitionProvider).updateAcDefinition(any(AutomationCompositionDefinition.class),
153                 eq(CommonTestData.TOSCA_COMP_NAME));
154         verify(participantRestartPublisher).send(any(), any(AutomationCompositionDefinition.class), any());
155     }
156
157     @Test
158     void testHandleParticipantStatus() {
159         var participantStatusMessage = createParticipantStatus();
160         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
161
162         var participantProvider = mock(ParticipantProvider.class);
163         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
164         var handler =
165                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
166                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
167                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
168                         mock(AcRuntimeParameterGroup.class));
169         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
170         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
171                 .thenReturn(Optional.of(participant));
172         handler.handleParticipantMessage(participantStatusMessage);
173
174         verify(automationCompositionProvider).upgradeStates(any());
175     }
176
177     @Test
178     void testAcDefinitionOutProperties() {
179         var participantStatusMessage = createParticipantStatus();
180         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
181         var participantDefinition = new ParticipantDefinition();
182         participantStatusMessage.setParticipantDefinitionUpdates(List.of(participantDefinition));
183         participantDefinition.setParticipantId(participantStatusMessage.getParticipantId());
184         var acElementDefinition = new AutomationCompositionElementDefinition();
185         acElementDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier("code", "1.0.0"));
186         participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
187
188         var compositionId = UUID.randomUUID();
189         participantStatusMessage.setCompositionId(compositionId);
190         var acDefinition = new AutomationCompositionDefinition();
191         acDefinition.setState(AcTypeState.COMMISSIONED);
192         acDefinition.setCompositionId(compositionId);
193         var nodeTemplateState = new NodeTemplateState();
194         acDefinition.setElementStateMap(
195                 Map.of(acElementDefinition.getAcElementDefinitionId().getName(), nodeTemplateState));
196         var acDefinitionProvider = mock(AcDefinitionProvider.class);
197         when(acDefinitionProvider.findAcDefinition(compositionId)).thenReturn(Optional.of(acDefinition));
198
199         var participantProvider = mock(ParticipantProvider.class);
200         var handler =
201                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
202                         mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
203                         acDefinitionProvider, mock(ParticipantRestartPublisher.class),
204                        CommonTestData.getTestParamaterGroup());
205         handler.handleParticipantMessage(participantStatusMessage);
206
207         verify(acDefinitionProvider).updateAcDefinition(acDefinition, CommonTestData.TOSCA_COMP_NAME);
208     }
209
210     @Test
211     void testHandleParticipantStatusNotRegisterd() {
212         var participantStatusMessage = createParticipantStatus();
213         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
214
215         var participantProvider = mock(ParticipantProvider.class);
216         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
217         var handler =
218                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
219                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
220                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
221                         mock(AcRuntimeParameterGroup.class));
222         handler.handleParticipantMessage(participantStatusMessage);
223
224         verify(participantProvider).saveParticipant(any());
225         verify(automationCompositionProvider).upgradeStates(any());
226     }
227
228     @Test
229     void testHandleParticipantStatusCheckOnline() {
230         var participantStatusMessage = createParticipantStatus();
231         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
232
233         var participantProvider = mock(ParticipantProvider.class);
234         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
235         var handler =
236                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
237                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
238                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class),
239                         mock(AcRuntimeParameterGroup.class));
240         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
241         participant.setParticipantState(ParticipantState.OFF_LINE);
242         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
243                 .thenReturn(Optional.of(participant));
244         handler.handleParticipantMessage(participantStatusMessage);
245
246         verify(participantProvider).saveParticipant(any());
247         verify(automationCompositionProvider).upgradeStates(any());
248     }
249
250     private ParticipantStatus createParticipantStatus() {
251         var statusMessage = new ParticipantStatus();
252         statusMessage.setParticipantId(CommonTestData.getParticipantId());
253         statusMessage.setState(ParticipantState.ON_LINE);
254         var supportedElementType = CommonTestData.createParticipantSupportedElementType();
255         statusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
256         return statusMessage;
257     }
258 }