8a5b7b2e641cfa00c31ffaab11dd7c0c23876069
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2018 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.common.endpoints.event.comm.bus;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28
29 import java.util.Arrays;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
33 import org.onap.policy.common.endpoints.event.comm.TopicListener;
34
35 public class NoopTopicSinkTest extends TopicTestBase {
36
37     private NoopTopicSink sink;
38
39     /**
40      * Creates the object to be tested.
41      */
42     @Before
43     public void setUp() {
44         super.setUp();
45
46         sink = new NoopTopicSink(servers, MY_TOPIC);
47     }
48
49     @Test
50     public void testToString() {
51         assertTrue(sink.toString().startsWith("NoopTopicSink ["));
52     }
53
54     @Test
55     public void testSend() {
56         TopicListener listener = mock(TopicListener.class);
57         sink.register(listener);
58         sink.start();
59
60         assertTrue(sink.send(MY_MESSAGE));
61
62         assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(sink.getRecentEvents()));
63         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
64
65         // generate exception during broadcast
66         sink = new NoopTopicSink(servers, MY_TOPIC) {
67             @Override
68             protected boolean broadcast(String message) {
69                 throw new RuntimeException(EXPECTED);
70             }
71
72         };
73
74         sink.start();
75         assertFalse(sink.send(MY_MESSAGE));
76     }
77
78     @Test(expected = IllegalArgumentException.class)
79     public void testSend_NullMessage() {
80         sink.send(null);
81     }
82
83     @Test(expected = IllegalArgumentException.class)
84     public void testSend_EmptyMessage() {
85         sink.send("");
86     }
87
88     @Test(expected = IllegalStateException.class)
89     public void testSend_NotStarted() {
90         sink.send(MY_MESSAGE);
91     }
92
93     @Test
94     public void testGetTopicCommInfrastructure() {
95         assertEquals(CommInfrastructure.NOOP, sink.getTopicCommInfrastructure());
96     }
97
98     @Test
99     public void testStart_testStop_testShutdown() {
100         sink.start();
101         assertTrue(sink.isAlive());
102
103         // start again
104         sink.start();
105         assertTrue(sink.isAlive());
106
107         // stop
108         sink.stop();
109         assertFalse(sink.isAlive());
110
111         // re-start again
112         sink.start();
113         assertTrue(sink.isAlive());
114
115         // shutdown
116         sink.shutdown();
117         assertFalse(sink.isAlive());
118     }
119
120     @Test(expected = IllegalStateException.class)
121     public void testStart_Locked() {
122         sink.lock();
123         sink.start();
124     }
125
126 }