diff options
author | Jorge Hernandez <jorge.hernandez-herrero@att.com> | 2018-11-29 16:27:36 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-11-29 16:27:36 +0000 |
commit | 5ab81d06125316e6df489e139ccfee9aba4faf3c (patch) | |
tree | bc7075f52428cc6e98311112fd1964b6d286279d /policy-management | |
parent | dfdb7deac7d857e67d68fe8f6ceb122ae682c3f8 (diff) | |
parent | 2808374bda04dbe709253ca079dfb89d5dbea7dd (diff) |
Merge "Remove Factory from various drools-pdp classes"
Diffstat (limited to 'policy-management')
2 files changed, 85 insertions, 88 deletions
diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java index 581184ee..cd578fca 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/AggregatedPolicyController.java @@ -53,11 +53,6 @@ public class AggregatedPolicyController implements PolicyController, TopicListen private static final Logger logger = LoggerFactory.getLogger(AggregatedPolicyController.class); /** - * Used to access various objects. Can be overridden by junit tests. - */ - private static Factory factory = new Factory(); - - /** * identifier for this policy controller. */ private final String name; @@ -121,14 +116,14 @@ public class AggregatedPolicyController implements PolicyController, TopicListen // Create/Reuse Readers/Writers for all event sources endpoints - this.sources = factory.getEndpointManager().addTopicSources(properties); - this.sinks = factory.getEndpointManager().addTopicSinks(properties); + this.sources = getEndpointManager().addTopicSources(properties); + this.sinks = getEndpointManager().addTopicSinks(properties); initDrools(properties); initSinks(); /* persist new properties */ - factory.getPersistenceManager().storeController(name, properties); + getPersistenceManager().storeController(name, properties); this.properties = properties; } @@ -140,7 +135,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen private void initDrools(Properties properties) { try { // Register with drools infrastructure - this.droolsController = factory.getDroolsFactory().build(properties, sources, sinks); + this.droolsController = getDroolsFactory().build(properties, sources, sinks); } catch (Exception | LinkageError e) { logger.error("{}: cannot init-drools because of {}", this, e.getMessage(), e); throw new IllegalArgumentException(e); @@ -183,7 +178,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen this.properties.setProperty(DroolsProperties.RULES_ARTIFACTID, newDroolsConfiguration.getArtifactId()); this.properties.setProperty(DroolsProperties.RULES_VERSION, newDroolsConfiguration.getVersion()); - factory.getPersistenceManager().storeController(name, this.properties); + getPersistenceManager().storeController(name, this.properties); this.initDrools(this.properties); @@ -226,7 +221,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen public boolean start() { logger.info("{}: start", this); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeStart(this)) { return true; @@ -265,7 +260,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen } } - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterStart(this)) { return true; @@ -286,7 +281,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen public boolean stop() { logger.info("{}: stop", this); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeStop(this)) { return true; @@ -315,7 +310,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen boolean success = this.droolsController.stop(); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterStop(this)) { return true; @@ -336,7 +331,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen public void shutdown() { logger.info("{}: shutdown", this); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeShutdown(this)) { return; @@ -349,9 +344,9 @@ public class AggregatedPolicyController implements PolicyController, TopicListen this.stop(); - factory.getDroolsFactory().shutdown(this.droolsController); + getDroolsFactory().shutdown(this.droolsController); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterShutdown(this)) { return; @@ -370,7 +365,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen public void halt() { logger.info("{}: halt", this); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeHalt(this)) { return; @@ -382,10 +377,10 @@ public class AggregatedPolicyController implements PolicyController, TopicListen } this.stop(); - factory.getDroolsFactory().destroy(this.droolsController); - factory.getPersistenceManager().deleteController(this.name); + getDroolsFactory().destroy(this.droolsController); + getPersistenceManager().deleteController(this.name); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterHalt(this)) { return; @@ -405,7 +400,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen logger.debug("{}: event offered from {}:{}: {}", this, commType, topic, event); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeOffer(this, commType, topic, event)) { return; @@ -426,7 +421,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen boolean success = this.droolsController.offer(topic, event); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterOffer(this, commType, topic, event, success)) { return; @@ -446,7 +441,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen logger.debug("{}: deliver event to {}:{}: {}", this, commType, topic, event); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeDeliver(this, commType, topic, event)) { return true; @@ -481,7 +476,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen boolean success = this.droolsController.deliver(this.topic2Sinks.get(topic), event); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterDeliver(this, commType, topic, event, success)) { return success; @@ -510,7 +505,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen public boolean lock() { logger.info("{}: lock", this); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeLock(this)) { return true; @@ -534,7 +529,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen boolean success = this.droolsController.lock(); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterLock(this)) { return true; @@ -556,7 +551,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen logger.info("{}: unlock", this); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.beforeUnlock(this)) { return true; @@ -577,7 +572,7 @@ public class AggregatedPolicyController implements PolicyController, TopicListen boolean success = this.droolsController.unlock(); - for (PolicyControllerFeatureAPI feature : factory.getFeatureProviders()) { + for (PolicyControllerFeatureAPI feature : getProviders()) { try { if (feature.afterUnlock(this)) { return true; @@ -639,26 +634,22 @@ public class AggregatedPolicyController implements PolicyController, TopicListen + ", locked=" + locked + ", droolsController=" + droolsController + "]"; } - /** - * Factory to access various objects. Can be overridden by junit tests. - */ - public static class Factory { - - public SystemPersistence getPersistenceManager() { - return SystemPersistence.manager; - } + // the following methods may be overridden by junit tests + + protected SystemPersistence getPersistenceManager() { + return SystemPersistence.manager; + } - public TopicEndpoint getEndpointManager() { - return TopicEndpoint.manager; - } + protected TopicEndpoint getEndpointManager() { + return TopicEndpoint.manager; + } - public DroolsControllerFactory getDroolsFactory() { - return DroolsController.factory; - } + protected DroolsControllerFactory getDroolsFactory() { + return DroolsController.factory; + } - public List<PolicyControllerFeatureAPI> getFeatureProviders() { - return PolicyControllerFeatureAPI.providers.getList(); - } + protected List<PolicyControllerFeatureAPI> getProviders() { + return PolicyControllerFeatureAPI.providers.getList(); } } diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java index 4f26419f..bb944fc9 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java @@ -37,9 +37,7 @@ import java.util.List; import java.util.Properties; import java.util.function.BiConsumer; import java.util.function.Consumer; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; @@ -50,16 +48,9 @@ import org.onap.policy.drools.controller.DroolsControllerFactory; import org.onap.policy.drools.features.PolicyControllerFeatureAPI; import org.onap.policy.drools.persistence.SystemPersistence; import org.onap.policy.drools.protocol.configuration.DroolsConfiguration; -import org.onap.policy.drools.system.internal.AggregatedPolicyController.Factory; -import org.powermock.reflect.Whitebox; public class AggregatedPolicyControllerTest { - /** - * Name of the "factory" field within the {@link AggregatedPolicyController} class. - */ - private static final String FACTORY_FIELD = "factory"; - private static final String AGG_NAME = "agg-name"; private static final String SINK_TOPIC1 = "sink-a"; private static final String SINK_TOPIC2 = "sink-b"; @@ -78,10 +69,7 @@ public class AggregatedPolicyControllerTest { private static final String GROUP2 = "group-b"; private static final String VERSION2 = "version-b"; - private static Factory savedFactory; - private Properties properties; - private Factory factory; private TopicEndpoint endpointMgr; private List<TopicSource> sources; private TopicSource source1; @@ -98,16 +86,6 @@ public class AggregatedPolicyControllerTest { private PolicyControllerFeatureAPI prov2; private AggregatedPolicyController apc; - @BeforeClass - public static void setUpBeforeClass() { - savedFactory = Whitebox.getInternalState(AggregatedPolicyController.class, FACTORY_FIELD); - } - - @AfterClass - public static void tearDownAfterClass() { - Whitebox.setInternalState(AggregatedPolicyController.class, FACTORY_FIELD, savedFactory); - } - /** * Initializes the object to be tested. */ @@ -158,26 +136,16 @@ public class AggregatedPolicyControllerTest { providers = Arrays.asList(prov1, prov2); - factory = mock(Factory.class); - Whitebox.setInternalState(AggregatedPolicyController.class, FACTORY_FIELD, factory); - - when(factory.getEndpointManager()).thenReturn(endpointMgr); - when(factory.getPersistenceManager()).thenReturn(persist); - when(factory.getDroolsFactory()).thenReturn(droolsFactory); - when(factory.getFeatureProviders()).thenReturn(providers); - - apc = new AggregatedPolicyController(AGG_NAME, properties); + apc = new AggregatedPolicyControllerImpl(AGG_NAME, properties); } @Test public void testFactory() { - assertNotNull(savedFactory); - - Factory factory = new Factory(); - assertNotNull(factory.getDroolsFactory()); - assertNotNull(factory.getEndpointManager()); - assertNotNull(factory.getFeatureProviders()); - assertNotNull(factory.getPersistenceManager()); + apc = new AggregatedPolicyController(AGG_NAME, properties); + assertNotNull(apc.getDroolsFactory()); + assertNotNull(apc.getEndpointManager()); + assertNotNull(apc.getProviders()); + assertNotNull(apc.getPersistenceManager()); } @Test @@ -187,14 +155,22 @@ public class AggregatedPolicyControllerTest { @Test(expected = IllegalArgumentException.class) public void testInitDrools_Ex() { - when(factory.getDroolsFactory()).thenThrow(new RuntimeException(EXPECTED)); - new AggregatedPolicyController(AGG_NAME, properties); + new AggregatedPolicyControllerImpl(AGG_NAME, properties) { + @Override + protected DroolsControllerFactory getDroolsFactory() { + throw new RuntimeException(EXPECTED); + } + }; } @Test(expected = IllegalArgumentException.class) public void testInitDrools_Error() { - when(factory.getDroolsFactory()).thenThrow(new LinkageError(EXPECTED)); - new AggregatedPolicyController(AGG_NAME, properties); + new AggregatedPolicyControllerImpl(AGG_NAME, properties) { + @Override + protected DroolsControllerFactory getDroolsFactory() { + throw new LinkageError(EXPECTED); + } + }; } @Test @@ -945,4 +921,34 @@ public class AggregatedPolicyControllerTest { assertThrows(AssertionError.class, () -> verifyAfter.accept(prov1)); assertThrows(AssertionError.class, () -> verifyAfter.accept(prov2)); } + + /** + * Controller with overrides. + */ + private class AggregatedPolicyControllerImpl extends AggregatedPolicyController { + + public AggregatedPolicyControllerImpl(String name, Properties properties) { + super(name, properties); + } + + @Override + protected SystemPersistence getPersistenceManager() { + return persist; + } + + @Override + protected TopicEndpoint getEndpointManager() { + return endpointMgr; + } + + @Override + protected DroolsControllerFactory getDroolsFactory() { + return droolsFactory; + } + + @Override + protected List<PolicyControllerFeatureAPI> getProviders() { + return providers; + } + } } |