2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.comm;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertSame;
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;
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.Before;
39 import org.junit.Test;
40 import org.mockito.internal.util.reflection.Whitebox;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
43 import org.onap.policy.common.endpoints.event.comm.TopicListener;
44 import org.onap.policy.common.endpoints.event.comm.TopicSink;
46 public class PdpClientTest {
47 private static final String SINK_FIELD_NAME = "sink";
48 private static final String TOPIC = "my-topic";
50 private PdpClient client;
51 private TopicSink sink;
52 private List<TopicSink> sinks;
55 * Creates mocks and an initial client object.
57 * @throws Exception if an error occurs
60 public void setUp() throws Exception {
61 sink = mock(TopicSink.class);
62 when(sink.send(anyString())).thenReturn(true);
64 sinks = Arrays.asList(sink, null);
66 client = new PdpClient2(TOPIC);
70 * Uses a real NO-OP topic sink.
73 public void testGetTopicSinks() throws Exception {
74 // clear all topics and then configure one topic
75 TopicEndpoint.manager.shutdown();
77 Properties props = new Properties();
78 props.setProperty("noop.sink.topics", TOPIC);
79 TopicEndpoint.manager.addTopicSinks(props);
81 sink = TopicEndpoint.manager.getNoopTopicSink(TOPIC);
84 AtomicReference<String> evref = new AtomicReference<>(null);
86 sink.register(new TopicListener() {
88 public void onTopicEvent(CommInfrastructure infra, String topic, String event) {
95 client = new PdpClient(TOPIC);
98 assertEquals("100", evref.get());
102 public void testPdpClient_testGetTopic() {
103 assertEquals(TOPIC, client.getTopic());
104 assertSame(sink, Whitebox.getInternalState(client, SINK_FIELD_NAME));
106 // unknown topic -> should throw exception
107 sinks = new LinkedList<>();
108 assertThatThrownBy(() -> new PdpClient2(TOPIC)).isInstanceOf(PdpClientException.class)
109 .hasMessage("no sinks for topic: my-topic");
113 public void testSend() throws Exception {
114 client.send(Arrays.asList("abc", "def"));
115 verify(sink).send("['abc','def']".replace('\'', '"'));
118 when(sink.send(anyString())).thenReturn(false);
119 assertFalse(client.send("ghi"));
121 // sink send throws an exception
122 RuntimeException ex = new RuntimeException("expected exception");
123 when(sink.send(anyString())).thenThrow(ex);
124 assertFalse(client.send("jkl"));
128 * PdpClient with some overrides.
130 private class PdpClient2 extends PdpClient {
132 public PdpClient2(String topic) throws PdpClientException {
137 protected List<TopicSink> getTopicSinks(String topic) {