1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.clamp.controlloop.runtime.supervision;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils;
import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
class SupervisionHandlerTest {
private static final String TOSCA_TEMPLATE_YAML =
"src/test/resources/rest/servicetemplates/tosca-for-smoke-testing.yaml";
private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json";
private static final ToscaConceptIdentifier identifier = new ToscaConceptIdentifier("PMSHInstance0Crud", "1.0.1");
private static final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("ParticipantId", "1.0.0");
private static final ToscaConceptIdentifier participantType =
new ToscaConceptIdentifier("ParticipantType", "1.0.0");
@Test
void testTriggerControlLoopSupervisionEmpty() throws ControlLoopException, PfModelException, CoderException {
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
assertThatThrownBy(() -> handler.triggerControlLoopSupervision(List.of()))
.hasMessageMatching("The list of control loops for supervision is empty");
}
@Test
void testTriggerControlLoopSupervision() throws ControlLoopException, PfModelException, CoderException {
var controlLoopProvider = mock(ControlLoopProvider.class);
var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class);
var handler = createSupervisionHandler(controlLoopProvider, mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), controlLoopUpdatePublisher,
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
handler.triggerControlLoopSupervision(List.of(identifier));
verify(controlLoopUpdatePublisher).send(any(ControlLoop.class));
verify(controlLoopProvider).updateControlLoop(any(ControlLoop.class));
}
@Test
void testTriggerControlLoopUninitialised() throws ControlLoopException, PfModelException, CoderException {
var controlLoopProvider = mock(ControlLoopProvider.class);
var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class);
var handler = createSupervisionHandler(controlLoopProvider, mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), controlLoopUpdatePublisher,
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.UNINITIALISED);
assertThatThrownBy(() -> handler.triggerControlLoopSupervision(List.of(identifier)))
.hasMessageMatching("Control loop is already in state UNINITIALISED");
}
@Test
void testTriggerControlLoopRunning() throws ControlLoopException, PfModelException, CoderException {
var controlLoopProvider = mock(ControlLoopProvider.class);
var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class);
var handler = createSupervisionHandler(controlLoopProvider, mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), controlLoopUpdatePublisher,
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.RUNNING);
assertThatThrownBy(() -> handler.triggerControlLoopSupervision(List.of(identifier)))
.hasMessageMatching("Control loop can't transition from state UNINITIALISED to state RUNNING");
}
@Test
void testHandleControlLoopStateChangeAckMessage() throws PfModelException, CoderException {
var controlLoopProvider = mock(ControlLoopProvider.class);
var handler = createSupervisionHandler(controlLoopProvider, mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
var controlLoopAckMessage = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
controlLoopAckMessage.setControlLoopResultMap(Map.of());
controlLoopAckMessage.setControlLoopId(identifier);
handler.handleControlLoopStateChangeAckMessage(controlLoopAckMessage);
verify(controlLoopProvider).updateControlLoop(any(ControlLoop.class));
}
@Test
void testHandleControlLoopUpdateAckMessage() throws PfModelException, CoderException {
var controlLoopAckMessage = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_UPDATE_ACK);
controlLoopAckMessage.setParticipantId(participantId);
controlLoopAckMessage.setParticipantType(participantType);
controlLoopAckMessage.setControlLoopResultMap(Map.of());
controlLoopAckMessage.setControlLoopId(identifier);
var controlLoopProvider = mock(ControlLoopProvider.class);
var handler = createSupervisionHandler(controlLoopProvider, mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
handler.handleControlLoopUpdateAckMessage(controlLoopAckMessage);
verify(controlLoopProvider).updateControlLoop(any(ControlLoop.class));
}
@Test
void testHandleParticipantDeregister() throws PfModelException, CoderException {
var participant = new Participant();
participant.setName(participantId.getName());
participant.setVersion(participantId.getVersion());
participant.setParticipantType(participantType);
var participantProvider = mock(ParticipantProvider.class);
when(participantProvider.findParticipant(participantId.getName(), participantId.getVersion()))
.thenReturn(Optional.of(participant));
var participantDeregisterMessage = new ParticipantDeregister();
participantDeregisterMessage.setMessageId(UUID.randomUUID());
participantDeregisterMessage.setParticipantId(participantId);
participantDeregisterMessage.setParticipantType(participantType);
var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), participantProvider,
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
participantDeregisterAckPublisher, mock(ControlLoopUpdatePublisher.class),
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
handler.handleParticipantMessage(participantDeregisterMessage);
verify(participantProvider).saveParticipant(any());
verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
}
@Test
void testHandleParticipantRegister() throws PfModelException, CoderException {
var participant = new Participant();
participant.setName(participantId.getName());
participant.setVersion(participantId.getVersion());
participant.setParticipantType(participantType);
var participantRegisterMessage = new ParticipantRegister();
participantRegisterMessage.setMessageId(UUID.randomUUID());
participantRegisterMessage.setParticipantId(participantId);
participantRegisterMessage.setParticipantType(participantType);
var participantProvider = mock(ParticipantProvider.class);
var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), participantProvider,
mock(MonitoringProvider.class), participantRegisterAckPublisher,
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
handler.handleParticipantMessage(participantRegisterMessage);
verify(participantProvider).saveParticipant(any());
verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId,
participantType);
}
@Test
void testParticipantUpdateAck() throws PfModelException, CoderException {
var participant = new Participant();
participant.setName(participantId.getName());
participant.setVersion(participantId.getVersion());
participant.setParticipantType(participantType);
var participantProvider = mock(ParticipantProvider.class);
when(participantProvider.findParticipant(participantId.getName(), participantId.getVersion()))
.thenReturn(Optional.of(participant));
var participantUpdateAckMessage = new ParticipantUpdateAck();
participantUpdateAckMessage.setParticipantId(participantId);
participantUpdateAckMessage.setParticipantType(participantType);
participantUpdateAckMessage.setState(ParticipantState.PASSIVE);
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), participantProvider,
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
mock(ParticipantUpdatePublisher.class), ControlLoopOrderedState.PASSIVE);
handler.handleParticipantMessage(participantUpdateAckMessage);
verify(participantProvider).saveParticipant(any());
}
@Test
void testHandleParticipantStatus() throws PfModelException, CoderException {
var participantStatusMessage = new ParticipantStatus();
participantStatusMessage.setParticipantId(participantId);
participantStatusMessage.setParticipantType(participantType);
participantStatusMessage.setState(ParticipantState.PASSIVE);
participantStatusMessage.setHealthStatus(ParticipantHealthStatus.HEALTHY);
participantStatusMessage.setParticipantStatistics(new ParticipantStatistics());
var participantProvider = mock(ParticipantProvider.class);
var monitoringProvider = mock(MonitoringProvider.class);
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), participantProvider, monitoringProvider,
mock(ParticipantRegisterAckPublisher.class), mock(ParticipantDeregisterAckPublisher.class),
mock(ControlLoopUpdatePublisher.class), mock(ParticipantUpdatePublisher.class),
ControlLoopOrderedState.PASSIVE);
handler.handleParticipantMessage(participantStatusMessage);
verify(participantProvider).saveParticipant(any());
verify(monitoringProvider).createParticipantStatistics(anyList());
}
@Test
void testHandleSendCommissionMessage() throws PfModelException, CoderException {
var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class);
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
participantUpdatePublisher, ControlLoopOrderedState.PASSIVE);
handler.handleSendCommissionMessage(participantId.getName(), participantId.getVersion());
verify(participantUpdatePublisher).sendComissioningBroadcast(participantId.getName(),
participantId.getVersion());
}
@Test
void testHandleSendDeCommissionMessage() throws PfModelException, CoderException {
var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class);
var handler = createSupervisionHandler(mock(ControlLoopProvider.class), mock(ParticipantProvider.class),
mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
mock(ParticipantDeregisterAckPublisher.class), mock(ControlLoopUpdatePublisher.class),
participantUpdatePublisher, ControlLoopOrderedState.PASSIVE);
handler.handleSendDeCommissionMessage();
verify(participantUpdatePublisher).sendDecomisioning();
}
private SupervisionHandler createSupervisionHandler(ControlLoopProvider controlLoopProvider,
ParticipantProvider participantProvider, MonitoringProvider monitoringProvider,
ParticipantRegisterAckPublisher participantRegisterAckPublisher,
ParticipantDeregisterAckPublisher participantDeregisterAckPublisher,
ControlLoopUpdatePublisher controlLoopUpdatePublisher,
ParticipantUpdatePublisher participantUpdatePublisher,
ControlLoopOrderedState orderedState) throws PfModelException, CoderException {
var controlLoopsCreate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud");
var controlLoop = controlLoopsCreate.getControlLoopList().get(0);
controlLoop.setOrderedState(orderedState);
var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class);
when(controlLoopProvider.getControlLoop(identifier)).thenReturn(controlLoop);
var serviceTemplateProvider = Mockito.mock(ServiceTemplateProvider.class);
when(serviceTemplateProvider.getServiceTemplateList(any(), any()))
.thenReturn(List.of(InstantiationUtils.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML)));
return new SupervisionHandler(controlLoopProvider, participantProvider, monitoringProvider,
serviceTemplateProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher,
participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher);
}
}
|