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.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.Mockito.verify;
 
  30 import static org.mockito.Mockito.when;
 
  32 import java.util.Arrays;
 
  33 import org.junit.Before;
 
  34 import org.junit.Test;
 
  35 import org.mockito.Mock;
 
  36 import org.mockito.MockitoAnnotations;
 
  37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 
  38 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
 
  39 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 
  40 import org.onap.policy.common.endpoints.event.comm.TopicSource;
 
  41 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
 
  43 public class BidirectionalTopicHandlerTest {
 
  44     private static final String UNKNOWN = "unknown";
 
  45     private static final String MY_SOURCE = "my-source";
 
  46     private static final String MY_SINK = "my-sink";
 
  47     private static final String KEY1 = "requestId";
 
  48     private static final String KEY2 = "subRequestId";
 
  51     private TopicSink publisher;
 
  54     private TopicSource subscriber;
 
  57     private TopicEndpoint mgr;
 
  59     private MyTopicHandler handler;
 
  66     public void setUp() throws BidirectionalTopicClientException {
 
  67         MockitoAnnotations.initMocks(this);
 
  69         when(mgr.getTopicSinks(MY_SINK)).thenReturn(Arrays.asList(publisher));
 
  70         when(mgr.getTopicSources(Arrays.asList(MY_SOURCE))).thenReturn(Arrays.asList(subscriber));
 
  72         when(publisher.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
 
  74         handler = new MyTopicHandler(MY_SINK, MY_SOURCE);
 
  80     public void testBidirectionalTopicHandler_testGetSource_testGetTarget() {
 
  81         assertEquals(MY_SOURCE, handler.getSourceTopic());
 
  82         assertEquals(MY_SINK, handler.getSinkTopic());
 
  84         verify(mgr).getTopicSinks(anyString());
 
  85         verify(mgr).getTopicSources(any());
 
  88         assertThatThrownBy(() -> new MyTopicHandler(MY_SINK, UNKNOWN))
 
  89                         .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sources")
 
  90                         .hasMessageContaining(UNKNOWN);
 
  93         assertThatThrownBy(() -> new MyTopicHandler(UNKNOWN, MY_SOURCE))
 
  94                         .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sinks")
 
  95                         .hasMessageContaining(UNKNOWN);
 
  99     public void testShutdown() {
 
 101         verify(subscriber).unregister(any());
 
 105     public void testStart() {
 
 106         verify(subscriber).register(any());
 
 110     public void testStop() {
 
 112         verify(subscriber).unregister(any());
 
 116     public void testAddForwarder() {
 
 118         Forwarder forwarder = handler.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2));
 
 119         assertNotNull(forwarder);
 
 121         // repeat using list form
 
 122         assertSame(forwarder, handler.addForwarder(Arrays.asList(new SelectorKey(KEY1), new SelectorKey(KEY2))));
 
 126     public void testGetTopicEndpointManager() {
 
 127         // setting "mgr" to null should cause it to use the superclass' method
 
 129         assertNotNull(handler.getTopicEndpointManager());
 
 133     private class MyTopicHandler extends BidirectionalTopicHandler {
 
 134         public MyTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
 
 135             super(sinkTopic, sourceTopic);
 
 139         protected TopicEndpoint getTopicEndpointManager() {
 
 140             return (mgr != null ? mgr : super.getTopicEndpointManager());