2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.controller.internal;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.doThrow;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.List;
43 import java.util.TreeMap;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.kie.api.KieBase;
47 import org.kie.api.definition.KiePackage;
48 import org.kie.api.definition.rule.Query;
49 import org.kie.api.runtime.KieContainer;
50 import org.kie.api.runtime.KieSession;
51 import org.kie.api.runtime.rule.FactHandle;
52 import org.kie.api.runtime.rule.QueryResults;
53 import org.kie.api.runtime.rule.QueryResultsRow;
54 import org.mockito.ArgumentCaptor;
55 import org.mockito.Mock;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.policy.common.endpoints.event.comm.TopicSink;
58 import org.onap.policy.common.utils.services.OrderedServiceImpl;
59 import org.onap.policy.drools.core.PolicyContainer;
60 import org.onap.policy.drools.core.PolicySession;
61 import org.onap.policy.drools.features.DroolsControllerFeatureApi;
62 import org.onap.policy.drools.features.DroolsControllerFeatureApiConstants;
63 import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
64 import org.onap.policy.drools.protocol.coders.EventProtocolCoderConstants;
65 import org.onap.policy.drools.protocol.coders.EventProtocolParams;
66 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
67 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
68 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
69 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.PotentialCoderFilter;
71 public class MavenDroolsController2Test {
72 private static final int FACT1_OBJECT = 1000;
73 private static final int FACT3_OBJECT = 1001;
75 private static final long FACT_COUNT = 200L;
77 private static final String EXPECTED_EXCEPTION = "expected exception";
78 private static final RuntimeException RUNTIME_EX = new RuntimeException(EXPECTED_EXCEPTION);
79 private static final IllegalArgumentException ARG_EX = new IllegalArgumentException(EXPECTED_EXCEPTION);
81 private static final String UNKNOWN_CLASS = "unknown class";
83 private static final String GROUP = "my-group";
84 private static final String ARTIFACT = "my-artifact";
85 private static final String VERSION = "my-version";
87 private static final String GROUP2 = "my-groupB";
88 private static final String ARTIFACT2 = "my-artifactB";
89 private static final String VERSION2 = "my-versionB";
91 private static final String TOPIC = "my-topic";
92 private static final String TOPIC2 = "my-topic";
94 private static final ClassLoader CLASS_LOADER = MavenDroolsController2Test.class.getClassLoader();
95 private static final int CLASS_LOADER_HASHCODE = CLASS_LOADER.hashCode();
97 private static final String SESSION1 = "session-A";
98 private static final String SESSION2 = "session-B";
99 private static final String FULL_SESSION1 = "full-A";
100 private static final String FULL_SESSION2 = "full-B";
102 private static final String EVENT_TEXT = "my-event-text";
103 private static final Object EVENT = new Object();
105 private static final String QUERY = "my-query";
106 private static final String QUERY2 = "my-query-B";
107 private static final String ENTITY = "my-entity";
108 private static final Object PARM1 = "parmA";
109 private static final Object PARM2 = "parmB";
112 private EventProtocolCoder coderMgr;
114 private DroolsControllerFeatureApi prov1;
116 private DroolsControllerFeatureApi prov2;
118 private OrderedServiceImpl<DroolsControllerFeatureApi> droolsProviders;
120 private TopicCoderFilterConfiguration decoder1;
122 private TopicCoderFilterConfiguration decoder2;
124 private TopicCoderFilterConfiguration encoder1;
126 private TopicCoderFilterConfiguration encoder2;
128 private PolicyContainer container;
130 private CustomGsonCoder gson1;
132 private CustomGsonCoder gson2;
134 private PotentialCoderFilter filter1a;
136 private JsonProtocolFilter jsonFilter1a;
138 private PotentialCoderFilter filter1b;
140 private JsonProtocolFilter jsonFilter1b;
142 private PotentialCoderFilter filter2;
144 private JsonProtocolFilter jsonFilter2;
146 private PolicySession sess1;
148 private PolicySession sess2;
150 private KieSession kieSess;
152 private KieSession kieSess2;
154 private TopicSink sink;
156 private FactHandle fact1;
158 private FactHandle fact2;
160 private FactHandle fact3;
162 private FactHandle factex;
164 private KieBase kieBase;
166 private KiePackage pkg1;
168 private KiePackage pkg2;
170 private Query query1;
172 private Query query2;
174 private Query query3;
176 private QueryResults queryResults;
178 private QueryResultsRow row1;
180 private QueryResultsRow row2;
182 private List<TopicCoderFilterConfiguration> decoders;
183 private List<TopicCoderFilterConfiguration> encoders;
185 private MavenDroolsController drools;
188 * Initializes objects, including the drools controller.
191 public void setUp() {
192 MockitoAnnotations.initMocks(this);
194 when(droolsProviders.getList()).thenReturn(Arrays.asList(prov1, prov2));
196 when(coderMgr.isDecodingSupported(GROUP, ARTIFACT, TOPIC)).thenReturn(true);
197 when(coderMgr.decode(GROUP, ARTIFACT, TOPIC, EVENT_TEXT)).thenReturn(EVENT);
199 when(kieSess.getFactCount()).thenReturn(FACT_COUNT);
200 when(kieSess.getFactHandles()).thenReturn(Arrays.asList(fact1, fact2, factex, fact3));
201 when(kieSess.getFactHandles(any())).thenReturn(Arrays.asList(fact1, fact3));
202 when(kieSess.getKieBase()).thenReturn(kieBase);
203 when(kieSess.getQueryResults(QUERY, PARM1, PARM2)).thenReturn(queryResults);
205 when(kieSess.getObject(fact1)).thenReturn(FACT1_OBJECT);
206 when(kieSess.getObject(fact2)).thenReturn("");
207 when(kieSess.getObject(fact3)).thenReturn(FACT3_OBJECT);
208 when(kieSess.getObject(factex)).thenThrow(RUNTIME_EX);
210 when(kieSess2.getFactHandles()).thenReturn(Collections.emptyList());
212 when(kieBase.getKiePackages()).thenReturn(Arrays.asList(pkg1, pkg2));
214 when(pkg1.getQueries()).thenReturn(Arrays.asList(query3));
215 when(pkg2.getQueries()).thenReturn(Arrays.asList(query2, query1));
217 when(query1.getName()).thenReturn(QUERY);
218 when(query2.getName()).thenReturn(QUERY2);
220 when(queryResults.iterator()).thenReturn(Arrays.asList(row1, row2).iterator());
222 when(row1.get(ENTITY)).thenReturn(FACT1_OBJECT);
223 when(row2.get(ENTITY)).thenReturn(FACT3_OBJECT);
225 when(row1.getFactHandle(ENTITY)).thenReturn(fact1);
226 when(row2.getFactHandle(ENTITY)).thenReturn(fact3);
228 when(sess1.getKieSession()).thenReturn(kieSess);
229 when(sess2.getKieSession()).thenReturn(kieSess2);
231 when(sess1.getName()).thenReturn(SESSION1);
232 when(sess2.getName()).thenReturn(SESSION2);
234 when(sess1.getFullName()).thenReturn(FULL_SESSION1);
235 when(sess2.getFullName()).thenReturn(FULL_SESSION2);
237 when(container.getClassLoader()).thenReturn(CLASS_LOADER);
238 when(container.getPolicySessions()).thenReturn(Arrays.asList(sess1, sess2));
239 when(container.insertAll(EVENT)).thenReturn(true);
241 when(decoder1.getTopic()).thenReturn(TOPIC);
242 when(decoder2.getTopic()).thenReturn(TOPIC2);
244 when(encoder1.getTopic()).thenReturn(TOPIC);
245 when(encoder2.getTopic()).thenReturn(TOPIC2);
247 decoders = Arrays.asList(decoder1, decoder2);
248 encoders = Arrays.asList(encoder1, encoder2);
250 when(decoder1.getCustomGsonCoder()).thenReturn(gson1);
251 when(encoder2.getCustomGsonCoder()).thenReturn(gson2);
253 when(filter1a.getCodedClass()).thenReturn(Object.class.getName());
254 when(filter1a.getFilter()).thenReturn(jsonFilter1a);
256 when(filter1b.getCodedClass()).thenReturn(String.class.getName());
257 when(filter1b.getFilter()).thenReturn(jsonFilter1b);
259 when(filter2.getCodedClass()).thenReturn(Integer.class.getName());
260 when(filter2.getFilter()).thenReturn(jsonFilter2);
262 when(decoder1.getCoderFilters()).thenReturn(Arrays.asList(filter1a, filter1b));
263 when(decoder2.getCoderFilters()).thenReturn(Collections.emptyList());
265 when(encoder1.getCoderFilters()).thenReturn(Collections.emptyList());
266 when(encoder2.getCoderFilters()).thenReturn(Arrays.asList(filter2));
268 when(sink.getTopic()).thenReturn(TOPIC);
269 when(sink.send(EVENT_TEXT)).thenReturn(true);
271 drools = new MyDrools(GROUP, ARTIFACT, VERSION, null, null);
273 when(coderMgr.encode(TOPIC, EVENT, drools)).thenReturn(EVENT_TEXT);
277 public void testMavenDroolsController_InvalidArgs() {
278 assertThatIllegalArgumentException().isThrownBy(() -> new MyDrools(null, ARTIFACT, VERSION, null, null))
279 .withMessageContaining("group");
280 assertThatIllegalArgumentException().isThrownBy(() -> new MyDrools("", ARTIFACT, VERSION, null, null))
281 .withMessageContaining("group");
283 assertThatIllegalArgumentException().isThrownBy(() -> new MyDrools(GROUP, null, VERSION, null, null))
284 .withMessageContaining("artifact");
285 assertThatIllegalArgumentException().isThrownBy(() -> new MyDrools(GROUP, "", VERSION, null, null))
286 .withMessageContaining("artifact");
288 assertThatIllegalArgumentException().isThrownBy(() -> new MyDrools(GROUP, ARTIFACT, null, null, null))
289 .withMessageContaining("version");
290 assertThatIllegalArgumentException().isThrownBy(() -> new MyDrools(GROUP, ARTIFACT, "", null, null))
291 .withMessageContaining("version");
295 public void testUpdateToVersion() {
297 drools.updateToVersion(GROUP, ARTIFACT, VERSION2, decoders, encoders);
299 verify(container).updateToVersion(VERSION2);
301 // nothing removed the first time
302 verify(coderMgr, never()).removeDecoders(GROUP, ARTIFACT, TOPIC2);
303 verify(coderMgr, never()).removeEncoders(GROUP, ARTIFACT, TOPIC);
305 verify(coderMgr, times(2)).addDecoder(any());
306 verify(coderMgr, times(1)).addEncoder(any());
309 when(container.getVersion()).thenReturn(VERSION2);
310 drools.updateToVersion(GROUP, ARTIFACT, VERSION, null, null);
312 verify(container).updateToVersion(VERSION);
314 verify(coderMgr, times(2)).removeDecoders(GROUP, ARTIFACT, TOPIC2);
315 verify(coderMgr, times(2)).removeEncoders(GROUP, ARTIFACT, TOPIC);
318 verify(coderMgr, times(2)).addDecoder(any());
319 verify(coderMgr, times(1)).addEncoder(any());
323 public void testUpdateToVersion_Unchanged() {
324 drools.updateToVersion(GROUP, ARTIFACT, VERSION, decoders, encoders);
326 verify(coderMgr, never()).addDecoder(any());
327 verify(coderMgr, never()).addEncoder(any());
331 public void testUpdateToVersion_InvalidArgs() {
332 assertThatIllegalArgumentException()
333 .isThrownBy(() -> drools.updateToVersion(null, ARTIFACT, VERSION, null, null))
334 .withMessageContaining("group");
335 assertThatIllegalArgumentException().isThrownBy(() -> drools.updateToVersion("", ARTIFACT, VERSION, null, null))
336 .withMessageContaining("group");
338 assertThatIllegalArgumentException().isThrownBy(() -> drools.updateToVersion(GROUP, null, VERSION, null, null))
339 .withMessageContaining("artifact");
340 assertThatIllegalArgumentException().isThrownBy(() -> drools.updateToVersion(GROUP, "", VERSION, null, null))
341 .withMessageContaining("artifact");
343 assertThatIllegalArgumentException().isThrownBy(() -> drools.updateToVersion(GROUP, ARTIFACT, null, null, null))
344 .withMessageContaining("version");
345 assertThatIllegalArgumentException().isThrownBy(() -> drools.updateToVersion(GROUP, ARTIFACT, "", null, null))
346 .withMessageContaining("version");
348 assertThatIllegalArgumentException()
349 .isThrownBy(() -> drools.updateToVersion("no-group-id", ARTIFACT, VERSION, null, null))
350 .withMessageContaining("BRAINLESS");
352 assertThatIllegalArgumentException()
353 .isThrownBy(() -> drools.updateToVersion(GROUP, "no-artifact-id", VERSION, null, null))
354 .withMessageContaining("BRAINLESS");
356 assertThatIllegalArgumentException()
357 .isThrownBy(() -> drools.updateToVersion(GROUP, ARTIFACT, "no-version", null, null))
358 .withMessageContaining("BRAINLESS");
360 assertThatIllegalArgumentException()
361 .isThrownBy(() -> drools.updateToVersion(GROUP2, ARTIFACT, VERSION, null, null))
362 .withMessageContaining("coordinates must be identical");
364 assertThatIllegalArgumentException()
365 .isThrownBy(() -> drools.updateToVersion(GROUP, ARTIFACT2, VERSION, null, null))
366 .withMessageContaining("coordinates must be identical");
370 public void testInitCoders_NullCoders() {
371 // already constructed with null coders
372 verify(coderMgr, never()).addDecoder(any());
373 verify(coderMgr, never()).addEncoder(any());
377 public void testInitCoders_NullOrEmptyFilters() {
378 when(decoder1.getCoderFilters()).thenReturn(Collections.emptyList());
379 when(decoder2.getCoderFilters()).thenReturn(null);
381 when(encoder1.getCoderFilters()).thenReturn(null);
382 when(encoder2.getCoderFilters()).thenReturn(Collections.emptyList());
384 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
386 verify(coderMgr, never()).addDecoder(any());
387 verify(coderMgr, never()).addEncoder(any());
391 public void testInitCoders_GsonClass() {
392 when(gson1.getClassContainer()).thenReturn("");
393 when(gson2.getClassContainer()).thenReturn(Long.class.getName());
395 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
397 // all should be added
398 verify(coderMgr, times(2)).addDecoder(any());
399 verify(coderMgr, times(1)).addEncoder(any());
403 public void testInitCoders_InvalidGsonClass() {
404 when(gson1.getClassContainer()).thenReturn(UNKNOWN_CLASS);
406 assertThatIllegalArgumentException()
407 .isThrownBy(() -> new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders))
408 .withMessageContaining("cannot be retrieved");
412 public void testInitCoders_InvalidFilterClass() {
413 when(filter2.getCodedClass()).thenReturn(UNKNOWN_CLASS);
415 assertThatIllegalArgumentException()
416 .isThrownBy(() -> new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders))
417 .withMessageContaining("cannot be retrieved");
421 public void testInitCoders_Filters() {
423 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
425 ArgumentCaptor<EventProtocolParams> dec = ArgumentCaptor.forClass(EventProtocolParams.class);
426 verify(coderMgr, times(2)).addDecoder(dec.capture());
428 ArgumentCaptor<EventProtocolParams> enc = ArgumentCaptor.forClass(EventProtocolParams.class);
429 verify(coderMgr, times(1)).addEncoder(enc.capture());
431 // validate parameters
432 EventProtocolParams params = dec.getAllValues().get(0);
433 assertEquals(ARTIFACT, params.getArtifactId());
434 assertEquals(gson1, params.getCustomCoder());
435 assertEquals(Object.class.getName(), params.getEventClass());
436 assertEquals(GROUP, params.getGroupId());
437 assertEquals(CLASS_LOADER_HASHCODE, params.getModelClassLoaderHash());
438 assertEquals(jsonFilter1a, params.getProtocolFilter());
439 assertEquals(TOPIC, params.getTopic());
441 params = dec.getAllValues().get(1);
442 assertEquals(ARTIFACT, params.getArtifactId());
443 assertEquals(gson1, params.getCustomCoder());
444 assertEquals(String.class.getName(), params.getEventClass());
445 assertEquals(GROUP, params.getGroupId());
446 assertEquals(CLASS_LOADER_HASHCODE, params.getModelClassLoaderHash());
447 assertEquals(jsonFilter1b, params.getProtocolFilter());
448 assertEquals(TOPIC, params.getTopic());
450 params = enc.getAllValues().get(0);
451 assertEquals(ARTIFACT, params.getArtifactId());
452 assertEquals(gson2, params.getCustomCoder());
453 assertEquals(Integer.class.getName(), params.getEventClass());
454 assertEquals(GROUP, params.getGroupId());
455 assertEquals(CLASS_LOADER_HASHCODE, params.getModelClassLoaderHash());
456 assertEquals(jsonFilter2, params.getProtocolFilter());
457 assertEquals(TOPIC, params.getTopic());
461 public void testOwnsCoder() {
462 int hc = CLASS_LOADER_HASHCODE;
465 assertFalse(drools.ownsCoder(String.class, hc + 1));
468 assertTrue(drools.ownsCoder(String.class, hc));
471 drools = new MyDrools(GROUP, ARTIFACT, VERSION, null, null) {
473 protected boolean isClass(String className) {
477 assertFalse(drools.ownsCoder(String.class, hc));
481 public void testStart_testStop_testIsAlive() {
482 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
484 when(container.start()).thenReturn(true);
485 when(container.stop()).thenReturn(true);
487 assertFalse(drools.isAlive());
490 assertTrue(drools.start());
491 verify(container).start();
492 assertTrue(drools.isAlive());
494 // repeat - no changes
495 assertTrue(drools.start());
496 verify(container).start();
497 assertTrue(drools.isAlive());
500 assertTrue(drools.stop());
501 verify(container).stop();
502 assertFalse(drools.isAlive());
504 // repeat - no changes
505 assertTrue(drools.stop());
506 verify(container).stop();
507 assertFalse(drools.isAlive());
509 // now check with container returning false - should still be invoked
510 when(container.start()).thenReturn(false);
511 when(container.stop()).thenReturn(false);
512 assertFalse(drools.start());
513 assertTrue(drools.isAlive());
514 assertFalse(drools.stop());
515 assertFalse(drools.isAlive());
516 verify(container, times(2)).start();
517 verify(container, times(2)).stop();
519 // coders should still be intact
520 verify(coderMgr, never()).removeDecoders(any(), any(), any());
521 verify(coderMgr, never()).removeEncoders(any(), any(), any());
523 verify(container, never()).shutdown();
524 verify(container, never()).destroy();
528 public void testShutdown() {
529 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
537 verify(container).stop();
538 assertFalse(drools.isAlive());
540 // coders should have been removed
541 verify(coderMgr, times(2)).removeDecoders(any(), any(), any());
542 verify(coderMgr, times(2)).removeEncoders(any(), any(), any());
544 verify(container).shutdown();
545 verify(container, never()).destroy();
549 public void testShutdown_Ex() {
550 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
555 when(container.stop()).thenThrow(RUNTIME_EX);
560 assertFalse(drools.isAlive());
562 verify(container).shutdown();
563 verify(container, never()).destroy();
567 public void testHalt() {
568 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
576 verify(container).stop();
577 assertFalse(drools.isAlive());
579 // coders should have been removed
580 verify(coderMgr, times(2)).removeDecoders(any(), any(), any());
581 verify(coderMgr, times(2)).removeEncoders(any(), any(), any());
583 verify(container).destroy();
587 public void testHalt_Ex() {
588 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders);
593 when(container.stop()).thenThrow(RUNTIME_EX);
598 assertFalse(drools.isAlive());
600 verify(container).destroy();
604 public void testRemoveCoders_Ex() {
605 drools = new MyDrools(GROUP, ARTIFACT, VERSION, decoders, encoders) {
607 protected void removeDecoders() {
612 protected void removeEncoders() {
617 assertThatCode(() -> drools.updateToVersion(GROUP, ARTIFACT, VERSION2, null, null)).doesNotThrowAnyException();
621 public void testOfferStringString() {
623 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
625 verify(container).insertAll(EVENT);
629 public void testOfferStringString_NoDecode() {
630 when(coderMgr.isDecodingSupported(GROUP, ARTIFACT, TOPIC)).thenReturn(false);
633 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
635 verify(container, never()).insertAll(EVENT);
639 public void testOfferStringString_DecodeUnsupported() {
640 when(coderMgr.decode(GROUP, ARTIFACT, TOPIC, EVENT_TEXT))
641 .thenThrow(new UnsupportedOperationException(EXPECTED_EXCEPTION));
644 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
646 verify(container, never()).insertAll(EVENT);
650 public void testOfferStringString_DecodeEx() {
651 when(coderMgr.decode(GROUP, ARTIFACT, TOPIC, EVENT_TEXT)).thenThrow(RUNTIME_EX);
654 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
656 verify(container, never()).insertAll(EVENT);
660 public void testOfferStringString_Ignored() {
664 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
665 assertEquals(0, drools.getRecentSourceEvents().length);
669 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
670 assertEquals(0, drools.getRecentSourceEvents().length);
674 when(container.getPolicySessions()).thenReturn(Collections.emptyList());
675 assertTrue(drools.offer(TOPIC, EVENT_TEXT));
676 assertEquals(0, drools.getRecentSourceEvents().length);
680 public void testOfferT() {
682 assertTrue(drools.offer(EVENT));
683 assertEquals(1, drools.getRecentSourceEvents().length);
684 assertEquals(EVENT, drools.getRecentSourceEvents()[0]);
685 verify(container).insertAll(EVENT);
687 verify(prov1).beforeInsert(drools, EVENT);
688 verify(prov2).beforeInsert(drools, EVENT);
690 verify(prov1).afterInsert(drools, EVENT, true);
691 verify(prov2).afterInsert(drools, EVENT, true);
695 public void testOfferT_Ex() {
696 when(prov1.beforeInsert(drools, EVENT)).thenThrow(RUNTIME_EX);
697 when(prov1.afterInsert(drools, EVENT, true)).thenThrow(RUNTIME_EX);
700 assertTrue(drools.offer(EVENT));
701 assertEquals(1, drools.getRecentSourceEvents().length);
702 assertEquals(EVENT, drools.getRecentSourceEvents()[0]);
703 verify(container).insertAll(EVENT);
705 // should still invoke prov2
706 verify(prov2).beforeInsert(drools, EVENT);
708 verify(prov2).afterInsert(drools, EVENT, true);
712 public void testOfferT_NotInserted() {
713 when(container.insertAll(EVENT)).thenReturn(false);
716 assertTrue(drools.offer(EVENT));
717 assertEquals(1, drools.getRecentSourceEvents().length);
718 assertEquals(EVENT, drools.getRecentSourceEvents()[0]);
719 verify(container).insertAll(EVENT);
721 verify(prov1).beforeInsert(drools, EVENT);
722 verify(prov2).beforeInsert(drools, EVENT);
724 verify(prov1).afterInsert(drools, EVENT, false);
725 verify(prov2).afterInsert(drools, EVENT, false);
729 public void testOfferT_BeforeInsertIntercept() {
731 when(prov1.beforeInsert(drools, EVENT)).thenReturn(true);
733 assertTrue(drools.offer(EVENT));
734 assertEquals(1, drools.getRecentSourceEvents().length);
735 assertEquals(EVENT, drools.getRecentSourceEvents()[0]);
736 verify(container, never()).insertAll(EVENT);
738 verify(prov1).beforeInsert(drools, EVENT);
740 // nothing else invoked
741 verify(prov2, never()).beforeInsert(drools, EVENT);
742 verify(prov1, never()).afterInsert(drools, EVENT, true);
743 verify(prov2, never()).afterInsert(drools, EVENT, true);
747 public void testOfferT_AfterInsertIntercept() {
750 when(prov1.afterInsert(drools, EVENT, true)).thenReturn(true);
752 assertTrue(drools.offer(EVENT));
753 assertEquals(1, drools.getRecentSourceEvents().length);
754 assertEquals(EVENT, drools.getRecentSourceEvents()[0]);
755 verify(container).insertAll(EVENT);
757 verify(prov1).beforeInsert(drools, EVENT);
758 verify(prov2).beforeInsert(drools, EVENT);
760 verify(prov1).afterInsert(drools, EVENT, true);
762 // prov2 is never called
763 verify(prov2, never()).afterInsert(drools, EVENT, true);
767 public void testOfferT_Ignored() {
771 assertTrue(drools.offer(EVENT));
772 assertEquals(0, drools.getRecentSourceEvents().length);
776 assertTrue(drools.offer(EVENT));
777 assertEquals(0, drools.getRecentSourceEvents().length);
781 when(container.getPolicySessions()).thenReturn(Collections.emptyList());
782 assertTrue(drools.offer(EVENT));
783 assertEquals(0, drools.getRecentSourceEvents().length);
787 public void testDeliver() {
789 assertTrue(drools.deliver(sink, EVENT));
790 assertEquals(1, drools.getRecentSinkEvents().length);
791 assertEquals(EVENT_TEXT, drools.getRecentSinkEvents()[0]);
793 verify(sink).send(EVENT_TEXT);
795 verify(prov1).beforeDeliver(drools, sink, EVENT);
796 verify(prov2).beforeDeliver(drools, sink, EVENT);
798 verify(prov1).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
799 verify(prov2).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
803 public void testDeliver_InvalidArgs() {
806 assertThatIllegalArgumentException().isThrownBy(() -> drools.deliver(null, EVENT))
807 .withMessageContaining("sink");
809 assertThatIllegalArgumentException().isThrownBy(() -> drools.deliver(sink, null))
810 .withMessageContaining("event");
813 assertThatIllegalStateException().isThrownBy(() -> drools.deliver(sink, EVENT)).withMessageContaining("locked");
817 assertThatIllegalStateException().isThrownBy(() -> drools.deliver(sink, EVENT))
818 .withMessageContaining("stopped");
821 assertEquals(0, drools.getRecentSinkEvents().length);
825 public void testDeliver_BeforeIntercept() {
826 when(prov1.beforeDeliver(drools, sink, EVENT)).thenReturn(true);
829 assertTrue(drools.deliver(sink, EVENT));
830 assertEquals(0, drools.getRecentSinkEvents().length);
832 verify(prov1).beforeDeliver(drools, sink, EVENT);
834 // nothing else should have been invoked
835 verify(sink, never()).send(EVENT_TEXT);
836 verify(prov2, never()).beforeDeliver(drools, sink, EVENT);
837 verify(prov1, never()).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
838 verify(prov2, never()).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
842 public void testDeliver_AfterIntercept() {
843 when(prov1.afterDeliver(drools, sink, EVENT, EVENT_TEXT, true)).thenReturn(true);
846 assertTrue(drools.deliver(sink, EVENT));
847 assertEquals(1, drools.getRecentSinkEvents().length);
848 assertEquals(EVENT_TEXT, drools.getRecentSinkEvents()[0]);
850 verify(prov1).beforeDeliver(drools, sink, EVENT);
851 verify(prov2).beforeDeliver(drools, sink, EVENT);
853 verify(sink).send(EVENT_TEXT);
855 verify(prov1).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
857 // nothing else should have been invoked
858 verify(prov2, never()).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
862 public void testDeliver_InterceptEx() {
863 when(prov1.beforeDeliver(drools, sink, EVENT)).thenThrow(RUNTIME_EX);
864 when(prov1.afterDeliver(drools, sink, EVENT, EVENT_TEXT, true)).thenThrow(RUNTIME_EX);
867 assertTrue(drools.deliver(sink, EVENT));
869 verify(sink).send(EVENT_TEXT);
871 // should still invoke prov2
872 verify(prov2).beforeDeliver(drools, sink, EVENT);
873 verify(prov2).afterDeliver(drools, sink, EVENT, EVENT_TEXT, true);
877 public void testGetXxx() {
878 assertEquals(VERSION, drools.getVersion());
879 assertEquals(ARTIFACT, drools.getArtifactId());
880 assertEquals(GROUP, drools.getGroupId());
881 assertEquals(CLASS_LOADER_HASHCODE, drools.getModelClassLoaderHash());
882 assertSame(container, drools.getContainer());
883 assertEquals(Arrays.asList(sess1, sess2), drools.getSessions());
885 // test junit methods - need a controller with fewer overrides
886 drools = new MavenDroolsController(GROUP, ARTIFACT, VERSION, null, null) {
888 protected PolicyContainer makePolicyContainer(String groupId, String artifactId, String version) {
893 assertSame(EventProtocolCoderConstants.getManager(), drools.getCoderManager());
894 assertSame(DroolsControllerFeatureApiConstants.getProviders(), drools.getDroolsProviders());
898 public void testLock_testUnlock_testIsLocked() {
899 assertFalse(drools.isLocked());
901 assertTrue(drools.lock());
902 assertTrue(drools.isLocked());
904 assertTrue(drools.unlock());
905 assertFalse(drools.isLocked());
908 assertTrue(drools.lock());
909 assertTrue(drools.isLocked());
911 assertTrue(drools.unlock());
912 assertFalse(drools.isLocked());
916 public void testGetSessionNames_testGetCanonicalSessionNames() {
917 assertEquals("[session-A, session-B]", drools.getSessionNames(true).toString());
918 assertEquals("[full-A, full-B]", drools.getSessionNames(false).toString());
920 assertEquals("[session-A, session-B]", drools.getSessionNames().toString());
922 assertEquals("[full-A, full-B]", drools.getCanonicalSessionNames().toString());
925 when(container.getPolicySessions()).thenThrow(RUNTIME_EX);
926 assertEquals("[expected exception]", drools.getSessionNames().toString());
930 public void testGetBaseDomainNames() {
931 KieContainer kiecont = mock(KieContainer.class);
932 when(kiecont.getKieBaseNames()).thenReturn(Arrays.asList("kieA", "kieB"));
933 when(container.getKieContainer()).thenReturn(kiecont);
935 assertEquals("[kieA, kieB]", drools.getBaseDomainNames().toString());
939 public void testGetSession() {
940 assertThatIllegalArgumentException().isThrownBy(() -> drools.getSession(null))
941 .withMessageContaining("must be provided");
943 assertThatIllegalArgumentException().isThrownBy(() -> drools.getSession(""))
944 .withMessageContaining("must be provided");
946 assertSame(sess1, drools.getSession(SESSION1));
947 assertSame(sess1, drools.getSession(FULL_SESSION1));
949 assertSame(sess2, drools.getSession(SESSION2));
951 assertThatIllegalArgumentException().isThrownBy(() -> drools.getSession("unknown session"))
952 .withMessageContaining("Invalid Session Name");
956 public void testFactClassNames() {
957 // copy to a sorted map so the order remains unchanged
958 Map<String, Integer> map = new TreeMap<>(drools.factClassNames(SESSION1));
959 assertEquals("{java.lang.Integer=2, java.lang.String=1}", map.toString());
961 assertThatIllegalArgumentException().isThrownBy(() -> drools.factClassNames(null))
962 .withMessageContaining("Invalid Session Name");
964 assertThatIllegalArgumentException().isThrownBy(() -> drools.factClassNames(""))
965 .withMessageContaining("Invalid Session Name");
969 public void testFactCount() {
970 assertEquals(FACT_COUNT, drools.factCount(SESSION1));
972 assertThatIllegalArgumentException().isThrownBy(() -> drools.factCount(null))
973 .withMessageContaining("Invalid Session Name");
975 assertThatIllegalArgumentException().isThrownBy(() -> drools.factCount(""))
976 .withMessageContaining("Invalid Session Name");
980 public void testFactsStringStringBoolean() {
981 assertEquals("[1000, 1001]", drools.facts(SESSION1, Integer.class.getName(), false).toString());
982 verify(kieSess, never()).delete(fact1);
983 verify(kieSess, never()).delete(fact2);
984 verify(kieSess, never()).delete(fact3);
985 verify(kieSess, never()).delete(factex);
987 // now delete - but should only delete 1 & 3
988 assertEquals("[1000, 1001]", drools.facts(SESSION1, Integer.class.getName(), true).toString());
989 verify(kieSess).delete(fact1);
990 verify(kieSess, never()).delete(fact2);
991 verify(kieSess).delete(fact3);
992 verify(kieSess, never()).delete(factex);
994 assertThatIllegalArgumentException().isThrownBy(() -> drools.facts(null, Integer.class.getName(), false))
995 .withMessageContaining("Invalid Session Name");
997 assertThatIllegalArgumentException().isThrownBy(() -> drools.facts("", Integer.class.getName(), false))
998 .withMessageContaining("Invalid Session Name");
1000 assertThatIllegalArgumentException().isThrownBy(() -> drools.facts(SESSION1, null, false))
1001 .withMessageContaining("Invalid Class Name");
1003 assertThatIllegalArgumentException().isThrownBy(() -> drools.facts(SESSION1, "", false))
1004 .withMessageContaining("Invalid Class Name");
1006 assertThatIllegalArgumentException().isThrownBy(() -> drools.facts(SESSION1, UNKNOWN_CLASS, false))
1007 .withMessageContaining("classloader");
1011 public void testFactsStringStringBoolean_DeleteEx() {
1012 doThrow(RUNTIME_EX).when(kieSess).delete(fact1);
1014 assertEquals("[1000, 1001]", drools.facts(SESSION1, Integer.class.getName(), true).toString());
1016 // should still have deleted #3
1017 verify(kieSess).delete(fact3);
1021 public void testFactsStringClassOfT() {
1022 assertEquals("[1000, 1001]", drools.facts(SESSION1, Integer.class).toString());
1026 public void testFactQuery() {
1027 assertEquals("[1000, 1001]", drools.factQuery(SESSION1, QUERY, ENTITY, false, PARM1, PARM2).toString());
1029 verify(kieSess, never()).delete(fact1);
1030 verify(kieSess, never()).delete(fact3);
1032 assertThatIllegalArgumentException()
1033 .isThrownBy(() -> drools.factQuery(null, QUERY, ENTITY, false, PARM1, PARM2))
1034 .withMessageContaining("Invalid Session Name");
1036 assertThatIllegalArgumentException().isThrownBy(() -> drools.factQuery("", QUERY, ENTITY, false, PARM1, PARM2))
1037 .withMessageContaining("Invalid Session Name");
1039 assertThatIllegalArgumentException()
1040 .isThrownBy(() -> drools.factQuery(SESSION1, null, ENTITY, false, PARM1, PARM2))
1041 .withMessageContaining("Invalid Query Name");
1043 assertThatIllegalArgumentException()
1044 .isThrownBy(() -> drools.factQuery(SESSION1, "", ENTITY, false, PARM1, PARM2))
1045 .withMessageContaining("Invalid Query Name");
1047 assertThatIllegalArgumentException()
1048 .isThrownBy(() -> drools.factQuery(SESSION1, QUERY, null, false, PARM1, PARM2))
1049 .withMessageContaining("Invalid Queried Entity");
1051 assertThatIllegalArgumentException()
1052 .isThrownBy(() -> drools.factQuery(SESSION1, QUERY, "", false, PARM1, PARM2))
1053 .withMessageContaining("Invalid Queried Entity");
1055 assertThatIllegalArgumentException().isThrownBy(
1056 () -> drools.factQuery(SESSION1, QUERY + "-unknown-query", ENTITY, false, PARM1, PARM2))
1057 .withMessageContaining("Invalid Query Name");
1061 public void testFactQuery_Delete() {
1062 doThrow(RUNTIME_EX).when(kieSess).delete(fact1);
1064 assertEquals("[1000, 1001]", drools.factQuery(SESSION1, QUERY, ENTITY, true, PARM1, PARM2).toString());
1066 // should still delete fact #3
1067 verify(kieSess).delete(fact3);
1071 public void testDeleteStringT() {
1072 assertTrue(drools.delete(SESSION1, FACT3_OBJECT));
1074 verify(kieSess, never()).delete(fact1);
1075 verify(kieSess).delete(fact3);
1078 assertFalse(drools.delete(SESSION1, "hello"));
1080 // repeat, but generate exception while getting the first object
1081 when(kieSess.getObject(fact1)).thenThrow(RUNTIME_EX);
1082 assertTrue(drools.delete(SESSION1, FACT3_OBJECT));
1084 verify(kieSess, never()).delete(fact1);
1086 // should still delete fact #3
1087 verify(kieSess, times(2)).delete(fact3);
1091 public void testDeleteT() {
1092 assertTrue(drools.delete(FACT3_OBJECT));
1094 verify(kieSess).delete(fact3);
1098 public void testDeleteStringClassOfT() {
1099 assertTrue(drools.delete(SESSION1, Integer.class));
1101 verify(kieSess).delete(fact1);
1102 verify(kieSess).delete(fact3);
1106 public void testDeleteStringClassOfT_Ex() {
1107 doThrow(RUNTIME_EX).when(kieSess).delete(fact1);
1109 assertFalse(drools.delete(SESSION1, Integer.class));
1111 // should still delete fact #3
1112 verify(kieSess).delete(fact3);
1116 public void testDeleteClassOfT() {
1117 assertTrue(drools.delete(Integer.class));
1119 verify(kieSess).delete(fact1);
1120 verify(kieSess).delete(fact3);
1124 public void testFetchModelClass() {
1125 assertSame(Long.class, drools.fetchModelClass(Long.class.getName()));
1129 public void testIsBrained() {
1130 assertTrue(drools.isBrained());
1134 public void testToString() {
1135 assertNotNull(drools.toString());
1138 private class MyDrools extends MavenDroolsController {
1140 public MyDrools(String groupId, String artifactId, String version,
1141 List<TopicCoderFilterConfiguration> decoderConfigurations,
1142 List<TopicCoderFilterConfiguration> encoderConfigurations) {
1144 super(groupId, artifactId, version, decoderConfigurations, encoderConfigurations);
1148 protected EventProtocolCoder getCoderManager() {
1153 protected OrderedServiceImpl<DroolsControllerFeatureApi> getDroolsProviders() {
1154 return droolsProviders;
1158 protected PolicyContainer makePolicyContainer(String groupId, String artifactId, String version) {
1159 when(container.getGroupId()).thenReturn(groupId);
1160 when(container.getArtifactId()).thenReturn(artifactId);
1161 when(container.getVersion()).thenReturn(version);