2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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.common.endpoints.event.comm.client;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.Arrays;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Properties;
37 import java.util.concurrent.atomic.AtomicReference;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
42 import org.onap.policy.common.endpoints.event.comm.TopicSink;
44 public class TopicSinkClientTest {
45 private static final String TOPIC = "my-topic";
47 private TopicSinkClient client;
48 private TopicSink sink;
49 private List<TopicSink> sinks;
52 * Creates mocks and an initial client object.
54 * @throws Exception if an error occurs
57 public void setUp() throws Exception {
58 sink = mock(TopicSink.class);
59 when(sink.send(anyString())).thenReturn(true);
61 sinks = Arrays.asList(sink, null);
63 client = new TopicSinkClient2(TOPIC);
65 Properties props = new Properties();
66 props.setProperty("noop.sink.topics", TOPIC);
68 // clear all topics and then configure one topic
69 TopicEndpointManager.getManager().shutdown();
70 TopicEndpointManager.getManager().addTopicSinks(props);
74 public static void tearDown() {
75 // clear all topics after the tests
76 TopicEndpointManager.getManager().shutdown();
80 * Uses a real NO-OP topic sink.
83 public void testGetTopicSinks() throws Exception {
85 sink = TopicEndpointManager.getManager().getNoopTopicSink(TOPIC);
88 final AtomicReference<String> evref = new AtomicReference<>(null);
90 sink.register((infra, topic, event) -> evref.set(event));
93 client = new TopicSinkClient(TOPIC);
96 assertEquals("100", evref.get());
100 public void testTopicSinkClient() {
101 // unknown topic -> should throw exception
102 sinks = new LinkedList<>();
103 assertThatThrownBy(() -> new TopicSinkClient2(TOPIC)).isInstanceOf(TopicSinkClientException.class)
104 .hasMessage("no sinks for topic: my-topic");
108 public void testTopicSinkClient_GetTopic() throws TopicSinkClientException {
109 assertEquals(TOPIC, new TopicSinkClient(TopicEndpointManager.getManager().getNoopTopicSink(TOPIC)).getTopic());
110 assertEquals(TOPIC, new TopicSinkClient(TOPIC).getTopic());
112 assertThatThrownBy(() -> new TopicSinkClient((TopicSink) null)).isInstanceOf(IllegalArgumentException.class);
113 assertThatThrownBy(() -> new TopicSinkClient("blah")).isInstanceOf(TopicSinkClientException.class)
114 .hasMessage("no sinks for topic: blah");
118 public void testSend() {
119 client.send(Arrays.asList("abc", "def"));
120 verify(sink).send("['abc','def']".replace('\'', '"'));
123 when(sink.send(anyString())).thenReturn(false);
124 assertFalse(client.send("ghi"));
126 // sink send throws an exception
127 final RuntimeException ex = new RuntimeException("expected exception");
128 when(sink.send(anyString())).thenThrow(ex);
129 assertFalse(client.send("jkl"));
133 * TopicSinkClient with some overrides.
135 private class TopicSinkClient2 extends TopicSinkClient {
137 public TopicSinkClient2(final String topic) throws TopicSinkClientException {
142 protected List<TopicSink> getTopicSinks(final String topic) {