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