aboutsummaryrefslogtreecommitdiffstats
path: root/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
blob: 49aa0309619d5f41056b5025044863af1eb523eb (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 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.mockito.ArgumentMatchers.any;
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.Set;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
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.ParticipantRestartPublisher;
import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
import org.onap.policy.clamp.models.acm.concepts.Participant;
import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
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.persistence.provider.AcDefinitionProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;

class SupervisionParticipantHandlerTest {

    private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";

    @Test
    void testHandleParticipantDeregister() {
        var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());

        var participantProvider = mock(ParticipantProvider.class);
        when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
                .thenReturn(Optional.of(participant));

        var participantDeregisterMessage = new ParticipantDeregister();
        participantDeregisterMessage.setMessageId(UUID.randomUUID());
        participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
        var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
        var handler =
                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
                        participantDeregisterAckPublisher, mock(AutomationCompositionProvider.class),
                        mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));

        handler.handleParticipantMessage(participantDeregisterMessage);

        verify(participantProvider).updateParticipant(any());
        verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
    }

    @Test
    void testHandleParticipantRegister() {
        var participantRegisterMessage = new ParticipantRegister();
        participantRegisterMessage.setMessageId(UUID.randomUUID());
        participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
        participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));

        var participantProvider = mock(ParticipantProvider.class);
        var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
        var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
                mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
                mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
        handler.handleParticipantMessage(participantRegisterMessage);

        verify(participantProvider).saveParticipant(any());
        verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
                CommonTestData.getParticipantId());
    }

    @Test
    void testHandleParticipantRestart() {
        var participantRegisterMessage = new ParticipantRegister();
        participantRegisterMessage.setMessageId(UUID.randomUUID());
        var participantId = CommonTestData.getParticipantId();
        participantRegisterMessage.setParticipantId(participantId);
        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
        participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));

        var participant = new Participant();
        participant.setParticipantId(participantId);
        var participantProvider = mock(ParticipantProvider.class);
        when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
        var compositionId = UUID.randomUUID();
        var composition2Id = UUID.randomUUID();
        when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));

        var acDefinitionProvider = mock(AcDefinitionProvider.class);
        var acDefinition = new AutomationCompositionDefinition();
        acDefinition.setState(AcTypeState.COMMISSIONED);
        acDefinition.setCompositionId(composition2Id);
        when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);

        acDefinition = new AutomationCompositionDefinition();
        acDefinition.setCompositionId(compositionId);
        acDefinition.setState(AcTypeState.PRIMED);
        var nodeTemplateState = new NodeTemplateState();
        nodeTemplateState.setParticipantId(participantId);
        acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
        when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);

        var automationComposition =
                InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
        automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
                .thenReturn(List.of(automationComposition));

        var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
        var participantRestartPublisher = mock(ParticipantRestartPublisher.class);
        var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
                mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
                participantRestartPublisher);
        handler.handleParticipantMessage(participantRegisterMessage);

        verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId);
        verify(acDefinitionProvider).updateAcDefinition(any(AutomationCompositionDefinition.class));
        verify(participantRestartPublisher).send(any(), any(AutomationCompositionDefinition.class), any());
    }

    @Test
    void testHandleParticipantStatus() {
        var participantStatusMessage = new ParticipantStatus();
        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
        participantStatusMessage.setState(ParticipantState.ON_LINE);
        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
        participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));

        var participantProvider = mock(ParticipantProvider.class);
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var handler =
                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
                        mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
                        mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
        var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
        when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
                .thenReturn(Optional.of(participant));
        handler.handleParticipantMessage(participantStatusMessage);

        verify(automationCompositionProvider).upgradeStates(any());
    }

    @Test
    void testHandleParticipantStatusNotRegisterd() {
        var participantStatusMessage = new ParticipantStatus();
        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
        participantStatusMessage.setState(ParticipantState.ON_LINE);
        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
        participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));

        var participantProvider = mock(ParticipantProvider.class);
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var handler =
                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
                        mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
                        mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
        handler.handleParticipantMessage(participantStatusMessage);

        verify(participantProvider).saveParticipant(any());
        verify(automationCompositionProvider).upgradeStates(any());
    }

    @Test
    void testHandleParticipantStatusCheckOnline() {
        var participantStatusMessage = new ParticipantStatus();
        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
        participantStatusMessage.setState(ParticipantState.ON_LINE);
        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
        participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));

        var participantProvider = mock(ParticipantProvider.class);
        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
        var handler =
                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
                        mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
                        mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
        var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
        participant.setParticipantState(ParticipantState.OFF_LINE);
        when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
                .thenReturn(Optional.of(participant));
        handler.handleParticipantMessage(participantStatusMessage);

        verify(participantProvider).saveParticipant(any());
        verify(automationCompositionProvider).upgradeStates(any());
    }
}