e30f38a76326e90e3f20cd66349fa39449a36c95
[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.mockito.Matchers.anyString;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
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;
43
44 public class TopicSinkClientTest {
45     private static final String TOPIC = "my-topic";
46
47     private TopicSinkClient client;
48     private TopicSink sink;
49     private List<TopicSink> sinks;
50
51     /**
52      * Creates mocks and an initial client object.
53      *
54      * @throws Exception if an error occurs
55      */
56     @Before
57     public void setUp() throws Exception {
58         sink = mock(TopicSink.class);
59         when(sink.send(anyString())).thenReturn(true);
60
61         sinks = Arrays.asList(sink, null);
62
63         client = new TopicSinkClient2(TOPIC);
64
65         Properties props = new Properties();
66         props.setProperty("noop.sink.topics", TOPIC);
67
68         // clear all topics and then configure one topic
69         TopicEndpointManager.getManager().shutdown();
70         TopicEndpointManager.getManager().addTopicSinks(props);
71     }
72
73     @AfterClass
74     public static void tearDown() {
75         // clear all topics after the tests
76         TopicEndpointManager.getManager().shutdown();
77     }
78
79     /**
80      * Uses a real NO-OP topic sink.
81      */
82     @Test
83     public void testGetTopicSinks() throws Exception {
84
85         sink = TopicEndpointManager.getManager().getNoopTopicSink(TOPIC);
86         assertNotNull(sink);
87
88         final AtomicReference<String> evref = new AtomicReference<>(null);
89
90         sink.register((infra, topic, event) -> evref.set(event));
91         sink.start();
92
93         client = new TopicSinkClient(TOPIC);
94         client.send(100);
95
96         assertEquals("100", evref.get());
97     }
98
99     @Test
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");
105     }
106
107     @Test
108     public void testTopicSinkClient_GetTopic() throws TopicSinkClientException {
109         assertEquals(TOPIC, new TopicSinkClient(TopicEndpointManager.getManager().getNoopTopicSink(TOPIC)).getTopic());
110         assertEquals(TOPIC, new TopicSinkClient(TOPIC).getTopic());
111
112         assertThatThrownBy(() -> new TopicSinkClient((TopicSink) null)).isInstanceOf(IllegalArgumentException.class);
113         assertThatThrownBy(() -> new TopicSinkClient("blah")).isInstanceOf(TopicSinkClientException.class)
114                             .hasMessage("no sinks for topic: blah");
115     }
116
117     @Test
118     public void testSend() {
119         client.send(Arrays.asList("abc", "def"));
120         verify(sink).send("['abc','def']".replace('\'', '"'));
121
122         // sink send fails
123         when(sink.send(anyString())).thenReturn(false);
124         assertFalse(client.send("ghi"));
125
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"));
130     }
131
132     /**
133      * TopicSinkClient with some overrides.
134      */
135     private class TopicSinkClient2 extends TopicSinkClient {
136
137         public TopicSinkClient2(final String topic) throws TopicSinkClientException {
138             super(topic);
139         }
140
141         @Override
142         protected List<TopicSink> getTopicSinks(final String topic) {
143             return sinks;
144         }
145     }
146 }