9463d797793d6a084f4b1886e78ac9742c7c7fe7
[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 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41
42 class SupervisionParticipantHandlerTest {
43     private static final ToscaConceptIdentifier PARTICIPANT_TYPE =
44         new ToscaConceptIdentifier("ParticipantType", "1.0.0");
45
46     @Test
47     void testHandleParticipantDeregister() {
48         var participant = CommonTestData.createParticipant(PARTICIPANT_TYPE, CommonTestData.getParticipantId());
49
50         var participantProvider = mock(ParticipantProvider.class);
51         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
52                 .thenReturn(Optional.of(participant));
53
54         var participantDeregisterMessage = new ParticipantDeregister();
55         participantDeregisterMessage.setMessageId(UUID.randomUUID());
56         participantDeregisterMessage.setParticipantId(CommonTestData.getParticipantId());
57         participantDeregisterMessage.setParticipantType(PARTICIPANT_TYPE);
58         var participantDeregisterAckPublisher = mock(ParticipantDeregisterAckPublisher.class);
59         var handler = new SupervisionParticipantHandler(participantProvider,
60             mock(ParticipantRegisterAckPublisher.class), participantDeregisterAckPublisher);
61
62         handler.handleParticipantMessage(participantDeregisterMessage);
63
64         verify(participantProvider).updateParticipant(any());
65         verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId());
66     }
67
68     @Test
69     void testHandleParticipantRegister() {
70         var participantRegisterMessage = new ParticipantRegister();
71         participantRegisterMessage.setMessageId(UUID.randomUUID());
72         participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
73         participantRegisterMessage.setParticipantType(PARTICIPANT_TYPE);
74         var participantProvider = mock(ParticipantProvider.class);
75         var participantRegisterAckPublisher = mock(ParticipantRegisterAckPublisher.class);
76         var handler = new SupervisionParticipantHandler(participantProvider, participantRegisterAckPublisher,
77             mock(ParticipantDeregisterAckPublisher.class));
78         participantRegisterMessage.setParticipantSupportedElementType(new ArrayList<>());
79
80         handler.handleParticipantMessage(participantRegisterMessage);
81
82         verify(participantProvider).saveParticipant(any());
83         verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(),
84                 CommonTestData.getParticipantId(), PARTICIPANT_TYPE);
85     }
86
87     @Test
88     void testHandleParticipantStatus() {
89         var participantStatusMessage = new ParticipantStatus();
90         participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
91         participantStatusMessage.setParticipantType(PARTICIPANT_TYPE);
92         participantStatusMessage.setState(ParticipantState.ON_LINE);
93         participantStatusMessage.setParticipantSupportedElementType(new ArrayList<>());
94
95         var participantProvider = mock(ParticipantProvider.class);
96         var handler = new SupervisionParticipantHandler(participantProvider,
97             mock(ParticipantRegisterAckPublisher.class), mock(ParticipantDeregisterAckPublisher.class));
98         var participant = CommonTestData.createParticipant(PARTICIPANT_TYPE, CommonTestData.getParticipantId());
99         when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
100                 .thenReturn(Optional.of(participant));
101         handler.handleParticipantMessage(participantStatusMessage);
102
103         verify(participantProvider).updateParticipant(any());
104     }
105 }