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