Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-management / src / test / java / org / onap / policy / drools / controller / internal / MavenDroolsControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.controller.internal;
23
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;
27
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;
39
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/";
47
48     private static volatile ReleaseId releaseId;
49     private static volatile CountDownLatch running;
50
51     /**
52      * Set up.
53      *
54      * @throws IOException throws an IO exception
55      */
56     @BeforeAll
57     public static void setUpBeforeClass() throws IOException {
58         releaseId =
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());
63     }
64
65     @BeforeEach
66     public void setUp() {
67         running = new CountDownLatch(1);
68     }
69
70     public static void setRunning() {
71         running.countDown();
72     }
73
74     @Test
75     void stop() throws InterruptedException {
76         createDroolsController(10000L).stop();
77     }
78
79     @Test
80     void shutdown() throws InterruptedException {
81         createDroolsController(10000L).shutdown();
82     }
83
84     @Test
85     void testLock() throws InterruptedException {
86         DroolsController controller = createDroolsController(30000L);
87
88         controller.lock();
89         assertTrue(controller.isLocked());
90
91         controller.unlock();
92         assertFalse(controller.isLocked());
93
94         controller.halt();
95         assertFalse(controller.isAlive());
96
97         new GsonTestUtils().compareGson(controller, MavenDroolsControllerTest.class);
98     }
99
100     @Test
101     void testFact() throws InterruptedException {
102         DroolsController controller = createDroolsController(30000L);
103
104         Integer one = 1;
105         Integer two = 2;
106
107         assertTrue(controller.offer(one));
108         assertTrue(controller.exists(one));
109         assertFalse(controller.exists(two));
110
111         assertTrue(controller.offer(two));
112         assertTrue(controller.exists(one));
113         assertTrue(controller.exists(two));
114
115         Integer three = 3;
116         assertFalse(controller.delete(three));
117         assertTrue(controller.exists(one));
118         assertTrue(controller.exists(two));
119
120         assertTrue(controller.delete(two));
121         assertTrue(controller.exists(one));
122         assertFalse(controller.exists(two));
123
124         assertTrue(controller.delete(one));
125         assertFalse(controller.exists(one));
126         assertFalse(controller.exists(two));
127     }
128
129     private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
130         if (releaseId == null) {
131             throw new IllegalStateException("no prereq artifact installed in maven repository");
132         }
133
134         DroolsController controller = new MavenDroolsController(releaseId.getGroupId(),
135             releaseId.getArtifactId(), releaseId.getVersion(), null, null);
136
137         assertFalse(controller.isAlive());
138         assertTrue(controller.isBrained());
139
140         controller.start();
141
142         assertTrue(controller.isAlive());
143         assertTrue(controller.isBrained());
144
145         assertEquals(releaseId.getGroupId(), controller.getGroupId());
146         assertEquals(releaseId.getArtifactId(), controller.getArtifactId());
147         assertEquals(releaseId.getVersion(), controller.getVersion());
148
149         assertEquals(releaseId.getGroupId(), controller.getContainer().getGroupId());
150         assertEquals(releaseId.getArtifactId(), controller.getContainer().getArtifactId());
151         assertEquals(releaseId.getVersion(), controller.getContainer().getVersion());
152
153         /* allow full initialization from local maven repository */
154         assertTrue(running.await(courtesyStartTimeMs, TimeUnit.MILLISECONDS));
155
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));
160
161         assertEquals(JUNIT_ECHO_KBASE, String.join(",", controller.getBaseDomainNames()));
162
163         return controller;
164     }
165 }