ffa7c5a1d1a308082666bb0df6e9f48fff219b17
[policy/common.git] /
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.common.endpoints.event.comm.client;
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.assertNotSame;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35 import java.util.Properties;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
43 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
44 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
45 import org.onap.policy.common.endpoints.event.comm.TopicListener;
46 import org.onap.policy.common.endpoints.event.comm.TopicSink;
47 import org.onap.policy.common.endpoints.event.comm.TopicSource;
48
49 public class BidirectionalTopicClientTest {
50     private static final String SINK_TOPIC = "my-sink-topic";
51     private static final String SOURCE_TOPIC = "my-source-topic";
52
53     private static final CommInfrastructure SINK_INFRA = CommInfrastructure.UEB;
54     private static final CommInfrastructure SOURCE_INFRA = CommInfrastructure.NOOP;
55
56     @Mock
57     private TopicSink sink;
58     @Mock
59     private TopicSource source;
60     @Mock
61     private TopicEndpoint endpoint;
62     @Mock
63     private TopicListener listener;
64
65     private BidirectionalTopicClient client;
66
67     /**
68      * Configures the endpoints.
69      */
70     @BeforeClass
71     public static void setUpBeforeClass() {
72         Properties props = new Properties();
73         props.setProperty("noop.sink.topics", SINK_TOPIC);
74         props.setProperty("noop.source.topics", SOURCE_TOPIC);
75
76         // clear all topics and then configure one sink and one source
77         TopicEndpointManager.getManager().shutdown();
78         TopicEndpointManager.getManager().addTopicSinks(props);
79         TopicEndpointManager.getManager().addTopicSources(props);
80     }
81
82     @AfterClass
83     public static void tearDownAfterClass() {
84         // clear all topics after the tests
85         TopicEndpointManager.getManager().shutdown();
86     }
87
88     /**
89      * Creates mocks and an initial client object.
90      */
91     @Before
92     public void setUp() throws Exception {
93         MockitoAnnotations.initMocks(this);
94
95         when(sink.send(anyString())).thenReturn(true);
96         when(sink.getTopicCommInfrastructure()).thenReturn(SINK_INFRA);
97
98         when(source.offer(anyString())).thenReturn(true);
99         when(source.getTopicCommInfrastructure()).thenReturn(SOURCE_INFRA);
100
101         when(endpoint.getTopicSinks(anyString())).thenReturn(Arrays.asList());
102         when(endpoint.getTopicSinks(SINK_TOPIC)).thenReturn(Arrays.asList(sink));
103
104         when(endpoint.getTopicSources(any())).thenReturn(Arrays.asList());
105         when(endpoint.getTopicSources(Arrays.asList(SOURCE_TOPIC))).thenReturn(Arrays.asList(source));
106
107         client = new BidirectionalTopicClient2(SINK_TOPIC, SOURCE_TOPIC);
108     }
109
110     @Test
111     public void testBidirectionalTopicClient_testGetters() {
112         assertSame(sink, client.getSink());
113         assertSame(source, client.getSource());
114         assertEquals(SINK_TOPIC, client.getSinkTopic());
115         assertEquals(SOURCE_TOPIC, client.getSourceTopic());
116         assertEquals(SINK_INFRA, client.getSinkTopicCommInfrastructure());
117         assertEquals(SOURCE_INFRA, client.getSourceTopicCommInfrastructure());
118     }
119
120     /**
121      * Tests the constructor when the sink or source cannot be found.
122      */
123     @Test
124     public void testBidirectionalTopicClientExceptions() {
125         assertThatThrownBy(() -> new BidirectionalTopicClient2("unknown-sink", SOURCE_TOPIC))
126                         .isInstanceOf(BidirectionalTopicClientException.class)
127                         .hasMessage("no sinks for topic: unknown-sink");
128
129         assertThatThrownBy(() -> new BidirectionalTopicClient2(SINK_TOPIC, "unknown-source"))
130                         .isInstanceOf(BidirectionalTopicClientException.class)
131                         .hasMessage("no sources for topic: unknown-source");
132
133         // too many sources
134         when(endpoint.getTopicSources(Arrays.asList(SOURCE_TOPIC))).thenReturn(Arrays.asList(source, source));
135
136         assertThatThrownBy(() -> new BidirectionalTopicClient2(SINK_TOPIC, SOURCE_TOPIC))
137                         .isInstanceOf(BidirectionalTopicClientException.class)
138                         .hasMessage("too many sources for topic: my-source-topic");
139     }
140
141     /**
142      * Tests the "delegate" methods.
143      */
144     @Test
145     public void testDelegates() {
146         assertTrue(client.send("hello"));
147         verify(sink).send("hello");
148
149         assertTrue(client.offer("incoming"));
150         verify(source).offer("incoming");
151
152         client.register(listener);
153         verify(source).register(listener);
154
155         client.unregister(listener);
156         verify(source).unregister(listener);
157     }
158
159     @Test
160     public void testGetTopicEndpointManager() throws BidirectionalTopicClientException {
161         // use a real manager
162         client = new BidirectionalTopicClient(SINK_TOPIC, SOURCE_TOPIC);
163         assertNotNull(client.getTopicEndpointManager());
164
165         assertNotNull(client.getSink());
166         assertNotNull(client.getSource());
167
168         assertNotSame(sink, client.getSink());
169         assertNotSame(source, client.getSource());
170     }
171
172
173     /**
174      * BidirectionalTopicClient with some overrides.
175      */
176     private class BidirectionalTopicClient2 extends BidirectionalTopicClient {
177
178         public BidirectionalTopicClient2(String sinkTopic, String sourceTopic)
179                         throws BidirectionalTopicClientException {
180             super(sinkTopic, sourceTopic);
181         }
182
183         @Override
184         protected TopicEndpoint getTopicEndpointManager() {
185             return endpoint;
186         }
187     }
188 }