0d8bdfab468ec18f124cd7add67e7d010b64fe01
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.controller.internal;
22
23 import java.io.IOException;
24 import java.nio.file.Paths;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.kie.api.builder.ReleaseId;
32 import org.onap.policy.common.utils.gson.GsonTestUtils;
33 import org.onap.policy.drools.controller.DroolsController;
34 import org.onap.policy.drools.util.KieUtils;
35
36 public class MavenDroolsControllerTest {
37
38     public static final String JUNIT_ECHO_KSESSION = "echo";
39     public static final String JUNIT_ECHO_KBASE = "onap.policies.test";
40     public static final String JUNIT_ECHO_KMODULE_DRL_PATH = "src/test/resources/echo.drl";
41     public static final String JUNIT_ECHO_KMODULE_POM_PATH = "src/test/resources/echo.pom";
42     public static final String JUNIT_ECHO_KMODULE_PATH = "src/test/resources/echo.kmodule";
43     public static final String JUNIT_ECHO_KJAR_DRL_PATH =
44         "src/main/resources/kbEcho/org/onap/policy/drools/test/echo.drl";
45
46     private static volatile ReleaseId releaseId;
47
48     private static volatile CountDownLatch running;
49
50     /**
51      * Set up.
52      *
53      * @throws IOException throws an IO exception
54      */
55     @BeforeClass
56     public static void setUpBeforeClass() throws IOException {
57         releaseId =
58             KieUtils.installArtifact(Paths.get(JUNIT_ECHO_KMODULE_PATH).toFile(),
59                 Paths.get(JUNIT_ECHO_KMODULE_POM_PATH).toFile(),
60                 JUNIT_ECHO_KJAR_DRL_PATH,
61                 Paths.get(JUNIT_ECHO_KMODULE_DRL_PATH).toFile());
62     }
63
64     @Before
65     public void setUp() {
66         running = new CountDownLatch(1);
67     }
68
69     public static void setRunning() {
70         running.countDown();
71     }
72
73     @Test
74     public void stop() throws InterruptedException {
75         createDroolsController(10000L).stop();
76     }
77
78     @Test
79     public void shutdown() throws InterruptedException {
80         createDroolsController(10000L).shutdown();
81     }
82
83     @Test
84     public void lock() throws InterruptedException {
85         DroolsController controller = createDroolsController(30000L);
86
87         controller.lock();
88         Assert.assertTrue(controller.isLocked());
89
90         controller.unlock();
91         Assert.assertFalse(controller.isLocked());
92
93         controller.halt();
94         Assert.assertFalse(controller.isAlive());
95
96         new GsonTestUtils().compareGson(controller, MavenDroolsControllerTest.class);
97     }
98
99     private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
100         if (releaseId == null) {
101             throw new IllegalStateException("no prereq artifact installed in maven repository");
102         }
103
104         DroolsController controller = new MavenDroolsController(releaseId.getGroupId(),
105             releaseId.getArtifactId(), releaseId.getVersion(), null, null);
106
107         Assert.assertFalse(controller.isAlive());
108         Assert.assertTrue(controller.isBrained());
109
110         controller.start();
111
112         Assert.assertTrue(controller.isAlive());
113         Assert.assertTrue(controller.isBrained());
114
115         Assert.assertEquals(releaseId.getGroupId(), controller.getGroupId());
116         Assert.assertEquals(releaseId.getArtifactId(), controller.getArtifactId());
117         Assert.assertEquals(releaseId.getVersion(), controller.getVersion());
118
119         Assert.assertEquals(releaseId.getGroupId(), controller.getContainer().getGroupId());
120         Assert.assertEquals(releaseId.getArtifactId(), controller.getContainer().getArtifactId());
121         Assert.assertEquals(releaseId.getVersion(), controller.getContainer().getVersion());
122
123         /* allow full initialization from local maven repository */
124         Assert.assertTrue(running.await(courtesyStartTimeMs, TimeUnit.MILLISECONDS));
125
126         Assert.assertEquals(1, controller.getSessionNames().size());
127         Assert.assertEquals(JUNIT_ECHO_KSESSION, controller.getSessionNames().get(0));
128         Assert.assertEquals(1, controller.getCanonicalSessionNames().size());
129         Assert.assertTrue(controller.getCanonicalSessionNames().get(0).contains(JUNIT_ECHO_KSESSION));
130
131         Assert.assertEquals(JUNIT_ECHO_KBASE, String.join(",", controller.getBaseDomainNames()));
132
133         return controller;
134     }
135 }