aboutsummaryrefslogtreecommitdiffstats
path: root/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionHandlerTest.java
blob: 99e509c0eb4535ed7d86c1cd91c78b945fb2ae3a (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
/*-
 * ============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.acm.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.acm.runtime.instantiation.InstantiationUtils;
import org.onap.policy.clamp.acm.runtime.monitoring.MonitoringProvider;
import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionStateChangePublisher;
import org.onap.policy.clamp.acm.runtime.supervision.comm.AutomationCompositionUpdatePublisher;
import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantUpdatePublisher;
import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
import org.onap.policy.clamp.models.acm.concepts.Participant;
import org.onap.policy.clamp.models.acm.concepts.ParticipantHealthStatus;
import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
import org.onap.policy.clamp.models.acm.concepts.ParticipantStatistics;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionAck;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.ServiceTemplateProvider;
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 AC_INSTANTIATION_CREATE_JSON =
        "src/test/resources/rest/acm/AutomationCompositions.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 testTriggerAutomationCompositionSupervisionEmpty()
        throws AutomationCompositionException, PfModelException, CoderException {
        var handler =
            createSupervisionHandler(mock(AutomationCompositionProvider.class), mock(ParticipantProvider.class),
                mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
                mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
                mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.PASSIVE);

        assertThatThrownBy(() -> handler.triggerAutomationCompositionSupervision(List.of()))
            .hasMessageMatching("The list of automation compositions for supervision is empty");
    }

    @Test
    void testTriggerAutomationCompositionSupervision()
        throws AutomationCompositionException, PfModelException, CoderException {
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var automationCompositionUpdatePublisher = mock(AutomationCompositionUpdatePublisher.class);
        var handler = createSupervisionHandler(automationCompositionProvider, mock(ParticipantProvider.class),
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), automationCompositionUpdatePublisher,
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.PASSIVE);

        handler.triggerAutomationCompositionSupervision(List.of(identifier));

        verify(automationCompositionUpdatePublisher).send(any(AutomationComposition.class));
        verify(automationCompositionProvider).saveAutomationComposition(any(AutomationComposition.class));
    }

    @Test
    void testTriggerAutomationCompositionUninitialised()
        throws AutomationCompositionException, PfModelException, CoderException {
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var automationCompositionUpdatePublisher = mock(AutomationCompositionUpdatePublisher.class);
        var handler = createSupervisionHandler(automationCompositionProvider, mock(ParticipantProvider.class),
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), automationCompositionUpdatePublisher,
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.UNINITIALISED);

        assertThatThrownBy(() -> handler.triggerAutomationCompositionSupervision(List.of(identifier)))
            .hasMessageMatching("Automation composition is already in state UNINITIALISED");
    }

    @Test
    void testTriggerAutomationCompositionRunning()
        throws AutomationCompositionException, PfModelException, CoderException {
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var automationCompositionUpdatePublisher = mock(AutomationCompositionUpdatePublisher.class);
        var handler = createSupervisionHandler(automationCompositionProvider, mock(ParticipantProvider.class),
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), automationCompositionUpdatePublisher,
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.RUNNING);

        assertThatThrownBy(() -> handler.triggerAutomationCompositionSupervision(List.of(identifier)))
            .hasMessageMatching("Automation composition can't transition from state UNINITIALISED to state RUNNING");
    }

    @Test
    void testHandleAutomationCompositionStateChangeAckMessage() throws PfModelException, CoderException {
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var handler = createSupervisionHandler(automationCompositionProvider, mock(ParticipantProvider.class),
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.PASSIVE);
        var automationCompositionAckMessage =
            new AutomationCompositionAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
        automationCompositionAckMessage.setAutomationCompositionResultMap(Map.of());
        automationCompositionAckMessage.setAutomationCompositionId(identifier);

        handler.handleAutomationCompositionStateChangeAckMessage(automationCompositionAckMessage);

        verify(automationCompositionProvider).saveAutomationComposition(any(AutomationComposition.class));
    }

    @Test
    void testHandleAutomationCompositionUpdateAckMessage() throws PfModelException, CoderException {
        var automationCompositionAckMessage =
            new AutomationCompositionAck(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE_ACK);
        automationCompositionAckMessage.setParticipantId(participantId);
        automationCompositionAckMessage.setParticipantType(participantType);
        automationCompositionAckMessage.setAutomationCompositionResultMap(Map.of());
        automationCompositionAckMessage.setAutomationCompositionId(identifier);
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var handler = createSupervisionHandler(automationCompositionProvider, mock(ParticipantProvider.class),
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.PASSIVE);

        handler.handleAutomationCompositionUpdateAckMessage(automationCompositionAckMessage);

        verify(automationCompositionProvider).saveAutomationComposition(any(AutomationComposition.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(AutomationCompositionProvider.class), participantProvider,
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            participantDeregisterAckPublisher, mock(AutomationCompositionUpdatePublisher.class),
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.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(AutomationCompositionProvider.class), participantProvider,
            mock(MonitoringProvider.class), participantRegisterAckPublisher,
            mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.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(AutomationCompositionProvider.class), participantProvider,
            mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.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(AutomationCompositionProvider.class), participantProvider,
            monitoringProvider, mock(ParticipantRegisterAckPublisher.class),
            mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
            mock(ParticipantUpdatePublisher.class), AutomationCompositionOrderedState.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(AutomationCompositionProvider.class), mock(ParticipantProvider.class),
                mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
                mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
                participantUpdatePublisher, AutomationCompositionOrderedState.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(AutomationCompositionProvider.class), mock(ParticipantProvider.class),
                mock(MonitoringProvider.class), mock(ParticipantRegisterAckPublisher.class),
                mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionUpdatePublisher.class),
                participantUpdatePublisher, AutomationCompositionOrderedState.PASSIVE);
        handler.handleSendDeCommissionMessage();

        verify(participantUpdatePublisher).sendDecomisioning();
    }

    private SupervisionHandler createSupervisionHandler(AutomationCompositionProvider automationCompositionProvider,
        ParticipantProvider participantProvider, MonitoringProvider monitoringProvider,
        ParticipantRegisterAckPublisher participantRegisterAckPublisher,
        ParticipantDeregisterAckPublisher participantDeregisterAckPublisher,
        AutomationCompositionUpdatePublisher automationCompositionUpdatePublisher,
        ParticipantUpdatePublisher participantUpdatePublisher, AutomationCompositionOrderedState orderedState)
        throws PfModelException, CoderException {
        var automationCompositionsCreate =
            InstantiationUtils.getAutomationCompositionsFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");

        var automationComposition = automationCompositionsCreate.getAutomationCompositionList().get(0);
        automationComposition.setOrderedState(orderedState);

        when(automationCompositionProvider.findAutomationComposition(identifier))
            .thenReturn(Optional.of(automationComposition));
        when(automationCompositionProvider.getAutomationComposition(identifier)).thenReturn(automationComposition);

        var serviceTemplateProvider = Mockito.mock(ServiceTemplateProvider.class);
        when(serviceTemplateProvider.getServiceTemplateList(any(), any()))
            .thenReturn(List.of(InstantiationUtils.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML)));

        var automationCompositionStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);

        return new SupervisionHandler(automationCompositionProvider, participantProvider, monitoringProvider,
            serviceTemplateProvider, automationCompositionUpdatePublisher, automationCompositionStateChangePublisher,
            participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher);

    }
}