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