22823bfbb78669362bff487734339d15fcf30d8a
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / PdpClientTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
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;
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.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;
45
46 public class PdpClientTest {
47     private static final String SINK_FIELD_NAME = "sink";
48     private static final String TOPIC = "my-topic";
49
50     private PdpClient client;
51     private TopicSink sink;
52     private List<TopicSink> sinks;
53
54     /**
55      * Creates mocks and an initial client object.
56      *
57      * @throws Exception if an error occurs
58      */
59     @Before
60     public void setUp() throws Exception {
61         sink = mock(TopicSink.class);
62         when(sink.send(anyString())).thenReturn(true);
63
64         sinks = Arrays.asList(sink, null);
65
66         client = new PdpClient2(TOPIC);
67     }
68
69     /**
70      * Uses a real NO-OP topic sink.
71      */
72     @Test
73     public void testGetTopicSinks() throws Exception {
74         // clear all topics and then configure one topic
75         TopicEndpoint.manager.shutdown();
76
77         Properties props = new Properties();
78         props.setProperty("noop.sink.topics", TOPIC);
79         TopicEndpoint.manager.addTopicSinks(props);
80
81         sink = TopicEndpoint.manager.getNoopTopicSink(TOPIC);
82         assertNotNull(sink);
83
84         AtomicReference<String> evref = new AtomicReference<>(null);
85
86         sink.register(new TopicListener() {
87             @Override
88             public void onTopicEvent(CommInfrastructure infra, String topic, String event) {
89                 evref.set(event);
90             }
91         });
92
93         sink.start();
94
95         client = new PdpClient(TOPIC);
96         client.send(100);
97
98         assertEquals("100", evref.get());
99     }
100
101     @Test
102     public void testPdpClient_testGetTopic() {
103         assertEquals(TOPIC, client.getTopic());
104         assertSame(sink, Whitebox.getInternalState(client, SINK_FIELD_NAME));
105
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");
110     }
111
112     @Test
113     public void testSend() throws Exception {
114         client.send(Arrays.asList("abc", "def"));
115         verify(sink).send("['abc','def']".replace('\'', '"'));
116
117         // sink send fails
118         when(sink.send(anyString())).thenReturn(false);
119         assertFalse(client.send("ghi"));
120
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"));
125     }
126
127     /**
128      * PdpClient with some overrides.
129      */
130     private class PdpClient2 extends PdpClient {
131
132         public PdpClient2(String topic) throws PdpClientException {
133             super(topic);
134         }
135
136         @Override
137         protected List<TopicSink> getTopicSinks(String topic) {
138             return sinks;
139         }
140     }
141 }