246ba7dc784bfdfcaa01490fa5fbac8af7f03e95
[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.ArrayList;
29 import java.util.Optional;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
33 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantRegisterAckPublisher;
34 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
36 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
39 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
40
41 class SupervisionParticipantHandlerTest {
42     @Test
43     void testHandleParticipantDeregister() {
44         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
45
46         var participantProvider = mock(ParticipantProvider.class);
47         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
48                 .thenReturn(Optional.of(participant));
49
50         var participantDeregisterMessage = new ParticipantDeregister();
51         participantDeregisterMessage.setMessageId(UUID.randomUUID());
52         participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
53         var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
54         var handler = new SupervisionParticipantHandler(participantProvider,
55             mock(ParticipantRegisterAckPublisher.class), participantDeregisterAckPublisher);
56
57         handler.handleParticipantMessage(participantDeregisterMessage);
58
59         verify(participantProvider).updateParticipant(any());
60         verify(participantProvider).resetParticipantAcElementState(any());
61         verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
62     }
63
64     @Test
65     void testHandleParticipantRegister() {
66         var participantRegisterMessage = new ParticipantRegister();
67         participantRegisterMessage.setMessageId(UUID.randomUUID());
68         participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
69         var participantProvider = mock(ParticipantProvider.class);
70         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
71         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
72             mock(ParticipantDeregisterAckPublisher.class));
73         participantRegisterMessage.setParticipantSupportedElementType(new ArrayList<>());
74
75         handler.handleParticipantMessage(participantRegisterMessage);
76
77         verify(participantProvider).saveParticipant(any());
78         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
79                 CommonTestData.getParticipantId());
80     }
81
82     @Test
83     void testHandleParticipantStatus() {
84         var participantStatusMessage = new ParticipantStatus();
85         participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
86         participantStatusMessage.setState(ParticipantState.ON_LINE);
87         participantStatusMessage.setParticipantSupportedElementType(new ArrayList<>());
88
89         var participantProvider = mock(ParticipantProvider.class);
90         var handler = new SupervisionParticipantHandler(participantProvider,
91             mock(ParticipantRegisterAckPublisher.class), mock(ParticipantDeregisterAckPublisher.class));
92         var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
93         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
94                 .thenReturn(Optional.of(participant));
95         handler.handleParticipantMessage(participantStatusMessage);
96
97         verify(participantProvider).updateParticipant(any());
98     }
99 }