diff options
Diffstat (limited to 'controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/ListenerTest.java')
-rw-r--r-- | controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/ListenerTest.java | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/ListenerTest.java b/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/ListenerTest.java index 7a7add5a7..ed30515bc 100644 --- a/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/ListenerTest.java +++ b/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/ListenerTest.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,10 +23,11 @@ package org.onap.policy.controlloop.common.rules.test; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -33,21 +35,17 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink; import org.onap.policy.common.endpoints.parameters.TopicParameters; -@RunWith(MockitoJUnitRunner.class) -public class ListenerTest { +class ListenerTest { private static final String EXPECTED_EXCEPTION = "expected exception"; private static final String MY_TOPIC = "my-topic"; private static final String MESSAGE = "the-message"; @@ -55,21 +53,20 @@ public class ListenerTest { private static final String MSG_SUFFIX = "s"; private static final String DECODED_MESSAGE = MESSAGE + MSG_SUFFIX; - @Mock - private NoopTopicSink sink; - @Mock - private TopicEndpoint mgr; + private final NoopTopicSink sink = mock(NoopTopicSink.class); + + private final TopicEndpoint mgr = mock(TopicEndpoint.class); private Listener<String> listener; /** * Creates topics. */ - @BeforeClass + @BeforeAll public static void setUpBeforeClass() { TopicEndpointManager.getManager().shutdown(); - TopicParameters params = new TopicParameters(); + var params = new TopicParameters(); params.setTopic(MY_TOPIC); params.setManaged(true); params.setTopicCommInfrastructure("NOOP"); @@ -77,7 +74,7 @@ public class ListenerTest { TopicEndpointManager.getManager().addTopicSinks(List.of(params)); } - @AfterClass + @AfterAll public static void tearDownAfterClass() { TopicEndpointManager.getManager().shutdown(); } @@ -85,7 +82,7 @@ public class ListenerTest { /** * Sets up. */ - @Before + @BeforeEach public void setUp() { when(mgr.getNoopTopicSink(MY_TOPIC)).thenReturn(sink); @@ -98,12 +95,12 @@ public class ListenerTest { } @Test - public void testListener() { + void testListener() { verify(sink).register(listener); } @Test - public void testAwait_testAwaitLongTimeUnit_testIsEmpty() { + void testAwait_testAwaitLongTimeUnit_testIsEmpty() { assertTrue(listener.isEmpty()); listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE); @@ -115,7 +112,7 @@ public class ListenerTest { } @Test - public void testAwaitPredicateOfT() { + void testAwaitPredicateOfT() { listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE); listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE2); assertEquals(MESSAGE2 + MSG_SUFFIX, listener.await(msg -> msg.startsWith("other-"))); @@ -125,7 +122,7 @@ public class ListenerTest { * Tests await() when the remaining time is negative. */ @Test - public void testAwaitLongTimeUnitPredicateNoTime() { + void testAwaitLongTimeUnitPredicateNoTime() { assertThatThrownBy(() -> listener.await(-1, TimeUnit.SECONDS)).isInstanceOf(TopicException.class); } @@ -133,7 +130,7 @@ public class ListenerTest { * Tests await() when the poll() returns {@code null}. */ @Test - public void testAwaitLongTimeUnitPredicateNoMessage() { + void testAwaitLongTimeUnitPredicateNoMessage() { assertThatThrownBy(() -> listener.await(1, TimeUnit.MILLISECONDS)).isInstanceOf(TopicException.class); } @@ -141,7 +138,7 @@ public class ListenerTest { * Tests await() when the poll() is interrupted. */ @Test - public void testAwaitLongTimeUnitPredicateInterrupted() throws InterruptedException { + void testAwaitLongTimeUnitPredicateInterrupted() throws InterruptedException { listener = new Listener<String>(MY_TOPIC, msg -> msg) { @Override protected String pollMessage(long remainingMs) throws InterruptedException { @@ -150,9 +147,9 @@ public class ListenerTest { }; AtomicReference<TopicException> exref = new AtomicReference<>(); - CountDownLatch interrupted = new CountDownLatch(1); + var interrupted = new CountDownLatch(1); - Thread thread = new Thread() { + var thread = new Thread() { @Override public void run() { try { @@ -173,13 +170,13 @@ public class ListenerTest { } @Test - public void testUnregister() { + void testUnregister() { listener.unregister(); verify(sink).unregister(listener); } @Test - public void testOnTopicEvent() { + void testOnTopicEvent() { listener = new Listener<>(MY_TOPIC, msg -> { throw new IllegalArgumentException(EXPECTED_EXCEPTION); }); @@ -193,7 +190,7 @@ public class ListenerTest { } @Test - public void testGetTopicManager() { + void testGetTopicManager() { // use a listener with a real manager assertNotNull(new Listener<>(MY_TOPIC, msg -> msg).getTopicManager()); } |