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