a87d8e1aa7bef193e75f118d984b2b4c5f4af8ef
[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.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30 import java.time.Instant;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
36 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantHealthStatus;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47
48 class ParticipantHandlerTest {
49
50     private final CommonTestData commonTestData = new CommonTestData();
51     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
52     private static final String ID_VERSION = "1.0.1";
53
54     @Test
55     void mockParticipantHandlerTest() {
56         var participantHandler = commonTestData.getMockParticipantHandler();
57         assertNull(participantHandler.getParticipant(null, null));
58         assertEquals("org.onap.PM_CDS_Blueprint 1.0.1", participantHandler.getParticipantId().toString());
59
60         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
61         assertEquals(id, participantHandler.getParticipantId());
62         assertEquals(id, participantHandler.getParticipantType());
63         assertThat(participantHandler.getAcElementDefinitionCommonProperties(id)).isEmpty();
64
65     }
66
67     @Test
68     void handleUpdateTest() {
69         var parameters = CommonTestData.getParticipantParameters();
70         var automationCompositionHander = commonTestData.getMockAutomationCompositionHandler();
71         var publisher = new ParticipantMessagePublisher();
72         var emptyParticipantHandler =
73                 new ParticipantHandler(parameters, publisher, automationCompositionHander);
74         var participantUpdateMsg = new ParticipantUpdate();
75
76         assertThatThrownBy(() ->
77                 emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg))
78                 .isInstanceOf(RuntimeException.class);
79
80         var participantHandler = commonTestData.getMockParticipantHandler();
81
82         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
83         participantUpdateMsg.setAutomationCompositionId(id);
84         participantUpdateMsg.setParticipantId(id);
85         participantUpdateMsg.setParticipantType(id);
86         participantUpdateMsg.setMessageId(UUID.randomUUID());
87         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
88
89         var heartbeatF = participantHandler.makeHeartbeat(false);
90         assertEquals(id, heartbeatF.getParticipantId());
91         assertThat(heartbeatF.getAutomationCompositionInfoList()).isEmpty();
92
93         participantHandler.handleParticipantUpdate(participantUpdateMsg);
94         assertThat(participantHandler.getAcElementDefinitionCommonProperties(id)).isEmpty();
95
96         var heartbeatT = participantHandler.makeHeartbeat(true);
97         assertEquals(id, heartbeatT.getParticipantId());
98         assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
99         assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
100
101         var pum = setListParticipantDefinition(participantUpdateMsg);
102         participantHandler.handleParticipantUpdate(pum);
103         var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true);
104         assertEquals(id, heartbeatTAfterUpdate.getParticipantId());
105     }
106
107     private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) {
108         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
109         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
110         var def = new ParticipantDefinition();
111         def.setParticipantId(id);
112         def.setParticipantType(id);
113         participantDefinitionUpdates.add(def);
114         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
115         return participantUpdateMsg;
116     }
117
118     @Test
119     void handleParticipantTest() {
120         var participantHandler = commonTestData.getMockParticipantHandler();
121         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
122         var p = participantHandler.getParticipant(id.getName(), id.getVersion());
123         assertEquals(ParticipantState.UNKNOWN, p.getParticipantState());
124
125         participantHandler.updateParticipantState(id, ParticipantState.PASSIVE);
126         var p2 = participantHandler.getParticipant(id.getName(), id.getVersion());
127         assertEquals(ParticipantState.PASSIVE, p2.getParticipantState());
128
129         var participantRegisterAckMsg = new ParticipantRegisterAck();
130         participantRegisterAckMsg.setState(ParticipantState.TERMINATED);
131         participantHandler.handleParticipantRegisterAck(participantRegisterAckMsg);
132         assertEquals(ParticipantHealthStatus.HEALTHY, participantHandler.makeHeartbeat(false).getHealthStatus());
133
134         var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
135         assertNull(participantHandler.updateParticipantState(emptyid, ParticipantState.PASSIVE));
136
137         var sameid = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
138         var participant = participantHandler.updateParticipantState(sameid, ParticipantState.PASSIVE);
139         assertEquals(participant.getDefinition(), sameid);
140
141     }
142
143     @Test
144     void checkAppliesTo() {
145         var participantHandler = commonTestData.getMockParticipantHandler();
146         var participantAckMsg =
147                 new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
148         assertTrue(participantHandler.appliesTo(participantAckMsg));
149
150         var participantMsg =
151                 new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
152         assertTrue(participantHandler.appliesTo(participantMsg));
153
154         var emptyid = new ToscaConceptIdentifier("", ID_VERSION);
155         participantMsg.setParticipantType(emptyid);
156         assertFalse(participantHandler.appliesTo(participantMsg));
157
158     }
159
160     @Test
161     void getAutomationCompositionInfoListTest() throws CoderException {
162         var participantHandler = commonTestData.getParticipantHandlerAutomationCompositions();
163         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
164         participantHandler.sendHeartbeat();
165         assertEquals(id, participantHandler.makeHeartbeat(false)
166                 .getAutomationCompositionInfoList()
167                 .get(0)
168                 .getAutomationCompositionId());
169
170     }
171
172 }