diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2019-11-11 16:18:27 -0600 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2019-11-11 18:58:37 -0600 |
commit | 2b0f80062f0609f483eea287bdf503be45f12472 (patch) | |
tree | 11d4054f01c8c2a0c8bc536f3b5a8d9a02e25a4e /policy-management/src/test/java | |
parent | 6149ece02f525206242e0489ff1dcda8d9577484 (diff) |
Add drools factory clean up statements to junits
An attempt to tackle some intermittent junit errors
experienced by team mates.
Issue-ID: POLICY-1407
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I68a90dc50fe2fa5bfc7214aa26e2e5f78f1e6c23
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'policy-management/src/test/java')
3 files changed, 271 insertions, 144 deletions
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController3Test.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController3Test.java deleted file mode 100644 index e7f65296..00000000 --- a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController3Test.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2019 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.controller.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.junit.BeforeClass; -import org.junit.Test; -import org.kie.api.builder.ReleaseId; -import org.onap.policy.drools.controller.DroolsController; -import org.onap.policy.drools.util.KieUtils; -import org.onap.policy.drools.utils.logging.LoggerUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MavenDroolsController3Test { - private static final String DROOLS_RESOURCES_DIR = "src/test/resources/"; - private static final String DROOLS_KJAR_RESOURCES_DIR = "src/main/resources/"; - private static final String DRL_EXT = ".drl"; - private static final String POM_EXT = ".pom"; - private static final String KMODULE_EXT = ".kmodule"; - - private static final String RULES_BASE = "rules"; - private static final String KBNAME_RULES = "kbRules"; - private static final String KBSESSION_RULES = RULES_BASE; - private static final String KBPACKAGE_RULES = RULES_BASE; - - public static final CountDownLatch running1a = new CountDownLatch(1); - public static final CountDownLatch running1b = new CountDownLatch(1); - public static final CountDownLatch running2a = new CountDownLatch(1); - public static final CountDownLatch running2b = new CountDownLatch(1); - - private static final Logger logger = LoggerFactory.getLogger(MavenDroolsController3Test.class); - - private static ReleaseId install(String name, List<File> drls) throws IOException { - return - KieUtils.installArtifact( - Paths.get(DROOLS_RESOURCES_DIR + RULES_BASE + KMODULE_EXT).toFile(), - Paths.get(DROOLS_RESOURCES_DIR + name + POM_EXT).toFile(), - DROOLS_KJAR_RESOURCES_DIR + KBNAME_RULES + "/" + KBPACKAGE_RULES + "/", - drls); - } - - public static ReleaseId rulesDescriptor1; - public static ReleaseId rulesDescriptor2; - - /** - * Test Class Initialization. - */ - @BeforeClass - public static void setUpBeforeClass() throws IOException { - rulesDescriptor1 = - install("rules1", - Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile()).collect(Collectors.toList())); - - rulesDescriptor2 = - install("rules2", - Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile(), - Paths.get(DROOLS_RESOURCES_DIR + "rules2" + DRL_EXT).toFile()) - .collect(Collectors.toList())); - - LoggerUtil.setLevel("ROOT", "WARN"); - LoggerUtil.setLevel("org.onap.policy.drools.controller.internal", "INFO"); - } - - @Test - public void upgrades() throws InterruptedException { - DroolsController rules = - new MavenDroolsController( - rulesDescriptor1.getGroupId(), rulesDescriptor1.getArtifactId(), rulesDescriptor1.getVersion(), - null, null); - - assertTrue(rules.start()); - assertTrue(running1a.await(30, TimeUnit.SECONDS)); - summary(rules); - assertKie(rules, Arrays.asList("SETUP.1", "VERSION.12"), 1); - - rules.updateToVersion( - rulesDescriptor2.getGroupId(), - rulesDescriptor2.getArtifactId(), - rulesDescriptor2.getVersion(), - null, null); - - assertTrue(running2a.await(30, TimeUnit.SECONDS)); - assertTrue(running2b.await(30, TimeUnit.SECONDS)); - summary(rules); - assertKie(rules, Arrays.asList("SETUP.1", "VERSION.12", "SETUP.2", "VERSION.2"), 2); - - rules.updateToVersion( - rulesDescriptor1.getGroupId(), - rulesDescriptor1.getArtifactId(), - rulesDescriptor1.getVersion(), - null, null); - - assertTrue(running1b.await(30, TimeUnit.SECONDS)); - summary(rules); - assertKie(rules, Arrays.asList("SETUP.1", "VERSION.12"), 1); - } - - private void summary(DroolsController rules) { - logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); - logger.info("Controller: " + rules.getGroupId() + ":" + rules.getArtifactId() + ":" + rules.getVersion()); - logger.info("....................................................................."); - logger.info("KIE-BASES: " + KieUtils.getBases(rules.getContainer().getKieContainer())); - logger.info("KIE-PACKAGE-NAMES: " + KieUtils.getPackageNames(rules.getContainer().getKieContainer())); - logger.info("KIE-RULE-NAMES: " + KieUtils.getRuleNames(rules.getContainer().getKieContainer())); - logger.info("FACTS: " + rules.facts(KBSESSION_RULES, Object.class)); - logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); - } - - private void assertKie(DroolsController controller, List<String> expectedRuleNames, long expectedFactCount) { - assertEquals(Arrays.asList("kbRules"), KieUtils.getBases(controller.getContainer().getKieContainer())); - assertEquals(expectedRuleNames, KieUtils.getRuleNames(controller.getContainer().getKieContainer())); - assertEquals(expectedFactCount, controller.factCount(controller.getSessionNames().get(0))); - } -} diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerUpgradesTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerUpgradesTest.java new file mode 100644 index 00000000..604ddad6 --- /dev/null +++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerUpgradesTest.java @@ -0,0 +1,270 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.controller.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.kie.api.builder.ReleaseId; +import org.kie.api.event.rule.AfterMatchFiredEvent; +import org.kie.api.event.rule.AgendaEventListener; +import org.kie.api.event.rule.AgendaGroupPoppedEvent; +import org.kie.api.event.rule.AgendaGroupPushedEvent; +import org.kie.api.event.rule.BeforeMatchFiredEvent; +import org.kie.api.event.rule.MatchCancelledEvent; +import org.kie.api.event.rule.MatchCreatedEvent; +import org.kie.api.event.rule.ObjectDeletedEvent; +import org.kie.api.event.rule.ObjectInsertedEvent; +import org.kie.api.event.rule.ObjectUpdatedEvent; +import org.kie.api.event.rule.RuleFlowGroupActivatedEvent; +import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent; +import org.kie.api.event.rule.RuleRuntimeEventListener; +import org.onap.policy.drools.controller.DroolsController; +import org.onap.policy.drools.util.KieUtils; +import org.onap.policy.drools.utils.logging.LoggerUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MavenDroolsControllerUpgradesTest { + public static final CountDownLatch running1a = new CountDownLatch(1); + public static final CountDownLatch running1b = new CountDownLatch(1); + public static final CountDownLatch running2a = new CountDownLatch(1); + public static final CountDownLatch running2b = new CountDownLatch(1); + + private static final String DROOLS_RESOURCES_DIR = "src/test/resources/"; + private static final String DROOLS_KJAR_RESOURCES_DIR = "src/main/resources/"; + private static final String DRL_EXT = ".drl"; + private static final String POM_EXT = ".pom"; + private static final String KMODULE_EXT = ".kmodule"; + + private static final String RULES_BASE = "rules"; + private static final String KBNAME_RULES = "kbRules"; + private static final String KBSESSION_RULES = RULES_BASE; + private static final String KBPACKAGE_RULES = RULES_BASE; + + private static ReleaseId rulesDescriptor1; + private static ReleaseId rulesDescriptor2; + + private DroolsController controller; + + private static final Logger logger = LoggerFactory.getLogger(MavenDroolsControllerUpgradesTest.class); + + private static ReleaseId install(String name, List<File> drls) throws IOException { + return + KieUtils.installArtifact( + Paths.get(DROOLS_RESOURCES_DIR + RULES_BASE + KMODULE_EXT).toFile(), + Paths.get(DROOLS_RESOURCES_DIR + name + POM_EXT).toFile(), + DROOLS_KJAR_RESOURCES_DIR + KBNAME_RULES + "/" + KBPACKAGE_RULES + "/", + drls); + } + + /** + * Test Class Initialization. + */ + @BeforeClass + public static void setUpBeforeClass() throws IOException { + rulesDescriptor1 = + install("rules1", + Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile()).collect(Collectors.toList())); + + rulesDescriptor2 = + install("rules2", + Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile(), + Paths.get(DROOLS_RESOURCES_DIR + "rules2" + DRL_EXT).toFile()) + .collect(Collectors.toList())); + + LoggerUtil.setLevel("ROOT", "WARN"); + LoggerUtil.setLevel("org.onap.policy.drools.controller.internal", "INFO"); + } + + /** + * Creates a controller before each test. + */ + @Before + public void beforeTest() { + controller = + new MavenDroolsController( + rulesDescriptor1.getGroupId(), rulesDescriptor1.getArtifactId(), rulesDescriptor1.getVersion(), + null, null); + } + + /** + * Shuts down the controller after each test. + */ + @After + public void afterTest() { + if (controller != null) { + controller.halt(); + } + } + + /** + * Upgrades test. + */ + @Test + public void upgrades() throws InterruptedException { + assertTrue(controller.start()); + logKieEvents(); + + assertTrue(running1a.await(30, TimeUnit.SECONDS)); + summary(); + assertKie(Arrays.asList("SETUP.1", "VERSION.12"), 1); + + controller.updateToVersion( + rulesDescriptor2.getGroupId(), + rulesDescriptor2.getArtifactId(), + rulesDescriptor2.getVersion(), + null, null); + + assertTrue(running2a.await(30, TimeUnit.SECONDS)); + assertTrue(running2b.await(30, TimeUnit.SECONDS)); + summary(); + assertKie(Arrays.asList("SETUP.1", "VERSION.12", "SETUP.2", "VERSION.2"), 2); + + controller.updateToVersion( + rulesDescriptor1.getGroupId(), + rulesDescriptor1.getArtifactId(), + rulesDescriptor1.getVersion(), + null, null); + + assertTrue(running1b.await(30, TimeUnit.SECONDS)); + summary(); + assertKie(Arrays.asList("SETUP.1", "VERSION.12"), 1); + } + + private void summary() { + logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + logger.info("Controller: " + controller.getGroupId() + ":" + controller.getArtifactId() + + ":" + controller.getVersion()); + logger.info("....................................................................."); + logger.info("KIE-BASES: " + KieUtils.getBases(controller.getContainer().getKieContainer())); + logger.info("KIE-PACKAGE-NAMES: " + KieUtils.getPackageNames(controller.getContainer().getKieContainer())); + logger.info("KIE-RULE-NAMES: " + KieUtils.getRuleNames(controller.getContainer().getKieContainer())); + logger.info("FACTS: " + controller.facts(KBSESSION_RULES, Object.class)); + logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + } + + private void assertKie(List<String> expectedRuleNames, long expectedFactCount) { + assertEquals(Collections.singletonList("kbRules"), + KieUtils.getBases(controller.getContainer().getKieContainer())); + assertEquals(expectedRuleNames, KieUtils.getRuleNames(controller.getContainer().getKieContainer())); + assertEquals(expectedFactCount, controller.factCount(controller.getSessionNames().get(0))); + } + + private void logKieEvents() { + controller.getContainer() + .getPolicySession(KBSESSION_RULES) + .getKieSession() + .addEventListener( new RuleRuntimeEventListener() { + @Override + public void objectInserted(ObjectInsertedEvent objectInsertedEvent) { + logger.info("RULE {}: inserting {}", + objectInsertedEvent.getRule().getName(), objectInsertedEvent.getObject()); + } + + @Override + public void objectUpdated(ObjectUpdatedEvent objectUpdatedEvent) { + logger.info("RULE {}: updating {}", + objectUpdatedEvent.getRule().getName(), objectUpdatedEvent.getObject()); + } + + @Override + public void objectDeleted(ObjectDeletedEvent objectDeletedEvent) { + logger.info("RULE {}: deleting {}", + objectDeletedEvent.getRule().getName(), objectDeletedEvent.getOldObject()); + } + } ); + + controller.getContainer() + .getPolicySession(KBSESSION_RULES) + .getKieSession() + .addEventListener(new AgendaEventListener() { + @Override + public void matchCreated(MatchCreatedEvent matchCreatedEvent) { + logger.info("RULE {}: matchCreated", matchCreatedEvent.getMatch().getRule().getName()); + } + + @Override + public void matchCancelled(MatchCancelledEvent matchCancelledEvent) { + logger.info("RULE {}: matchCancelled", matchCancelledEvent.getMatch().getRule().getName()); + } + + @Override + public void beforeMatchFired(BeforeMatchFiredEvent beforeMatchFiredEvent) { + logger.info("RULE {}: beforeMatchFired", beforeMatchFiredEvent.getMatch().getRule().getName()); + } + + @Override + public void afterMatchFired(AfterMatchFiredEvent afterMatchFiredEvent) { + logger.info("RULE {}: afterMatchFired", afterMatchFiredEvent.getMatch().getRule().getName()); + } + + @Override + public void agendaGroupPopped(AgendaGroupPoppedEvent agendaGroupPoppedEvent) { + /* do nothing */ + } + + @Override + public void agendaGroupPushed(AgendaGroupPushedEvent agendaGroupPushedEvent) { + /* do nothing */ + + } + + @Override + public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent ruleFlowGroupActivatedEvent) { + /* do nothing */ + + } + + @Override + public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent ruleFlowGroupActivatedEvent) { + /* do nothing */ + + } + + @Override + public void beforeRuleFlowGroupDeactivated( + RuleFlowGroupDeactivatedEvent ruleFlowGroupDeactivatedEvent) { + /* do nothing */ + + } + + @Override + public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent ruleFlowGroupDeactivatedEvent) { + /* do nothing */ + } + } ); + } +} 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 8607fd6d..456c22f8 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 @@ -74,6 +74,7 @@ public class ProtocolCoderToolsetTest { public void testToolsets() { createController(); testGsonToolset(createFilterSet()); + DroolsControllerConstants.getFactory().destroy(); } /** |