Policy Clamp Code Coverage 86/125086/2
authorlapentafd <francesco.lapenta@est.tech>
Mon, 11 Oct 2021 11:50:24 +0000 (12:50 +0100)
committerlapentafd <francesco.lapenta@est.tech>
Tue, 19 Oct 2021 13:05:17 +0000 (14:05 +0100)
Participant Handler Test Cases

Issue-ID: POLICY-3452
Change-Id: I8325206fdfbfbc199f593f7e058e28fa0ef2a649
Signed-off-by: lapentafd <francesco.lapenta@est.tech>
participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java [new file with mode: 0644]
participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java

diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java
new file mode 100644 (file)
index 0000000..63db364
--- /dev/null
@@ -0,0 +1,118 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.participant.intermediary.handler;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.time.Instant;
+import java.util.UUID;
+import org.junit.jupiter.api.Test;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
+import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
+import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
+import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
+import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
+import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
+import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+
+
+class ParticipantHandlerTest {
+
+    private CommonTestData commonTestData = new CommonTestData();
+    private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
+    private static final String ID_VERSION = "1.0.1";
+
+    @Test
+    void mockParticipantHandlerTest() {
+        ParticipantHandler participantHandler = commonTestData.getMockParticipantHandler();
+        assertNull(participantHandler.getParticipant(null, null));
+        assertEquals("org.onap.PM_CDS_Blueprint 1.0.1", participantHandler.getParticipantId().toString());
+
+        ToscaConceptIdentifier id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
+        assertEquals(id, participantHandler.getParticipantId());
+        assertEquals(id, participantHandler.getParticipantType());
+        assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty();
+
+    }
+
+    @Test
+    void handleUpdateTest() {
+        ParticipantParameters parameters = CommonTestData.getParticipantParameters();
+        ControlLoopHandler controlLoopHander = commonTestData.getMockControlLoopHandler();
+        ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
+        ParticipantHandler emptyParticipantHandler =
+                new ParticipantHandler(parameters, publisher, controlLoopHander);
+        ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
+
+        assertThatThrownBy(() ->
+                emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg))
+                .isInstanceOf(RuntimeException.class);
+
+        ParticipantHandler participantHandler = commonTestData.getMockParticipantHandler();
+        ToscaConceptIdentifier id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
+        participantUpdateMsg.setControlLoopId(id);
+        participantUpdateMsg.setParticipantId(id);
+        participantUpdateMsg.setParticipantType(id);
+        participantUpdateMsg.setMessageId(UUID.randomUUID());
+        participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
+
+
+        ParticipantStatus heartbeatF = participantHandler.makeHeartbeat(false);
+        assertEquals(id, heartbeatF.getParticipantId());
+        assertEquals(ParticipantState.UNKNOWN, heartbeatF.getParticipantStatistics().getState());
+        assertThat(heartbeatF.getControlLoopInfoList()).isEmpty();
+
+        participantHandler.handleParticipantUpdate(participantUpdateMsg);
+        assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty();
+
+        ParticipantStatus heartbeatT = participantHandler.makeHeartbeat(true);
+        assertEquals(id, heartbeatT.getParticipantId());
+        assertEquals(ParticipantState.TERMINATED, heartbeatT.getParticipantStatistics().getState());
+        assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty();
+        assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId());
+
+    }
+
+    @Test
+    void handleParticipantTest() {
+        ParticipantHandler participantHandler = commonTestData.getMockParticipantHandler();
+        ToscaConceptIdentifier id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
+        Participant p = participantHandler.getParticipant(id.getName(), id.getVersion());
+        assertEquals(ParticipantState.UNKNOWN, p.getParticipantState());
+
+        participantHandler.updateParticipantState(id, ParticipantState.PASSIVE);
+        Participant p2 = participantHandler.getParticipant(id.getName(), id.getVersion());
+        assertEquals(ParticipantState.PASSIVE, p2.getParticipantState());
+
+        ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
+        participantRegisterAckMsg.setState(ParticipantState.TERMINATED);
+        participantHandler.handleParticipantRegisterAck(participantRegisterAckMsg);
+        assertEquals(ParticipantHealthStatus.HEALTHY, participantHandler.makeHeartbeat(false).getHealthStatus());
+
+    }
+
+}
index ae20f4b..af88b5a 100644 (file)
@@ -29,7 +29,9 @@ import org.mockito.Mockito;
 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler;
 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.DummyParticipantParameters;
+import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
+import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 import org.onap.policy.common.endpoints.parameters.TopicParameters;
 import org.onap.policy.common.utils.coder.Coder;
@@ -45,7 +47,6 @@ public class CommonTestData {
     public static final String DESCRIPTION = "Participant description";
     public static final long TIME_INTERVAL = 2000;
     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
-
     public static final Coder CODER = new StandardCoder();
     private static final Object lockit = new Object();
 
@@ -168,4 +169,19 @@ public class CommonTestData {
                 getParticipantParameters(),
                 getParticipantMessagePublisher());
     }
+
+    /**
+     * Returns a mocked ParticipantHandler for test cases.
+     *
+     * @return participant Handler
+     */
+    public ParticipantHandler getMockParticipantHandler() {
+        ParticipantParameters parameters = getParticipantParameters();
+        ControlLoopHandler controlLoopHander = getMockControlLoopHandler();
+        ParticipantMessagePublisher publisher = new ParticipantMessagePublisher();
+        publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
+        ParticipantHandler participantHandler = new ParticipantHandler(parameters, publisher, controlLoopHander);
+        return participantHandler;
+    }
+
 }