Fix sonars from depeendency upgrade
[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-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
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.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Arrays;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
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 @RunWith(MockitoJUnitRunner.class)
45 public class BidirectionalTopicHandlerTest {
46     private static final String UNKNOWN = "unknown";
47     private static final String MY_SOURCE = "my-source";
48     private static final String MY_SINK = "my-sink";
49     private static final String KEY1 = "requestId";
50     private static final String KEY2 = "subRequestId";
51
52     @Mock
53     private TopicSink publisher;
54
55     @Mock
56     private TopicSource subscriber;
57
58     @Mock
59     private TopicEndpoint mgr;
60
61     private MyTopicHandler handler;
62
63
64     /**
65      * Sets up.
66      */
67     @Before
68     public void setUp() throws BidirectionalTopicClientException {
69         when(mgr.getTopicSinks(MY_SINK)).thenReturn(Arrays.asList(publisher));
70         when(mgr.getTopicSources(Arrays.asList(MY_SOURCE))).thenReturn(Arrays.asList(subscriber));
71
72         when(publisher.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
73
74         handler = new MyTopicHandler(MY_SINK, MY_SOURCE);
75
76         handler.start();
77     }
78
79     @Test
80     public void testBidirectionalTopicHandler_testGetSource_testGetTarget() {
81         assertEquals(MY_SOURCE, handler.getSourceTopic());
82         assertEquals(MY_SINK, handler.getSinkTopic());
83
84         verify(mgr).getTopicSinks(anyString());
85         verify(mgr).getTopicSources(any());
86
87         // source not found
88         assertThatThrownBy(() -> new MyTopicHandler(MY_SINK, UNKNOWN))
89                         .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sources")
90                         .hasMessageContaining(UNKNOWN);
91
92         // target not found
93         assertThatThrownBy(() -> new MyTopicHandler(UNKNOWN, MY_SOURCE))
94                         .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sinks")
95                         .hasMessageContaining(UNKNOWN);
96     }
97
98     @Test
99     public void testShutdown() {
100         handler.shutdown();
101         verify(subscriber).unregister(any());
102     }
103
104     @Test
105     public void testStart() {
106         verify(subscriber).register(any());
107     }
108
109     @Test
110     public void testStop() {
111         handler.stop();
112         verify(subscriber).unregister(any());
113     }
114
115     @Test
116     public void testAddForwarder() {
117         // array form
118         Forwarder forwarder = handler.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2));
119         assertNotNull(forwarder);
120
121         // repeat using list form
122         assertSame(forwarder, handler.addForwarder(Arrays.asList(new SelectorKey(KEY1), new SelectorKey(KEY2))));
123     }
124
125     @Test
126     public void testGetTopicEndpointManager() {
127         // setting "mgr" to null should cause it to use the superclass' method
128         mgr = null;
129         assertNotNull(handler.getTopicEndpointManager());
130     }
131
132
133     private class MyTopicHandler extends BidirectionalTopicHandler {
134         public MyTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
135             super(sinkTopic, sourceTopic);
136         }
137
138         @Override
139         protected TopicEndpoint getTopicEndpointManager() {
140             return (mgr != null ? mgr : super.getTopicEndpointManager());
141         }
142     }
143 }