2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actorserviceprovider.topic;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertSame;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.Arrays;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.TestInstance;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
42 import org.onap.policy.common.endpoints.event.comm.TopicSink;
43 import org.onap.policy.common.endpoints.event.comm.TopicSource;
44 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
46 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
47 @ExtendWith(MockitoExtension.class)
48 class BidirectionalTopicHandlerTest {
49 private static final String UNKNOWN = "unknown";
50 private static final String MY_SOURCE = "my-source";
51 private static final String MY_SINK = "my-sink";
52 private static final String KEY1 = "requestId";
53 private static final String KEY2 = "subRequestId";
56 private TopicSink publisher;
59 private TopicSource subscriber;
62 private TopicEndpoint mgr;
64 private MyTopicHandler handler;
71 void setUp() throws BidirectionalTopicClientException {
72 when(mgr.getTopicSinks(MY_SINK)).thenReturn(Arrays.asList(publisher));
73 when(mgr.getTopicSources(Arrays.asList(MY_SOURCE))).thenReturn(Arrays.asList(subscriber));
75 when(publisher.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
77 handler = new MyTopicHandler(MY_SINK, MY_SOURCE);
83 void testBidirectionalTopicHandler_testGetSource_testGetTarget() {
84 assertEquals(MY_SOURCE, handler.getSourceTopic());
85 assertEquals(MY_SINK, handler.getSinkTopic());
87 verify(mgr).getTopicSinks(anyString());
88 verify(mgr).getTopicSources(any());
91 assertThatThrownBy(() -> new MyTopicHandler(MY_SINK, UNKNOWN))
92 .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sources")
93 .hasMessageContaining(UNKNOWN);
96 assertThatThrownBy(() -> new MyTopicHandler(UNKNOWN, MY_SOURCE))
97 .isInstanceOf(BidirectionalTopicClientException.class).hasMessageContaining("sinks")
98 .hasMessageContaining(UNKNOWN);
102 void testShutdown() {
104 verify(subscriber).unregister(any());
109 verify(subscriber).register(any());
115 verify(subscriber).unregister(any());
119 void testAddForwarder() {
121 Forwarder forwarder = handler.addForwarder(new SelectorKey(KEY1), new SelectorKey(KEY2));
122 assertNotNull(forwarder);
124 // repeat using list form
125 assertSame(forwarder, handler.addForwarder(Arrays.asList(new SelectorKey(KEY1), new SelectorKey(KEY2))));
129 void testGetTopicEndpointManager() {
130 // setting "mgr" to null should cause it to use the superclass' method
132 assertNotNull(handler.getTopicEndpointManager());
136 private class MyTopicHandler extends BidirectionalTopicHandler {
137 MyTopicHandler(String sinkTopic, String sourceTopic) throws BidirectionalTopicClientException {
138 super(sinkTopic, sourceTopic);
142 protected TopicEndpoint getTopicEndpointManager() {
143 return (mgr != null ? mgr : super.getTopicEndpointManager());