From 8382101e6c43578b6c78dfc10bacbd165e413c69 Mon Sep 17 00:00:00 2001 From: "Straubs, Ralph (rs8887)" Date: Mon, 13 Jul 2020 10:26:50 -0400 Subject: Support multiple Policy/Controller types using 'controller.type' property It provides a feature base to allow for custom Policy and Drools Controllers. Issue-ID: POLICY-2415 Change-Id: Ibe3f11e3ecd925537ffd03d2420bb3b8214029c9 Signed-off-by: Straubs, Ralph (rs8887) Signed-off-by: jhh --- .../controller/DroolsControllerFactoryTest.java | 73 +++++++++++++++++++ .../drools/system/PolicyControllerFactoryTest.java | 85 ++++++++++++++++++++++ ...licy.drools.features.DroolsControllerFeatureApi | 2 + ...licy.drools.features.PolicyControllerFeatureApi | 1 + 4 files changed, 161 insertions(+) create mode 100644 policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.DroolsControllerFeatureApi create mode 100644 policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureApi (limited to 'policy-management/src/test') diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java index 7733ca1f..03301470 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java @@ -22,15 +22,26 @@ package org.onap.policy.drools.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +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 java.util.List; import java.util.Properties; import org.junit.Test; +import org.onap.policy.drools.controller.internal.NullDroolsController; +import org.onap.policy.drools.features.DroolsControllerFeatureApi; +import org.onap.policy.drools.properties.DroolsPropertyConstants; +import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration; public class DroolsControllerFactoryTest { + private static final String DROOLS_CONTROLLER_BUILDER_TAG = "DroolsControllerFactoryTest"; + public static final String TEST_GROUP_ID = "testGroupId"; + public static final String TEST_ARTIFACT_ID = "testArtifactId"; + public static final String TEST_VERSION = "testVersion"; + @Test public void testBuildNullController() { Properties droolsProps = new Properties(); @@ -95,6 +106,28 @@ public class DroolsControllerFactoryTest { return DroolsControllerConstants.NO_VERSION.equals(droolsController.getVersion()); } + @Test + public void testControllerType() { + DroolsControllerFactory droolsFactory = new IndexedDroolsControllerFactory(); + Properties props = new Properties(); + + // this should build a 'NullDroolsController' + DroolsController ctrl1 = droolsFactory.build(props, null, null); + assertSame(NullDroolsController.class, ctrl1.getClass()); + + props.setProperty(PROPERTY_CONTROLLER_TYPE, DROOLS_CONTROLLER_BUILDER_TAG); + props.setProperty(DroolsPropertyConstants.RULES_GROUPID, TEST_GROUP_ID); + props.setProperty(DroolsPropertyConstants.RULES_ARTIFACTID, TEST_ARTIFACT_ID); + props.setProperty(DroolsPropertyConstants.RULES_VERSION, TEST_VERSION); + + // this should build a 'TestDroolsController' + DroolsController ctrl2 = droolsFactory.build(props, null, null); + assertSame(TestDroolsController.class, ctrl2.getClass()); + + // verify that we can find the controller in the factory table + assertSame(ctrl2, droolsFactory.get(ctrl2.getGroupId(), ctrl2.getArtifactId(), null)); + } + private boolean isActualController(DroolsController droolsController) { if (droolsController == null) { return false; @@ -110,4 +143,44 @@ public class DroolsControllerFactoryTest { return droolsController.getVersion() != null && droolsController.getVersion().substring(0, 1).matches("[0-9]"); } + + /** + * This class provides an alternate DroolsController implementation, + * for the purpose of easy identification within a Junit test. + */ + public static class TestDroolsController extends NullDroolsController { + @Override + public String getGroupId() { + return TEST_GROUP_ID; + } + + @Override + public String getArtifactId() { + return TEST_ARTIFACT_ID; + } + } + + /** + * An instance of this class is created by 'IndexedDroolsControllerFactory', + * using features. It does the build operation when the value of the + * 'controller.type' property matches the value of DROOLS_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 decoderConfigurations, + List encoderConfigurations) { + + if (DROOLS_CONTROLLER_BUILDER_TAG.equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) { + return new TestDroolsController(); + } + return null; + } + } } diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java index 1c5eb61d..818f8fc1 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java @@ -24,12 +24,14 @@ 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.assertNotNull; +import static org.junit.Assert.assertSame; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY_CONTROLLER_TYPE; import java.util.Arrays; import java.util.Collections; @@ -39,10 +41,15 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.common.utils.gson.GsonTestUtils; import org.onap.policy.drools.controller.DroolsController; +import org.onap.policy.drools.controller.internal.NullDroolsController; +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.protocol.configuration.DroolsConfiguration; +import org.onap.policy.drools.system.internal.AggregatedPolicyController; public class PolicyControllerFactoryTest { + private static final String POLICY_CONTROLLER_BUILDER_TAG = "PolicyControllerFactoryTest"; private static final String MY_NAME = "my-name-a"; private static final String MY_NAME2 = "my-name-b"; @@ -403,6 +410,30 @@ public class PolicyControllerFactoryTest { assertEquals(feature2, ipc.getFeatureProvider(FEATURE2)); } + @Test + public void testControllerType() { + PolicyControllerFactory factory = new IndexedPolicyControllerFactory(); + Properties props = new Properties(); + + // this should build an 'AggregatedPolicyController' + final String name1 = "ctrl1"; + PolicyController ctrl1 = factory.build(name1, props); + + // this should build a 'TestPolicyController' + final String name2 = "ctrl2"; + props.setProperty(PROPERTY_CONTROLLER_TYPE, POLICY_CONTROLLER_BUILDER_TAG); + PolicyController ctrl2 = factory.build(name2, props); + + // verify controller types + assertSame(AggregatedPolicyController.class, ctrl1.getClass()); + assertSame(TestPolicyController.class, ctrl2.getClass()); + assertSame(NullDroolsController.class, ctrl2.getDrools().getClass()); + + // verify controller lookups + assertSame(ctrl1, factory.get(name1)); + assertSame(ctrl2, factory.get(name2)); + } + /** * Factory with overrides. */ @@ -427,4 +458,58 @@ public class PolicyControllerFactoryTest { return providers; } } + + /** + * This class provides an alternate PolicyController implementation, + * for the purpose of easy identification within a junit test. + */ + public static class TestPolicyController extends AggregatedPolicyController { + public TestPolicyController(String name, Properties properties) { + super(name, properties); + } + } + + /** + * An instance of this class is created by 'IndexedPolicyControllerFactory', + * using features. It does the build operation when the value of the + * 'controller.type' property matches the value of POLICY_CONTROLLER_BUILDER_TAG. + */ + public static class PolicyBuilder implements PolicyControllerFeatureApi { + @Override + public int getSequenceNumber() { + return 1; + } + + @Override + public PolicyController beforeInstance(String name, Properties properties) { + if (POLICY_CONTROLLER_BUILDER_TAG.equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) { + return new TestPolicyController(name, properties); + } + return null; + } + } + + /** + * An instance of this class is created by 'IndexedDroolsControllerFactory', + * using features. It does the build operation when the value of the + * 'controller.type' property matches the value of POLICY_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 decoderConfigurations, + List encoderConfigurations) { + + if (POLICY_CONTROLLER_BUILDER_TAG.equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) { + return new NullDroolsController(); + } + return null; + } + } } diff --git a/policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.DroolsControllerFeatureApi b/policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.DroolsControllerFeatureApi new file mode 100644 index 00000000..3c765a78 --- /dev/null +++ b/policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.DroolsControllerFeatureApi @@ -0,0 +1,2 @@ +org.onap.policy.drools.controller.DroolsControllerFactoryTest$DroolsBuilder +org.onap.policy.drools.system.PolicyControllerFactoryTest$DroolsBuilder diff --git a/policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureApi b/policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureApi new file mode 100644 index 00000000..2a4408a8 --- /dev/null +++ b/policy-management/src/test/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureApi @@ -0,0 +1 @@ +org.onap.policy.drools.system.PolicyControllerFactoryTest$PolicyBuilder -- cgit 1.2.3-korg