From 4f77be6b3524aefe5763393babc2ffdcb64cf72e Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 21 Sep 2018 16:11:46 -0400 Subject: new sonar issues in drools-pdp No "@Test" methods - renamed class from XxxTest to XxxMock. Changed functional call syntax. Change-Id: I954c2549705e80c00aed8ecbcb19f386cc8b0310 Issue-ID: POLICY-1137 Signed-off-by: Jim Hahn --- .../onap/policy/drools/pooling/state/State.java | 2 +- .../policy/drools/core/DroolsContainerTest.java | 22 ++-- .../drools/core/PolicySessionFeatureApiMock.java | 137 +++++++++++++++++++++ .../drools/core/PolicySessionFeatureApiTest.java | 137 --------------------- ...onap.policy.drools.core.PolicySessionFeatureAPI | 2 +- 5 files changed, 150 insertions(+), 150 deletions(-) create mode 100644 policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiMock.java delete mode 100644 policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiTest.java diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java index edffb3fe..3d939392 100644 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java +++ b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/state/State.java @@ -86,7 +86,7 @@ public abstract class State { * Cancels the timers added by this state. */ public final void cancelTimers() { - timers.forEach(timer -> timer.cancel()); + timers.forEach(CancellableScheduledTask::cancel); } /** diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java index d2d0dec2..76fa1350 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java @@ -67,12 +67,12 @@ public class DroolsContainerTest { @Test public void createAndUpdate() throws Exception { // make sure feature log starts out clean - PolicySessionFeatureApiTest.getLog(); + PolicySessionFeatureApiMock.getLog(); // run 'globalInit', and verify expected feature hook fired PolicyContainer.globalInit(new String[0]); assertEquals(Arrays.asList("globalInit"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); // initial conditions -- there should be no containers assertEquals(0, PolicyContainer.getPolicyContainers().size()); @@ -88,7 +88,7 @@ public class DroolsContainerTest { assertEquals(Arrays.asList("activatePolicySession", "newPolicySession", "selectThreadModel"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); // this container should be on the list { @@ -137,7 +137,7 @@ public class DroolsContainerTest { // verify expected feature hooks fired assertEquals(Arrays.asList("selectThreadModel"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); // verify new container attributes assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT", @@ -163,7 +163,7 @@ public class DroolsContainerTest { // verify expected feature hooks fired assertEquals(Arrays.asList("disposeKieSession"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); } // final conditions -- there should be no containers @@ -179,15 +179,15 @@ public class DroolsContainerTest { @Test public void versionList() throws Exception { // make sure feature log starts out clean - PolicySessionFeatureApiTest.getLog(); + PolicySessionFeatureApiMock.getLog(); // trigger exceptions in all feature hooks - PolicySessionFeatureApiTest.setExceptionTrigger(true); + PolicySessionFeatureApiMock.setExceptionTrigger(true); // run 'globalInit', and verify expected feature hook fired PolicyContainer.globalInit(new String[0]); assertEquals(Arrays.asList("globalInit-exception"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); // initial conditions -- there should be no containers assertEquals(0, PolicyContainer.getPolicyContainers().size()); @@ -208,7 +208,7 @@ public class DroolsContainerTest { assertEquals(Arrays.asList("activatePolicySession-exception", "newPolicySession-exception", "selectThreadModel-exception"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); // this container should be on the list { @@ -292,10 +292,10 @@ public class DroolsContainerTest { // verify expected feature hooks fired assertEquals(Arrays.asList("destroyKieSession-exception"), - PolicySessionFeatureApiTest.getLog()); + PolicySessionFeatureApiMock.getLog()); // clear exception trigger - PolicySessionFeatureApiTest.setExceptionTrigger(false); + PolicySessionFeatureApiMock.setExceptionTrigger(false); } // final conditions -- there should be no containers diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiMock.java b/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiMock.java new file mode 100644 index 00000000..d838f6bd --- /dev/null +++ b/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiMock.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * policy-core + * ================================================================================ + * Copyright (C) 2017-2018 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.drools.core; + +import java.util.ArrayList; +import org.kie.api.runtime.KieSession; + +/** + * This class supports 'DroolsContainerTest' by implementing + * 'PolicySessionFeatureAPI', and providing a means to indicate + * which hooks have been invoked. + */ +public class PolicySessionFeatureApiMock implements PolicySessionFeatureAPI { + // contains the log entries since the most recent 'getLog()' call + private static ArrayList log = new ArrayList<>(); + + // if 'true', trigger an exception right after doing the log, + // to verify that exceptions are handled + private static boolean exceptionTrigger = false; + + /** + * Get log. + * + * @return the current contents of the log, and clear the log + */ + public static ArrayList getLog() { + synchronized (log) { + ArrayList rval = new ArrayList<>(log); + log.clear(); + return rval; + } + } + + /** + * This method controls whether these hooks trigger an exception after + * being invoked. + * + * @param indicator if 'true', subsequent hook method calls will trigger + * an exception; if 'false', no exception is triggered + */ + public static void setExceptionTrigger(boolean indicator) { + exceptionTrigger = indicator; + } + + /** + * This method adds an entry to the log, and possibly triggers an exception. + * + * @param arg value to add to the log + */ + private static void addLog(String arg) { + if (exceptionTrigger) { + // the log entry will include a '-exception' appended to the end + synchronized (log) { + log.add(arg + "-exception"); + } + System.out.println("*** " + arg + "-exception invoked ***"); + + // throw an exception -- it is up to the invoking code to catch it + throw(new IllegalStateException("Triggered from " + arg)); + } else { + // create a log entry, and display to standard output + synchronized (log) { + log.add(arg); + } + System.out.println("*** " + arg + " invoked ***"); + } + } + + /** + * {@inheritDoc}. + */ + public int getSequenceNumber() { + return 1; + } + + /** + * {@inheritDoc}. + */ + public void globalInit(String[] args, String configDir) { + addLog("globalInit"); + } + + /** + * {@inheritDoc}. + */ + public KieSession activatePolicySession(PolicyContainer policyContainer, String name, String kieBaseName) { + addLog("activatePolicySession"); + return null; + } + + /** + * {@inheritDoc}. + */ + public void newPolicySession(PolicySession policySession) { + addLog("newPolicySession"); + } + + /** + * {@inheritDoc}. + */ + public PolicySession.ThreadModel selectThreadModel(PolicySession session) { + addLog("selectThreadModel"); + return null; + } + + /** + * {@inheritDoc}. + */ + public void disposeKieSession(PolicySession policySession) { + addLog("disposeKieSession"); + } + + /** + * {@inheritDoc}. + */ + public void destroyKieSession(PolicySession policySession) { + addLog("destroyKieSession"); + } +} diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiTest.java deleted file mode 100644 index aef4e265..00000000 --- a/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiTest.java +++ /dev/null @@ -1,137 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-core - * ================================================================================ - * Copyright (C) 2017-2018 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.drools.core; - -import java.util.ArrayList; -import org.kie.api.runtime.KieSession; - -/** - * This class supports 'DroolsContainerTest' by implementing - * 'PolicySessionFeatureAPI', and providing a means to indicate - * which hooks have been invoked. - */ -public class PolicySessionFeatureApiTest implements PolicySessionFeatureAPI { - // contains the log entries since the most recent 'getLog()' call - private static ArrayList log = new ArrayList<>(); - - // if 'true', trigger an exception right after doing the log, - // to verify that exceptions are handled - private static boolean exceptionTrigger = false; - - /** - * Get log. - * - * @return the current contents of the log, and clear the log - */ - public static ArrayList getLog() { - synchronized (log) { - ArrayList rval = new ArrayList<>(log); - log.clear(); - return rval; - } - } - - /** - * This method controls whether these hooks trigger an exception after - * being invoked. - * - * @param indicator if 'true', subsequent hook method calls will trigger - * an exception; if 'false', no exception is triggered - */ - public static void setExceptionTrigger(boolean indicator) { - exceptionTrigger = indicator; - } - - /** - * This method adds an entry to the log, and possibly triggers an exception. - * - * @param arg value to add to the log - */ - private static void addLog(String arg) { - if (exceptionTrigger) { - // the log entry will include a '-exception' appended to the end - synchronized (log) { - log.add(arg + "-exception"); - } - System.out.println("*** " + arg + "-exception invoked ***"); - - // throw an exception -- it is up to the invoking code to catch it - throw(new IllegalStateException("Triggered from " + arg)); - } else { - // create a log entry, and display to standard output - synchronized (log) { - log.add(arg); - } - System.out.println("*** " + arg + " invoked ***"); - } - } - - /** - * {@inheritDoc}. - */ - public int getSequenceNumber() { - return 1; - } - - /** - * {@inheritDoc}. - */ - public void globalInit(String[] args, String configDir) { - addLog("globalInit"); - } - - /** - * {@inheritDoc}. - */ - public KieSession activatePolicySession(PolicyContainer policyContainer, String name, String kieBaseName) { - addLog("activatePolicySession"); - return null; - } - - /** - * {@inheritDoc}. - */ - public void newPolicySession(PolicySession policySession) { - addLog("newPolicySession"); - } - - /** - * {@inheritDoc}. - */ - public PolicySession.ThreadModel selectThreadModel(PolicySession session) { - addLog("selectThreadModel"); - return null; - } - - /** - * {@inheritDoc}. - */ - public void disposeKieSession(PolicySession policySession) { - addLog("disposeKieSession"); - } - - /** - * {@inheritDoc}. - */ - public void destroyKieSession(PolicySession policySession) { - addLog("destroyKieSession"); - } -} diff --git a/policy-core/src/test/resources/META-INF/services/org.onap.policy.drools.core.PolicySessionFeatureAPI b/policy-core/src/test/resources/META-INF/services/org.onap.policy.drools.core.PolicySessionFeatureAPI index 6bc87035..3af9beea 100644 --- a/policy-core/src/test/resources/META-INF/services/org.onap.policy.drools.core.PolicySessionFeatureAPI +++ b/policy-core/src/test/resources/META-INF/services/org.onap.policy.drools.core.PolicySessionFeatureAPI @@ -1 +1 @@ -org.onap.policy.drools.core.PolicySessionFeatureApiTest +org.onap.policy.drools.core.PolicySessionFeatureApiMock -- cgit 1.2.3-korg