From 59e9b9a8b56d563814ef21a23716959f772f9194 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 14 Aug 2019 17:31:50 -0400 Subject: Fix more sonar issues in drools-pdp Addressed issues of cyclomatic complexity and deep nesting by refactoring code into separate methods. In some cases, had to refactor the code into nested classes to avoid passing too many parameters to the newly extracted methods. Addressed issue "too many conditionals" by breaking conditionals apart. Addressed issue "Remove usage of generic wildcard type" by eliminating "? extends" from return values. Addressed issue "Remove this use of 'Thread.sleep()'" in junit tests by introducing latches or using Awaitility. Note: this won't build until ApiUtils has been merged. Change-Id: I0d5596b4cb918a36bc22f426f426bd238195b458 Issue-ID: POLICY-1968 Signed-off-by: Jim Hahn --- .../internal/MavenDroolsControllerTest.java | 20 +++++++++++++++++--- .../protocol/coders/ProtocolCoderToolsetTest.java | 2 +- .../drools/server/restful/test/RestManagerTest.java | 10 +++++++--- .../onap/policy/drools/system/PolicyEngineTest.java | 5 +++-- .../internal/AggregatedPolicyControllerTest.java | 2 +- 5 files changed, 29 insertions(+), 10 deletions(-) (limited to 'policy-management/src/test/java/org') diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java index 4c262775..0d8bdfab 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java @@ -22,7 +22,10 @@ package org.onap.policy.drools.controller.internal; import java.io.IOException; import java.nio.file.Paths; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.kie.api.builder.ReleaseId; @@ -42,13 +45,15 @@ public class MavenDroolsControllerTest { private static volatile ReleaseId releaseId; + private static volatile CountDownLatch running; + /** * Set up. * * @throws IOException throws an IO exception */ @BeforeClass - public static void setUp() throws IOException { + public static void setUpBeforeClass() throws IOException { releaseId = KieUtils.installArtifact(Paths.get(JUNIT_ECHO_KMODULE_PATH).toFile(), Paths.get(JUNIT_ECHO_KMODULE_POM_PATH).toFile(), @@ -56,6 +61,15 @@ public class MavenDroolsControllerTest { Paths.get(JUNIT_ECHO_KMODULE_DRL_PATH).toFile()); } + @Before + public void setUp() { + running = new CountDownLatch(1); + } + + public static void setRunning() { + running.countDown(); + } + @Test public void stop() throws InterruptedException { createDroolsController(10000L).stop(); @@ -106,8 +120,8 @@ public class MavenDroolsControllerTest { Assert.assertEquals(releaseId.getArtifactId(), controller.getContainer().getArtifactId()); Assert.assertEquals(releaseId.getVersion(), controller.getContainer().getVersion()); - /* courtesy timer to allow full initialization from local maven repository */ - Thread.sleep(courtesyStartTimeMs); + /* allow full initialization from local maven repository */ + Assert.assertTrue(running.await(courtesyStartTimeMs, TimeUnit.MILLISECONDS)); Assert.assertEquals(1, controller.getSessionNames().size()); Assert.assertEquals(JUNIT_ECHO_KSESSION, controller.getSessionNames().get(0)); diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java index bd595725..7787a7b6 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java @@ -231,7 +231,7 @@ public class ProtocolCoderToolsetTest { Properties sinkConfig = new Properties(); sinkConfig.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, JUNIT_PROTOCOL_CODER_TOPIC); - final List noopTopics = TopicEndpointManager.getManager().addTopicSinks(sinkConfig); + final List noopTopics = TopicEndpointManager.getManager().addTopicSinks(sinkConfig); Properties droolsControllerConfig = new Properties(); droolsControllerConfig.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId()); diff --git a/policy-management/src/test/java/org/onap/policy/drools/server/restful/test/RestManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/server/restful/test/RestManagerTest.java index 173c1738..237bd4df 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/server/restful/test/RestManagerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/server/restful/test/RestManagerTest.java @@ -171,13 +171,17 @@ public class RestManagerTest { * @throws InterruptedException Interrupted exception */ @AfterClass - public static void tearDown() throws IOException, InterruptedException { + public static void tearDown() throws IOException { + try { + client.close(); + } catch (IOException ex) { + logger.warn("cannot close HTTP client connection", ex); + } + /* Shutdown managed resources */ PolicyControllerConstants.getFactory().shutdown(); TopicEndpointManager.getManager().shutdown(); PolicyEngineConstants.getManager().stop(); - Thread.sleep(10000L); - client.close(); cleanUpWorkingDirs(); } diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java index df1f6cca..997fc03e 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java @@ -20,6 +20,7 @@ package org.onap.policy.drools.system; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -29,6 +30,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; +import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.FixMethodOrder; @@ -302,7 +304,6 @@ public class PolicyEngineTest { TopicEndpointManager.getManager().shutdown(); PolicyEngineConstants.getManager().stop(); - Thread.sleep(10000L); - assertFalse(PolicyEngineConstants.getManager().isAlive()); + await().atMost(10, TimeUnit.SECONDS).until(() -> !PolicyEngineConstants.getManager().isAlive()); } } diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java index 6f09ab9b..695893d4 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java @@ -924,7 +924,7 @@ public class AggregatedPolicyControllerTest { // remaining methods should not have been invoked assertThatThrownBy(() -> verifyBefore.accept(prov2)).isInstanceOf(AssertionError.class); - assertThatThrownBy(() -> verifyMiddle.run()).isInstanceOf(AssertionError.class); + assertThatThrownBy(verifyMiddle::run).isInstanceOf(AssertionError.class); assertThatThrownBy(() -> verifyAfter.accept(prov1)).isInstanceOf(AssertionError.class); assertThatThrownBy(() -> verifyAfter.accept(prov2)).isInstanceOf(AssertionError.class); -- cgit 1.2.3-korg