From b7239e0e2859d998d5f8448879409eb30c3bc658 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 30 Sep 2020 14:32:56 -0400 Subject: Fix new sonars in drools-pdp Addressed the following sonar issues: - extract common test method - too many assertions in test method - don't always return the same value Issue-ID: POLICY-2616 Change-Id: Iab836cdcd667b35e2db9db7d6ffc7896df33e1c8 Signed-off-by: Jim Hahn --- .../drools/pooling/PoolingManagerImplTest.java | 105 +++++++++------------ 1 file changed, 46 insertions(+), 59 deletions(-) (limited to 'feature-pooling-dmaap/src/test/java/org') diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java index bd3d90b6..2a0066b7 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java @@ -640,27 +640,17 @@ public class PoolingManagerImplTest { @Test public void testBeforeInsert_NoIntercept() throws Exception { - startMgr(); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateUnhandled(CommInfrastructure.UEB); } @Test public void testHandleExternalCommInfrastructureStringStringString_NullReqId() throws Exception { - startMgr(); - - when(extractors.extract(any())).thenReturn(null); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateHandleReqId(null); } @Test public void testHandleExternalCommInfrastructureStringStringString_EmptyReqId() throws Exception { - startMgr(); - - when(extractors.extract(any())).thenReturn(""); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateHandleReqId(""); } @Test @@ -672,50 +662,28 @@ public class PoolingManagerImplTest { @Test public void testHandleExternalCommInfrastructureStringStringString() throws Exception { - startMgr(); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateUnhandled(CommInfrastructure.UEB); } @Test public void testHandleExternalForward_NoAssignments() throws Exception { - startMgr(); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateUnhandled(CommInfrastructure.UEB); } @Test public void testHandleExternalForward() throws Exception { - startMgr(); - - // route the message to this host - mgr.startDistributing(makeAssignments(true)); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateNoForward(); } @Test public void testHandleEvent_NullTarget() throws Exception { - startMgr(); - // buckets have null targets - mgr.startDistributing(new BucketAssignments(new String[] {null, null})); - - assertTrue(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); - - verify(dmaap, times(START_PUB)).publish(any()); + validateHandled(new BucketAssignments(new String[] {null, null}), START_PUB); } @Test public void testHandleEvent_SameHost() throws Exception { - startMgr(); - - // route the message to this host - mgr.startDistributing(makeAssignments(true)); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); - - verify(dmaap, times(START_PUB)).publish(any()); + validateNoForward(); } @Test @@ -736,14 +704,7 @@ public class PoolingManagerImplTest { @Test public void testHandleEvent_DiffHost_Forward() throws Exception { - startMgr(); - - // route the message to the *OTHER* host - mgr.startDistributing(makeAssignments(false)); - - assertTrue(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); - - verify(dmaap, times(START_PUB + 1)).publish(any()); + validateHandled(makeAssignments(false), START_PUB + 1); } @Test @@ -755,11 +716,7 @@ public class PoolingManagerImplTest { @Test public void testExtractRequestId_NullReqId() throws Exception { - startMgr(); - - when(extractors.extract(any())).thenReturn(null); - - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + validateHandleReqId(null); } @Test @@ -1009,12 +966,7 @@ public class PoolingManagerImplTest { @Test public void testStartDistributing() throws Exception { - startMgr(); - - // route the message to this host - mgr.startDistributing(makeAssignments(true)); - assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); - verify(dmaap, times(START_PUB)).publish(any()); + validateNoForward(); // null assignments should cause message to be processed locally @@ -1133,6 +1085,41 @@ public class PoolingManagerImplTest { assertEquals(1, latch.getCount()); } + private void validateHandleReqId(String requestId) throws PoolingFeatureException { + startMgr(); + + when(extractors.extract(any())).thenReturn(requestId); + + assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + } + + private void validateNoForward() throws PoolingFeatureException { + startMgr(); + + // route the message to this host + mgr.startDistributing(makeAssignments(true)); + + assertFalse(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + + verify(dmaap, times(START_PUB)).publish(any()); + } + + private void validateHandled(BucketAssignments assignments, int publishCount) throws PoolingFeatureException { + startMgr(); + + // route the message to the *OTHER* host + mgr.startDistributing(assignments); + + assertTrue(mgr.beforeInsert(CommInfrastructure.UEB, TOPIC2, THE_EVENT, DECODED_EVENT)); + + verify(dmaap, times(publishCount)).publish(any()); + } + + private void validateUnhandled(CommInfrastructure infra) throws PoolingFeatureException { + startMgr(); + assertFalse(mgr.beforeInsert(infra, TOPIC2, THE_EVENT, DECODED_EVENT)); + } + /** * Configure the mock controller to act like a real controller, invoking beforeOffer * and then beforeInsert, so we can make sure they pass through. We'll keep count to -- cgit 1.2.3-korg