summaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-tdjam/src/test
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2020-09-30 12:52:25 -0400
committerJim Hahn <jrh3@att.com>2020-09-30 14:59:39 -0400
commitfb9bd637567096f6174bbc2e52a5e149a4eed882 (patch)
tree7b0d2df1aefbb3635788af501be1157fbe05d798 /controlloop/common/controller-tdjam/src/test
parente8e477ab80c6762fb05aebfe9becc630d2d51e39 (diff)
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 <jrh3@att.com>
Diffstat (limited to 'controlloop/common/controller-tdjam/src/test')
-rw-r--r--controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java3
-rw-r--r--controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java92
2 files changed, 61 insertions, 34 deletions
diff --git a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java
index 1426865c6..990e473db 100644
--- a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java
+++ b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java
@@ -22,6 +22,7 @@ package org.onap.policy.controlloop.tdjam;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
@@ -211,7 +212,7 @@ public class TdjamControllerTest {
clp.setClosedLoopControlName(closedLoopControlName);
if (tc != null) {
- assertTrue(tc.addControlLoopParams(clp) != clp);
+ assertNotSame(clp, tc.addControlLoopParams(clp));
}
return clp;
diff --git a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java
index ee96cb893..57f98bd0a 100644
--- a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java
+++ b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java
@@ -36,6 +36,8 @@ import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
+import org.junit.After;
+import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
@@ -52,15 +54,37 @@ public class NonDroolsPolicyControllerTest {
//public static boolean loop = true;
private static Properties prop;
+ private NonDroolsPolicyController controller;
+
@BeforeClass
public static void setupClass() throws Exception {
prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
}
+ /**
+ * Resets the stats and creates the controller.
+ */
+ @Before
+ public void setUp() {
+ DroolsControllerFeatureHandler.resetStats();
+
+ controller = buildController("tdjam");
+ }
+
+ /**
+ * Destroys the controller.
+ */
+ @After
+ public void tearDown() {
+ String name = controller.getName();
+ assertSame(controller, PolicyControllerConstants.getFactory().get(name));
+ PolicyControllerConstants.getFactory().destroy(controller);
+ assertThatIllegalArgumentException().isThrownBy(
+ () -> PolicyControllerConstants.getFactory().get(name));
+ }
+
@Test
public void testState() {
- NonDroolsPolicyController controller = buildController("tdjam");
-
assertEquals("nondrools", controller.getName());
assertEquals("NonDroolsPolicyController", controller.getGroupId());
assertEquals("nondrools", controller.getArtifactId());
@@ -81,48 +105,60 @@ public class NonDroolsPolicyControllerTest {
assertFalse(controller.isLocked());
// test a few stubbed-off methods
+ assertNull(controller.getContainer());
+ assertThatIllegalStateException().isThrownBy(() -> controller.updateToVersion(null, null, null, null, null));
+
+ controller.lock();
+ assertTrue(controller.isAlive());
+ assertTrue(controller.isLocked());
+
+ controller.stop();
+ assertFalse(controller.isAlive());
+ assertTrue(controller.isLocked());
+
+ controller.unlock();
+ assertFalse(controller.isAlive());
+ assertFalse(controller.isLocked());
+ }
+
+ @Test
+ public void testNames() {
assertTrue(controller.getSessionNames().isEmpty());
assertTrue(controller.getCanonicalSessionNames().isEmpty());
assertTrue(controller.getBaseDomainNames().isEmpty());
+ }
+
+ @Test
+ public void testOffer() {
+ controller.start();
+
assertFalse(controller.offer("topic", "event"));
assertFalse(controller.offer("event"));
assertEquals(0, controller.getRecentSourceEvents().length);
assertEquals(0, controller.getRecentSinkEvents().length);
- assertNull(controller.getContainer());
+ }
+
+ @Test
+ public void testFacts() {
assertThatIllegalArgumentException().isThrownBy(
() -> controller.fetchModelClass("NoSuchClass"));
- assertThatIllegalStateException().isThrownBy(
- () -> controller.updateToVersion(null, null, null, null, null));
assertTrue(controller.factClassNames(null).isEmpty());
assertEquals(0, controller.factCount(null));
assertTrue(controller.facts(null, null, false).isEmpty());
assertTrue(controller.facts("sessionName", String.class).isEmpty());
assertTrue(controller.factQuery(null, null, null, false).isEmpty());
+ }
+
+ @Test
+ public void testDelete() {
assertFalse(controller.delete("sessionName", "fact"));
assertFalse(controller.delete("fact"));
assertFalse(controller.delete("sessionName", String.class));
assertFalse(controller.delete(String.class));
-
- controller.lock();
- assertTrue(controller.isAlive());
- assertTrue(controller.isLocked());
-
- controller.stop();
- assertFalse(controller.isAlive());
- assertTrue(controller.isLocked());
-
- controller.unlock();
- assertFalse(controller.isAlive());
- assertFalse(controller.isLocked());
-
- destroy(controller);
}
@Test
- public void deliverTest() {
- DroolsControllerFeatureHandler.resetStats();
- final NonDroolsPolicyController controller = buildController("tdjam");
-
+ public void testDeliver() {
final TopicSink topicSink = mock(TopicSink.class);
when(topicSink.getTopic()).thenReturn("POLICY-CL-MGT");
when(topicSink.send(any())).thenReturn(false);
@@ -201,8 +237,6 @@ public class NonDroolsPolicyControllerTest {
assertFalse(signal.apply("nothing in particular"));
assertEquals(1, DroolsControllerFeatureHandler.afterDeliverFalse);
-
- destroy(controller);
}
private NonDroolsPolicyController buildController(String type) {
@@ -213,14 +247,6 @@ public class NonDroolsPolicyControllerTest {
return (NonDroolsPolicyController) controller;
}
- private void destroy(PolicyController controller) {
- String name = controller.getName();
- assertSame(controller, PolicyControllerConstants.getFactory().get(name));
- PolicyControllerConstants.getFactory().destroy(controller);
- assertThatIllegalArgumentException().isThrownBy(
- () -> PolicyControllerConstants.getFactory().get(name));
- }
-
/* ============================================================ */
/**