2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.controller.internal;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.io.IOException;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import java.util.stream.Collectors;
37 import java.util.stream.Stream;
38 import org.junit.jupiter.api.AfterEach;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.kie.api.builder.ReleaseId;
43 import org.kie.api.event.rule.AfterMatchFiredEvent;
44 import org.kie.api.event.rule.AgendaEventListener;
45 import org.kie.api.event.rule.AgendaGroupPoppedEvent;
46 import org.kie.api.event.rule.AgendaGroupPushedEvent;
47 import org.kie.api.event.rule.BeforeMatchFiredEvent;
48 import org.kie.api.event.rule.MatchCancelledEvent;
49 import org.kie.api.event.rule.MatchCreatedEvent;
50 import org.kie.api.event.rule.ObjectDeletedEvent;
51 import org.kie.api.event.rule.ObjectInsertedEvent;
52 import org.kie.api.event.rule.ObjectUpdatedEvent;
53 import org.kie.api.event.rule.RuleFlowGroupActivatedEvent;
54 import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent;
55 import org.kie.api.event.rule.RuleRuntimeEventListener;
56 import org.onap.policy.common.utils.logging.LoggerUtils;
57 import org.onap.policy.drools.controller.DroolsController;
58 import org.onap.policy.drools.util.KieUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 public class MavenDroolsControllerUpgradesTest {
63 public static final CountDownLatch running1a = new CountDownLatch(1);
64 public static final CountDownLatch running1b = new CountDownLatch(1);
65 public static final CountDownLatch running2a = new CountDownLatch(1);
66 public static final CountDownLatch running2b = new CountDownLatch(1);
68 private static final String DROOLS_RESOURCES_DIR = "src/test/resources/";
69 private static final String DROOLS_KJAR_RESOURCES_DIR = "src/main/resources/";
70 private static final String DRL_EXT = ".drl";
71 private static final String POM_EXT = ".pom";
72 private static final String KMODULE_EXT = ".kmodule";
74 private static final String RULES_BASE = "rules";
75 private static final String KBNAME_RULES = "kbRules";
76 private static final String KBSESSION_RULES = RULES_BASE;
77 private static final String KBPACKAGE_RULES = RULES_BASE;
79 private static ReleaseId rulesDescriptor1;
80 private static ReleaseId rulesDescriptor2;
82 private DroolsController controller;
84 private static final Logger logger = LoggerFactory.getLogger(MavenDroolsControllerUpgradesTest.class);
86 private static ReleaseId install(String name, List<File> drls) throws IOException {
88 KieUtils.installArtifact(
89 Paths.get(DROOLS_RESOURCES_DIR + RULES_BASE + KMODULE_EXT).toFile(),
90 Paths.get(DROOLS_RESOURCES_DIR + name + POM_EXT).toFile(),
91 DROOLS_KJAR_RESOURCES_DIR + KBNAME_RULES + "/" + KBPACKAGE_RULES + "/",
96 * Test Class Initialization.
99 public static void setUpBeforeClass() throws IOException {
100 Path path = Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT);
103 Stream.of(path.toFile()).collect(Collectors.toList()));
107 Stream.of(path.toFile(),
108 Paths.get(DROOLS_RESOURCES_DIR + "rules2" + DRL_EXT).toFile())
109 .collect(Collectors.toList()));
111 LoggerUtils.setLevel("ROOT", "WARN");
112 LoggerUtils.setLevel("org.onap.policy.drools.controller.internal", "INFO");
116 * Creates a controller before each test.
119 public void beforeTest() {
121 new MavenDroolsController(
122 rulesDescriptor1.getGroupId(), rulesDescriptor1.getArtifactId(), rulesDescriptor1.getVersion(),
127 * Shuts down the controller after each test.
130 public void afterTest() {
131 if (controller != null) {
140 void upgrades() throws InterruptedException {
141 assertTrue(controller.start());
144 assertTrue(running1a.await(30, TimeUnit.SECONDS));
146 assertKie(Arrays.asList("run-drools-runnable", "SETUP.1", "VERSION.12"));
148 controller.updateToVersion(
149 rulesDescriptor2.getGroupId(),
150 rulesDescriptor2.getArtifactId(),
151 rulesDescriptor2.getVersion(),
154 assertTrue(running2a.await(30, TimeUnit.SECONDS));
155 assertTrue(running2b.await(30, TimeUnit.SECONDS));
156 assertTrue(running1b.await(30, TimeUnit.SECONDS));
158 assertKie(Arrays.asList("run-drools-runnable", "SETUP.1", "VERSION.12", "SETUP.2", "VERSION.2"));
160 controller.updateToVersion(
161 rulesDescriptor1.getGroupId(),
162 rulesDescriptor1.getArtifactId(),
163 rulesDescriptor1.getVersion(),
167 assertKie(Arrays.asList("run-drools-runnable", "SETUP.1", "VERSION.12"));
170 private void summary() {
171 logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
172 logger.info("Controller: " + controller.getGroupId() + ":" + controller.getArtifactId()
173 + ":" + controller.getVersion());
174 logger.info(".....................................................................");
175 logger.info("KIE-BASES: " + KieUtils.getBases(controller.getContainer().getKieContainer()));
176 logger.info("KIE-PACKAGE-NAMES: " + KieUtils.getPackageNames(controller.getContainer().getKieContainer()));
177 logger.info("KIE-RULE-NAMES: " + KieUtils.getRuleNames(controller.getContainer().getKieContainer()));
178 logger.info("FACTS: " + controller.facts(KBSESSION_RULES, Object.class));
179 logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
182 private void assertKie(List<String> expectedRuleNames) {
183 assertEquals(Collections.singletonList("kbRules"),
184 KieUtils.getBases(controller.getContainer().getKieContainer()));
185 assertEquals(expectedRuleNames, KieUtils.getRuleNames(controller.getContainer().getKieContainer()));
186 assertEquals(1, controller.factCount(controller.getSessionNames().get(0)));
189 private void logKieEvents() {
190 controller.getContainer()
191 .getPolicySession(KBSESSION_RULES)
193 .addEventListener(new RuleRuntimeEventListener() {
195 public void objectInserted(ObjectInsertedEvent objectInsertedEvent) {
196 logger.info("RULE {}: inserting {}",
197 objectInsertedEvent.getRule().getName(), objectInsertedEvent.getObject());
201 public void objectUpdated(ObjectUpdatedEvent objectUpdatedEvent) {
202 logger.info("RULE {}: updating {}",
203 objectUpdatedEvent.getRule().getName(), objectUpdatedEvent.getObject());
207 public void objectDeleted(ObjectDeletedEvent objectDeletedEvent) {
208 logger.info("RULE {}: deleting {}",
209 objectDeletedEvent.getRule().getName(), objectDeletedEvent.getOldObject());
213 controller.getContainer()
214 .getPolicySession(KBSESSION_RULES)
216 .addEventListener(new AgendaEventListener() {
218 public void matchCreated(MatchCreatedEvent matchCreatedEvent) {
219 logger.info("RULE {}: matchCreated", matchCreatedEvent.getMatch().getRule().getName());
223 public void matchCancelled(MatchCancelledEvent matchCancelledEvent) {
224 logger.info("RULE {}: matchCancelled", matchCancelledEvent.getMatch().getRule().getName());
228 public void beforeMatchFired(BeforeMatchFiredEvent beforeMatchFiredEvent) {
229 logger.info("RULE {}: beforeMatchFired", beforeMatchFiredEvent.getMatch().getRule().getName());
233 public void afterMatchFired(AfterMatchFiredEvent afterMatchFiredEvent) {
234 logger.info("RULE {}: afterMatchFired", afterMatchFiredEvent.getMatch().getRule().getName());
238 public void agendaGroupPopped(AgendaGroupPoppedEvent agendaGroupPoppedEvent) {
243 public void agendaGroupPushed(AgendaGroupPushedEvent agendaGroupPushedEvent) {
249 public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent ruleFlowGroupActivatedEvent) {
255 public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent ruleFlowGroupActivatedEvent) {
261 public void beforeRuleFlowGroupDeactivated(
262 RuleFlowGroupDeactivatedEvent ruleFlowGroupDeactivatedEvent) {
268 public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent ruleFlowGroupDeactivatedEvent) {