diff options
author | lapentafd <francesco.lapenta@est.tech> | 2021-07-21 09:06:53 +0100 |
---|---|---|
committer | lapentafd <francesco.lapenta@est.tech> | 2021-09-20 17:17:56 +0100 |
commit | 4795e854738b13d3ef4d324900865e9f4df9295e (patch) | |
tree | ced740e641010276983525117283f487dfa7d294 /participant/participant-intermediary/src/test/java | |
parent | 859749a4143c104f6bd63392c8d2cb2839408029 (diff) |
Code Coverage in clamp intermediary
Issue-ID: POLICY-3452
Change-Id: Ieb0d1153f5f2323bfc73cae7221dfba73b59b725
Signed-off-by: lapentafd <francesco.lapenta@est.tech>
Diffstat (limited to 'participant/participant-intermediary/src/test/java')
4 files changed, 285 insertions, 2 deletions
diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java new file mode 100644 index 000000000..29387959b --- /dev/null +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java @@ -0,0 +1,93 @@ +/*- + * ============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.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; + +import java.time.Instant; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class ControlLoopHandlerTest { + + private CommonTestData commonTestData = new CommonTestData(); + + @Test + void controlLoopHandlerTest() { + ControlLoopHandler clh = commonTestData.getMockControlLoopHandler(); + assertNotNull(clh.getControlLoops()); + + assertNotNull(clh.getControlLoopMap()); + assertNotNull(clh.getElementsOnThisParticipant()); + + UUID elementId1 = UUID.randomUUID(); + ControlLoopElement element = new ControlLoopElement(); + element.setId(elementId1); + element.setDefinition(new ToscaConceptIdentifier( + "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1")); + + element.setOrderedState(ControlLoopOrderedState.PASSIVE); + + ControlLoopElementListener listener = mock(ControlLoopElementListener.class); + clh.registerControlLoopElementListener(listener); + assertThat(clh.getListeners()).contains(listener); + + } + + @Test + void updateNullControlLoopHandlerTest() { + UUID id = UUID.randomUUID(); + + ControlLoopHandler clh = commonTestData.getMockControlLoopHandler(); + assertNull(clh.updateControlLoopElementState(null, null, ControlLoopOrderedState.UNINITIALISED, + ControlLoopState.PASSIVE)); + + assertNull(clh.updateControlLoopElementState(null, id, ControlLoopOrderedState.UNINITIALISED, + ControlLoopState.PASSIVE)); + + ClElementStatistics clElementStatistics = new ClElementStatistics(); + ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1"); + clElementStatistics.setParticipantId(controlLoopId); + clElementStatistics.setControlLoopState(ControlLoopState.RUNNING); + clElementStatistics.setTimeStamp(Instant.now()); + + clh.updateControlLoopElementStatistics(id, clElementStatistics); + assertNull(clh.updateControlLoopElementState(controlLoopId, id, ControlLoopOrderedState.UNINITIALISED, + ControlLoopState.PASSIVE)); + + + + } + +} diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/DummyParticipantParameters.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/DummyParticipantParameters.java new file mode 100644 index 000000000..d60bb71bc --- /dev/null +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/DummyParticipantParameters.java @@ -0,0 +1,35 @@ +/*- + * ============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 javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; +import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters; + +@Getter +@Setter +public class DummyParticipantParameters implements ParticipantParameters { + + @NotNull + private ParticipantIntermediaryParameters intermediaryParameters; +} diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivatorTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivatorTest.java new file mode 100644 index 000000000..bbe0412ed --- /dev/null +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivatorTest.java @@ -0,0 +1,97 @@ +/*- + * ============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.assertThatIllegalStateException; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq; +import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusReqListener; +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.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardCoderObject; + +class IntermediaryActivatorTest { + private static final Coder CODER = new StandardCoder(); + + private static final String TOPIC_FIRST = "TOPIC1"; + private static final String TOPIC_SECOND = "TOPIC2"; + + @Test + void testStartAndStop() throws Exception { + ParticipantParameters parameters = CommonTestData.getParticipantParameters(); + + var publisherFirst = spy(mock(Publisher.class)); + var publisherSecond = spy(mock(Publisher.class)); + var publishers = List.of(publisherFirst, publisherSecond); + + var listenerFirst = spy(mock(ParticipantStatusReqListener.class)); + when(listenerFirst.getType()).thenReturn(TOPIC_FIRST); + when(listenerFirst.getScoListener()).thenReturn(listenerFirst); + + var listenerSecond = spy(mock(ParticipantStatusReqListener.class)); + when(listenerSecond.getType()).thenReturn(TOPIC_SECOND); + when(listenerSecond.getScoListener()).thenReturn(listenerSecond); + + List<Listener<ParticipantStatusReq>> listeners = List.of(listenerFirst, listenerSecond); + + ParticipantHandler handler = mock(ParticipantHandler.class); + try (var activator = new IntermediaryActivator(parameters, handler, publishers, listeners)) { + + assertFalse(activator.isAlive()); + activator.start(); + assertTrue(activator.isAlive()); + + // repeat start - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.start()); + assertTrue(activator.isAlive()); + verify(publisherFirst, times(1)).active(anyList()); + verify(publisherSecond, times(1)).active(anyList()); + + StandardCoderObject sco = CODER.decode("{messageType:" + TOPIC_FIRST + "}", StandardCoderObject.class); + activator.getMsgDispatcher().onTopicEvent(null, "msg", sco); + verify(listenerFirst, times(1)).onTopicEvent(any(), any(), any()); + + sco = CODER.decode("{messageType:" + TOPIC_SECOND + "}", StandardCoderObject.class); + activator.getMsgDispatcher().onTopicEvent(null, "msg", sco); + verify(listenerSecond, times(1)).onTopicEvent(any(), any(), any()); + + activator.stop(); + assertFalse(activator.isAlive()); + + // repeat stop - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.stop()); + assertFalse(activator.isAlive()); + } + } +} 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 9353cde81..ae20f4b7f 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 @@ -21,10 +21,16 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.TreeMap; +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.parameters.ParticipantIntermediaryParameters; +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; import org.onap.policy.common.utils.coder.CoderException; @@ -41,6 +47,7 @@ public class CommonTestData { public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams()); public static final Coder CODER = new StandardCoder(); + private static final Object lockit = new Object(); /** * Get ParticipantIntermediaryParameters. @@ -57,12 +64,38 @@ public class CommonTestData { } /** + * Get ParticipantParameters. + * + * @return ParticipantParameters + */ + public static DummyParticipantParameters getParticipantParameters() { + try { + return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), + DummyParticipantParameters.class); + } catch (final CoderException e) { + throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e); + } + } + + /** + * Returns a property map for a Parameters map for test cases. + * + * @param name name of the parameters + * @return a property map suitable for constructing an object + */ + public static Map<String, Object> getParametersMap(final String name) { + final Map<String, Object> map = new TreeMap<>(); + map.put("intermediaryParameters", getIntermediaryParametersMap(name)); + return map; + } + + /** * Returns a property map for a intermediaryParameters map for test cases. * * @param name name of the parameters * @return a property map suitable for constructing an object */ - public Map<String, Object> getIntermediaryParametersMap(final String name) { + public static Map<String, Object> getIntermediaryParametersMap(final String name) { final Map<String, Object> map = new TreeMap<>(); map.put("name", name); map.put("participantId", getParticipantId()); @@ -80,7 +113,7 @@ public class CommonTestData { * @param isEmpty boolean value to represent that object created should be empty or not * @return a property map suitable for constructing an object */ - public Map<String, Object> getTopicParametersMap(final boolean isEmpty) { + public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) { final Map<String, Object> map = new TreeMap<>(); if (!isEmpty) { map.put("topicSources", TOPIC_PARAMS); @@ -110,4 +143,29 @@ public class CommonTestData { public static ToscaConceptIdentifier getParticipantId() { return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1"); } + + /** + * Returns a participantMessagePublisher for MessageSender. + * + * @return participant Message Publisher + */ + private ParticipantMessagePublisher getParticipantMessagePublisher() { + synchronized (lockit) { + ParticipantMessagePublisher participantMessagePublisher = + new ParticipantMessagePublisher(); + participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); + return participantMessagePublisher; + } + } + + /** + * Returns a mocked ControlLoopHandler for test cases. + * + * @return ControlLoopHandler + */ + public ControlLoopHandler getMockControlLoopHandler() { + return new ControlLoopHandler( + getParticipantParameters(), + getParticipantMessagePublisher()); + } } |