diff options
Diffstat (limited to 'policy-core/src/test')
10 files changed, 481 insertions, 508 deletions
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 1da6e011..d2d0dec2 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 @@ -26,330 +26,302 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.nio.file.Paths; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.drools.util.KieUtils; /** - * These tests focus on the following classes: - * PolicyContainer - * PolicySession - * PolicySessionFeatureAPI + * These tests focus on the following classes. + * PolicyContainer + * PolicySession + * PolicySessionFeatureAPI */ -public class DroolsContainerTest -{ - /** - * This test is centered around the creation of a 'PolicyContainer' - * and 'PolicySession', and the updating of that container to a new - * version. - */ - @BeforeClass - public static void setUp() throws Exception - { - KieUtils.installArtifact - (Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(), - Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(), - "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl", - Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile()); - - KieUtils.installArtifact - (Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/META-INF/kmodule.xml").toFile(), - Paths.get("src/test/resources/drools-artifact-1.2/pom.xml").toFile(), - "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl", - Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/rules.drl").toFile()); - } - - /** - * This test is centered around the creation of a 'PolicyContainer' - * and 'PolicySession', and the updating of that container to a new - * version. - */ - @Test - public void createAndUpdate() throws Exception - { - // make sure feature log starts out clean - TestPolicySessionFeatureAPI.getLog(); - - // run 'globalInit', and verify expected feature hook fired - PolicyContainer.globalInit(new String[0]); - assertEquals(buildArrayList("globalInit"), - TestPolicySessionFeatureAPI.getLog()); - - // initial conditions -- there should be no containers - assertEquals(0, PolicyContainer.getPolicyContainers().size()); - - // create the container, and start it - PolicyContainer container = - new PolicyContainer("org.onap.policy.drools-pdp", - "drools-artifact1", "17.1.0-SNAPSHOT"); - container.start(); - assertTrue(container.isAlive()); - - // verify expected feature hooks fired - assertEquals(buildArrayList("activatePolicySession", - "newPolicySession", - "selectThreadModel"), - TestPolicySessionFeatureAPI.getLog()); - - // this container should be on the list - { - Collection<PolicyContainer> containers = - PolicyContainer.getPolicyContainers(); - assertEquals(1, containers.size()); - assertTrue(containers.contains(container)); - } - - // verify initial container attributes - assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT", - container.getName()); - assertEquals("org.onap.policy.drools-pdp", container.getGroupId()); - assertEquals("drools-artifact1", container.getArtifactId()); - assertEquals("17.1.0-SNAPSHOT", container.getVersion()); - - try - { - // fetch the session, and verify that it exists - PolicySession session = container.getPolicySession("session1"); - assertTrue(session != null); - - // get all sessions, and verify that this one is the only one - { - Collection<PolicySession> sessions = container.getPolicySessions(); - assertEquals(1, sessions.size()); - assertTrue(sessions.contains(session)); - } - - // verify session attributes - assertEquals(container, session.getPolicyContainer()); - assertEquals("session1", session.getName()); - assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT:session1", - session.getFullName()); - - // insert a new fact - int[] a = new int[]{0, 3, 8, 2}; - session.getKieSession().insert(a); - - // the Drools rules should add 3 + 8 + 2, and store 13 in a[0] - assertTrue(waitForChange(a) == 13); - - // update the container to a new version -- - // the rules will then multiply values rather than add them - assertEquals("[]", - container.updateToVersion("17.2.0-SNAPSHOT").toString()); - - // verify expected feature hooks fired - assertEquals(buildArrayList("selectThreadModel"), - TestPolicySessionFeatureAPI.getLog()); - - // verify new container attributes - assertEquals - ("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT", - container.getName()); - assertEquals("org.onap.policy.drools-pdp", container.getGroupId()); - assertEquals("drools-artifact1", container.getArtifactId()); - assertEquals("17.2.0-SNAPSHOT", container.getVersion()); - - // verify new session attributes - assertEquals(container, session.getPolicyContainer()); - assertEquals("session1", session.getName()); - assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT:session1", - session.getFullName()); - - // the updated rules should now multiply 3 * 8 * 2, and return 48 - - a[0] = 0; - container.insert("session1", a); - assertTrue(waitForChange(a) == 48); - } - finally - { - container.shutdown(); - assertFalse(container.isAlive()); - - // verify expected feature hooks fired - assertEquals(buildArrayList("disposeKieSession"), - TestPolicySessionFeatureAPI.getLog()); - } - - // final conditions -- there should be no containers - assertEquals(0, PolicyContainer.getPolicyContainers().size()); - } - - /** - * This test create a 'PolicyContainer' and 'PolicySession', and verifies - * their behavior, but uses alternate interfaces to increase code coverage. - * In addition, feature hook invocations will trigger exceptions in this - * test, also to increase code coverage. - */ - @Test - public void versionList() throws Exception - { - // make sure feature log starts out clean - TestPolicySessionFeatureAPI.getLog(); - - // trigger exceptions in all feature hooks - TestPolicySessionFeatureAPI.setExceptionTrigger(true); - - // run 'globalInit', and verify expected feature hook fired - PolicyContainer.globalInit(new String[0]); - assertEquals(buildArrayList("globalInit-exception"), - TestPolicySessionFeatureAPI.getLog()); - - // initial conditions -- there should be no containers - assertEquals(0, PolicyContainer.getPolicyContainers().size()); - - String versionList = - "17.3.0-SNAPSHOT,17.1.0-SNAPSHOT,17.2.0-SNAPSHOT"; - - // versions should be tried in order -- the 17.1.0-SNAPSHOT should "win", - // given the fact that '17.3.0-SNAPSHOT' doesn't exist - PolicyContainer container = - new PolicyContainer("org.onap.policy.drools-pdp", - "drools-artifact1", versionList); - // the following should be equivalent to 'container.start()' - PolicyContainer.activate(); - assertTrue(container.isAlive()); - - // verify expected feature hooks fired - assertEquals(buildArrayList("activatePolicySession-exception", - "newPolicySession-exception", - "selectThreadModel-exception"), - TestPolicySessionFeatureAPI.getLog()); - - // this container should be on the list - { - Collection<PolicyContainer> containers = - PolicyContainer.getPolicyContainers(); - assertEquals(1, containers.size()); - assertTrue(containers.contains(container)); - } - - // verify initial container attributes - assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT", - container.getName()); - assertEquals("org.onap.policy.drools-pdp", container.getGroupId()); - assertEquals("drools-artifact1", container.getArtifactId()); - assertEquals("17.1.0-SNAPSHOT", container.getVersion()); - - // some container adjunct tests - { - Object bogusAdjunct = new Object(); - - // initially, no adjunct - assertSame(null, container.getAdjunct(this)); - - // set and verify adjunct - container.setAdjunct(this, bogusAdjunct); - assertSame(bogusAdjunct, container.getAdjunct(this)); - - // clear and verify adjunct - container.setAdjunct(this, null); - assertSame(null, container.getAdjunct(this)); - } - - try - { - // fetch the session, and verify that it exists - PolicySession session = container.getPolicySession("session1"); - assertTrue(session != null); - - // get all sessions, and verify that this one is the only one - { - Collection<PolicySession> sessions = container.getPolicySessions(); - assertEquals(1, sessions.size()); - assertTrue(sessions.contains(session)); - } - - // verify session attributes - assertEquals(container, session.getPolicyContainer()); - assertEquals("session1", session.getName()); - assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT:session1", - session.getFullName()); - - // some session adjunct tests - { - Object bogusAdjunct = new Object(); - - // initially, no adjunct - assertSame(null, session.getAdjunct(this)); - - // set and verify adjunct - session.setAdjunct(this, bogusAdjunct); - assertSame(bogusAdjunct, session.getAdjunct(this)); - - // clear and verify adjunct - session.setAdjunct(this, null); - assertSame(null, session.getAdjunct(this)); - } - - // insert a new fact (using 'insertAll') - int[] a = new int[]{0, 7, 3, 4}; - container.insertAll(a); - - // the Drools rules should add 7 + 3 + 4, and store 14 in a[0] - assertTrue(waitForChange(a) == 14); - - // exercise some more API methods - assertEquals(container.getClassLoader(), - container.getKieContainer().getClassLoader()); - } - finally - { - // should be equivalent to 'shutdown' without persistence - container.destroy(); - assertFalse(container.isAlive()); - - // verify expected feature hooks fired - assertEquals(buildArrayList("destroyKieSession-exception"), - TestPolicySessionFeatureAPI.getLog()); - - // clear exception trigger - TestPolicySessionFeatureAPI.setExceptionTrigger(false); - } - - // final conditions -- there should be no containers - assertEquals(0, PolicyContainer.getPolicyContainers().size()); - } - - /** - * This method is tied to the expected behavior of the drools sessions. - * Initially, the value of 'array[0]' should be 0. The Drools rules - * will either add or multiply 'array[1]' through 'array[n-1]', depending - * upon the version. It waits up to 30 seconds for a non-zero value - * to appear. - */ - private int waitForChange(int[] array) throws InterruptedException - { - int rval = -1; - - // the value is tested every 1/100 of a second, and it waits up to - // 3000 iterations (= 30 seconds) for a non-zero value - for (int i = 0 ; i < 3000 ; i += 1) - { - // wait for 10 milliseconds = 1/100 of a second - Thread.sleep(10); - if ((rval = array[0]) != 0) - { - // a non-zero value has been stored - break; - } - } - return(rval); - } - - /** - * @param args an array of string arguments - * @return an ArrayList constructed from the provided arguments - */ - private ArrayList<String> buildArrayList(String... args) - { - ArrayList<String> rval = new ArrayList<>(); - for (String arg : args) - { - rval.add(arg); - } - return(rval); - } +public class DroolsContainerTest { + /** + * This test is centered around the creation of a 'PolicyContainer' + * and 'PolicySession', and the updating of that container to a new + * version. + */ + @BeforeClass + public static void setUp() throws Exception { + KieUtils.installArtifact( + Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(), + Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(), + "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl", + Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile()); + + KieUtils.installArtifact( + Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/META-INF/kmodule.xml").toFile(), + Paths.get("src/test/resources/drools-artifact-1.2/pom.xml").toFile(), + "src/main/resources/rules/org/onap/policy/drools/core/test/rules.drl", + Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/rules.drl").toFile()); + } + + /** + * This test is centered around the creation of a 'PolicyContainer' + * and 'PolicySession', and the updating of that container to a new + * version. + */ + @Test + public void createAndUpdate() throws Exception { + // make sure feature log starts out clean + PolicySessionFeatureApiTest.getLog(); + + // run 'globalInit', and verify expected feature hook fired + PolicyContainer.globalInit(new String[0]); + assertEquals(Arrays.asList("globalInit"), + PolicySessionFeatureApiTest.getLog()); + + // initial conditions -- there should be no containers + assertEquals(0, PolicyContainer.getPolicyContainers().size()); + + // create the container, and start it + PolicyContainer container = + new PolicyContainer("org.onap.policy.drools-pdp", + "drools-artifact1", "17.1.0-SNAPSHOT"); + container.start(); + assertTrue(container.isAlive()); + + // verify expected feature hooks fired + assertEquals(Arrays.asList("activatePolicySession", + "newPolicySession", + "selectThreadModel"), + PolicySessionFeatureApiTest.getLog()); + + // this container should be on the list + { + Collection<PolicyContainer> containers = + PolicyContainer.getPolicyContainers(); + assertEquals(1, containers.size()); + assertTrue(containers.contains(container)); + } + + // verify initial container attributes + assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT", + container.getName()); + assertEquals("org.onap.policy.drools-pdp", container.getGroupId()); + assertEquals("drools-artifact1", container.getArtifactId()); + assertEquals("17.1.0-SNAPSHOT", container.getVersion()); + + try { + // fetch the session, and verify that it exists + PolicySession session = container.getPolicySession("session1"); + assertTrue(session != null); + + // get all sessions, and verify that this one is the only one + { + Collection<PolicySession> sessions = container.getPolicySessions(); + assertEquals(1, sessions.size()); + assertTrue(sessions.contains(session)); + } + + // verify session attributes + assertEquals(container, session.getPolicyContainer()); + assertEquals("session1", session.getName()); + assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT:session1", + session.getFullName()); + + // insert a new fact + int[] facts = new int[]{0, 3, 8, 2}; + session.getKieSession().insert(facts); + + // the Drools rules should add 3 + 8 + 2, and store 13 in a[0] + assertTrue(waitForChange(facts) == 13); + + // update the container to a new version -- + // the rules will then multiply values rather than add them + assertEquals("[]", + container.updateToVersion("17.2.0-SNAPSHOT").toString()); + + // verify expected feature hooks fired + assertEquals(Arrays.asList("selectThreadModel"), + PolicySessionFeatureApiTest.getLog()); + + // verify new container attributes + assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT", + container.getName()); + assertEquals("org.onap.policy.drools-pdp", container.getGroupId()); + assertEquals("drools-artifact1", container.getArtifactId()); + assertEquals("17.2.0-SNAPSHOT", container.getVersion()); + + // verify new session attributes + assertEquals(container, session.getPolicyContainer()); + assertEquals("session1", session.getName()); + assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT:session1", + session.getFullName()); + + // the updated rules should now multiply 3 * 8 * 2, and return 48 + + facts[0] = 0; + container.insert("session1", facts); + assertTrue(waitForChange(facts) == 48); + } finally { + container.shutdown(); + assertFalse(container.isAlive()); + + // verify expected feature hooks fired + assertEquals(Arrays.asList("disposeKieSession"), + PolicySessionFeatureApiTest.getLog()); + } + + // final conditions -- there should be no containers + assertEquals(0, PolicyContainer.getPolicyContainers().size()); + } + + /** + * This test create a 'PolicyContainer' and 'PolicySession', and verifies + * their behavior, but uses alternate interfaces to increase code coverage. + * In addition, feature hook invocations will trigger exceptions in this + * test, also to increase code coverage. + */ + @Test + public void versionList() throws Exception { + // make sure feature log starts out clean + PolicySessionFeatureApiTest.getLog(); + + // trigger exceptions in all feature hooks + PolicySessionFeatureApiTest.setExceptionTrigger(true); + + // run 'globalInit', and verify expected feature hook fired + PolicyContainer.globalInit(new String[0]); + assertEquals(Arrays.asList("globalInit-exception"), + PolicySessionFeatureApiTest.getLog()); + + // initial conditions -- there should be no containers + assertEquals(0, PolicyContainer.getPolicyContainers().size()); + + String versionList = + "17.3.0-SNAPSHOT,17.1.0-SNAPSHOT,17.2.0-SNAPSHOT"; + + // versions should be tried in order -- the 17.1.0-SNAPSHOT should "win", + // given the fact that '17.3.0-SNAPSHOT' doesn't exist + PolicyContainer container = + new PolicyContainer("org.onap.policy.drools-pdp", + "drools-artifact1", versionList); + // the following should be equivalent to 'container.start()' + PolicyContainer.activate(); + assertTrue(container.isAlive()); + + // verify expected feature hooks fired + assertEquals(Arrays.asList("activatePolicySession-exception", + "newPolicySession-exception", + "selectThreadModel-exception"), + PolicySessionFeatureApiTest.getLog()); + + // this container should be on the list + { + Collection<PolicyContainer> containers = + PolicyContainer.getPolicyContainers(); + assertEquals(1, containers.size()); + assertTrue(containers.contains(container)); + } + + // verify initial container attributes + assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT", + container.getName()); + assertEquals("org.onap.policy.drools-pdp", container.getGroupId()); + assertEquals("drools-artifact1", container.getArtifactId()); + assertEquals("17.1.0-SNAPSHOT", container.getVersion()); + + // some container adjunct tests + { + Object bogusAdjunct = new Object(); + + // initially, no adjunct + assertSame(null, container.getAdjunct(this)); + + // set and verify adjunct + container.setAdjunct(this, bogusAdjunct); + assertSame(bogusAdjunct, container.getAdjunct(this)); + + // clear and verify adjunct + container.setAdjunct(this, null); + assertSame(null, container.getAdjunct(this)); + } + + try { + // fetch the session, and verify that it exists + PolicySession session = container.getPolicySession("session1"); + assertTrue(session != null); + + // get all sessions, and verify that this one is the only one + { + Collection<PolicySession> sessions = container.getPolicySessions(); + assertEquals(1, sessions.size()); + assertTrue(sessions.contains(session)); + } + + // verify session attributes + assertEquals(container, session.getPolicyContainer()); + assertEquals("session1", session.getName()); + assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT:session1", + session.getFullName()); + + // some session adjunct tests + { + Object bogusAdjunct = new Object(); + + // initially, no adjunct + assertSame(null, session.getAdjunct(this)); + + // set and verify adjunct + session.setAdjunct(this, bogusAdjunct); + assertSame(bogusAdjunct, session.getAdjunct(this)); + + // clear and verify adjunct + session.setAdjunct(this, null); + assertSame(null, session.getAdjunct(this)); + } + + // insert a new fact (using 'insertAll') + int[] facts = new int[]{0, 7, 3, 4}; + container.insertAll(facts); + + // the Drools rules should add 7 + 3 + 4, and store 14 in a[0] + assertTrue(waitForChange(facts) == 14); + + // exercise some more API methods + assertEquals(container.getClassLoader(), + container.getKieContainer().getClassLoader()); + } finally { + // should be equivalent to 'shutdown' without persistence + container.destroy(); + assertFalse(container.isAlive()); + + // verify expected feature hooks fired + assertEquals(Arrays.asList("destroyKieSession-exception"), + PolicySessionFeatureApiTest.getLog()); + + // clear exception trigger + PolicySessionFeatureApiTest.setExceptionTrigger(false); + } + + // final conditions -- there should be no containers + assertEquals(0, PolicyContainer.getPolicyContainers().size()); + } + + /** + * This method is tied to the expected behavior of the drools sessions. + * Initially, the value of 'array[0]' should be 0. The Drools rules + * will either add or multiply 'array[1]' through 'array[n-1]', depending + * upon the version. It waits up to 30 seconds for a non-zero value + * to appear. + */ + private int waitForChange(int[] array) throws InterruptedException { + int rval = -1; + + // the value is tested every 1/100 of a second, and it waits up to + // 3000 iterations (= 30 seconds) for a non-zero value + for (int i = 0 ; i < 3000 ; i += 1) { + // wait for 10 milliseconds = 1/100 of a second + Thread.sleep(10); + if ((rval = array[0]) != 0) { + // a non-zero value has been stored + break; + } + } + return (rval); + } } 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 new file mode 100644 index 00000000..aef4e265 --- /dev/null +++ b/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionFeatureApiTest.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 PolicySessionFeatureApiTest implements PolicySessionFeatureAPI { + // contains the log entries since the most recent 'getLog()' call + private static ArrayList<String> 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<String> getLog() { + synchronized (log) { + ArrayList<String> 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/TestPolicySessionFeatureAPI.java b/policy-core/src/test/java/org/onap/policy/drools/core/TestPolicySessionFeatureAPI.java deleted file mode 100644 index f456d814..00000000 --- a/policy-core/src/test/java/org/onap/policy/drools/core/TestPolicySessionFeatureAPI.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-core - * ================================================================================ - * Copyright (C) 2017 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 TestPolicySessionFeatureAPI implements PolicySessionFeatureAPI -{ - // contains the log entries since the most recent 'getLog()' call - static private ArrayList<String> log = new ArrayList<>(); - - // if 'true', trigger an exception right after doing the log, - // to verify that exceptions are handled - static private boolean exceptionTrigger = false; - - /** - * @return the current contents of the log, and clear the log - */ - static public ArrayList<String> getLog() - { - synchronized(log) - { - ArrayList<String> 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 - */ - static public 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 - */ - static private 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 ***"); - } - } - - /***************************************/ - /* 'PolicySessionFeatureAPI' interface */ - /***************************************/ - - /** - * {@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/lock/LockTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockTest.java index bd11dcf9..5a88b02f 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/LockTest.java @@ -20,7 +20,10 @@ package org.onap.policy.drools.core.lock; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import org.junit.Before; import org.junit.Test; import org.onap.policy.drools.core.lock.Lock.RemoveResult; diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPITest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPITest.java index 0d7d9437..522a3f51 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPITest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPITest.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.core.lock; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; + import org.junit.Before; import org.junit.Test; import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI.OperResult; @@ -33,6 +34,9 @@ public class PolicyResourceLockFeatureAPITest { private PolicyResourceLockFeatureAPI api; + /** + * set up. + */ @Before public void setUp() { api = new PolicyResourceLockFeatureAPI() { diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java index 5cbed304..962fb96a 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java @@ -32,7 +32,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.onap.policy.drools.core.lock.TestUtils.expectException; +import static org.onap.policy.drools.core.lock.TestingUtils.expectException; + import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -79,6 +80,9 @@ public class PolicyResourceLockManagerTest { PolicyResourceLockManager.setFactory(saveFactory); } + /** + * Set up. + */ @Before public void setUp() { impl1 = mock(PolicyResourceLockFeatureAPI.class); @@ -104,7 +108,7 @@ public class PolicyResourceLockManagerTest { /** * Initializes an implementer so it always returns {@code null}. * - * @param impl + * @param impl implementer */ private void initImplementer(PolicyResourceLockFeatureAPI impl) { when(impl.beforeLock(anyString(), anyString(), anyInt())).thenReturn(OperResult.OPER_UNHANDLED); diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java index feabdbc0..6afe5a83 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java @@ -24,7 +24,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.onap.policy.drools.core.lock.TestUtils.expectException; +import static org.onap.policy.drools.core.lock.TestingUtils.expectException; + import java.util.LinkedList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -78,6 +79,9 @@ public class SimpleLockManagerTest { Whitebox.setInternalState(SimpleLockManager.class, TIME_FIELD, savedTime); } + /** + * Set up. + */ @Before public void setUp() { testTime = new TestTime(); @@ -112,18 +116,18 @@ public class SimpleLockManagerTest { mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC); // sleep half of the cycle - testTime.sleep(MAX_AGE_MS/2); + testTime.sleep(MAX_AGE_MS / 2); assertTrue(mgr.isLockedBy(RESOURCE_A, OWNER1)); // extend the lock mgr.refresh(RESOURCE_A, OWNER1, MAX_AGE_SEC); // verify still locked after sleeping the other half of the cycle - testTime.sleep(MAX_AGE_MS/2+1); + testTime.sleep(MAX_AGE_MS / 2 + 1); assertTrue(mgr.isLockedBy(RESOURCE_A, OWNER1)); // and should release after another half cycle - testTime.sleep(MAX_AGE_MS/2); + testTime.sleep(MAX_AGE_MS / 2); assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER1)); } @@ -338,13 +342,13 @@ public class SimpleLockManagerTest { assertTrue(mgr.isLocked(RESOURCE_A)); assertTrue(mgr.isLocked(RESOURCE_B)); - testTime.sleep(MAX_AGE_MS/4); + testTime.sleep(MAX_AGE_MS / 4); mgr.lock(RESOURCE_C, OWNER1, MAX_AGE_SEC); assertTrue(mgr.isLocked(RESOURCE_A)); assertTrue(mgr.isLocked(RESOURCE_B)); assertTrue(mgr.isLocked(RESOURCE_C)); - testTime.sleep(MAX_AGE_MS/4); + testTime.sleep(MAX_AGE_MS / 4); mgr.lock(RESOURCE_D, OWNER1, MAX_AGE_SEC); assertTrue(mgr.isLocked(RESOURCE_A)); assertTrue(mgr.isLocked(RESOURCE_B)); @@ -352,19 +356,19 @@ public class SimpleLockManagerTest { assertTrue(mgr.isLocked(RESOURCE_D)); // sleep remainder of max age - first two should expire - testTime.sleep(MAX_AGE_MS/2); + testTime.sleep(MAX_AGE_MS / 2); assertFalse(mgr.isLocked(RESOURCE_A)); assertFalse(mgr.isLocked(RESOURCE_B)); assertTrue(mgr.isLocked(RESOURCE_C)); assertTrue(mgr.isLocked(RESOURCE_D)); // another quarter - next one should expire - testTime.sleep(MAX_AGE_MS/4); + testTime.sleep(MAX_AGE_MS / 4); assertFalse(mgr.isLocked(RESOURCE_C)); assertTrue(mgr.isLocked(RESOURCE_D)); // another quarter - last one should expire - testTime.sleep(MAX_AGE_MS/4); + testTime.sleep(MAX_AGE_MS / 4); assertFalse(mgr.isLocked(RESOURCE_D)); } @@ -389,9 +393,7 @@ public class SimpleLockManagerTest { long ttime = System.currentTimeMillis() + 50; Data data = new Data(OWNER1, RESOURCE_A, ttime); Data dataSame = new Data(OWNER1, RESOURCE_A, ttime); - Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime+1); - Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime); - Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime); + Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime + 1); assertEquals(0, data.compareTo(data)); assertEquals(0, data.compareTo(dataSame)); @@ -399,6 +401,9 @@ public class SimpleLockManagerTest { assertTrue(data.compareTo(dataDiffExpire) < 0); assertTrue(dataDiffExpire.compareTo(data) > 0); + Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime); + Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime); + assertTrue(data.compareTo(dataDiffOwner) < 0); assertTrue(dataDiffOwner.compareTo(data) > 0); @@ -411,17 +416,19 @@ public class SimpleLockManagerTest { long ttime = System.currentTimeMillis() + 1; Data data = new Data(OWNER1, RESOURCE_A, ttime); Data dataSame = new Data(OWNER1, RESOURCE_A, ttime); - Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime+1); + Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime + 1); Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime); - Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime); - Data dataNullOwner = new Data(null, RESOURCE_A, ttime); - Data dataNullResource = new Data(OWNER1, null, ttime); int hc1 = data.hashCode(); assertEquals(hc1, dataSame.hashCode()); assertTrue(hc1 != dataDiffExpire.hashCode()); assertTrue(hc1 != dataDiffOwner.hashCode()); + + Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime); + Data dataNullOwner = new Data(null, RESOURCE_A, ttime); + Data dataNullResource = new Data(OWNER1, null, ttime); + assertTrue(hc1 != dataDiffResource.hashCode()); assertTrue(hc1 != dataNullOwner.hashCode()); assertTrue(hc1 != dataNullResource.hashCode()); @@ -432,15 +439,14 @@ public class SimpleLockManagerTest { long ttime = System.currentTimeMillis() + 50; Data data = new Data(OWNER1, RESOURCE_A, ttime); Data dataSame = new Data(OWNER1, RESOURCE_A, ttime); - Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime+1); - Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime); - Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime); - Data dataNullOwner = new Data(null, RESOURCE_A, ttime); - Data dataNullResource = new Data(OWNER1, null, ttime); + Data dataDiffExpire = new Data(OWNER1, RESOURCE_A, ttime + 1); assertTrue(data.equals(data)); assertTrue(data.equals(dataSame)); + Data dataDiffOwner = new Data(OWNER2, RESOURCE_A, ttime); + Data dataDiffResource = new Data(OWNER1, RESOURCE_B, ttime); + assertFalse(data.equals(dataDiffExpire)); assertFalse(data.equals(dataDiffOwner)); assertFalse(data.equals(dataDiffResource)); @@ -448,6 +454,9 @@ public class SimpleLockManagerTest { assertFalse(data.equals(null)); assertFalse(data.equals("string")); + Data dataNullOwner = new Data(null, RESOURCE_A, ttime); + Data dataNullResource = new Data(OWNER1, null, ttime); + assertFalse(dataNullOwner.equals(data)); assertFalse(dataNullResource.equals(data)); @@ -464,7 +473,7 @@ public class SimpleLockManagerTest { String[] resources = {RESOURCE_A, RESOURCE_B}; - AtomicInteger nfail = new AtomicInteger(0); + final AtomicInteger nfail = new AtomicInteger(0); CountDownLatch stopper = new CountDownLatch(1); CountDownLatch completed = new CountDownLatch(nthreads); @@ -472,7 +481,7 @@ public class SimpleLockManagerTest { for (int x = 0; x < nthreads; ++x) { String owner = "owner." + x; - Thread t = new Thread(() -> { + Thread thread = new Thread(() -> { for (int y = 0; y < nlocks; ++y) { String res = resources[y % resources.length]; @@ -495,8 +504,8 @@ public class SimpleLockManagerTest { completed.countDown(); }); - t.setDaemon(true); - threads.add(t); + thread.setDaemon(true); + threads.add(thread); } // start the threads diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestUtils.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestingUtils.java index f843f6ab..17a11a09 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestUtils.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestingUtils.java @@ -20,14 +20,14 @@ package org.onap.policy.drools.core.lock; -public class TestUtils { +public class TestingUtils { /** * Invokes a function that is expected to throw an exception. * * @param clazz class of exception that is expected - * @param func - * @return + * @param func function + * @return object */ public static <T> T expectException(Class<T> clazz, VoidFunction func) { try { diff --git a/policy-core/src/test/java/org/onap/policy/drools/util/FeatureEnabledCheckerTest.java b/policy-core/src/test/java/org/onap/policy/drools/util/FeatureEnabledCheckerTest.java index cd79592f..c118b897 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/util/FeatureEnabledCheckerTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/util/FeatureEnabledCheckerTest.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.util; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; + import java.util.Properties; import org.junit.Test; 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 d6b088c3..6bc87035 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.TestPolicySessionFeatureAPI +org.onap.policy.drools.core.PolicySessionFeatureApiTest |