dc04440caf1e7b6f0e0e44dbd3c28f9fa4ec4f17
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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.participant.intermediary.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31
32 import java.time.Instant;
33 import java.util.List;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
37 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
44 import org.onap.policy.common.utils.coder.CoderException;
45
46 class ParticipantHandlerTest {
47
48     private final CommonTestData commonTestData = new CommonTestData();
49
50     @Test
51     void handleUpdateTest() {
52         var parameters = CommonTestData.getParticipantParameters();
53         var automationCompositionHander = commonTestData.getMockAutomationCompositionHandler();
54         var publisher = new ParticipantMessagePublisher();
55         var emptyParticipantHandler =
56                 new ParticipantHandler(parameters, publisher, automationCompositionHander);
57         var participantUpdateMsg = new ParticipantUpdate();
58
59         assertThatThrownBy(() ->
60                 emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg))
61                 .isInstanceOf(RuntimeException.class);
62
63         var participantHandler = commonTestData.getMockParticipantHandler();
64
65         var participantId = CommonTestData.getParticipantId();
66         participantUpdateMsg.setAutomationCompositionId(CommonTestData.AC_ID_1);
67         participantUpdateMsg.setCompositionId(CommonTestData.AC_ID_1);
68         participantUpdateMsg.setParticipantId(participantId);
69         participantUpdateMsg.setMessageId(UUID.randomUUID());
70         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
71
72         var heartbeatF = participantHandler.makeHeartbeat(false);
73         assertEquals(participantId, heartbeatF.getParticipantId());
74         assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
75
76         participantHandler.handleParticipantUpdate(participantUpdateMsg);
77
78         var heartbeatT = participantHandler.makeHeartbeat(true);
79         assertEquals(participantId, heartbeatT.getParticipantId());
80         assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
81         assertEquals(participantId, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
82
83         var pum = setListParticipantDefinition(participantUpdateMsg);
84         participantHandler.handleParticipantUpdate(pum);
85         var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
86         assertEquals(participantId, heartbeatTAfterUpdate.getParticipantId());
87     }
88
89     private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) {
90         var def = new ParticipantDefinition();
91         def.setParticipantId(CommonTestData.getParticipantId());
92         participantUpdateMsg.setParticipantDefinitionUpdates(List.of(def));
93         return participantUpdateMsg;
94     }
95
96     @Test
97     void checkAppliesTo() {
98         var participantHandler = commonTestData.getMockParticipantHandler();
99         var participantAckMsg =
100                 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
101         assertTrue(participantHandler.appliesTo(participantAckMsg));
102
103         var participantMsg =
104                 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
105         assertTrue(participantHandler.appliesTo(participantMsg));
106
107         var randomId = UUID.randomUUID();
108         participantMsg.setParticipantId(randomId);
109         assertFalse(participantHandler.appliesTo(participantMsg));
110
111     }
112
113     @Test
114     void getAutomationCompositionInfoListTest() throws CoderException {
115         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
116         participantHandler.sendHeartbeat();
117         assertEquals(CommonTestData.AC_ID_1, participantHandler.makeHeartbeat(false)
118                 .getAutomationCompositionInfoList()
119                 .get(0)
120                 .getAutomationCompositionId());
121
122     }
123
124     @Test
125     void testHandleParticipantRegisterAck() {
126         var parameters = CommonTestData.getParticipantParameters();
127         var automationCompositionHandler = commonTestData.getMockAutomationCompositionHandler();
128         var publisher = mock(ParticipantMessagePublisher.class);
129         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
130
131         participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
132         verify(publisher).sendParticipantStatus(any());
133     }
134 }