From a56d3929f2387252525577fb36f9e03933064b8f Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 3 Apr 2020 09:44:26 -0400 Subject: Address sonar issues in common Addressed the following sonar issues: - missing assertion in junit test case - disable sonars about setAccessible() as it's required for jackson emulation - sleep in junit - don't use wild-cards (e.g., "*") with java.util Pattern - use re2j instead of java.util Pattern - use String methods (e.g., startsWith()) - duplicate method bodies - duplicate code in Coder classes - string concatenation in logger calls - UTF-8 encoding - return primitive instead of boxed primitive - add assertion to tests - renamed support methods from doTestXxx to verifyXxx - cognitive complexity - use AtomicRef instead of volatile - use specific Functionals (e.g., IntConsumer) - function always returns the same value - serializable vs transient Issue-ID: POLICY-2305 Change-Id: I08eb7aa495a80bdc1d26827ba17a7946c83b9828 Signed-off-by: Jim Hahn --- .../event/comm/bus/internal/BusConsumerTest.java | 23 ++++++++++++++-------- .../event/comm/bus/internal/BusPublisherTest.java | 9 ++++++--- .../event/comm/bus/internal/BusTopicBaseTest.java | 5 +++-- .../comm/bus/internal/InlineBusTopicSinkTest.java | 4 +++- .../bus/internal/InlineDmaapTopicSinkTest.java | 8 +++++--- .../comm/bus/internal/InlineUebTopicSinkTest.java | 8 +++++--- .../internal/SingleThreadedBusTopicSourceTest.java | 9 ++++++--- .../SingleThreadedDmaapTopicSourceTest.java | 9 ++++++--- .../internal/SingleThreadedUebTopicSourceTest.java | 6 ++++-- .../event/comm/bus/internal/TopicBaseTest.java | 3 ++- 10 files changed, 55 insertions(+), 29 deletions(-) (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm') diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumerTest.java index 0255c100..5264b2f4 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -65,7 +66,9 @@ public class BusConsumerTest extends TopicTestBase { new CambriaConsumerWrapper(makeBuilder().apiKey(null).apiSecret(null).build()); new CambriaConsumerWrapper(makeBuilder().userName(null).build()); new CambriaConsumerWrapper(makeBuilder().password(null).build()); - new CambriaConsumerWrapper(makeBuilder().userName(null).password(null).build()); + + assertThatCode(() -> new CambriaConsumerWrapper(makeBuilder().userName(null).password(null).build())) + .doesNotThrowAnyException(); } @Test @@ -101,7 +104,8 @@ public class BusConsumerTest extends TopicTestBase { // set filter several times to cause different branches of close() to be executed for (int count = 0; count < 3; ++count) { cons.close(); - cons.setFilter("close=" + count); + final int count2 = count; + assertThatCode(() -> cons.setFilter("close=" + count2)).doesNotThrowAnyException(); } } @@ -110,7 +114,8 @@ public class BusConsumerTest extends TopicTestBase { // set filter several times to cause different branches to be executed CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build()); for (int count = 0; count < 3; ++count) { - cons.setFilter("set-filter=" + count); + final int count2 = count; + assertThatCode(() -> cons.setFilter("set-filter=" + count2)).doesNotThrowAnyException(); } } @@ -122,7 +127,7 @@ public class BusConsumerTest extends TopicTestBase { @Test public void testDmaapConsumerWrapper() throws Exception { // verify that different wrappers can be built - new DmaapAafConsumerWrapper(makeBuilder().build()); + assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().build())).doesNotThrowAnyException(); } @Test(expected = IllegalArgumentException.class) @@ -167,7 +172,7 @@ public class BusConsumerTest extends TopicTestBase { @Test public void testDmaapConsumerWrapperClose() throws Exception { - new DmaapAafConsumerWrapper(makeBuilder().build()).close(); + assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().build()).close()).doesNotThrowAnyException(); } @Test @@ -179,7 +184,8 @@ public class BusConsumerTest extends TopicTestBase { public void testDmaapAafConsumerWrapper() throws Exception { // verify that different wrappers can be built new DmaapAafConsumerWrapper(makeBuilder().useHttps(true).build()); - new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build()); + assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build())) + .doesNotThrowAnyException(); } @Test(expected = IllegalArgumentException.class) @@ -207,7 +213,8 @@ public class BusConsumerTest extends TopicTestBase { addProps.put(ROUTE_PROP, MY_ROUTE); new DmaapDmeConsumerWrapper(makeBuilder().build()); - new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build()); + assertThatCode(() -> new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build())) + .doesNotThrowAnyException(); } @Test(expected = IllegalArgumentException.class) diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisherTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisherTest.java index 5a933e9b..513673bd 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisherTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisherTest.java @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; @@ -66,7 +67,8 @@ public class BusPublisherTest extends TopicTestBase { new CambriaPublisherWrapper(makeBuilder().apiKey(null).apiSecret(null).build()); new CambriaPublisherWrapper(makeBuilder().userName(null).build()); new CambriaPublisherWrapper(makeBuilder().password(null).build()); - new CambriaPublisherWrapper(makeBuilder().userName(null).password(null).build()); + assertThatCode(() -> new CambriaPublisherWrapper(makeBuilder().userName(null).password(null).build())) + .doesNotThrowAnyException(); } @Test @@ -109,7 +111,8 @@ public class BusPublisherTest extends TopicTestBase { // verify with different constructor arguments new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true); new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, false); - new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, MY_TOPIC, MY_USERNAME, MY_PASS, true) {}; + assertThatCode(() -> new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, MY_TOPIC, MY_USERNAME, + MY_PASS, true) {}).doesNotThrowAnyException(); } @Test(expected = IllegalArgumentException.class) @@ -188,7 +191,7 @@ public class BusPublisherTest extends TopicTestBase { new DmaapDmePublisherWrapper(makeBuilder().partner(null).build()); addProps.put("null-value", null); - new DmaapDmePublisherWrapper(makeBuilder().build()); + assertThatCode(() -> new DmaapDmePublisherWrapper(makeBuilder().build())).doesNotThrowAnyException(); } @Test(expected = IllegalArgumentException.class) diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicBaseTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicBaseTest.java index 01028045..0a2a5d34 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicBaseTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicBaseTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -53,7 +54,7 @@ public class BusTopicBaseTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(base, BusTopicBaseTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(base, BusTopicBaseTest.class)).doesNotThrowAnyException(); } @Test 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 index 634ee762..e6eec799 100644 --- 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 @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -61,7 +62,8 @@ public class InlineBusTopicSinkTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class)) + .doesNotThrowAnyException(); } @Test diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSinkTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSinkTest.java index d9bc990b..239bf33a 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSinkTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineDmaapTopicSinkTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -51,7 +52,8 @@ public class InlineDmaapTopicSinkTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(sink, InlineDmaapTopicSinkTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineDmaapTopicSinkTest.class)) + .doesNotThrowAnyException(); } @Test @@ -70,7 +72,7 @@ public class InlineDmaapTopicSinkTest extends TopicTestBase { sink = new InlineDmaapTopicSink(makeBuilder().environment(null).aftEnvironment(null).latitude(null) .longitude(null).partner(null).build()); sink.init(); - sink.shutdown(); + assertThatCode(() -> sink.shutdown()).doesNotThrowAnyException(); } @Test diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSinkTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSinkTest.java index a45504f2..674f379f 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSinkTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineUebTopicSinkTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -51,7 +52,8 @@ public class InlineUebTopicSinkTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(sink, InlineUebTopicSinkTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineUebTopicSinkTest.class)) + .doesNotThrowAnyException(); } @Test @@ -61,7 +63,7 @@ public class InlineUebTopicSinkTest extends TopicTestBase { @Test public void testInit() { - sink.init(); + assertThatCode(() -> sink.init()).doesNotThrowAnyException(); } @Test diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSourceTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSourceTest.java index 16d74df2..1e95924d 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSourceTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSourceTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -72,7 +73,8 @@ public class SingleThreadedBusTopicSourceTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(source, SingleThreadedBusTopicSourceTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(source, SingleThreadedBusTopicSourceTest.class)) + .doesNotThrowAnyException(); } @Test @@ -163,7 +165,8 @@ public class SingleThreadedBusTopicSourceTest extends TopicTestBase { new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).build()); new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerInstance(null).build()); new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchTimeout(-1).build()); - new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchLimit(-1).build()); + assertThatCode(() -> new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchLimit(-1).build())) + .doesNotThrowAnyException(); } @Test diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSourceTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSourceTest.java index b7faf161..c7d30025 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSourceTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedDmaapTopicSourceTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -53,7 +54,8 @@ public class SingleThreadedDmaapTopicSourceTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(source, SingleThreadedDmaapTopicSourceTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(source, SingleThreadedDmaapTopicSourceTest.class)) + .doesNotThrowAnyException(); } @Test @@ -75,9 +77,10 @@ public class SingleThreadedDmaapTopicSourceTest extends TopicTestBase { @Test public void testInit() { // verify with different parameters - new SingleThreadedDmaapTopicSource(makeBuilder().userName(null).build()).shutdown(); new SingleThreadedDmaapTopicSource(makeBuilder().environment(null).aftEnvironment(null).latitude(null) .longitude(null).partner(null).build()).shutdown(); + assertThatCode(() -> new SingleThreadedDmaapTopicSource(makeBuilder().userName(null).build()).shutdown()) + .doesNotThrowAnyException(); } @Test(expected = IllegalArgumentException.class) diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSourceTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSourceTest.java index 2ff353b8..6536d0e8 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSourceTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedUebTopicSourceTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -51,7 +52,8 @@ public class SingleThreadedUebTopicSourceTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(source, SingleThreadedUebTopicSourceTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(source, SingleThreadedUebTopicSourceTest.class)) + .doesNotThrowAnyException(); } @Test diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/TopicBaseTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/TopicBaseTest.java index 0cf1486f..67b84ea8 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/TopicBaseTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/TopicBaseTest.java @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -97,7 +98,7 @@ public class TopicBaseTest extends TopicTestBase { @Test public void testSerialize() { - new GsonTestUtils().compareGson(base, TopicBaseTest.class); + assertThatCode(() -> new GsonTestUtils().compareGson(base, TopicBaseTest.class)).doesNotThrowAnyException(); } @Test -- cgit 1.2.3-korg