f6988b0e8525d1a8af88c05871d09c3c329fb57c
[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.ArrayList;
31 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
35 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
37 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43
44 class ParticipantHandlerTest {
45
46     private final CommonTestData commonTestData = new CommonTestData();
47     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
48     private static final String ID_VERSION = "1.0.1";
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 id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
66         participantUpdateMsg.setAutomationCompositionId(CommonTestData.AC_ID_1);
67         participantUpdateMsg.setCompositionId(CommonTestData.AC_ID_1);
68         participantUpdateMsg.setParticipantId(id);
69         participantUpdateMsg.setParticipantType(id);
70         participantUpdateMsg.setMessageId(UUID.randomUUID());
71         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
72
73         var heartbeatF = participantHandler.makeHeartbeat(false);
74         assertEquals(id, heartbeatF.getParticipantId());
75         assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
76
77         participantHandler.handleParticipantUpdate(participantUpdateMsg);
78
79         var heartbeatT = participantHandler.makeHeartbeat(true);
80         assertEquals(id, heartbeatT.getParticipantId());
81         assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
82         assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
83
84         var pum = setListParticipantDefinition(participantUpdateMsg);
85         participantHandler.handleParticipantUpdate(pum);
86         var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
87         assertEquals(id, heartbeatTAfterUpdate.getParticipantId());
88     }
89
90     private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) {
91         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
92         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
93         var def = new ParticipantDefinition();
94         def.setParticipantId(id);
95         def.setParticipantType(id);
96         participantDefinitionUpdates.add(def);
97         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
98         return participantUpdateMsg;
99     }
100
101     @Test
102     void checkAppliesTo() {
103         var participantHandler = commonTestData.getMockParticipantHandler();
104         var participantAckMsg =
105                 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
106         assertTrue(participantHandler.appliesTo(participantAckMsg));
107
108         var participantMsg =
109                 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
110         assertTrue(participantHandler.appliesTo(participantMsg));
111
112         var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
113         participantMsg.setParticipantType(emptyid);
114         assertFalse(participantHandler.appliesTo(participantMsg));
115
116     }
117
118     @Test
119     void getAutomationCompositionInfoListTest() throws CoderException {
120         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
121         participantHandler.sendHeartbeat();
122         assertEquals(CommonTestData.AC_ID_1, participantHandler.makeHeartbeat(false)
123                 .getAutomationCompositionInfoList()
124                 .get(0)
125                 .getAutomationCompositionId());
126
127     }
128
129 }