ed8a856648769848c5e986a212ecadbd0d4248ab
[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.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.Set;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
35 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
36 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
37 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRestartPublisher;
38 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
39 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
42 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
43 import org.onap.policy.clamp.models.acm.concepts.Participant;
44 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
45 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
49 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
50 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
51 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
52
53 class SupervisionParticipantHandlerTest {
54
55     private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
56
57     @Test
58     void testHandleParticipantDeregister() {
59         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
60
61         var participantProvider = mock(ParticipantProvider.class);
62         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
63                 .thenReturn(Optional.of(participant));
64
65         var participantDeregisterMessage = new ParticipantDeregister();
66         participantDeregisterMessage.setMessageId(UUID.randomUUID());
67         participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
68         var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
69         var handler =
70                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
71                         participantDeregisterAckPublisher, mock(AutomationCompositionProvider.class),
72                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
73
74         handler.handleParticipantMessage(participantDeregisterMessage);
75
76         verify(participantProvider).updateParticipant(any());
77         verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
78     }
79
80     @Test
81     void testHandleParticipantRegister() {
82         var participantRegisterMessage = new ParticipantRegister();
83         participantRegisterMessage.setMessageId(UUID.randomUUID());
84         participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
85         var supportedElementType = new ParticipantSupportedElementType();
86         supportedElementType.setTypeName("Type");
87         supportedElementType.setTypeVersion("1.0.0");
88         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
89
90         var participantProvider = mock(ParticipantProvider.class);
91         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
92         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
93                 mock(ParticipantDeregisterAckPublisher.class), mock(AutomationCompositionProvider.class),
94                 mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
95         handler.handleParticipantMessage(participantRegisterMessage);
96
97         verify(participantProvider).saveParticipant(any());
98         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
99                 CommonTestData.getParticipantId());
100     }
101
102     @Test
103     void testHandleParticipantRestart() {
104         var participantRegisterMessage = new ParticipantRegister();
105         participantRegisterMessage.setMessageId(UUID.randomUUID());
106         var participantId = CommonTestData.getParticipantId();
107         participantRegisterMessage.setParticipantId(participantId);
108         var supportedElementType = new ParticipantSupportedElementType();
109         supportedElementType.setTypeName("Type");
110         supportedElementType.setTypeVersion("1.0.0");
111         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
112
113         var participant = new Participant();
114         participant.setParticipantId(participantId);
115         var participantProvider = mock(ParticipantProvider.class);
116         when(participantProvider.findParticipant(participantId)).thenReturn(Optional.of(participant));
117         var compositionId = UUID.randomUUID();
118         var composition2Id = UUID.randomUUID();
119         when(participantProvider.getCompositionIds(participantId)).thenReturn(Set.of(compositionId, composition2Id));
120
121         var acDefinitionProvider = mock(AcDefinitionProvider.class);
122         var acDefinition = new AutomationCompositionDefinition();
123         acDefinition.setState(AcTypeState.COMMISSIONED);
124         acDefinition.setCompositionId(composition2Id);
125         when(acDefinitionProvider.getAcDefinition(composition2Id)).thenReturn(acDefinition);
126
127         acDefinition = new AutomationCompositionDefinition();
128         acDefinition.setCompositionId(compositionId);
129         acDefinition.setState(AcTypeState.PRIMED);
130         var nodeTemplateState = new NodeTemplateState();
131         nodeTemplateState.setParticipantId(participantId);
132         acDefinition.setElementStateMap(Map.of("code", nodeTemplateState));
133         when(acDefinitionProvider.getAcDefinition(compositionId)).thenReturn(acDefinition);
134
135         var automationComposition =
136                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Crud");
137         automationComposition.getElements().values().iterator().next().setParticipantId(participantId);
138         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
139         when(automationCompositionProvider.getAcInstancesByCompositionId(compositionId))
140                 .thenReturn(List.of(automationComposition));
141
142         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
143         var participantRestartPublisher = mock(ParticipantRestartPublisher.class);
144         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
145                 mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider, acDefinitionProvider,
146                 participantRestartPublisher);
147         handler.handleParticipantMessage(participantRegisterMessage);
148
149         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId);
150         verify(acDefinitionProvider).updateAcDefinition(any(AutomationCompositionDefinition.class));
151         verify(participantRestartPublisher).send(any(), any(AutomationCompositionDefinition.class), any());
152     }
153
154     @Test
155     void testHandleParticipantStatus() {
156         var participantStatusMessage = new ParticipantStatus();
157         participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
158         participantStatusMessage.setState(ParticipantState.ON_LINE);
159         var supportedElementType = new ParticipantSupportedElementType();
160         supportedElementType.setTypeName("Type");
161         supportedElementType.setTypeVersion("1.0.0");
162         participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
163         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
164
165         var participantProvider = mock(ParticipantProvider.class);
166         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
167         var handler =
168                 new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
169                         mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
170                         mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
171         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
172         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
173                 .thenReturn(Optional.of(participant));
174         handler.handleParticipantMessage(participantStatusMessage);
175
176         verify(automationCompositionProvider).upgradeStates(any());
177     }
178 }