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