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