2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.junit.Assert.assertSame;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.Arrays;
35 import java.util.LinkedList;
36 import java.util.List;
37 import java.util.Properties;
38 import java.util.concurrent.atomic.AtomicReference;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.internal.util.reflection.Whitebox;
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.TopicListener;
47 import org.onap.policy.common.endpoints.event.comm.TopicSink;
49 public class TopicSinkClientTest {
50 private static final String SINK_FIELD_NAME = "sink";
51 private static final String TOPIC = "my-topic";
53 private TopicSinkClient client;
54 private TopicSink sink;
55 private List<TopicSink> sinks;
58 * Creates mocks and an initial client object.
60 * @throws Exception if an error occurs
63 public void setUp() throws Exception {
64 sink = mock(TopicSink.class);
65 when(sink.send(anyString())).thenReturn(true);
67 sinks = Arrays.asList(sink, null);
69 client = new TopicSinkClient2(TOPIC);
73 public static void tearDown() throws Exception {
74 // clear all topics after the tests
75 TopicEndpoint.manager.shutdown();
79 * Uses a real NO-OP topic sink.
82 public void testGetTopicSinks() throws Exception {
83 // clear all topics and then configure one topic
84 TopicEndpoint.manager.shutdown();
86 final Properties props = new Properties();
87 props.setProperty("noop.sink.topics", TOPIC);
88 TopicEndpoint.manager.addTopicSinks(props);
90 sink = TopicEndpoint.manager.getNoopTopicSink(TOPIC);
93 final AtomicReference<String> evref = new AtomicReference<>(null);
95 sink.register(new TopicListener() {
97 public void onTopicEvent(final CommInfrastructure infra, final String topic, final String event) {
104 client = new TopicSinkClient(TOPIC);
107 assertEquals("100", evref.get());
111 public void testTopicSinkClient_testGetTopic() {
112 assertEquals(TOPIC, client.getTopic());
113 assertSame(sink, Whitebox.getInternalState(client, SINK_FIELD_NAME));
115 // unknown topic -> should throw exception
116 sinks = new LinkedList<>();
117 assertThatThrownBy(() -> new TopicSinkClient2(TOPIC)).isInstanceOf(TopicSinkClientException.class)
118 .hasMessage("no sinks for topic: my-topic");
122 public void testSend() throws Exception {
123 client.send(Arrays.asList("abc", "def"));
124 verify(sink).send("['abc','def']".replace('\'', '"'));
127 when(sink.send(anyString())).thenReturn(false);
128 assertFalse(client.send("ghi"));
130 // sink send throws an exception
131 final RuntimeException ex = new RuntimeException("expected exception");
132 when(sink.send(anyString())).thenThrow(ex);
133 assertFalse(client.send("jkl"));
137 * TopicSinkClient with some overrides.
139 private class TopicSinkClient2 extends TopicSinkClient {
141 public TopicSinkClient2(final String topic) throws TopicSinkClientException {
146 protected List<TopicSink> getTopicSinks(final String topic) {