5df6eb8dea83b25163ace6ef9702866e722ece2b
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 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 org.junit.Assert;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.kie.api.builder.ReleaseId;
29 import org.onap.policy.drools.controller.DroolsController;
30 import org.onap.policy.drools.util.KieUtils;
31
32 public class MavenDroolsControllerTest {
33
34     public static final String JUNIT_ECHO_KSESSION = "echo";
35     public static final String JUNIT_ECHO_KMODULE_DRL_PATH = "src/test/resources/echo.drl";
36     public static final String JUNIT_ECHO_KMODULE_POM_PATH = "src/test/resources/echo.pom";
37     public static final String JUNIT_ECHO_KMODULE_PATH = "src/test/resources/echo.kmodule";
38     public static final String JUNIT_ECHO_KJAR_DRL_PATH =
39         "src/main/resources/kbEcho/org/onap/policy/drools/test/echo.drl";
40
41     private static volatile ReleaseId releaseId;
42
43     /**
44      * Set up.
45      * 
46      * @throws IOException throws an IO exception
47      */
48     @BeforeClass
49     public static void setUp() throws IOException {
50         releaseId =
51             KieUtils.installArtifact(Paths.get(JUNIT_ECHO_KMODULE_PATH).toFile(),
52                 Paths.get(JUNIT_ECHO_KMODULE_POM_PATH).toFile(),
53                 JUNIT_ECHO_KJAR_DRL_PATH,
54                 Paths.get(JUNIT_ECHO_KMODULE_DRL_PATH).toFile());
55     }
56
57     @Test
58     public void stop() throws IOException, InterruptedException {
59         createDroolsController(10000L).stop();
60     }
61
62     @Test
63     public void shutdown() throws IOException, InterruptedException {
64         createDroolsController(10000L).shutdown();
65     }
66
67     @Test
68     public void lock() throws IOException, InterruptedException {
69         DroolsController controller = createDroolsController(30000L);
70
71         controller.lock();
72         Assert.assertTrue(controller.isLocked());
73
74         controller.unlock();
75         Assert.assertFalse(controller.isLocked());
76
77         controller.halt();
78         Assert.assertFalse(controller.isAlive());
79     }
80
81     private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
82         if (releaseId == null) {
83             throw new IllegalStateException("no prereq artifact installed in maven repository");
84         }
85
86         DroolsController controller = new MavenDroolsController(releaseId.getGroupId(),
87             releaseId.getArtifactId(), releaseId.getVersion(), null, null);
88
89         Assert.assertFalse(controller.isAlive());
90         Assert.assertTrue(controller.isBrained());
91
92         controller.start();
93
94         Assert.assertTrue(controller.isAlive());
95         Assert.assertTrue(controller.isBrained());
96
97         Assert.assertEquals(releaseId.getGroupId(), controller.getGroupId());
98         Assert.assertEquals(releaseId.getArtifactId(), controller.getArtifactId());
99         Assert.assertEquals(releaseId.getVersion(), controller.getVersion());
100
101         Assert.assertEquals(releaseId.getGroupId(), controller.getContainer().getGroupId());
102         Assert.assertEquals(releaseId.getArtifactId(), controller.getContainer().getArtifactId());
103         Assert.assertEquals(releaseId.getVersion(), controller.getContainer().getVersion());
104
105         /* courtesy timer to allow full initialization from local maven repository */
106         Thread.sleep(courtesyStartTimeMs);
107
108         Assert.assertTrue(controller.getSessionNames().size() == 1);
109         Assert.assertEquals(JUNIT_ECHO_KSESSION, controller.getSessionNames().get(0));
110         Assert.assertTrue(controller.getCanonicalSessionNames().size() == 1);
111         Assert.assertTrue(controller.getCanonicalSessionNames().get(0).contains(JUNIT_ECHO_KSESSION));
112
113         return controller;
114     }
115 }