30860308ec8d0e60a846d3565e321376333524ba
[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.clearInvocations;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.time.Instant;
35 import java.util.List;
36 import java.util.UUID;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
39 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.models.base.PfModelException;
49
50 class ParticipantHandlerTest {
51
52     private final CommonTestData commonTestData = new CommonTestData();
53
54     @Test
55     void handleUpdateTest() {
56         var parameters = CommonTestData.getParticipantParameters();
57         var automationCompositionHander = commonTestData.getMockAutomationCompositionHandler();
58         var publisher = new ParticipantMessagePublisher();
59         var emptyParticipantHandler =
60                 new ParticipantHandler(parameters, publisher, automationCompositionHander);
61         var participantPrimeMsg = new ParticipantPrime();
62
63         assertThatThrownBy(() ->
64                 emptyParticipantHandler.handleParticipantPrime(participantPrimeMsg))
65                 .isInstanceOf(RuntimeException.class);
66
67         var participantHandler = commonTestData.getMockParticipantHandler();
68
69         var participantId = CommonTestData.getParticipantId();
70         participantPrimeMsg.setCompositionId(CommonTestData.AC_ID_1);
71         participantPrimeMsg.setParticipantId(participantId);
72         participantPrimeMsg.setMessageId(UUID.randomUUID());
73         participantPrimeMsg.setTimestamp(Instant.ofEpochMilli(3000));
74
75         var heartbeatF = participantHandler.makeHeartbeat(false);
76         assertEquals(participantId, heartbeatF.getParticipantId());
77         assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
78
79         participantHandler.handleParticipantPrime(participantPrimeMsg);
80
81         var heartbeatT = participantHandler.makeHeartbeat(true);
82         assertEquals(participantId, heartbeatT.getParticipantId());
83         assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
84         assertEquals(participantId, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
85
86         var pum = setListParticipantDefinition(participantPrimeMsg);
87         participantHandler.handleParticipantPrime(pum);
88         var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
89         assertEquals(participantId, heartbeatTAfterUpdate.getParticipantId());
90     }
91
92     private ParticipantPrime setListParticipantDefinition(ParticipantPrime participantPrimeMsg) {
93         var def = new ParticipantDefinition();
94         def.setParticipantId(CommonTestData.getParticipantId());
95         participantPrimeMsg.setParticipantDefinitionUpdates(List.of(def));
96         return participantPrimeMsg;
97     }
98
99     @Test
100     void checkAppliesTo() {
101         var participantHandler = commonTestData.getMockParticipantHandler();
102         var participantAckMsg =
103                 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
104         assertTrue(participantHandler.appliesTo(participantAckMsg));
105
106         var participantMsg =
107                 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
108         assertTrue(participantHandler.appliesTo(participantMsg));
109
110         var randomId = UUID.randomUUID();
111         participantMsg.setParticipantId(randomId);
112         assertFalse(participantHandler.appliesTo(participantMsg));
113
114     }
115
116     @Test
117     void getAutomationCompositionInfoListTest() throws CoderException {
118         var automationCompositionHandler = mock(AutomationCompositionHandler.class);
119         var participantHandler =
120                 commonTestData.getParticipantHandlerAutomationCompositions(automationCompositionHandler);
121         clearInvocations(automationCompositionHandler);
122         participantHandler.sendHeartbeat();
123         verify(automationCompositionHandler).getAutomationCompositionInfoList();
124     }
125
126     @Test
127     void testHandleParticipantRegisterAck() {
128         var parameters = CommonTestData.getParticipantParameters();
129         var automationCompositionHandler = commonTestData.getMockAutomationCompositionHandler();
130         var publisher = mock(ParticipantMessagePublisher.class);
131         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
132
133         participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
134         verify(publisher).sendParticipantStatus(any());
135     }
136
137     @Test
138     void testSendParticipantDeregister() throws PfModelException {
139         var commonTestData = new CommonTestData();
140         var automationCompositionMap = commonTestData.getTestAutomationCompositionMap();
141         var automationCompositionHandler = mock(AutomationCompositionHandler.class);
142
143         automationCompositionMap.values().iterator().next().getElements().values().iterator().next()
144             .setParticipantId(CommonTestData.getParticipantId());
145         when(automationCompositionHandler.getAutomationCompositionMap()).thenReturn(automationCompositionMap);
146
147         var publisher = mock(ParticipantMessagePublisher.class);
148         var parameters = CommonTestData.getParticipantParameters();
149         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
150
151         participantHandler.sendParticipantDeregister();
152         verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
153         verify(automationCompositionHandler).undeployInstances();
154     }
155 }