2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus;
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;
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;
39 public abstract class NoopTopicEndpointTest<F extends NoopTopicFactory<T>, T extends NoopTopicEndpoint>
40 extends TopicTestBase {
42 protected final F factory;
45 public NoopTopicEndpointTest(F factory) {
46 this.factory = factory;
49 protected abstract boolean io(String message);
55 this.endpoint = this.factory.build(servers, MY_TOPIC);
60 TopicListener listener = mock(TopicListener.class);
61 this.endpoint.register(listener);
62 this.endpoint.start();
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);
69 this.endpoint.unregister(listener);
73 void testIoNullMessage() {
74 assertThatThrownBy(() -> io(null)).isInstanceOf(IllegalArgumentException.class);
78 void testIoEmptyMessage() {
79 assertThatThrownBy(() -> io("")).isInstanceOf(IllegalArgumentException.class);
83 void testOfferNotStarted() {
84 assertThatThrownBy(() -> io(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
88 void testGetTopicCommInfrastructure() {
89 assertEquals(CommInfrastructure.NOOP, this.endpoint.getTopicCommInfrastructure());
93 void testStart_testStop_testShutdown() {
94 this.endpoint.start();
95 assertTrue(this.endpoint.isAlive());
98 this.endpoint.start();
99 assertTrue(this.endpoint.isAlive());
102 this.endpoint.stop();
103 assertFalse(this.endpoint.isAlive());
106 this.endpoint.start();
107 assertTrue(this.endpoint.isAlive());
110 this.endpoint.shutdown();
111 assertFalse(this.endpoint.isAlive());
115 void testStart_Locked() {
116 this.endpoint.lock();
117 assertThatThrownBy(() -> this.endpoint.start()).isInstanceOf(IllegalStateException.class);