* ONAP
* ================================================================================
* Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.onap.policy.common.endpoints.event.comm.TopicListener;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
-public class DmaapManagerTest {
+class TopicMessageManagerTest {
private static final String EXPECTED = "expected";
private static final String MY_TOPIC = "my.topic";
private boolean gotSources;
private TopicSink sink;
private boolean gotSinks;
- private DmaapManager mgr;
+ private TopicMessageManager mgr;
/**
* Setup.
*
* @throws Exception throws an exception
*/
- @Before
+ @BeforeEach
public void setUp() throws Exception {
listener = mock(TopicListener.class);
source = mock(TopicSource.class);
when(sink.getTopic()).thenReturn(MY_TOPIC);
when(sink.send(any())).thenReturn(true);
- mgr = new DmaapManagerImpl(MY_TOPIC);
+ mgr = new TopicMessageManagerImpl(MY_TOPIC);
}
@Test
- public void testDmaapManager() {
+ void testTopicMessageManager() {
// verify that the init methods were called
assertTrue(gotSources);
assertTrue(gotSinks);
}
- @Test(expected = PoolingFeatureException.class)
- public void testDmaapManager_PoolingEx() throws PoolingFeatureException {
+ @Test
+ void testTopicMessageManager_PoolingEx() {
// force error by having no topics match
when(source.getTopic()).thenReturn("");
- new DmaapManagerImpl(MY_TOPIC);
+ assertThrows(PoolingFeatureException.class, () -> new TopicMessageManagerImpl(MY_TOPIC));
}
- @Test(expected = PoolingFeatureException.class)
- public void testDmaapManager_IllegalArgEx() throws PoolingFeatureException {
+ @Test
+ void testTopicMessageManager_IllegalArgEx() {
// force error
- new DmaapManagerImpl(MY_TOPIC) {
+ assertThrows(PoolingFeatureException.class, () ->
+ new TopicMessageManagerImpl(MY_TOPIC) {
@Override
protected List<TopicSource> getTopicSources() {
throw new IllegalArgumentException(EXPECTED);
}
- };
+ });
}
@Test
- public void testGetTopic() {
+ void testGetTopic() {
assertEquals(MY_TOPIC, mgr.getTopic());
}
- @Test(expected = PoolingFeatureException.class)
- public void testFindTopicSource_NotFound() throws PoolingFeatureException {
+ @Test
+ void testFindTopicSource_NotFound() {
// one item in list, and its topic doesn't match
- new DmaapManagerImpl(MY_TOPIC) {
+ assertThrows(PoolingFeatureException.class, () -> new TopicMessageManagerImpl(MY_TOPIC) {
@Override
protected List<TopicSource> getTopicSources() {
- return Arrays.asList(mock(TopicSource.class));
+ return Collections.singletonList(mock(TopicSource.class));
}
- };
+ });
}
- @Test(expected = PoolingFeatureException.class)
- public void testFindTopicSource_EmptyList() throws PoolingFeatureException {
+ @Test
+ void testFindTopicSource_EmptyList() {
// empty list
- new DmaapManagerImpl(MY_TOPIC) {
+ assertThrows(PoolingFeatureException.class, () -> new TopicMessageManagerImpl(MY_TOPIC) {
@Override
protected List<TopicSource> getTopicSources() {
return Collections.emptyList();
}
- };
+ });
}
- @Test(expected = PoolingFeatureException.class)
- public void testFindTopicSink_NotFound() throws PoolingFeatureException {
+ @Test
+ void testFindTopicSink_NotFound() {
// one item in list, and its topic doesn't match
- new DmaapManagerImpl(MY_TOPIC) {
+ assertThrows(PoolingFeatureException.class, () -> new TopicMessageManagerImpl(MY_TOPIC) {
@Override
protected List<TopicSink> getTopicSinks() {
- return Arrays.asList(mock(TopicSink.class));
+ return Collections.singletonList(mock(TopicSink.class));
}
- };
+ });
}
- @Test(expected = PoolingFeatureException.class)
- public void testFindTopicSink_EmptyList() throws PoolingFeatureException {
+ @Test
+ void testFindTopicSink_EmptyList() {
// empty list
- new DmaapManagerImpl(MY_TOPIC) {
+ assertThrows(PoolingFeatureException.class, () -> new TopicMessageManagerImpl(MY_TOPIC) {
@Override
protected List<TopicSink> getTopicSinks() {
return Collections.emptyList();
}
- };
+ });
}
@Test
- public void testStartPublisher() throws PoolingFeatureException {
+ void testStartPublisher() throws PoolingFeatureException {
mgr.startPublisher();
}
@Test
- public void testStopPublisher() throws PoolingFeatureException {
+ void testStopPublisher() {
// not publishing yet, so stopping should have no effect
mgr.stopPublisher(0);
}
@Test
- public void testStopPublisher_WithDelay() throws PoolingFeatureException {
+ void testStopPublisher_WithDelay() {
mgr.startPublisher();
}
@Test
- public void testStopPublisher_WithDelayInterrupted() throws Exception {
+ void testStopPublisher_WithDelayInterrupted() throws Exception {
mgr.startPublisher();
}
@Test
- public void testStartConsumer() {
+ void testStartConsumer() {
// not started yet
verify(source, never()).register(any());
}
@Test
- public void testStopConsumer() {
+ void testStopConsumer() {
// not consuming yet, so stopping should have no effect
mgr.stopConsumer(listener);
verify(source, never()).unregister(any());
}
@Test
- public void testPublish() throws PoolingFeatureException {
+ void testPublish() throws PoolingFeatureException {
// cannot publish before starting
assertThatThrownBy(() -> mgr.publish(MSG)).as("publish,pre").isInstanceOf(PoolingFeatureException.class);
assertThatThrownBy(() -> mgr.publish(MSG)).as("publish,stopped").isInstanceOf(PoolingFeatureException.class);
}
- @Test(expected = PoolingFeatureException.class)
- public void testPublish_SendFailed() throws PoolingFeatureException {
+ @Test
+ void testPublish_SendFailed() {
mgr.startPublisher();
// arrange for send() to fail
when(sink.send(MSG)).thenReturn(false);
- mgr.publish(MSG);
+ assertThrows(PoolingFeatureException.class, () -> mgr.publish(MSG));
}
- @Test(expected = PoolingFeatureException.class)
- public void testPublish_SendEx() throws PoolingFeatureException {
+ @Test
+ void testPublish_SendEx() {
mgr.startPublisher();
// arrange for send() to throw an exception
doThrow(new IllegalStateException(EXPECTED)).when(sink).send(MSG);
- mgr.publish(MSG);
+ assertThrows(PoolingFeatureException.class, () -> mgr.publish(MSG));
}
/**
* Manager with overrides.
*/
- private class DmaapManagerImpl extends DmaapManager {
+ private class TopicMessageManagerImpl extends TopicMessageManager {
- public DmaapManagerImpl(String topic) throws PoolingFeatureException {
+ public TopicMessageManagerImpl(String topic) throws PoolingFeatureException {
super(topic);
}