2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.core;
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;
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;
38 * These tests focus on the following classes.
41 * PolicySessionFeatureAPI
43 public class DroolsContainerTest {
45 private static final long TIMEOUT_SEC = 5;
48 * This test is centered around the creation of a 'PolicyContainer'
49 * and 'PolicySession', and the updating of that container to a new
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());
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());
68 * This test is centered around the creation of a 'PolicyContainer'
69 * and 'PolicySession', and the updating of that container to a new
73 public void createAndUpdate() throws Exception {
74 // make sure feature log starts out clean
75 PolicySessionFeatureApiMock.getLog();
77 // run 'globalInit', and verify expected feature hook fired
78 PolicyContainer.globalInit(new String[0]);
79 assertEquals(Arrays.asList("globalInit"),
80 PolicySessionFeatureApiMock.getLog());
82 // initial conditions -- there should be no containers
83 assertEquals(0, PolicyContainer.getPolicyContainers().size());
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");
90 assertTrue(container.isAlive());
92 // verify expected feature hooks fired
93 assertEquals(Arrays.asList("activatePolicySession",
96 PolicySessionFeatureApiMock.getLog());
98 // this container should be on the list
100 Collection<PolicyContainer> containers =
101 PolicyContainer.getPolicyContainers();
102 assertEquals(1, containers.size());
103 assertTrue(containers.contains(container));
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());
114 // fetch the session, and verify that it exists
115 PolicySession session = container.getPolicySession("session1");
116 assertTrue(session != null);
118 // get all sessions, and verify that this one is the only one
120 Collection<PolicySession> sessions = container.getPolicySessions();
121 assertEquals(1, sessions.size());
122 assertTrue(sessions.contains(session));
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());
132 LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>();
133 session.getKieSession().insert(Arrays.asList(3, 8, 2));
134 session.getKieSession().insert(result);
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());
139 // update the container to a new version --
140 // the rules will then multiply values rather than add them
142 container.updateToVersion("17.2.0-SNAPSHOT").toString());
144 // verify expected feature hooks fired
145 assertEquals(Arrays.asList("selectThreadModel"),
146 PolicySessionFeatureApiMock.getLog());
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());
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());
161 // the updated rules should now multiply 3 * 8 * 2, and return 48
163 result = new LinkedBlockingQueue<>();
164 container.insert("session1", Arrays.asList(3, 8, 2));
165 container.insert("session1", result);
167 assertEquals(48, result.poll(TIMEOUT_SEC, TimeUnit.SECONDS).intValue());
169 // verify that default KiePackages have been added by testing
170 // that 'DroolsRunnable' is functioning
172 final LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<>();
173 container.insert("session1", new DroolsRunnable() {
176 lbq.add("DroolsRunnable String");
179 assertEquals("DroolsRunnable String", lbq.poll(TIMEOUT_SEC, TimeUnit.SECONDS));
181 container.shutdown();
182 assertFalse(container.isAlive());
184 // verify expected feature hooks fired
185 assertEquals(Arrays.asList("disposeKieSession"),
186 PolicySessionFeatureApiMock.getLog());
189 // final conditions -- there should be no containers
190 assertEquals(0, PolicyContainer.getPolicyContainers().size());
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.
200 public void versionList() throws Exception {
201 // make sure feature log starts out clean
202 PolicySessionFeatureApiMock.getLog();
204 // trigger exceptions in all feature hooks
205 PolicySessionFeatureApiMock.setExceptionTrigger(true);
207 // run 'globalInit', and verify expected feature hook fired
208 PolicyContainer.globalInit(new String[0]);
209 assertEquals(Arrays.asList("globalInit-exception"),
210 PolicySessionFeatureApiMock.getLog());
212 // initial conditions -- there should be no containers
213 assertEquals(0, PolicyContainer.getPolicyContainers().size());
216 "17.3.0-SNAPSHOT,17.1.0-SNAPSHOT,17.2.0-SNAPSHOT";
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());
227 // verify expected feature hooks fired
228 assertEquals(Arrays.asList("activatePolicySession-exception",
229 "newPolicySession-exception",
230 "selectThreadModel-exception"),
231 PolicySessionFeatureApiMock.getLog());
233 // this container should be on the list
235 Collection<PolicyContainer> containers =
236 PolicyContainer.getPolicyContainers();
237 assertEquals(1, containers.size());
238 assertTrue(containers.contains(container));
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());
248 // some container adjunct tests
250 Object bogusAdjunct = new Object();
252 // initially, no adjunct
253 assertSame(null, container.getAdjunct(this));
255 // set and verify adjunct
256 container.setAdjunct(this, bogusAdjunct);
257 assertSame(bogusAdjunct, container.getAdjunct(this));
259 // clear and verify adjunct
260 container.setAdjunct(this, null);
261 assertSame(null, container.getAdjunct(this));
265 // fetch the session, and verify that it exists
266 PolicySession session = container.getPolicySession("session1");
267 assertTrue(session != null);
269 // get all sessions, and verify that this one is the only one
271 Collection<PolicySession> sessions = container.getPolicySessions();
272 assertEquals(1, sessions.size());
273 assertTrue(sessions.contains(session));
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());
282 // some session adjunct tests
284 Object bogusAdjunct = new Object();
286 // initially, no adjunct
287 assertSame(null, session.getAdjunct(this));
289 // set and verify adjunct
290 session.setAdjunct(this, bogusAdjunct);
291 assertSame(bogusAdjunct, session.getAdjunct(this));
293 // clear and verify adjunct
294 session.setAdjunct(this, null);
295 assertSame(null, session.getAdjunct(this));
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);
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());
306 // exercise some more API methods
307 assertEquals(container.getClassLoader(),
308 container.getKieContainer().getClassLoader());
310 // should be equivalent to 'shutdown' without persistence
312 assertFalse(container.isAlive());
314 // verify expected feature hooks fired
315 assertEquals(Arrays.asList("destroyKieSession-exception"),
316 PolicySessionFeatureApiMock.getLog());
318 // clear exception trigger
319 PolicySessionFeatureApiMock.setExceptionTrigger(false);
322 // final conditions -- there should be no containers
323 assertEquals(0, PolicyContainer.getPolicyContainers().size());