fd8f3a96475227b1227532020c9aea1dd81e83f7
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertSame;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31
32 import java.util.Arrays;
33 import java.util.Collections;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
37 import org.onap.policy.common.endpoints.event.comm.TopicListener;
38
39 public abstract class NoopTopicEndpointTest<F extends NoopTopicFactory<T>, T extends NoopTopicEndpoint>
40     extends TopicTestBase {
41
42     protected final F factory;
43     protected T endpoint;
44
45     public NoopTopicEndpointTest(F factory) {
46         this.factory = factory;
47     }
48
49     protected abstract boolean io(String message);
50
51     @BeforeEach
52     @Override
53     public void setUp() {
54         super.setUp();
55         this.endpoint = this.factory.build(servers, MY_TOPIC);
56     }
57
58     @Test
59     void testIo() {
60         TopicListener listener = mock(TopicListener.class);
61         this.endpoint.register(listener);
62         this.endpoint.start();
63
64         assertTrue(io(MY_MESSAGE));
65         assertSame(MY_MESSAGE, this.endpoint.getRecentEvents()[0]);
66         assertEquals(Collections.singletonList(MY_MESSAGE), Arrays.asList(this.endpoint.getRecentEvents()));
67         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
68
69         this.endpoint.unregister(listener);
70     }
71
72     @Test
73     void testIoNullMessage() {
74         assertThatThrownBy(() -> io(null)).isInstanceOf(IllegalArgumentException.class);
75     }
76
77     @Test
78     void testIoEmptyMessage() {
79         assertThatThrownBy(() -> io("")).isInstanceOf(IllegalArgumentException.class);
80     }
81
82     @Test
83     void testOfferNotStarted() {
84         assertThatThrownBy(() -> io(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
85     }
86
87     @Test
88     void testGetTopicCommInfrastructure() {
89         assertEquals(CommInfrastructure.NOOP, this.endpoint.getTopicCommInfrastructure());
90     }
91
92     @Test
93     void testStart_testStop_testShutdown() {
94         this.endpoint.start();
95         assertTrue(this.endpoint.isAlive());
96
97         // start again
98         this.endpoint.start();
99         assertTrue(this.endpoint.isAlive());
100
101         // stop
102         this.endpoint.stop();
103         assertFalse(this.endpoint.isAlive());
104
105         // re-start again
106         this.endpoint.start();
107         assertTrue(this.endpoint.isAlive());
108
109         // shutdown
110         this.endpoint.shutdown();
111         assertFalse(this.endpoint.isAlive());
112     }
113
114     @Test
115     void testStart_Locked() {
116         this.endpoint.lock();
117         assertThatThrownBy(() -> this.endpoint.start()).isInstanceOf(IllegalStateException.class);
118     }
119
120 }