725c0418ae82711da866c70e06214e39a4b4787b
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.client;
23
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;
33
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;
39
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;
48
49 public class TopicSinkClientTest {
50     private static final String SINK_FIELD_NAME = "sink";
51     private static final String TOPIC = "my-topic";
52
53     private TopicSinkClient client;
54     private TopicSink sink;
55     private List<TopicSink> sinks;
56
57     /**
58      * Creates mocks and an initial client object.
59      *
60      * @throws Exception if an error occurs
61      */
62     @Before
63     public void setUp() throws Exception {
64         sink = mock(TopicSink.class);
65         when(sink.send(anyString())).thenReturn(true);
66
67         sinks = Arrays.asList(sink, null);
68
69         client = new TopicSinkClient2(TOPIC);
70     }
71
72     @AfterClass
73     public static void tearDown() throws Exception {
74         // clear all topics after the tests
75         TopicEndpoint.manager.shutdown();
76     }
77
78     /**
79      * Uses a real NO-OP topic sink.
80      */
81     @Test
82     public void testGetTopicSinks() throws Exception {
83         // clear all topics and then configure one topic
84         TopicEndpoint.manager.shutdown();
85
86         final Properties props = new Properties();
87         props.setProperty("noop.sink.topics", TOPIC);
88         TopicEndpoint.manager.addTopicSinks(props);
89
90         sink = TopicEndpoint.manager.getNoopTopicSink(TOPIC);
91         assertNotNull(sink);
92
93         final AtomicReference<String> evref = new AtomicReference<>(null);
94
95         sink.register(new TopicListener() {
96             @Override
97             public void onTopicEvent(final CommInfrastructure infra, final String topic, final String event) {
98                 evref.set(event);
99             }
100         });
101
102         sink.start();
103
104         client = new TopicSinkClient(TOPIC);
105         client.send(100);
106
107         assertEquals("100", evref.get());
108     }
109
110     @Test
111     public void testTopicSinkClient_testGetTopic() {
112         assertEquals(TOPIC, client.getTopic());
113         assertSame(sink, Whitebox.getInternalState(client, SINK_FIELD_NAME));
114
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");
119     }
120
121     @Test
122     public void testSend() throws Exception {
123         client.send(Arrays.asList("abc", "def"));
124         verify(sink).send("['abc','def']".replace('\'', '"'));
125
126         // sink send fails
127         when(sink.send(anyString())).thenReturn(false);
128         assertFalse(client.send("ghi"));
129
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"));
134     }
135
136     /**
137      * TopicSinkClient with some overrides.
138      */
139     private class TopicSinkClient2 extends TopicSinkClient {
140
141         public TopicSinkClient2(final String topic) throws TopicSinkClientException {
142             super(topic);
143         }
144
145         @Override
146         protected List<TopicSink> getTopicSinks(final String topic) {
147             return sinks;
148         }
149     }
150 }