From c0dc5e1a33d0680da6612d096a3a91d9c13eb648 Mon Sep 17 00:00:00 2001
From: lapentafd <francesco.lapenta@est.tech>
Date: Mon, 11 Oct 2021 12:50:24 +0100
Subject: Policy Clamp Code Coverage

Participant Handler Test Cases

Issue-ID: POLICY-3452
Change-Id: I8325206fdfbfbc199f593f7e058e28fa0ef2a649
Signed-off-by: lapentafd <francesco.lapenta@est.tech>
---
 .../handler/ParticipantHandlerTest.java            | 118 +++++++++++++++++++++
 .../main/parameters/CommonTestData.java            |  18 +++-
 2 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java

(limited to 'participant/participant-intermediary/src')

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
index 000000000..63db364ca
--- /dev/null
+++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java
@@ -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());
+
+    }
+
+}
diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java
index ae20f4b7f..af88b5a8a 100644
--- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java
+++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java
@@ -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;
+    }
+
 }
-- 
cgit