Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / topic / BidirectionalTopicHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.topic;
22
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;
32
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;
43
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";
50
51     @Mock
52     private TopicSink publisher;
53
54     @Mock
55     private TopicSource subscriber;
56
57     @Mock
58     private TopicEndpoint mgr;
59
60     private MyTopicHandler handler;
61
62
63     /**
64      * Sets up.
65      */
66     @Before
67     public void setUp() throws BidirectionalTopicClientException {
68         MockitoAnnotations.initMocks(this);
69
70         when(mgr.getTopicSinks(MY_SINK)).thenReturn(Arrays.asList(publisher));
71         when(mgr.getTopicSources(eq(Arrays.asList(MY_SOURCE)))).thenReturn(Arrays.asList(subscriber));
72
73         when(publisher.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
74
75         handler = new MyTopicHandler(MY_SINK, MY_SOURCE);
76
77         handler.start();
78     }
79
80     @Test
81     public void testBidirectionalTopicHandler_testGetSource_testGetTarget() {
82         assertEquals(MY_SOURCE, handler.getSourceTopic());
83         assertEquals(MY_SINK, handler.getSinkTopic());
84
85         verify(mgr).getTopicSinks(anyString());
86         verify(mgr).getTopicSources(any());
87
88         // source not found
89         assertThatThrownBy(() -> new MyTopicHandler(MY_SINK, UNKNOWN))
90                         .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sources")
91                         .hasMessageContaining(UNKNOWN);
92
93         // target not found
94         assertThatThrownBy(() -> new MyTopicHandler(UNKNOWN, MY_SOURCE))
95                         .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sinks")
96                         .hasMessageContaining(UNKNOWN);
97     }
98
99     @Test
100     public void testShutdown() {
101         handler.shutdown();
102         verify(subscriber).unregister(any());
103     }
104
105     @Test
106     public void testStart() {
107         verify(subscriber).register(any());
108     }
109
110     @Test
111     public void testStop() {
112         handler.stop();
113         verify(subscriber).unregister(any());
114     }
115
116     @Test
117     public void testAddForwarder() {
118         // array form
119         Forwarder forwarder = handler.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2));
120         assertNotNull(forwarder);
121
122         // repeat using list form
123         assertSame(forwarder, handler.addForwarder(Arrays.asList(new SelectorKey(KEY1), new SelectorKey(KEY2))));
124     }
125
126     @Test
127     public void testGetTopicEndpointManager() {
128         // setting "mgr" to null should cause it to use the superclass' method
129         mgr = null;
130         assertNotNull(handler.getTopicEndpointManager());
131     }
132
133
134     private class MyTopicHandler extends BidirectionalTopicHandler {
135         public MyTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
136             super(sinkTopic, sourceTopic);
137         }
138
139         @Override
140         protected TopicEndpoint getTopicEndpointManager() {
141             return (mgr != null ? mgr : super.getTopicEndpointManager());
142         }
143     }
144 }