From bc6990f795e5ef222dfb7a9a12fe896be578e88b Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 26 Sep 2018 15:10:52 -0400 Subject: Added more coverage to policy-endpoints internal Fixed checkstyle error. Change-Id: Ie6a85633bab098a30ccb2c1c309e1640aa1c8131 Issue-ID: POLICY-1148 Signed-off-by: Jim Hahn --- .../comm/bus/internal/InlineBusTopicSinkTest.java | 224 +++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSinkTest.java (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSinkTest.java') diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSinkTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSinkTest.java new file mode 100644 index 00000000..69ad43d9 --- /dev/null +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSinkTest.java @@ -0,0 +1,224 @@ +/* + * ============LICENSE_START======================================================= + * policy-endpoints + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.endpoints.event.comm.bus.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.event.comm.TopicListener; +import org.onap.policy.common.endpoints.event.comm.bus.BusTopicTestBase; + +public class InlineBusTopicSinkTest extends BusTopicTestBase { + + private InlineBusTopicSinkImpl sink; + + /** + * Creates the object to be tested. + */ + @Before + public void setUp() { + super.setUp(); + + sink = new InlineBusTopicSinkImpl(makeBuilder().build()); + } + + @After + public void tearDown() { + sink.shutdown(); + } + + @Test + public void testInlineBusTopicSinkImpl() { + // verify that different wrappers can be built + sink = new InlineBusTopicSinkImpl(makeBuilder().build()); + assertEquals(MY_PARTITION, sink.getPartitionKey()); + + sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build()); + assertNotNull(sink.getPartitionKey()); + } + + @Test + public void testStart() { + assertTrue(sink.start()); + assertEquals(1, sink.initCount); + + // re-start, init() should not be invoked again + assertTrue(sink.start()); + assertEquals(1, sink.initCount); + } + + @Test(expected = IllegalStateException.class) + public void testStart_Locked() { + sink.lock(); + sink.start(); + } + + @Test + public void testStop() { + BusPublisher pub = mock(BusPublisher.class); + sink.publisher = pub; + + assertTrue(sink.stop()); + verify(pub).close(); + + // stop again, shouldn't not invoke close() again + assertFalse(sink.stop()); + verify(pub).close(); + + // publisher throws exception + sink = new InlineBusTopicSinkImpl(makeBuilder().build()); + sink.publisher = pub; + doThrow(new RuntimeException(EXPECTED)).when(pub).close(); + assertTrue(sink.stop()); + } + + @Test + public void testSend() { + sink.start(); + BusPublisher pub = mock(BusPublisher.class); + sink.publisher = pub; + + TopicListener listener = mock(TopicListener.class); + sink.register(listener); + + assertTrue(sink.send(MY_MESSAGE)); + + verify(pub).send(MY_PARTITION, MY_MESSAGE); + verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE); + assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(sink.getRecentEvents())); + + // arrange for send to throw an exception + when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED)); + + assertFalse(sink.send(MY_MESSAGE)); + + // no more event deliveries + verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE); + } + + @Test(expected = IllegalArgumentException.class) + public void testSend_NullMessage() { + sink.start(); + BusPublisher pub = mock(BusPublisher.class); + sink.publisher = pub; + + sink.send(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testSend_EmptyMessage() { + sink.start(); + BusPublisher pub = mock(BusPublisher.class); + sink.publisher = pub; + + sink.send(""); + } + + @Test(expected = IllegalStateException.class) + public void testSend_NotStarted() { + BusPublisher pub = mock(BusPublisher.class); + sink.publisher = pub; + + sink.send(MY_MESSAGE); + } + + @Test + public void testSetPartitionKey_getPartitionKey() { + assertEquals(MY_PARTITION, sink.getPartitionKey()); + + sink.setPartitionKey("part-B"); + assertEquals("part-B", sink.getPartitionKey()); + } + + @Test + public void testShutdown() { + BusPublisher pub = mock(BusPublisher.class); + sink.publisher = pub; + + sink.shutdown(); + verify(pub).close(); + } + + @Test + public void testAnyNullOrEmpty() { + assertFalse(sink.anyNullOrEmpty()); + assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B")); + + assertTrue(sink.anyNullOrEmpty(null, "any-first-null")); + assertTrue(sink.anyNullOrEmpty("any-middle-null", null, "any-middle-null-B")); + assertTrue(sink.anyNullOrEmpty("any-last-null", null)); + assertTrue(sink.anyNullOrEmpty("any-empty", "")); + } + + @Test + public void testAllNullOrEmpty() { + assertTrue(sink.allNullOrEmpty()); + assertTrue(sink.allNullOrEmpty("")); + assertTrue(sink.allNullOrEmpty(null, "")); + + assertFalse(sink.allNullOrEmpty("all-ok-only-one")); + assertFalse(sink.allNullOrEmpty("all-ok-one", "all-ok-two")); + assertFalse(sink.allNullOrEmpty("all-ok-null", null)); + assertFalse(sink.allNullOrEmpty("", "all-ok-empty")); + assertFalse(sink.allNullOrEmpty("", "all-one-ok", null)); + } + + @Test + public void testToString() { + assertTrue(sink.toString().startsWith("InlineBusTopicSink [")); + } + + /** + * Implementation of InlineBusTopicSink that tracks the number of times that init() is + * invoked. + */ + private static class InlineBusTopicSinkImpl extends InlineBusTopicSink { + + private int initCount = 0; + + public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) { + super(busTopicParams); + } + + @Override + public CommInfrastructure getTopicCommInfrastructure() { + return CommInfrastructure.NOOP; + } + + @Override + public void init() { + ++initCount; + } + + } +} -- cgit 1.2.3-korg