summaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-tdjam/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'controlloop/common/controller-tdjam/src/test/java')
-rw-r--r--controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/TdjamTest.java159
-rw-r--r--controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java240
-rw-r--r--controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java343
3 files changed, 742 insertions, 0 deletions
diff --git a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/TdjamTest.java b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/TdjamTest.java
new file mode 100644
index 000000000..cf40ba6d4
--- /dev/null
+++ b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/TdjamTest.java
@@ -0,0 +1,159 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop;
+
+import java.util.Properties;
+import lombok.Getter;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.runner.RunWith;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.controlloop.common.rules.test.BaseTest;
+import org.onap.policy.controlloop.common.rules.test.Listener;
+import org.onap.policy.controlloop.common.rules.test.NamedRunner;
+import org.onap.policy.controlloop.common.rules.test.Rules;
+import org.onap.policy.controlloop.common.rules.test.TestNames;
+import org.onap.policy.drools.persistence.SystemPersistence;
+import org.onap.policy.drools.persistence.SystemPersistenceConstants;
+import org.onap.policy.drools.system.PolicyController;
+import org.onap.policy.drools.system.PolicyControllerConstants;
+import org.onap.policy.drools.system.PolicyEngine;
+import org.onap.policy.drools.system.PolicyEngineConstants;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.simulators.Util;
+
+
+
+/**
+ * Tests use cases using BaseTest Set.
+ *
+ * <p/>
+ * Note: this runs ALL tests (i.e., any whose names start with "test").
+ */
+@RunWith(NamedRunner.class)
+@TestNames(prefixes = {"test"})
+
+public class TdjamTest extends BaseTest {
+ protected static final String CONTROLLER_NAME = "tdjam";
+ protected static PolicyController controller;
+
+ @Getter
+ private static final PolicyEngine pdpd = makeEngine();
+
+ @Getter
+ private static final SystemPersistence pdpdRepo = makePdpdRepo();
+
+ /**
+ * Sets up statics.
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() {
+ initStatics();
+ pdpdRepo.setConfigurationDir("src/test/resources/config");
+ pdpd.configure(new Properties());
+ controller = pdpd.createPolicyController(CONTROLLER_NAME, pdpdRepo.getControllerProperties(CONTROLLER_NAME));
+ pdpd.start();
+ httpClients.addClients("tdjam");
+ simulators.start(Util::buildAaiSim, Util::buildSoSim, Util::buildVfcSim, Util::buildGuardSim,
+ Util::buildSdncSim);
+ }
+
+ /**
+ * Cleans up statics.
+ */
+ @AfterClass
+ public static void tearDownAfterClass() {
+ finishStatics();
+ PolicyControllerConstants.getFactory().shutdown(controller);
+ pdpd.stop();
+ }
+
+ /**
+ * Sets up.
+ */
+ @Before
+ public void setUp() {
+ topics = topicMaker.get();
+ }
+
+ /**
+ * Tears down.
+ */
+ @After
+ public void tearDown() {
+ topics.destroy();
+ }
+
+ protected static PolicyEngine makeEngine() {
+ return PolicyEngineConstants.getManager();
+ }
+
+ protected static SystemPersistence makePdpdRepo() {
+ return SystemPersistenceConstants.getManager();
+ }
+
+ @Override
+ protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
+ String policyName = policy.getIdentifier().getName();
+
+ policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.ACTIVE
+ && (policyName + ".EVENT").equals(notif.getPolicyName()));
+
+ policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
+ && (policyName + ".EVENT.MANAGER.PROCESSING").equals(notif.getPolicyName())
+ && notif.getMessage().startsWith("Sending guard query"));
+
+ policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
+ && (policyName + ".EVENT.MANAGER.PROCESSING").equals(notif.getPolicyName())
+ && notif.getMessage().startsWith("Guard result") && notif.getMessage().endsWith("Permit"));
+
+ policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
+ && (policyName + ".EVENT.MANAGER.PROCESSING").equals(notif.getPolicyName())
+ && notif.getMessage().startsWith("actor="));
+ }
+
+ @Override
+ protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
+ Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
+
+ return policyClMgt.await(notif -> notif.getNotification() == finalType
+ && (policy.getIdentifier().getName() + ".EVENT.MANAGER.FINAL").equals(notif.getPolicyName()));
+ }
+
+ @Override
+ protected ToscaPolicy checkPolicy(String fileName) {
+ try {
+ policy = Rules.getPolicyFromFile(fileName);
+ } catch (CoderException e) {
+ throw new IllegalArgumentException(fileName, e);
+ }
+ controller.getDrools().offer(policy);
+ return policy;
+ }
+
+ @Override
+ protected Listener<VirtualControlLoopNotification> createNoficationTopicListener() {
+ return topics.createListener(POLICY_CL_MGT_TOPIC,
+ VirtualControlLoopNotification.class, controller);
+ }
+}
diff --git a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java
new file mode 100644
index 000000000..5edba8701
--- /dev/null
+++ b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/controlloop/tdjam/TdjamControllerTest.java
@@ -0,0 +1,240 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.tdjam;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY_CONTROLLER_TYPE;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.UUID;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.policy.controlloop.CanonicalOnset;
+import org.onap.policy.controlloop.VirtualControlLoopNotification;
+import org.onap.policy.controlloop.common.rules.test.Listener;
+import org.onap.policy.controlloop.common.rules.test.Topics;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.onap.policy.drools.controller.DroolsControllerConstants;
+import org.onap.policy.drools.system.PolicyControllerConstants;
+import org.onap.policy.drools.system.PolicyEngineConstants;
+import org.onap.policy.drools.utils.PropertyUtil;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.powermock.reflect.Whitebox;
+import org.slf4j.LoggerFactory;
+
+public class TdjamControllerTest {
+ private static Properties prop;
+ private static Logger logger = (Logger) LoggerFactory.getLogger(TdjamController.class);
+ private static ListAppender<ILoggingEvent> appender = new ListAppender<ILoggingEvent>();
+
+ /**
+ * Setup appender, and initialize properties.
+ */
+ @BeforeClass
+ public static void setupClass() throws Exception {
+ logger.setLevel(Level.DEBUG);
+ logger.addAppender(appender);
+
+ prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
+ prop.setProperty(PROPERTY_CONTROLLER_TYPE, "tdjam");
+
+ PolicyEngineConstants.getManager().configure(new Properties());
+ PolicyEngineConstants.getManager().start();
+
+ }
+
+ /**
+ * Remove appender.
+ */
+ @AfterClass
+ public static void cleanupClass() {
+
+ PolicyEngineConstants.getManager().stop();
+ PolicyEngineConstants.getManager().getExecutorService().shutdown();
+
+ appender.stop();
+ System.out.println("APPENDER:");
+ for (ILoggingEvent event : appender.list) {
+ System.out.println(" " + event);
+ }
+ logger.detachAppender(appender);
+ }
+
+ @Test
+ public void toscaPolicyTests() {
+ TdjamController tc = (TdjamController) PolicyControllerConstants.getFactory().build("tc", prop);
+ assertTrue(PolicyControllerConstants.getFactory().inventory().contains(tc));
+ assertTrue(DroolsControllerConstants.getFactory().inventory().contains(tc));
+
+ final HashSet<ToscaPolicy> toscaPolicies = new HashSet<>();
+ final HashSet<ControlLoopParams> controlLoopParams = new HashSet<>();
+
+ ToscaPolicy a1 = buildToscaPolicy("a", "1", tc);
+ ToscaPolicy a2 = buildToscaPolicy("a", "2", tc);
+ ToscaPolicy b1 = buildToscaPolicy("b", "1", tc);
+
+ toscaPolicies.add(a1);
+ toscaPolicies.add(a2);
+ toscaPolicies.add(b1);
+
+ assertSame(a1, tc.getToscaPolicy("a", "1"));
+ assertSame(a2, tc.getToscaPolicy("a", "2"));
+ assertSame(b1, tc.getToscaPolicy("b", "1"));
+ assertEquals(toscaPolicies, tc.getAllToscaPolicies());
+
+ // create associated ControlLoopParams
+ final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
+ final ControlLoopParams clpa2 = buildControlLoopParams("a", "2", "clpa2", tc);
+ final ControlLoopParams clpb1 = buildControlLoopParams("b", "1", "clpb1", tc);
+ final ControlLoopParams clpb3 = buildControlLoopParams("b", "3", "clpb3", null);
+
+ // the add for 'clpb3' should fail, because there is no ToscaPolicy
+ startLog();
+ assertSame(clpb3, tc.addControlLoopParams(clpb3));
+ stopLog();
+ assertLog(".*Missing ToscaPolicy, name=b, version=3.*");
+ assertNull(tc.removeControlLoopParams("clpb3"));
+
+ controlLoopParams.add(clpa1);
+ controlLoopParams.add(clpa2);
+ controlLoopParams.add(clpb1);
+ assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
+
+ // manually remove a ControlLoopParams
+ assertSame(clpa1, tc.removeControlLoopParams("clpa1"));
+ assertTrue(controlLoopParams.remove(clpa1));
+ assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
+
+ // tests of nonexistent policies
+ assertNull(tc.getToscaPolicy("c", "1")); // non-existent name
+ assertNull(tc.removeToscaPolicy("c", "1"));
+ assertNull(tc.getToscaPolicy("b", "3")); // non-existent version
+ assertNull(tc.removeToscaPolicy("b", "3"));
+
+ assertSame(a1, tc.removeToscaPolicy("a", "1"));
+ assertTrue(toscaPolicies.remove(a1));
+ assertEquals(toscaPolicies, tc.getAllToscaPolicies());
+ assertSame(a2, tc.removeToscaPolicy("a", "2"));
+ assertTrue(toscaPolicies.remove(a2));
+ assertEquals(toscaPolicies, tc.getAllToscaPolicies());
+
+ // ControlLoopParams removal should be automatic
+ assertTrue(controlLoopParams.remove(clpa2));
+ assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
+
+ // test reset method
+ tc.reset();
+ assertTrue(tc.getAllToscaPolicies().isEmpty());
+ assertTrue(tc.getAllControlLoopParams().isEmpty());
+ assertTrue(tc.getAllEventManagers().isEmpty());
+ assertTrue(tc.getAllOnsetToEventManager().isEmpty());
+
+ PolicyControllerConstants.getFactory().shutdown(tc);
+ assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
+ assertFalse(DroolsControllerConstants.getFactory().inventory().contains(tc));
+ }
+
+ @Test
+ public void onsetErrors() throws Exception {
+ TdjamController tc = (TdjamController) PolicyControllerConstants.getFactory().build("tc", prop);
+ assertTrue(PolicyControllerConstants.getFactory().inventory().contains(tc));
+ assertTrue(DroolsControllerConstants.getFactory().inventory().contains(tc));
+ tc.start();
+
+ buildToscaPolicy("a", "1", tc);
+ final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
+ assertTrue(tc.getAllControlLoopParams().contains(clpa1));
+
+ CanonicalOnset canonicalOnset = new CanonicalOnset();
+ startLog();
+ Whitebox.invokeMethod(tc, "processEvent", canonicalOnset);
+ stopLog();
+ assertLog(".*No ControlLoopParams for event: CanonicalOnset.*");
+
+ // set Name with new canonicalOnset
+ CanonicalOnset canonicalOnset2 = new CanonicalOnset();
+ canonicalOnset2.setClosedLoopControlName("clpa1");
+ // try with a non-null requestID, but missing target
+ canonicalOnset2.setRequestId(UUID.randomUUID());
+ startLog();
+ Whitebox.invokeMethod(tc, "processEvent", canonicalOnset2);
+ stopLog();
+ assertLog(".*Exception creating ControlLoopEventManager.*");
+
+ PolicyControllerConstants.getFactory().shutdown(tc);
+ assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
+ }
+
+ private ToscaPolicy buildToscaPolicy(String name, String version, TdjamController tc) {
+ ToscaPolicy tp = new ToscaPolicy();
+ tp.setName(name);
+ tp.setVersion(version);
+
+ if (tc != null) {
+ tc.addToscaPolicy(tp);
+ }
+ return tp;
+ }
+
+ private ControlLoopParams buildControlLoopParams(String name, String version,
+ String closedLoopControlName, TdjamController tc) {
+
+ ControlLoopParams clp = new ControlLoopParams();
+ clp.setPolicyName(name);
+ clp.setPolicyVersion(version);
+ clp.setClosedLoopControlName(closedLoopControlName);
+
+ if (tc != null) {
+ assertTrue(tc.addControlLoopParams(clp) != clp);
+ }
+
+ return clp;
+ }
+
+ private void startLog() {
+ appender.list.clear();
+ appender.start();
+ }
+
+ private void stopLog() {
+ appender.stop();
+ }
+
+ private void assertLog(String regexp) {
+ for (ILoggingEvent event : appender.list) {
+ if (event.toString().matches(regexp)) {
+ return;
+ }
+ }
+ fail("Missing log entry: " + regexp);
+ }
+}
diff --git a/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java
new file mode 100644
index 000000000..ee96cb893
--- /dev/null
+++ b/controlloop/common/controller-tdjam/src/test/java/org/onap/policy/extension/system/NonDroolsPolicyControllerTest.java
@@ -0,0 +1,343 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.extension.system;
+
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY_CONTROLLER_TYPE;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.function.Function;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.policy.common.endpoints.event.comm.TopicSink;
+import org.onap.policy.controlloop.VirtualControlLoopNotification;
+import org.onap.policy.drools.controller.DroolsController;
+import org.onap.policy.drools.features.DroolsControllerFeatureApi;
+import org.onap.policy.drools.features.PolicyControllerFeatureApi;
+import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
+import org.onap.policy.drools.system.PolicyController;
+import org.onap.policy.drools.system.PolicyControllerConstants;
+import org.onap.policy.drools.utils.PropertyUtil;
+
+public class NonDroolsPolicyControllerTest {
+ //public static boolean loop = true;
+ private static Properties prop;
+
+ @BeforeClass
+ public static void setupClass() throws Exception {
+ prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
+ }
+
+ @Test
+ public void testState() {
+ NonDroolsPolicyController controller = buildController("tdjam");
+
+ assertEquals("nondrools", controller.getName());
+ assertEquals("NonDroolsPolicyController", controller.getGroupId());
+ assertEquals("nondrools", controller.getArtifactId());
+ assertEquals("1.0", controller.getVersion());
+ assertTrue(controller.isBrained());
+
+ assertFalse(controller.isAlive());
+ assertFalse(controller.isLocked());
+
+ // first 'start()'
+ controller.start();
+ assertTrue(controller.isAlive());
+ assertFalse(controller.isLocked());
+
+ // second 'start()'
+ controller.start();
+ assertTrue(controller.isAlive());
+ assertFalse(controller.isLocked());
+
+ // test a few stubbed-off methods
+ assertTrue(controller.getSessionNames().isEmpty());
+ assertTrue(controller.getCanonicalSessionNames().isEmpty());
+ assertTrue(controller.getBaseDomainNames().isEmpty());
+ assertFalse(controller.offer("topic", "event"));
+ assertFalse(controller.offer("event"));
+ assertEquals(0, controller.getRecentSourceEvents().length);
+ assertEquals(0, controller.getRecentSinkEvents().length);
+ assertNull(controller.getContainer());
+ assertThatIllegalArgumentException().isThrownBy(
+ () -> controller.fetchModelClass("NoSuchClass"));
+ assertThatIllegalStateException().isThrownBy(
+ () -> controller.updateToVersion(null, null, null, null, null));
+ assertTrue(controller.factClassNames(null).isEmpty());
+ assertEquals(0, controller.factCount(null));
+ assertTrue(controller.facts(null, null, false).isEmpty());
+ assertTrue(controller.facts("sessionName", String.class).isEmpty());
+ assertTrue(controller.factQuery(null, null, null, false).isEmpty());
+ assertFalse(controller.delete("sessionName", "fact"));
+ assertFalse(controller.delete("fact"));
+ assertFalse(controller.delete("sessionName", String.class));
+ assertFalse(controller.delete(String.class));
+
+ controller.lock();
+ assertTrue(controller.isAlive());
+ assertTrue(controller.isLocked());
+
+ controller.stop();
+ assertFalse(controller.isAlive());
+ assertTrue(controller.isLocked());
+
+ controller.unlock();
+ assertFalse(controller.isAlive());
+ assertFalse(controller.isLocked());
+
+ destroy(controller);
+ }
+
+ @Test
+ public void deliverTest() {
+ DroolsControllerFeatureHandler.resetStats();
+ final NonDroolsPolicyController controller = buildController("tdjam");
+
+ final TopicSink topicSink = mock(TopicSink.class);
+ when(topicSink.getTopic()).thenReturn("POLICY-CL-MGT");
+ when(topicSink.send(any())).thenReturn(false);
+
+ final VirtualControlLoopNotification msg = new VirtualControlLoopNotification(null);
+
+ controller.lock();
+
+ // invalid sink
+ try {
+ controller.deliver(null, null);
+ fail("Expected IllegalArgumentException did not occur");
+ } catch (IllegalArgumentException ex) {
+ assertTrue(ex.getMessage(),
+ ex.getMessage().endsWith(" invalid sink"));
+ }
+
+ // invalid event
+ try {
+ controller.deliver(topicSink, null);
+ fail("Expected IllegalArgumentException did not occur");
+ } catch (IllegalArgumentException ex) {
+ assertTrue(ex.getMessage(),
+ ex.getMessage().endsWith(" invalid event"));
+ }
+
+ // is locked
+ try {
+ controller.deliver(topicSink, "event");
+ fail("Expected IllegalStateException did not occur");
+ } catch (IllegalStateException ex) {
+ assertTrue(ex.getMessage(),
+ ex.getMessage().endsWith(" is locked"));
+ }
+ controller.unlock();
+
+ // is stopped
+ try {
+ controller.deliver(topicSink, "event");
+ fail("Expected IllegalStateException did not occur");
+ } catch (IllegalStateException ex) {
+ assertTrue(ex.getMessage(),
+ ex.getMessage().endsWith(" is stopped"));
+ }
+
+ // there should have been 4 'beforeDeliver' calls up to this point
+ assertEquals(4, DroolsControllerFeatureHandler.beforeDeliverFalse);
+
+ Function<String, Boolean> signal = (sig) -> {
+ msg.getAai().put("signal", sig);
+ return controller.deliver(topicSink, msg);
+ };
+
+ controller.start();
+
+ // 'beforeDeliver' intercepts
+ DroolsControllerFeatureHandler.resetStats();
+
+ assertTrue(signal.apply("beforeDeliverTrue"));
+ assertEquals(1, DroolsControllerFeatureHandler.beforeDeliverTrue);
+ assertEquals(0, DroolsControllerFeatureHandler.afterDeliverFalse);
+
+ assertFalse(signal.apply("beforeDeliverException"));
+ assertEquals(1, DroolsControllerFeatureHandler.beforeDeliverException);
+ assertEquals(1, DroolsControllerFeatureHandler.afterDeliverFalse);
+ // it would be nice to check the log message at this point
+
+ // 'afterDeliver' intercepts
+ DroolsControllerFeatureHandler.resetStats();
+
+ assertTrue(signal.apply("afterDeliverTrue"));
+ assertEquals(1, DroolsControllerFeatureHandler.afterDeliverTrue);
+
+ assertFalse(signal.apply("afterDeliverException"));
+ assertEquals(1, DroolsControllerFeatureHandler.afterDeliverException);
+
+ assertFalse(signal.apply("nothing in particular"));
+ assertEquals(1, DroolsControllerFeatureHandler.afterDeliverFalse);
+
+ destroy(controller);
+ }
+
+ private NonDroolsPolicyController buildController(String type) {
+ prop.setProperty(PROPERTY_CONTROLLER_TYPE, type);
+ PolicyController controller =
+ PolicyControllerConstants.getFactory().build("nondrools", prop);
+ assertTrue(controller instanceof NonDroolsPolicyController);
+ return (NonDroolsPolicyController) controller;
+ }
+
+ private void destroy(PolicyController controller) {
+ String name = controller.getName();
+ assertSame(controller, PolicyControllerConstants.getFactory().get(name));
+ PolicyControllerConstants.getFactory().destroy(controller);
+ assertThatIllegalArgumentException().isThrownBy(
+ () -> PolicyControllerConstants.getFactory().get(name));
+ }
+
+ /* ============================================================ */
+
+ /**
+ * An instance of this class is called by 'IndexedPolicyControllerFactory'.
+ * It does the build operation when the value of the 'controller.type'
+ * property matches the value of TDJAM_CONTROLLER_BUILDER_TAG.
+ */
+ public static class PolicyBuilder implements PolicyControllerFeatureApi {
+ @Override
+ public int getSequenceNumber() {
+ return 1;
+ }
+
+ @Override
+ public PolicyController beforeInstance(String name, Properties properties) {
+ if ("nondrools".equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) {
+ return new NonDroolsPolicyController(name, properties);
+ }
+ return null;
+ }
+ }
+
+ /* ============================================================ */
+
+ /**
+ * An instance of this class is called by 'IndexedDroolsControllerFactory'.
+ * It does the build operation when the value of the 'controller.type'
+ * property matches the value of TDJAM_CONTROLLER_BUILDER_TAG.
+ */
+ public static class DroolsBuilder implements DroolsControllerFeatureApi {
+ @Override
+ public int getSequenceNumber() {
+ return 1;
+ }
+
+ @Override
+ public DroolsController beforeInstance(Properties properties,
+ String groupId, String artifactId, String version,
+ List<TopicCoderFilterConfiguration> decoderConfigurations,
+ List<TopicCoderFilterConfiguration> encoderConfigurations) throws LinkageError {
+
+ if ("nondrools".equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) {
+ return NonDroolsPolicyController.getBuildInProgress();
+ }
+ return null;
+ }
+ }
+
+ /* ============================================================ */
+
+ public static class DroolsControllerFeatureHandler implements DroolsControllerFeatureApi {
+ static int beforeDeliverFalse = 0;
+ static int beforeDeliverTrue = 0;
+ static int beforeDeliverException = 0;
+ static int afterDeliverFalse = 0;
+ static int afterDeliverTrue = 0;
+ static int afterDeliverException = 0;
+
+ private static void resetStats() {
+ beforeDeliverFalse = 0;
+ beforeDeliverTrue = 0;
+ beforeDeliverException = 0;
+ afterDeliverFalse = 0;
+ afterDeliverTrue = 0;
+ afterDeliverException = 0;
+ }
+
+ @Override
+ public int getSequenceNumber() {
+ return 1;
+ }
+
+ @Override
+ public boolean beforeDeliver(DroolsController controller, TopicSink sink, Object fact) {
+ if (fact instanceof VirtualControlLoopNotification) {
+ String factString = ((VirtualControlLoopNotification) fact).getAai().get("signal");
+ if (factString == null) {
+ // this hook is run during 'FrankfurtTest' as well
+ return false;
+ }
+ if (factString.contains("beforeDeliverTrue")) {
+ beforeDeliverTrue += 1;
+ return true;
+ }
+ if (factString.contains("beforeDeliverException")) {
+ beforeDeliverException += 1;
+ RuntimeException ex = new RuntimeException("beforeDeliver");
+ ex.printStackTrace();
+ throw ex;
+ }
+ }
+ beforeDeliverFalse += 1;
+ return false;
+ }
+
+
+ @Override
+ public boolean afterDeliver(DroolsController controller, TopicSink sink, Object fact,
+ String json, boolean success) {
+
+ if (fact instanceof VirtualControlLoopNotification) {
+ String factString = ((VirtualControlLoopNotification) fact).getAai().get("signal");
+ if (factString == null) {
+ // this hook is run during 'FrankfurtTest' as well
+ return false;
+ }
+ if (factString.contains("afterDeliverTrue")) {
+ afterDeliverTrue += 1;
+ return true;
+ }
+ if (factString.contains("afterDeliverException")) {
+ afterDeliverException += 1;
+ throw new RuntimeException("afterDeliver");
+ }
+ }
+ afterDeliverFalse += 1;
+ return false;
+ }
+ }
+}