package org.onap.policy.controlloop.actorserviceprovider.topic;
-import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
-import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
+import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
-public class TopicPairTest {
+public class BidirectionalTopicHandlerTest {
private static final String UNKNOWN = "unknown";
- private static final String MY_SOURCE = "pair-source";
- private static final String MY_TARGET = "pair-target";
- private static final String TEXT = "some text";
+ private static final String MY_SOURCE = "my-source";
+ private static final String MY_SINK = "my-sink";
+ private static final String KEY1 = "requestId";
+ private static final String KEY2 = "subRequestId";
@Mock
- private TopicSink publisher1;
+ private TopicSink publisher;
@Mock
- private TopicSink publisher2;
-
- @Mock
- private TopicSource subscriber1;
-
- @Mock
- private TopicSource subscriber2;
+ private TopicSource subscriber;
@Mock
private TopicEndpoint mgr;
- private TopicPair pair;
+ private MyTopicHandler handler;
/**
* Sets up.
*/
@Before
- public void setUp() {
+ public void setUp() throws BidirectionalTopicClientException {
MockitoAnnotations.initMocks(this);
- when(mgr.getTopicSinks(MY_TARGET)).thenReturn(Arrays.asList(publisher1, publisher2));
- when(mgr.getTopicSources(eq(Arrays.asList(MY_SOURCE)))).thenReturn(Arrays.asList(subscriber1, subscriber2));
+ when(mgr.getTopicSinks(MY_SINK)).thenReturn(Arrays.asList(publisher));
+ when(mgr.getTopicSources(eq(Arrays.asList(MY_SOURCE)))).thenReturn(Arrays.asList(subscriber));
- when(publisher1.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
- when(publisher2.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.UEB);
+ when(publisher.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
- pair = new MyTopicPair(MY_SOURCE, MY_TARGET);
+ handler = new MyTopicHandler(MY_SINK, MY_SOURCE);
- pair.start();
+ handler.start();
}
@Test
- public void testTopicPair_testGetSource_testGetTarget() {
- assertEquals(MY_SOURCE, pair.getSource());
- assertEquals(MY_TARGET, pair.getTarget());
+ public void testBidirectionalTopicHandler_testGetSource_testGetTarget() {
+ assertEquals(MY_SOURCE, handler.getSourceTopic());
+ assertEquals(MY_SINK, handler.getSinkTopic());
verify(mgr).getTopicSinks(anyString());
verify(mgr).getTopicSources(any());
// source not found
- assertThatIllegalArgumentException().isThrownBy(() -> new MyTopicPair(UNKNOWN, MY_TARGET))
- .withMessageContaining("sources").withMessageContaining(UNKNOWN);
+ assertThatThrownBy(() -> new MyTopicHandler(MY_SINK, UNKNOWN))
+ .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sources")
+ .hasMessageContaining(UNKNOWN);
// target not found
- assertThatIllegalArgumentException().isThrownBy(() -> new MyTopicPair(MY_SOURCE, UNKNOWN))
- .withMessageContaining("sinks").withMessageContaining(UNKNOWN);
+ assertThatThrownBy(() -> new MyTopicHandler(UNKNOWN, MY_SOURCE))
+ .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sinks")
+ .hasMessageContaining(UNKNOWN);
}
@Test
public void testShutdown() {
- pair.shutdown();
- verify(subscriber1).unregister(pair);
- verify(subscriber2).unregister(pair);
+ handler.shutdown();
+ verify(subscriber).unregister(any());
}
@Test
public void testStart() {
- verify(subscriber1).register(pair);
- verify(subscriber2).register(pair);
+ verify(subscriber).register(any());
}
@Test
public void testStop() {
- pair.stop();
- verify(subscriber1).unregister(pair);
- verify(subscriber2).unregister(pair);
+ handler.stop();
+ verify(subscriber).unregister(any());
}
@Test
- public void testPublish() {
- List<CommInfrastructure> infrastructures = pair.publish(TEXT);
- assertEquals(Arrays.asList(CommInfrastructure.NOOP, CommInfrastructure.UEB), infrastructures);
-
- verify(publisher1).send(TEXT);
- verify(publisher2).send(TEXT);
-
- // first one throws an exception - should have only published to the second
- when(publisher1.send(any())).thenThrow(new IllegalStateException("expected exception"));
-
- infrastructures = pair.publish(TEXT);
- assertEquals(Arrays.asList(CommInfrastructure.UEB), infrastructures);
+ public void testAddForwarder() {
+ // array form
+ Forwarder forwarder = handler.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2));
+ assertNotNull(forwarder);
- verify(publisher2, times(2)).send(TEXT);
+ // repeat using list form
+ assertSame(forwarder, handler.addForwarder(Arrays.asList(new SelectorKey(KEY1), new SelectorKey(KEY2))));
}
@Test
public void testGetTopicEndpointManager() {
// setting "mgr" to null should cause it to use the superclass' method
mgr = null;
- assertNotNull(pair.getTopicEndpointManager());
+ assertNotNull(handler.getTopicEndpointManager());
}
- private class MyTopicPair extends TopicPair {
- public MyTopicPair(String source, String target) {
- super(source, target);
+ private class MyTopicHandler extends BidirectionalTopicHandler {
+ public MyTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
+ super(sinkTopic, sourceTopic);
}
@Override