63db364ca21d16bc2ef5164a8e15fab822ced6ad
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.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.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27
28 import java.time.Instant;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41
42
43 class ParticipantHandlerTest {
44
45     private 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 mockParticipantHandlerTest() {
51         ParticipantHandler participantHandler = commonTestData.getMockParticipantHandler();
52         assertNull(participantHandler.getParticipant(null, null));
53         assertEquals("org.onap.PM_CDS_Blueprint 1.0.1", participantHandler.getParticipantId().toString());
54
55         ToscaConceptIdentifier id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
56         assertEquals(id, participantHandler.getParticipantId());
57         assertEquals(id, participantHandler.getParticipantType());
58         assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty();
59
60     }
61
62     @Test
63     void handleUpdateTest() {
64         ParticipantParameters parameters = CommonTestData.getParticipantParameters();
65         ControlLoopHandler controlLoopHander = commonTestData.getMockControlLoopHandler();
66         ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
67         ParticipantHandler emptyParticipantHandler =
68                 new ParticipantHandler(parameters, publisher, controlLoopHander);
69         ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
70
71         assertThatThrownBy(() ->
72                 emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg))
73                 .isInstanceOf(RuntimeException.class);
74
75         ParticipantHandler participantHandler = commonTestData.getMockParticipantHandler();
76         ToscaConceptIdentifier id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
77         participantUpdateMsg.setControlLoopId(id);
78         participantUpdateMsg.setParticipantId(id);
79         participantUpdateMsg.setParticipantType(id);
80         participantUpdateMsg.setMessageId(UUID.randomUUID());
81         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
82
83
84         ParticipantStatus heartbeatF = participantHandler.makeHeartbeat(false);
85         assertEquals(id, heartbeatF.getParticipantId());
86         assertEquals(ParticipantState.UNKNOWN, heartbeatF.getParticipantStatistics().getState());
87         assertThat(heartbeatF.getControlLoopInfoList()).isEmpty();
88
89         participantHandler.handleParticipantUpdate(participantUpdateMsg);
90         assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty();
91
92         ParticipantStatus heartbeatT = participantHandler.makeHeartbeat(true);
93         assertEquals(id, heartbeatT.getParticipantId());
94         assertEquals(ParticipantState.TERMINATED, heartbeatT.getParticipantStatistics().getState());
95         assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
96         assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
97
98     }
99
100     @Test
101     void handleParticipantTest() {
102         ParticipantHandler participantHandler = commonTestData.getMockParticipantHandler();
103         ToscaConceptIdentifier id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
104         Participant p = participantHandler.getParticipant(id.getName(), id.getVersion());
105         assertEquals(ParticipantState.UNKNOWN, p.getParticipantState());
106
107         participantHandler.updateParticipantState(id, ParticipantState.PASSIVE);
108         Participant p2 = participantHandler.getParticipant(id.getName(), id.getVersion());
109         assertEquals(ParticipantState.PASSIVE, p2.getParticipantState());
110
111         ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
112         participantRegisterAckMsg.setState(ParticipantState.TERMINATED);
113         participantHandler.handleParticipantRegisterAck(participantRegisterAckMsg);
114         assertEquals(ParticipantHealthStatus.HEALTHY, participantHandler.makeHeartbeat(false).getHealthStatus());
115
116     }
117
118 }