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.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.io.IOException;
29 import java.nio.file.Paths;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.kie.api.builder.ReleaseId;
36 import org.onap.policy.common.utils.gson.GsonTestUtils;
37 import org.onap.policy.drools.controller.DroolsController;
38 import org.onap.policy.drools.util.KieUtils;
40 public class MavenDroolsControllerTest {
41 public static final String JUNIT_ECHO_KSESSION = "echo";
42 public static final String JUNIT_ECHO_KBASE = "onap.policies.test";
43 public static final String JUNIT_ECHO_KMODULE_DRL_PATH = "src/test/resources/echo.drl";
44 public static final String JUNIT_ECHO_KMODULE_POM_PATH = "src/test/resources/echo.pom";
45 public static final String JUNIT_ECHO_KMODULE_PATH = "src/test/resources/echo.kmodule";
46 public static final String JUNIT_ECHO_KJAR_DRL_PATH = "src/main/resources/kbEcho/org/onap/policy/drools/test/";
48 private static volatile ReleaseId releaseId;
49 private static volatile CountDownLatch running;
54 * @throws IOException throws an IO exception
57 public static void setUpBeforeClass() throws IOException {
59 KieUtils.installArtifact(Paths.get(JUNIT_ECHO_KMODULE_PATH).toFile(),
60 Paths.get(JUNIT_ECHO_KMODULE_POM_PATH).toFile(),
61 JUNIT_ECHO_KJAR_DRL_PATH,
62 Paths.get(JUNIT_ECHO_KMODULE_DRL_PATH).toFile());
67 running = new CountDownLatch(1);
70 public static void setRunning() {
75 void stop() throws InterruptedException {
76 createDroolsController(10000L).stop();
80 void shutdown() throws InterruptedException {
81 createDroolsController(10000L).shutdown();
85 void testLock() throws InterruptedException {
86 DroolsController controller = createDroolsController(30000L);
89 assertTrue(controller.isLocked());
92 assertFalse(controller.isLocked());
95 assertFalse(controller.isAlive());
97 new GsonTestUtils().compareGson(controller, MavenDroolsControllerTest.class);
101 void testFact() throws InterruptedException {
102 DroolsController controller = createDroolsController(30000L);
107 assertTrue(controller.offer(one));
108 assertTrue(controller.exists(one));
109 assertFalse(controller.exists(two));
111 assertTrue(controller.offer(two));
112 assertTrue(controller.exists(one));
113 assertTrue(controller.exists(two));
116 assertFalse(controller.delete(three));
117 assertTrue(controller.exists(one));
118 assertTrue(controller.exists(two));
120 assertTrue(controller.delete(two));
121 assertTrue(controller.exists(one));
122 assertFalse(controller.exists(two));
124 assertTrue(controller.delete(one));
125 assertFalse(controller.exists(one));
126 assertFalse(controller.exists(two));
129 private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
130 if (releaseId == null) {
131 throw new IllegalStateException("no prereq artifact installed in maven repository");
134 DroolsController controller = new MavenDroolsController(releaseId.getGroupId(),
135 releaseId.getArtifactId(), releaseId.getVersion(), null, null);
137 assertFalse(controller.isAlive());
138 assertTrue(controller.isBrained());
142 assertTrue(controller.isAlive());
143 assertTrue(controller.isBrained());
145 assertEquals(releaseId.getGroupId(), controller.getGroupId());
146 assertEquals(releaseId.getArtifactId(), controller.getArtifactId());
147 assertEquals(releaseId.getVersion(), controller.getVersion());
149 assertEquals(releaseId.getGroupId(), controller.getContainer().getGroupId());
150 assertEquals(releaseId.getArtifactId(), controller.getContainer().getArtifactId());
151 assertEquals(releaseId.getVersion(), controller.getContainer().getVersion());
153 /* allow full initialization from local maven repository */
154 assertTrue(running.await(courtesyStartTimeMs, TimeUnit.MILLISECONDS));
156 assertEquals(1, controller.getSessionNames().size());
157 assertEquals(JUNIT_ECHO_KSESSION, controller.getSessionNames().get(0));
158 assertEquals(1, controller.getCanonicalSessionNames().size());
159 assertTrue(controller.getCanonicalSessionNames().get(0).contains(JUNIT_ECHO_KSESSION));
161 assertEquals(JUNIT_ECHO_KBASE, String.join(",", controller.getBaseDomainNames()));