Add 'DroolsRunnable' class
[policy/drools-pdp.git] / policy-core / src / test / java / org / onap / policy / drools / core / DroolsContainerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-core
4  * ================================================================================
5  * Copyright (C) 2017-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.core;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import java.nio.file.Paths;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.concurrent.LinkedBlockingQueue;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.drools.util.KieUtils;
36
37 /**
38  * These tests focus on the following classes.
39  *     PolicyContainer
40  *     PolicySession
41  *     PolicySessionFeatureAPI
42  */
43 public class DroolsContainerTest {
44     
45     private static final long TIMEOUT_SEC = 5;
46
47     /**
48      * This test is centered around the creation of a 'PolicyContainer'
49      * and 'PolicySession', and the updating of that container to a new
50      * version.
51      */
52     @BeforeClass
53     public static void setUp() throws Exception {
54         KieUtils.installArtifact(
55                 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
56                 Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
57                 "src/main/resources/rules/org/onap/policy/drools/core/test/",
58                 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());
59
60         KieUtils.installArtifact(
61                 Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/META-INF/kmodule.xml").toFile(),
62                 Paths.get("src/test/resources/drools-artifact-1.2/pom.xml").toFile(),
63                 "src/main/resources/rules/org/onap/policy/drools/core/test/",
64                 Paths.get("src/test/resources/drools-artifact-1.2/src/main/resources/rules.drl").toFile());
65     }
66
67     /**
68      * This test is centered around the creation of a 'PolicyContainer'
69      * and 'PolicySession', and the updating of that container to a new
70      * version.
71      */
72     @Test
73     public void createAndUpdate() throws Exception {
74         // make sure feature log starts out clean
75         PolicySessionFeatureApiMock.getLog();
76
77         // run 'globalInit', and verify expected feature hook fired
78         PolicyContainer.globalInit(new String[0]);
79         assertEquals(Arrays.asList("globalInit"),
80                 PolicySessionFeatureApiMock.getLog());
81
82         // initial conditions -- there should be no containers
83         assertEquals(0, PolicyContainer.getPolicyContainers().size());
84
85         // create the container, and start it
86         PolicyContainer container =
87                 new PolicyContainer("org.onap.policy.drools-pdp",
88                         "drools-artifact1", "17.1.0-SNAPSHOT");
89         container.start();
90         assertTrue(container.isAlive());
91
92         // verify expected feature hooks fired
93         assertEquals(Arrays.asList("activatePolicySession",
94                 "newPolicySession",
95                 "selectThreadModel"),
96                 PolicySessionFeatureApiMock.getLog());
97
98         // this container should be on the list
99         {
100             Collection<PolicyContainer> containers =
101                     PolicyContainer.getPolicyContainers();
102             assertEquals(1, containers.size());
103             assertTrue(containers.contains(container));
104         }
105
106         // verify initial container attributes
107         assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT",
108                 container.getName());
109         assertEquals("org.onap.policy.drools-pdp", container.getGroupId());
110         assertEquals("drools-artifact1", container.getArtifactId());
111         assertEquals("17.1.0-SNAPSHOT", container.getVersion());
112
113         try {
114             // fetch the session, and verify that it exists
115             PolicySession session = container.getPolicySession("session1");
116             assertTrue(session != null);
117
118             // get all sessions, and verify that this one is the only one
119             {
120                 Collection<PolicySession> sessions = container.getPolicySessions();
121                 assertEquals(1, sessions.size());
122                 assertTrue(sessions.contains(session));
123             }
124
125             // verify session attributes
126             assertEquals(container, session.getPolicyContainer());
127             assertEquals("session1", session.getName());
128             assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT:session1",
129                     session.getFullName());
130
131             // insert a new fact
132             LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>();
133             session.getKieSession().insert(Arrays.asList(3, 8, 2));
134             session.getKieSession().insert(result);
135
136             // the Drools rules should add 3 + 8 + 2, and store 13 in a[0]
137             assertEquals(13, result.poll(TIMEOUT_SEC, TimeUnit.SECONDS).intValue());
138
139             // update the container to a new version --
140             // the rules will then multiply values rather than add them
141             assertEquals("[]",
142                     container.updateToVersion("17.2.0-SNAPSHOT").toString());
143
144             // verify expected feature hooks fired
145             assertEquals(Arrays.asList("selectThreadModel"),
146                     PolicySessionFeatureApiMock.getLog());
147
148             // verify new container attributes
149             assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT",
150                     container.getName());
151             assertEquals("org.onap.policy.drools-pdp", container.getGroupId());
152             assertEquals("drools-artifact1", container.getArtifactId());
153             assertEquals("17.2.0-SNAPSHOT", container.getVersion());
154
155             // verify new session attributes
156             assertEquals(container, session.getPolicyContainer());
157             assertEquals("session1", session.getName());
158             assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.2.0-SNAPSHOT:session1",
159                     session.getFullName());
160
161             // the updated rules should now multiply 3 * 8 * 2, and return 48
162             
163             result = new LinkedBlockingQueue<>();
164             container.insert("session1", Arrays.asList(3, 8, 2));
165             container.insert("session1", result);
166
167             assertEquals(48, result.poll(TIMEOUT_SEC, TimeUnit.SECONDS).intValue());
168
169             // verify that default KiePackages have been added by testing
170             // that 'DroolsRunnable' is functioning
171
172             final LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<>();
173             container.insert("session1", new DroolsRunnable() {
174                     @Override
175                     public void run() {
176                         lbq.add("DroolsRunnable String");
177                     }
178                 });
179             assertEquals("DroolsRunnable String", lbq.poll(TIMEOUT_SEC, TimeUnit.SECONDS));
180         } finally {
181             container.shutdown();
182             assertFalse(container.isAlive());
183
184             // verify expected feature hooks fired
185             assertEquals(Arrays.asList("disposeKieSession"),
186                     PolicySessionFeatureApiMock.getLog());
187         }
188
189         // final conditions -- there should be no containers
190         assertEquals(0, PolicyContainer.getPolicyContainers().size());
191     }
192
193     /**
194      * This test create a 'PolicyContainer' and 'PolicySession', and verifies
195      * their behavior, but uses alternate interfaces to increase code coverage.
196      * In addition, feature hook invocations will trigger exceptions in this
197      * test, also to increase code coverage.
198      */
199     @Test
200     public void versionList() throws Exception {
201         // make sure feature log starts out clean
202         PolicySessionFeatureApiMock.getLog();
203
204         // trigger exceptions in all feature hooks
205         PolicySessionFeatureApiMock.setExceptionTrigger(true);
206
207         // run 'globalInit', and verify expected feature hook fired
208         PolicyContainer.globalInit(new String[0]);
209         assertEquals(Arrays.asList("globalInit-exception"),
210                 PolicySessionFeatureApiMock.getLog());
211
212         // initial conditions -- there should be no containers
213         assertEquals(0, PolicyContainer.getPolicyContainers().size());
214
215         String versionList =
216                 "17.3.0-SNAPSHOT,17.1.0-SNAPSHOT,17.2.0-SNAPSHOT";
217
218         // versions should be tried in order -- the 17.1.0-SNAPSHOT should "win",
219         // given the fact that '17.3.0-SNAPSHOT' doesn't exist
220         PolicyContainer container =
221                 new PolicyContainer("org.onap.policy.drools-pdp",
222                         "drools-artifact1", versionList);
223         // the following should be equivalent to 'container.start()'
224         PolicyContainer.activate();
225         assertTrue(container.isAlive());
226
227         // verify expected feature hooks fired
228         assertEquals(Arrays.asList("activatePolicySession-exception",
229                 "newPolicySession-exception",
230                 "selectThreadModel-exception"),
231                 PolicySessionFeatureApiMock.getLog());
232
233         // this container should be on the list
234         {
235             Collection<PolicyContainer> containers =
236                     PolicyContainer.getPolicyContainers();
237             assertEquals(1, containers.size());
238             assertTrue(containers.contains(container));
239         }
240
241         // verify initial container attributes
242         assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT",
243                 container.getName());
244         assertEquals("org.onap.policy.drools-pdp", container.getGroupId());
245         assertEquals("drools-artifact1", container.getArtifactId());
246         assertEquals("17.1.0-SNAPSHOT", container.getVersion());
247
248         // some container adjunct tests
249         {
250             Object bogusAdjunct = new Object();
251
252             // initially, no adjunct
253             assertSame(null, container.getAdjunct(this));
254
255             // set and verify adjunct
256             container.setAdjunct(this, bogusAdjunct);
257             assertSame(bogusAdjunct, container.getAdjunct(this));
258
259             // clear and verify adjunct
260             container.setAdjunct(this, null);
261             assertSame(null, container.getAdjunct(this));
262         }
263
264         try {
265             // fetch the session, and verify that it exists
266             PolicySession session = container.getPolicySession("session1");
267             assertTrue(session != null);
268
269             // get all sessions, and verify that this one is the only one
270             {
271                 Collection<PolicySession> sessions = container.getPolicySessions();
272                 assertEquals(1, sessions.size());
273                 assertTrue(sessions.contains(session));
274             }
275
276             // verify session attributes
277             assertEquals(container, session.getPolicyContainer());
278             assertEquals("session1", session.getName());
279             assertEquals("org.onap.policy.drools-pdp:drools-artifact1:17.1.0-SNAPSHOT:session1",
280                     session.getFullName());
281
282             // some session adjunct tests
283             {
284                 Object bogusAdjunct = new Object();
285
286                 // initially, no adjunct
287                 assertSame(null, session.getAdjunct(this));
288
289                 // set and verify adjunct
290                 session.setAdjunct(this, bogusAdjunct);
291                 assertSame(bogusAdjunct, session.getAdjunct(this));
292
293                 // clear and verify adjunct
294                 session.setAdjunct(this, null);
295                 assertSame(null, session.getAdjunct(this));
296             }
297
298             // insert a new fact (using 'insertAll')
299             LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>();
300             container.insertAll(Arrays.asList(7, 3, 4));
301             container.insertAll(result);
302
303             // the Drools rules should add 7 + 3 + 4, and store 14 in a[0]
304             assertEquals(14, result.poll(TIMEOUT_SEC, TimeUnit.SECONDS).intValue());
305
306             // exercise some more API methods
307             assertEquals(container.getClassLoader(),
308                     container.getKieContainer().getClassLoader());
309         } finally {
310             // should be equivalent to 'shutdown' without persistence
311             container.destroy();
312             assertFalse(container.isAlive());
313
314             // verify expected feature hooks fired
315             assertEquals(Arrays.asList("destroyKieSession-exception"),
316                     PolicySessionFeatureApiMock.getLog());
317
318             // clear exception trigger
319             PolicySessionFeatureApiMock.setExceptionTrigger(false);
320         }
321
322         // final conditions -- there should be no containers
323         assertEquals(0, PolicyContainer.getPolicyContainers().size());
324     }
325 }