diff options
author | Charles Cole <cc847m@att.com> | 2017-10-06 09:18:45 -0500 |
---|---|---|
committer | Charles Cole <cc847m@att.com> | 2017-10-11 16:07:04 -0500 |
commit | 777302bddf16316fb555ebc0d9f32dd2ecc0ebf4 (patch) | |
tree | 57cc8bab695c89985670760475621e4360e35c05 /controlloop/templates/template.demo/src/test | |
parent | 82b577dfb36788936d1503c7fd77320536cc2d10 (diff) |
Enable junits to work with the archetype template
Control loop junits now use the actual PolicyEngine instead of a
simulation, allowing them to work with the archetype template
Issue-ID: POLICY-101
Change-Id: I910961996aaf831f4e91b7d7173d05fdfdd77683
Signed-off-by: Charles Cole <cc847m@att.com>
Diffstat (limited to 'controlloop/templates/template.demo/src/test')
5 files changed, 161 insertions, 67 deletions
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java index 35b339e0f..0c633c477 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java @@ -44,12 +44,9 @@ import org.kie.api.builder.Results; import org.kie.api.builder.model.KieModuleModel; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; -import org.onap.policy.controlloop.ControlLoopLogger; -import org.onap.policy.controlloop.impl.ControlLoopLoggerStdOutImpl; import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; import org.onap.policy.drools.http.server.HttpServletServer; -import org.onap.policy.drools.impl.PolicyEngineJUnitImpl; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.guard.PolicyGuardYamlToXacml; import org.slf4j.Logger; @@ -57,9 +54,6 @@ import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; -import com.att.research.xacml.api.pdp.PDPEngine; -import com.att.research.xacml.api.pdp.PDPEngineFactory; -import com.att.research.xacml.util.FactoryException; import com.att.research.xacml.util.XACMLProperties; public final class Util { @@ -245,30 +239,11 @@ public final class Util { "src/main/resources/blacklist_template.xml", "src/test/resources/xacml/autogenerated_blacklist.xml"); - - // - // Insert our globals - // - final ControlLoopLogger controlLoopLogger = new ControlLoopLoggerStdOutImpl(); - kieSession.setGlobal("Logger", controlLoopLogger); - final PolicyEngineJUnitImpl engine = new PolicyEngineJUnitImpl(); - kieSession.setGlobal("Engine", engine); - - // // Creating an embedded XACML PDP // - final PDPEngine xacmlPdpEngine; System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml/xacml_guard.properties"); - PDPEngineFactory factory; - try { - factory = PDPEngineFactory.newInstance(); - xacmlPdpEngine = factory.newEngine(); - kieSession.setGlobal("XacmlPdpEngine", xacmlPdpEngine); - } catch (FactoryException e1) { - e1.printStackTrace(); - } return kieSession; } diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java index 4dc6b806f..ae8bcb4e4 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java @@ -26,6 +26,8 @@ import java.io.IOException; import java.net.URLEncoder; import java.time.Instant; import java.util.HashMap; +import java.util.List; +import java.util.Properties; import java.util.UUID; import org.junit.AfterClass; @@ -42,19 +44,26 @@ import org.onap.policy.controlloop.ControlLoopNotificationType; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.VirtualControlLoopNotification; import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.drools.PolicyEngineListener; +import org.onap.policy.drools.event.comm.Topic.CommInfrastructure; +import org.onap.policy.drools.event.comm.TopicEndpoint; +import org.onap.policy.drools.event.comm.TopicListener; +import org.onap.policy.drools.event.comm.TopicSink; import org.onap.policy.drools.http.server.HttpServletServer; -import org.onap.policy.drools.impl.PolicyEngineJUnitImpl; +import org.onap.policy.drools.properties.PolicyProperties; +import org.onap.policy.drools.protocol.coders.EventProtocolCoder; +import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; +import org.onap.policy.drools.system.PolicyEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class VCPEControlLoopTest implements PolicyEngineListener { +public class VCPEControlLoopTest implements TopicListener { private static final Logger logger = LoggerFactory.getLogger(VCPEControlLoopTest.class); + private static List<? extends TopicSink> noopTopics; + private KieSession kieSession; private Util.Pair<ControlLoopPolicy, String> pair; - private PolicyEngineJUnitImpl engine; private UUID requestID; static { @@ -66,6 +75,18 @@ public class VCPEControlLoopTest implements PolicyEngineListener { @BeforeClass public static void setUpSimulator() { + PolicyEngine.manager.configure(new Properties()); + assertTrue(PolicyEngine.manager.start()); + Properties noopSinkProperties = new Properties(); + noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-LCM-READ,POLICY-CL-MGT"); + noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events", "org.onap.policy.appclcm.LCMRequestWrapper"); + noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events.custom.gson", "org.onap.policy.appclcm.util.Serialization,gson"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events", "org.onap.policy.controlloop.VirtualControlLoopNotification"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson", "org.onap.policy.controlloop.util.Serialization,gsonPretty"); + noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties); + + EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT", "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null, 1111); + EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "APPC-LCM-READ", "org.onap.policy.appclcm.LCMRequestWrapper", new JsonProtocolFilter(), null, null, 1111); try { Util.buildAaiSim(); Util.buildGuardSim(); @@ -77,6 +98,7 @@ public class VCPEControlLoopTest implements PolicyEngineListener { @AfterClass public static void tearDownSimulator() { HttpServletServer.factory.destroy(); + PolicyEngine.manager.shutdown(); } @Test @@ -85,7 +107,7 @@ public class VCPEControlLoopTest implements PolicyEngineListener { * Start the kie session */ try { - kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl", + kieSession = startSession("../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl", "src/test/resources/yaml/policy_ControlLoop_vCPE.yaml", "service=ServiceDemo;resource=Res1Demo;type=operational", "CL_vCPE", @@ -101,7 +123,10 @@ public class VCPEControlLoopTest implements PolicyEngineListener { * notify that there is an event ready to be pulled * from the queue */ - engine.addListener(this); + for (TopicSink sink : noopTopics) { + assertTrue(sink.start()); + sink.register(this); + } /* * Create a unique requestId @@ -114,6 +139,7 @@ public class VCPEControlLoopTest implements PolicyEngineListener { * the rules */ sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET); + kieSession.fireUntilHalt(); @@ -179,7 +205,6 @@ public class VCPEControlLoopTest implements PolicyEngineListener { /* * Retrieve the Policy Engine */ - engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine"); logger.debug("============"); logger.debug(URLEncoder.encode(pair.b, "UTF-8")); @@ -192,12 +217,17 @@ public class VCPEControlLoopTest implements PolicyEngineListener { * (non-Javadoc) * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String) */ - public void newEventNotification(String topic) { + public void onTopicEvent(CommInfrastructure commType, String topic, String event) { /* * Pull the object that was sent out to DMAAP and make * sure it is a ControlLoopNoticiation of type active */ - Object obj = engine.subscribe("UEB", topic); + Object obj = null; + if ("POLICY-CL-MGT".equals(topic)) { + obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.controlloop.VirtualControlLoopNotification.class); + } + else if ("APPC-LCM-READ".equals(topic)) + obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.appclcm.LCMRequestWrapper.class); assertNotNull(obj); if (obj instanceof VirtualControlLoopNotification) { VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj; @@ -216,7 +246,7 @@ public class VCPEControlLoopTest implements PolicyEngineListener { logger.debug("Rule Fired: " + notification.policyName); assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification)); assertNotNull(notification.message); - assertTrue(notification.message.endsWith("PERMIT")); + assertTrue(notification.message.toLowerCase().endsWith("permit")); } else if (policyName.endsWith("GUARD_PERMITTED")) { logger.debug("Rule Fired: " + notification.policyName); @@ -305,4 +335,5 @@ public class VCPEControlLoopTest implements PolicyEngineListener { logger.debug("FACT: {}", handle); } } + } diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java index 641b56f13..c64764baf 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java @@ -29,6 +29,8 @@ import java.io.IOException; import java.net.URLEncoder; import java.time.Instant; import java.util.HashMap; +import java.util.List; +import java.util.Properties; import java.util.UUID; import org.junit.AfterClass; @@ -41,21 +43,27 @@ import org.onap.policy.controlloop.ControlLoopNotificationType; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.VirtualControlLoopNotification; import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.drools.PolicyEngineListener; +import org.onap.policy.drools.event.comm.TopicEndpoint; +import org.onap.policy.drools.event.comm.TopicListener; +import org.onap.policy.drools.event.comm.TopicSink; +import org.onap.policy.drools.event.comm.Topic.CommInfrastructure; import org.onap.policy.drools.http.server.HttpServletServer; -import org.onap.policy.drools.impl.PolicyEngineJUnitImpl; - +import org.onap.policy.drools.properties.PolicyProperties; +import org.onap.policy.drools.protocol.coders.EventProtocolCoder; +import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; +import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.so.SORequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class VDNSControlLoopTest implements PolicyEngineListener { +public class VDNSControlLoopTest implements TopicListener { private static final Logger logger = LoggerFactory.getLogger(VDNSControlLoopTest.class); + private static List<? extends TopicSink> noopTopics; + private KieSession kieSession; private Util.Pair<ControlLoopPolicy, String> pair; - private PolicyEngineJUnitImpl engine; private UUID requestID; static { @@ -68,6 +76,16 @@ public class VDNSControlLoopTest implements PolicyEngineListener { @BeforeClass public static void setUpSimulator() { + PolicyEngine.manager.configure(new Properties()); + assertTrue(PolicyEngine.manager.start()); + Properties noopSinkProperties = new Properties(); + noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, "POLICY-CL-MGT"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events", "org.onap.policy.controlloop.VirtualControlLoopNotification"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson", "org.onap.policy.controlloop.util.Serialization,gsonPretty"); + noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties); + + EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT", "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null, 1111); + try { Util.buildAaiSim(); Util.buildSoSim(); @@ -80,6 +98,7 @@ public class VDNSControlLoopTest implements PolicyEngineListener { @AfterClass public static void tearDownSimulator() { HttpServletServer.factory.destroy(); + PolicyEngine.manager.shutdown(); } @Test @@ -89,7 +108,7 @@ public class VDNSControlLoopTest implements PolicyEngineListener { * Start the kie session */ try { - kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl", + kieSession = startSession("../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl", "src/test/resources/yaml/policy_ControlLoop_SO-test.yaml", "type=operational", "CL_vDNS", @@ -105,7 +124,10 @@ public class VDNSControlLoopTest implements PolicyEngineListener { * notify that there is an event ready to be pulled * from the queue */ - engine.addListener(this); + for (TopicSink sink : noopTopics) { + assertTrue(sink.start()); + sink.register(this); + } /* * Create a unique requestId @@ -183,7 +205,6 @@ public class VDNSControlLoopTest implements PolicyEngineListener { /* * Retrieve the Policy Engine */ - engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine"); logger.debug("============"); logger.debug(URLEncoder.encode(pair.b, "UTF-8")); @@ -196,12 +217,15 @@ public class VDNSControlLoopTest implements PolicyEngineListener { * (non-Javadoc) * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String) */ - public void newEventNotification(String topic) { + public void onTopicEvent(CommInfrastructure commType, String topic, String event) { /* * Pull the object that was sent out to DMAAP and make * sure it is a ControlLoopNoticiation of type active */ - Object obj = engine.subscribe("UEB", topic); + Object obj = null; + if ("POLICY-CL-MGT".equals(topic)) { + obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.controlloop.VirtualControlLoopNotification.class); + } assertNotNull(obj); if (obj instanceof VirtualControlLoopNotification) { VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj; @@ -220,7 +244,7 @@ public class VDNSControlLoopTest implements PolicyEngineListener { logger.debug("Rule Fired: " + notification.policyName); assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification)); assertNotNull(notification.message); - assertTrue(notification.message.endsWith("PERMIT")); + assertTrue(notification.message.toLowerCase().endsWith("permit")); } else if (policyName.endsWith("GUARD_PERMITTED")) { logger.debug("Rule Fired: " + notification.policyName); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFCControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFCControlLoopTest.java index 6f0527773..c139ab339 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFCControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFCControlLoopTest.java @@ -29,6 +29,8 @@ import java.io.IOException; import java.net.URLEncoder; import java.time.Instant; import java.util.HashMap; +import java.util.List; +import java.util.Properties; import java.util.UUID; import org.junit.AfterClass; @@ -42,20 +44,28 @@ import org.onap.policy.controlloop.ControlLoopTargetType; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.VirtualControlLoopNotification; import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.drools.PolicyEngineListener; +import org.onap.policy.drools.event.comm.TopicEndpoint; +import org.onap.policy.drools.event.comm.TopicListener; +import org.onap.policy.drools.event.comm.TopicSink; +import org.onap.policy.drools.event.comm.Topic.CommInfrastructure; import org.onap.policy.drools.http.server.HttpServletServer; -import org.onap.policy.drools.impl.PolicyEngineJUnitImpl; +import org.onap.policy.drools.properties.PolicyProperties; +import org.onap.policy.drools.protocol.coders.EventProtocolCoder; +import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; +import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.vfc.VFCRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class VFCControlLoopTest implements PolicyEngineListener { +public class VFCControlLoopTest implements TopicListener { private static final Logger logger = LoggerFactory.getLogger(VFCControlLoopTest.class); + + private static List<? extends TopicSink> noopTopics; + private KieSession kieSession; private Util.Pair<ControlLoopPolicy, String> pair; - private PolicyEngineJUnitImpl engine; private UUID requestID; static { @@ -68,6 +78,16 @@ public class VFCControlLoopTest implements PolicyEngineListener { @BeforeClass public static void setUpSimulator() { + PolicyEngine.manager.configure(new Properties()); + assertTrue(PolicyEngine.manager.start()); + Properties noopSinkProperties = new Properties(); + noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, "POLICY-CL-MGT"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events", "org.onap.policy.controlloop.VirtualControlLoopNotification"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson", "org.onap.policy.controlloop.util.Serialization,gsonPretty"); + noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties); + + EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT", "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null, 1111); + try { Util.buildAaiSim(); Util.buildVfcSim(); @@ -80,6 +100,7 @@ public class VFCControlLoopTest implements PolicyEngineListener { @AfterClass public static void tearDownSimulator() { HttpServletServer.factory.destroy(); + PolicyEngine.manager.shutdown(); } @Test @@ -89,7 +110,7 @@ public class VFCControlLoopTest implements PolicyEngineListener { * Start the kie session */ try { - kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl", + kieSession = startSession("../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl", "src/test/resources/yaml/policy_ControlLoop_VFC.yaml", "type=operational", "CL_VoLTE", @@ -105,7 +126,10 @@ public class VFCControlLoopTest implements PolicyEngineListener { * notify that there is an event ready to be pulled * from the queue */ - engine.addListener(this); + for (TopicSink sink : noopTopics) { + assertTrue(sink.start()); + sink.register(this); + } /* * Create a unique requestId @@ -183,7 +207,6 @@ public class VFCControlLoopTest implements PolicyEngineListener { /* * Retrieve the Policy Engine */ - engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine"); logger.debug("============"); logger.debug(URLEncoder.encode(pair.b, "UTF-8")); @@ -196,12 +219,15 @@ public class VFCControlLoopTest implements PolicyEngineListener { * (non-Javadoc) * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String) */ - public void newEventNotification(String topic) { + public void onTopicEvent(CommInfrastructure commType, String topic, String event) { /* * Pull the object that was sent out to DMAAP and make * sure it is a ControlLoopNoticiation of type active */ - Object obj = engine.subscribe("UEB", topic); + Object obj = null; + if ("POLICY-CL-MGT".equals(topic)) { + obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.controlloop.VirtualControlLoopNotification.class); + } assertNotNull(obj); if (obj instanceof VirtualControlLoopNotification) { VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj; @@ -220,7 +246,7 @@ public class VFCControlLoopTest implements PolicyEngineListener { logger.debug("Rule Fired: " + notification.policyName); assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification)); assertNotNull(notification.message); - assertTrue(notification.message.endsWith("PERMIT")); + assertTrue(notification.message.toLowerCase().endsWith("permit")); } else if (policyName.endsWith("GUARD_PERMITTED")) { logger.debug("Rule Fired: " + notification.policyName); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java index fb4dc0265..cef39b5e8 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java @@ -29,6 +29,8 @@ import java.io.IOException; import java.net.URLEncoder; import java.time.Instant; import java.util.HashMap; +import java.util.List; +import java.util.Properties; import java.util.UUID; import org.junit.AfterClass; @@ -44,19 +46,26 @@ import org.onap.policy.controlloop.ControlLoopNotificationType; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.VirtualControlLoopNotification; import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.drools.PolicyEngineListener; +import org.onap.policy.drools.event.comm.TopicEndpoint; +import org.onap.policy.drools.event.comm.TopicListener; +import org.onap.policy.drools.event.comm.TopicSink; +import org.onap.policy.drools.event.comm.Topic.CommInfrastructure; import org.onap.policy.drools.http.server.HttpServletServer; -import org.onap.policy.drools.impl.PolicyEngineJUnitImpl; +import org.onap.policy.drools.properties.PolicyProperties; +import org.onap.policy.drools.protocol.coders.EventProtocolCoder; +import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; +import org.onap.policy.drools.system.PolicyEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class VFWControlLoopTest implements PolicyEngineListener { +public class VFWControlLoopTest implements TopicListener { private static final Logger logger = LoggerFactory.getLogger(VFWControlLoopTest.class); + private static List<? extends TopicSink> noopTopics; + private KieSession kieSession; private Util.Pair<ControlLoopPolicy, String> pair; - private PolicyEngineJUnitImpl engine; private UUID requestID; static { @@ -68,6 +77,19 @@ public class VFWControlLoopTest implements PolicyEngineListener { @BeforeClass public static void setUpSimulator() { + PolicyEngine.manager.configure(new Properties()); + assertTrue(PolicyEngine.manager.start()); + Properties noopSinkProperties = new Properties(); + noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL,POLICY-CL-MGT"); + noopSinkProperties.put("noop.sink.topics.APPC-CL.events", "org.onap.policy.appc.Response"); + noopSinkProperties.put("noop.sink.topics.APPC-CL.events.custom.gson", "org.onap.policy.appc.util.Serialization,gsonPretty"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events", "org.onap.policy.controlloop.VirtualControlLoopNotification"); + noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson", "org.onap.policy.controlloop.util.Serialization,gsonPretty"); + noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties); + + EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT", "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null, null, 1111); + EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "APPC-CL", "org.onap.policy.appc.Request", new JsonProtocolFilter(), null, null, 1111); + try { Util.buildAaiSim(); Util.buildGuardSim(); @@ -79,6 +101,7 @@ public class VFWControlLoopTest implements PolicyEngineListener { @AfterClass public static void tearDownSimulator() { HttpServletServer.factory.destroy(); + PolicyEngine.manager.shutdown(); } @Test @@ -88,7 +111,7 @@ public class VFWControlLoopTest implements PolicyEngineListener { * Start the kie session */ try { - kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl", + kieSession = startSession("../archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl", "src/test/resources/yaml/policy_ControlLoop_vFW.yaml", "service=ServiceDemo;resource=Res1Demo;type=operational", "CL_vFW", @@ -104,7 +127,10 @@ public class VFWControlLoopTest implements PolicyEngineListener { * notify that there is an event ready to be pulled * from the queue */ - engine.addListener(this); + for (TopicSink sink : noopTopics) { + assertTrue(sink.start()); + sink.register(this); + } /* * Create a unique requestId @@ -118,7 +144,15 @@ public class VFWControlLoopTest implements PolicyEngineListener { */ sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET); - kieSession.fireUntilHalt(); + try { + kieSession.fireUntilHalt(); + } + catch (Exception e) { + e.printStackTrace(); + logger.warn(e.toString()); + fail("fail"); + } + /* * The only fact in memory should be Params @@ -182,7 +216,6 @@ public class VFWControlLoopTest implements PolicyEngineListener { /* * Retrieve the Policy Engine */ - engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine"); logger.debug("============"); logger.debug(URLEncoder.encode(pair.b, "UTF-8")); @@ -194,12 +227,17 @@ public class VFWControlLoopTest implements PolicyEngineListener { /* * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String) */ - public void newEventNotification(String topic) { + public void onTopicEvent(CommInfrastructure commType, String topic, String event) { /* * Pull the object that was sent out to DMAAP and make * sure it is a ControlLoopNoticiation of type active */ - Object obj = engine.subscribe("UEB", topic); + Object obj = null; + if ("POLICY-CL-MGT".equals(topic)) { + obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event, org.onap.policy.controlloop.VirtualControlLoopNotification.class); + } + else if ("APPC-CL".equals(topic)) + obj = org.onap.policy.appc.util.Serialization.gsonPretty.fromJson(event, org.onap.policy.appc.Request.class); assertNotNull(obj); if (obj instanceof VirtualControlLoopNotification) { VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj; @@ -218,7 +256,7 @@ public class VFWControlLoopTest implements PolicyEngineListener { logger.debug("Rule Fired: " + notification.policyName); assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification)); assertNotNull(notification.message); - assertTrue(notification.message.endsWith("PERMIT")); + assertTrue(notification.message.toLowerCase().endsWith("permit")); } else if (policyName.endsWith("GUARD_PERMITTED")) { logger.debug("Rule Fired: " + notification.policyName); |