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