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