2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.actorserviceprovider.topic;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertSame;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.Arrays;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
40 import org.onap.policy.common.endpoints.event.comm.TopicSink;
41 import org.onap.policy.common.endpoints.event.comm.TopicSource;
42 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
44 public class BidirectionalTopicHandlerTest {
45 private static final String UNKNOWN = "unknown";
46 private static final String MY_SOURCE = "my-source";
47 private static final String MY_SINK = "my-sink";
48 private static final String KEY1 = "requestId";
49 private static final String KEY2 = "subRequestId";
52 private TopicSink publisher;
55 private TopicSource subscriber;
58 private TopicEndpoint mgr;
60 private MyTopicHandler handler;
67 public void setUp() throws BidirectionalTopicClientException {
68 MockitoAnnotations.initMocks(this);
70 when(mgr.getTopicSinks(MY_SINK)).thenReturn(Arrays.asList(publisher));
71 when(mgr.getTopicSources(eq(Arrays.asList(MY_SOURCE)))).thenReturn(Arrays.asList(subscriber));
73 when(publisher.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
75 handler = new MyTopicHandler(MY_SINK, MY_SOURCE);
81 public void testBidirectionalTopicHandler_testGetSource_testGetTarget() {
82 assertEquals(MY_SOURCE, handler.getSourceTopic());
83 assertEquals(MY_SINK, handler.getSinkTopic());
85 verify(mgr).getTopicSinks(anyString());
86 verify(mgr).getTopicSources(any());
89 assertThatThrownBy(() -> new MyTopicHandler(MY_SINK, UNKNOWN))
90 .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sources")
91 .hasMessageContaining(UNKNOWN);
94 assertThatThrownBy(() -> new MyTopicHandler(UNKNOWN, MY_SOURCE))
95 .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sinks")
96 .hasMessageContaining(UNKNOWN);
100 public void testShutdown() {
102 verify(subscriber).unregister(any());
106 public void testStart() {
107 verify(subscriber).register(any());
111 public void testStop() {
113 verify(subscriber).unregister(any());
117 public void testAddForwarder() {
119 Forwarder forwarder = handler.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2));
120 assertNotNull(forwarder);
122 // repeat using list form
123 assertSame(forwarder, handler.addForwarder(Arrays.asList(new SelectorKey(KEY1), new SelectorKey(KEY2))));
127 public void testGetTopicEndpointManager() {
128 // setting "mgr" to null should cause it to use the superclass' method
130 assertNotNull(handler.getTopicEndpointManager());
134 private class MyTopicHandler extends BidirectionalTopicHandler {
135 public MyTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
136 super(sinkTopic, sourceTopic);
140 protected TopicEndpoint getTopicEndpointManager() {
141 return (mgr != null ? mgr : super.getTopicEndpointManager());