2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.common.rules.test;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.List;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicReference;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
44 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
45 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
46 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
47 import org.onap.policy.common.endpoints.parameters.TopicParameters;
49 @RunWith(MockitoJUnitRunner.class)
50 public class ListenerTest {
51 private static final String EXPECTED_EXCEPTION = "expected exception";
52 private static final String MY_TOPIC = "my-topic";
53 private static final String MESSAGE = "the-message";
54 private static final String MESSAGE2 = "other-message";
55 private static final String MSG_SUFFIX = "s";
56 private static final String DECODED_MESSAGE = MESSAGE + MSG_SUFFIX;
59 private NoopTopicSink sink;
61 private TopicEndpoint mgr;
63 private Listener<String> listener;
69 public static void setUpBeforeClass() {
70 TopicEndpointManager.getManager().shutdown();
72 TopicParameters params = new TopicParameters();
73 params.setTopic(MY_TOPIC);
74 params.setManaged(true);
75 params.setTopicCommInfrastructure("NOOP");
77 TopicEndpointManager.getManager().addTopicSinks(List.of(params));
81 public static void tearDownAfterClass() {
82 TopicEndpointManager.getManager().shutdown();
90 when(mgr.getNoopTopicSink(MY_TOPIC)).thenReturn(sink);
92 listener = new Listener<>(MY_TOPIC, msg -> msg + MSG_SUFFIX) {
94 protected TopicEndpoint getTopicManager() {
101 public void testListener() {
102 verify(sink).register(listener);
106 public void testAwait_testAwaitLongTimeUnit_testIsEmpty() {
107 assertTrue(listener.isEmpty());
109 listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE);
110 assertFalse(listener.isEmpty());
112 assertEquals(DECODED_MESSAGE, listener.await());
114 assertTrue(listener.isEmpty());
118 public void testAwaitPredicateOfT() {
119 listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE);
120 listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE2);
121 assertEquals(MESSAGE2 + MSG_SUFFIX, listener.await(msg -> msg.startsWith("other-")));
125 * Tests await() when the remaining time is negative.
128 public void testAwaitLongTimeUnitPredicateNoTime() {
129 assertThatThrownBy(() -> listener.await(-1, TimeUnit.SECONDS)).isInstanceOf(TopicException.class);
133 * Tests await() when the poll() returns {@code null}.
136 public void testAwaitLongTimeUnitPredicateNoMessage() {
137 assertThatThrownBy(() -> listener.await(1, TimeUnit.MILLISECONDS)).isInstanceOf(TopicException.class);
141 * Tests await() when the poll() is interrupted.
144 public void testAwaitLongTimeUnitPredicateInterrupted() throws InterruptedException {
145 listener = new Listener<String>(MY_TOPIC, msg -> msg) {
147 protected String pollMessage(long remainingMs) throws InterruptedException {
148 throw new InterruptedException(EXPECTED_EXCEPTION);
152 AtomicReference<TopicException> exref = new AtomicReference<>();
153 CountDownLatch interrupted = new CountDownLatch(1);
155 Thread thread = new Thread() {
160 } catch (TopicException e) {
164 if (Thread.currentThread().isInterrupted()) {
165 interrupted.countDown();
171 assertTrue(interrupted.await(5, TimeUnit.SECONDS));
172 assertNotNull(exref.get());
176 public void testUnregister() {
177 listener.unregister();
178 verify(sink).unregister(listener);
182 public void testOnTopicEvent() {
183 listener = new Listener<>(MY_TOPIC, msg -> {
184 throw new IllegalArgumentException(EXPECTED_EXCEPTION);
187 // onTopicEvent() should not throw an exception
188 assertThatCode(() -> listener.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MESSAGE))
189 .doesNotThrowAnyException();
191 // should not have queued a message
192 assertTrue(listener.isEmpty());
196 public void testGetTopicManager() {
197 // use a listener with a real manager
198 assertNotNull(new Listener<>(MY_TOPIC, msg -> msg).getTopicManager());