From fb9bd637567096f6174bbc2e52a5e149a4eed882 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 30 Sep 2020 12:52:25 -0400 Subject: Fix new sonars in drools-apps Addressed the following sonars: - too many assertions in test method - rename test class - use static method to modify static field - use already defined constant - code always returns the same value - use assertNotSame - use appropriate class name to access static method - define a constant - extract nested try block - don't always return the same value - use remove() instead of set(null) for thread-local-storage - add @Override Issue-ID: POLICY-2852 Change-Id: Icc62acd4ad57afa2d44ed4cdca504a3ac0810228 Signed-off-by: Jim Hahn --- .../controlloop/common/rules/test/BaseTest.java | 11 ++-- .../common/rules/test/NamedRunner2Test.java | 58 ++++++++++++++++++++++ .../common/rules/test/NamedRunnerTest2.java | 58 ---------------------- 3 files changed, 66 insertions(+), 61 deletions(-) create mode 100644 controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunner2Test.java delete mode 100644 controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunnerTest2.java (limited to 'controlloop/common/rules-test') diff --git a/controlloop/common/rules-test/src/main/java/org/onap/policy/controlloop/common/rules/test/BaseTest.java b/controlloop/common/rules-test/src/main/java/org/onap/policy/controlloop/common/rules/test/BaseTest.java index f5346def8..83825bca9 100644 --- a/controlloop/common/rules-test/src/main/java/org/onap/policy/controlloop/common/rules/test/BaseTest.java +++ b/controlloop/common/rules-test/src/main/java/org/onap/policy/controlloop/common/rules/test/BaseTest.java @@ -30,6 +30,7 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import lombok.AccessLevel; import lombok.Getter; +import lombok.Setter; import org.awaitility.Awaitility; import org.junit.Test; import org.onap.policy.appc.Request; @@ -55,6 +56,9 @@ public abstract class BaseTest { private static final String APPC_RESTART_OP = "restart"; private static final String APPC_MODIFY_CONFIG_OP = "ModifyConfig"; + private static final String SDNR_MODIFY_CONFIG_OP = "ModifyConfig"; + private static final String SNDR_MODIFY_CONFIG_ANR_OP = "ModifyConfigANR"; + /* * Canonical Topic Names. */ @@ -135,6 +139,7 @@ public abstract class BaseTest { // used to inject and wait for messages @Getter(AccessLevel.PROTECTED) + @Setter(AccessLevel.PROTECTED) protected static Topics topics; // used to wait for messages on SINK topics @@ -168,7 +173,7 @@ public abstract class BaseTest { * Initializes {@link #topics} and {@link #controller}. */ public void init() { - topics = topicMaker.get(); + setTopics(topicMaker.get()); Map locks = getLockMap(); if (locks != null) { @@ -348,7 +353,7 @@ public abstract class BaseTest { */ @Test public void testVpciSunnyDayCompliant() { - sdnrSunnyDay(VPCI_TOSCA_COMPLIANT_POLICY, VPCI_ONSET, VPCI_SDNR_SUCCESS, "ModifyConfig"); + sdnrSunnyDay(VPCI_TOSCA_COMPLIANT_POLICY, VPCI_ONSET, VPCI_SDNR_SUCCESS, SDNR_MODIFY_CONFIG_OP); } // VSONH @@ -358,7 +363,7 @@ public abstract class BaseTest { */ @Test public void testVsonhSunnyDayCompliant() { - sdnrSunnyDay(VSONH_TOSCA_COMPLIANT_POLICY, VSONH_ONSET, VSONH_SDNR_SUCCESS, "ModifyConfigANR"); + sdnrSunnyDay(VSONH_TOSCA_COMPLIANT_POLICY, VSONH_ONSET, VSONH_SDNR_SUCCESS, SNDR_MODIFY_CONFIG_ANR_OP); } /** diff --git a/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunner2Test.java b/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunner2Test.java new file mode 100644 index 000000000..91e19ce79 --- /dev/null +++ b/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunner2Test.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 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. + * 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.controlloop.common.rules.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.runner.RunWith; + + +/** + * Tests NamedRunner when the TestNames annotation is missing - all tests should be + * executed. + */ +@RunWith(NamedRunner.class) +public class NamedRunner2Test { + + private static int testCount = 0; + + @AfterClass + public static void tearDownAfterClass() { + assertEquals(2, testCount); + } + + @Test + public void testAbc() { + checkTest(); + } + + @Test + public void testDef() { + checkTest(); + } + + + private static void checkTest() { + ++testCount; + } +} diff --git a/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunnerTest2.java b/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunnerTest2.java deleted file mode 100644 index 1ed5b20bc..000000000 --- a/controlloop/common/rules-test/src/test/java/org/onap/policy/controlloop/common/rules/test/NamedRunnerTest2.java +++ /dev/null @@ -1,58 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 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. - * 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.controlloop.common.rules.test; - -import static org.junit.Assert.assertEquals; - -import org.junit.AfterClass; -import org.junit.Test; -import org.junit.runner.RunWith; - - -/** - * Tests NamedRunner when the TestNames annotation is missing - all tests should be - * executed. - */ -@RunWith(NamedRunner.class) -public class NamedRunnerTest2 { - - private static int testCount = 0; - - @AfterClass - public static void tearDownAfterClass() { - assertEquals(2, testCount); - } - - @Test - public void testAbc() { - checkTest(); - } - - @Test - public void testDef() { - checkTest(); - } - - - private static void checkTest() { - ++testCount; - } -} -- cgit 1.2.3-korg