0974a0418602d19b4ac16dd2d6660e763ac768ad
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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.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.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29
30 import java.util.Arrays;
31 import java.util.Collections;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
36 import org.onap.policy.common.endpoints.event.comm.TopicListener;
37
38 public abstract class NoopTopicEndpointTest<F extends NoopTopicFactory<T>, T extends NoopTopicEndpoint>
39     extends TopicTestBase {
40
41     protected final F factory;
42     protected T endpoint;
43
44     public NoopTopicEndpointTest(F factory) {
45         this.factory = factory;
46     }
47
48     protected abstract boolean io(String message);
49
50     @Before
51     @Override
52     public void setUp() {
53         super.setUp();
54         this.endpoint = this.factory.build(servers, MY_TOPIC);
55     }
56
57     @Test
58     public void testIo() {
59         TopicListener listener = mock(TopicListener.class);
60         this.endpoint.register(listener);
61         this.endpoint.start();
62
63         assertTrue(io(MY_MESSAGE));
64         assertSame(MY_MESSAGE, this.endpoint.getRecentEvents()[0]);
65         assertEquals(Collections.singletonList(MY_MESSAGE), Arrays.asList(this.endpoint.getRecentEvents()));
66         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
67
68         this.endpoint.unregister(listener);
69     }
70
71     @Test(expected = IllegalArgumentException.class)
72     public void testIoNullMessage() {
73         io(null);
74     }
75
76     @Test(expected = IllegalArgumentException.class)
77     public void testIoEmptyMessage() {
78         io("");
79     }
80
81     @Test(expected = IllegalStateException.class)
82     public void testOfferNotStarted() {
83         io(MY_MESSAGE);
84     }
85
86     @Test
87     public void testGetTopicCommInfrastructure() {
88         assertEquals(CommInfrastructure.NOOP, this.endpoint.getTopicCommInfrastructure());
89     }
90
91     @Test
92     public void testStart_testStop_testShutdown() {
93         this.endpoint.start();
94         assertTrue(this.endpoint.isAlive());
95
96         // start again
97         this.endpoint.start();
98         assertTrue(this.endpoint.isAlive());
99
100         // stop
101         this.endpoint.stop();
102         assertFalse(this.endpoint.isAlive());
103
104         // re-start again
105         this.endpoint.start();
106         assertTrue(this.endpoint.isAlive());
107
108         // shutdown
109         this.endpoint.shutdown();
110         assertFalse(this.endpoint.isAlive());
111     }
112
113     @Test(expected = IllegalStateException.class)
114     public void testStart_Locked() {
115         this.endpoint.lock();
116         this.endpoint.start();
117     }
118
119 }