2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.controller.internal;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.kie.api.builder.ReleaseId;
41 import org.kie.api.event.rule.AfterMatchFiredEvent;
42 import org.kie.api.event.rule.AgendaEventListener;
43 import org.kie.api.event.rule.AgendaGroupPoppedEvent;
44 import org.kie.api.event.rule.AgendaGroupPushedEvent;
45 import org.kie.api.event.rule.BeforeMatchFiredEvent;
46 import org.kie.api.event.rule.MatchCancelledEvent;
47 import org.kie.api.event.rule.MatchCreatedEvent;
48 import org.kie.api.event.rule.ObjectDeletedEvent;
49 import org.kie.api.event.rule.ObjectInsertedEvent;
50 import org.kie.api.event.rule.ObjectUpdatedEvent;
51 import org.kie.api.event.rule.RuleFlowGroupActivatedEvent;
52 import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent;
53 import org.kie.api.event.rule.RuleRuntimeEventListener;
54 import org.onap.policy.drools.controller.DroolsController;
55 import org.onap.policy.drools.util.KieUtils;
56 import org.onap.policy.drools.utils.logging.LoggerUtil;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 public class MavenDroolsControllerUpgradesTest {
61 public static final CountDownLatch running1a = new CountDownLatch(1);
62 public static final CountDownLatch running1b = new CountDownLatch(1);
63 public static final CountDownLatch running2a = new CountDownLatch(1);
64 public static final CountDownLatch running2b = new CountDownLatch(1);
66 private static final String DROOLS_RESOURCES_DIR = "src/test/resources/";
67 private static final String DROOLS_KJAR_RESOURCES_DIR = "src/main/resources/";
68 private static final String DRL_EXT = ".drl";
69 private static final String POM_EXT = ".pom";
70 private static final String KMODULE_EXT = ".kmodule";
72 private static final String RULES_BASE = "rules";
73 private static final String KBNAME_RULES = "kbRules";
74 private static final String KBSESSION_RULES = RULES_BASE;
75 private static final String KBPACKAGE_RULES = RULES_BASE;
77 private static ReleaseId rulesDescriptor1;
78 private static ReleaseId rulesDescriptor2;
80 private DroolsController controller;
82 private static final Logger logger = LoggerFactory.getLogger(MavenDroolsControllerUpgradesTest.class);
84 private static ReleaseId install(String name, List<File> drls) throws IOException {
86 KieUtils.installArtifact(
87 Paths.get(DROOLS_RESOURCES_DIR + RULES_BASE + KMODULE_EXT).toFile(),
88 Paths.get(DROOLS_RESOURCES_DIR + name + POM_EXT).toFile(),
89 DROOLS_KJAR_RESOURCES_DIR + KBNAME_RULES + "/" + KBPACKAGE_RULES + "/",
94 * Test Class Initialization.
97 public static void setUpBeforeClass() throws IOException {
100 Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile()).collect(Collectors.toList()));
104 Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile(),
105 Paths.get(DROOLS_RESOURCES_DIR + "rules2" + DRL_EXT).toFile())
106 .collect(Collectors.toList()));
108 LoggerUtil.setLevel("ROOT", "WARN");
109 LoggerUtil.setLevel("org.onap.policy.drools.controller.internal", "INFO");
113 * Creates a controller before each test.
116 public void beforeTest() {
118 new MavenDroolsController(
119 rulesDescriptor1.getGroupId(), rulesDescriptor1.getArtifactId(), rulesDescriptor1.getVersion(),
124 * Shuts down the controller after each test.
127 public void afterTest() {
128 if (controller != null) {
137 public void upgrades() throws InterruptedException {
138 assertTrue(controller.start());
141 assertTrue(running1a.await(30, TimeUnit.SECONDS));
143 assertKie(Arrays.asList("run-drools-runnable", "SETUP.1", "VERSION.12"), 1);
145 controller.updateToVersion(
146 rulesDescriptor2.getGroupId(),
147 rulesDescriptor2.getArtifactId(),
148 rulesDescriptor2.getVersion(),
151 assertTrue(running2a.await(30, TimeUnit.SECONDS));
152 assertTrue(running2b.await(30, TimeUnit.SECONDS));
154 assertKie(Arrays.asList("run-drools-runnable", "SETUP.1", "VERSION.12", "SETUP.2", "VERSION.2"), 2);
156 controller.updateToVersion(
157 rulesDescriptor1.getGroupId(),
158 rulesDescriptor1.getArtifactId(),
159 rulesDescriptor1.getVersion(),
162 assertTrue(running1b.await(30, TimeUnit.SECONDS));
164 assertKie(Arrays.asList("run-drools-runnable", "SETUP.1", "VERSION.12"), 1);
167 private void summary() {
168 logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
169 logger.info("Controller: " + controller.getGroupId() + ":" + controller.getArtifactId()
170 + ":" + controller.getVersion());
171 logger.info(".....................................................................");
172 logger.info("KIE-BASES: " + KieUtils.getBases(controller.getContainer().getKieContainer()));
173 logger.info("KIE-PACKAGE-NAMES: " + KieUtils.getPackageNames(controller.getContainer().getKieContainer()));
174 logger.info("KIE-RULE-NAMES: " + KieUtils.getRuleNames(controller.getContainer().getKieContainer()));
175 logger.info("FACTS: " + controller.facts(KBSESSION_RULES, Object.class));
176 logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
179 private void assertKie(List<String> expectedRuleNames, long expectedFactCount) {
180 assertEquals(Collections.singletonList("kbRules"),
181 KieUtils.getBases(controller.getContainer().getKieContainer()));
182 assertEquals(expectedRuleNames, KieUtils.getRuleNames(controller.getContainer().getKieContainer()));
183 assertEquals(expectedFactCount, controller.factCount(controller.getSessionNames().get(0)));
186 private void logKieEvents() {
187 controller.getContainer()
188 .getPolicySession(KBSESSION_RULES)
190 .addEventListener( new RuleRuntimeEventListener() {
192 public void objectInserted(ObjectInsertedEvent objectInsertedEvent) {
193 logger.info("RULE {}: inserting {}",
194 objectInsertedEvent.getRule().getName(), objectInsertedEvent.getObject());
198 public void objectUpdated(ObjectUpdatedEvent objectUpdatedEvent) {
199 logger.info("RULE {}: updating {}",
200 objectUpdatedEvent.getRule().getName(), objectUpdatedEvent.getObject());
204 public void objectDeleted(ObjectDeletedEvent objectDeletedEvent) {
205 logger.info("RULE {}: deleting {}",
206 objectDeletedEvent.getRule().getName(), objectDeletedEvent.getOldObject());
210 controller.getContainer()
211 .getPolicySession(KBSESSION_RULES)
213 .addEventListener(new AgendaEventListener() {
215 public void matchCreated(MatchCreatedEvent matchCreatedEvent) {
216 logger.info("RULE {}: matchCreated", matchCreatedEvent.getMatch().getRule().getName());
220 public void matchCancelled(MatchCancelledEvent matchCancelledEvent) {
221 logger.info("RULE {}: matchCancelled", matchCancelledEvent.getMatch().getRule().getName());
225 public void beforeMatchFired(BeforeMatchFiredEvent beforeMatchFiredEvent) {
226 logger.info("RULE {}: beforeMatchFired", beforeMatchFiredEvent.getMatch().getRule().getName());
230 public void afterMatchFired(AfterMatchFiredEvent afterMatchFiredEvent) {
231 logger.info("RULE {}: afterMatchFired", afterMatchFiredEvent.getMatch().getRule().getName());
235 public void agendaGroupPopped(AgendaGroupPoppedEvent agendaGroupPoppedEvent) {
240 public void agendaGroupPushed(AgendaGroupPushedEvent agendaGroupPushedEvent) {
246 public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent ruleFlowGroupActivatedEvent) {
252 public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent ruleFlowGroupActivatedEvent) {
258 public void beforeRuleFlowGroupDeactivated(
259 RuleFlowGroupDeactivatedEvent ruleFlowGroupDeactivatedEvent) {
265 public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent ruleFlowGroupDeactivatedEvent) {