diff options
Diffstat (limited to 'policy-management/src/test/java')
2 files changed, 266 insertions, 74 deletions
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java new file mode 100644 index 00000000..09d9cae9 --- /dev/null +++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java @@ -0,0 +1,145 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2018 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.drools.controller.internal; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.policy.drools.controller.DroolsController; + +public class NullDroolsControllerTest { + + @Test + public void start() { + DroolsController controller = new NullDroolsController(); + controller.start(); + Assert.assertFalse(controller.isAlive()); + controller.stop(); + Assert.assertFalse(controller.isAlive()); + controller.shutdown(); + Assert.assertFalse(controller.isAlive()); + controller.halt(); + Assert.assertFalse(controller.isAlive()); + } + + @Test + public void lock() { + DroolsController controller = new NullDroolsController(); + controller.lock(); + Assert.assertFalse(controller.isLocked()); + controller.unlock(); + Assert.assertFalse(controller.isLocked()); + } + + @Test + public void getGroupId() { + Assert.assertEquals(new NullDroolsController().getGroupId(), DroolsController.NO_GROUP_ID); + } + + @Test + public void getArtifactId() { + Assert.assertEquals(new NullDroolsController().getArtifactId(), DroolsController.NO_ARTIFACT_ID); + } + + @Test + public void getVersion() { + Assert.assertEquals(new NullDroolsController().getVersion(), DroolsController.NO_VERSION); + } + + @Test + public void getSessionNames() { + Assert.assertTrue(new NullDroolsController().getSessionNames().isEmpty()); + } + + @Test + public void getCanonicalSessionNames() { + Assert.assertTrue(new NullDroolsController().getCanonicalSessionNames().isEmpty()); + } + + @Test + public void offer() { + Assert.assertFalse(new NullDroolsController().offer(null, null)); + } + + @Test(expected = IllegalStateException.class) + public void deliver() { + new NullDroolsController().deliver(null, null); + } + + @Test + public void getRecentSourceEvents() { + Assert.assertTrue(new NullDroolsController().getRecentSourceEvents().length == 0); + } + + @Test + public void getRecentSinkEvents() { + Assert.assertTrue(new NullDroolsController().getRecentSinkEvents().length == 0); + } + + @Test + public void getContainer() { + Assert.assertNull(new NullDroolsController().getContainer()); + } + + @Test(expected = IllegalStateException.class) + public void ownsCoder() { + new NullDroolsController().ownsCoder(null, 0); + } + + @Test(expected = IllegalArgumentException.class) + public void fetchModelClass() { + new NullDroolsController().fetchModelClass(this.getClass().getCanonicalName()); + } + + @Test + public void isBrained() { + Assert.assertFalse(new NullDroolsController().isBrained()); + } + + @Test + public void stringify() { + Assert.assertNotNull(new NullDroolsController().toString()); + } + + @Test(expected = IllegalArgumentException.class) + public void updateToVersion() { + new NullDroolsController().updateToVersion(null, null, null, null, null); + } + + @Test + public void factClassNames() { + Assert.assertTrue(new NullDroolsController().factClassNames(null).isEmpty()); + } + + @Test + public void factCount() { + Assert.assertTrue(new NullDroolsController().factCount(null) == 0); + } + + @Test + public void facts() { + Assert.assertTrue(new NullDroolsController().facts(null, null, true).isEmpty()); + } + + @Test + public void factQuery() { + Assert.assertTrue(new NullDroolsController().factQuery(null, null, null, false).isEmpty()); + } +}
\ No newline at end of file diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java index b91ad4a0..e3dd7c54 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java @@ -22,6 +22,8 @@ package org.onap.policy.drools.protocol.coders; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -39,6 +41,7 @@ import org.onap.policy.drools.event.comm.TopicSink; import org.onap.policy.drools.properties.PolicyProperties; import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters; import org.onap.policy.drools.protocol.coders.JsonProtocolFilter.FilterRule; +import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder; import org.onap.policy.drools.util.KieUtils; import org.onap.policy.drools.utils.Triple; import org.slf4j.Logger; @@ -50,11 +53,17 @@ import org.slf4j.LoggerFactory; public class ProtocolCoderToolsetTest { public static final String JUNIT_PROTOCOL_CODER_ARTIFACT_ID = "protocolcoder"; public static final String JUNIT_PROTOCOL_CODER_TOPIC = JUNIT_PROTOCOL_CODER_ARTIFACT_ID; + public static final String CONTROLLER_ID = "blah"; + public static final String ARTIFACT_ID_ECHO = "echo"; + public static final String ARTIFACT_ID_POM_LINE = + "<artifactId>" + ARTIFACT_ID_ECHO + "</artifactId>"; private static Logger logger = LoggerFactory.getLogger(ProtocolCoderToolset.class); private volatile ReleaseId releaseId; + public static final Gson customCoder = new GsonBuilder().create(); + @Before public void setUp() throws IOException { if (releaseId != null) @@ -63,10 +72,10 @@ public class ProtocolCoderToolsetTest { String pom = new String(Files.readAllBytes (Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_POM_PATH))); - if (!pom.contains("<artifactId>echo</artifactId>")) + if (!pom.contains(ARTIFACT_ID_POM_LINE)) throw new IllegalArgumentException("unexpected junit test pom"); - String newPom = pom.replace("echo", JUNIT_PROTOCOL_CODER_ARTIFACT_ID); + String newPom = pom.replace(ARTIFACT_ID_ECHO, JUNIT_PROTOCOL_CODER_ARTIFACT_ID); String kmodule = new String(Files.readAllBytes (Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_PATH))); @@ -80,15 +89,16 @@ public class ProtocolCoderToolsetTest { } @Test - public void testGsonToolset() { - if (releaseId == null) - throw new IllegalStateException("no prereq artifact installed in maven repository"); - - JsonProtocolFilter protocolFilter = createFilterSet(); + public void testToolsets() { + createController(); + testGsonToolset(createFilterSet()); + testJacksonToolset(createFilterSet()); + } + public void testGsonToolset(JsonProtocolFilter protocolFilter) { GsonProtocolCoderToolset gsonToolset = new GsonProtocolCoderToolset(JUNIT_PROTOCOL_CODER_TOPIC, - "blah", + CONTROLLER_ID, this.releaseId.getGroupId(), this.releaseId.getArtifactId(), Triple.class.getCanonicalName(), @@ -100,18 +110,22 @@ public class ProtocolCoderToolsetTest { Assert.assertNotNull(gsonToolset.getDecoder()); testToolset(protocolFilter, gsonToolset); - } - @Test - public void testJacksonToolset() { - if (releaseId == null) - throw new IllegalStateException("no prereq artifact installed in maven repository"); + Triple<String, String, String> triple = createTriple(); + gsonToolset.setCustomCoder(new CustomGsonCoder(this.getClass().getCanonicalName(), + "customCoder")); + String tripleEncoded = encode(gsonToolset, triple); + decode(protocolFilter, gsonToolset, triple, tripleEncoded); + } - JsonProtocolFilter protocolFilter = createFilterSet(); + private Triple<String, String, String> createTriple() { + return new Triple<>("v1", "v2", "v3"); + } + public void testJacksonToolset(JsonProtocolFilter protocolFilter) { JacksonProtocolCoderToolset jacksonToolset = new JacksonProtocolCoderToolset(JUNIT_PROTOCOL_CODER_TOPIC, - "blah", + CONTROLLER_ID, this.releaseId.getGroupId(), this.releaseId.getArtifactId(), Triple.class.getCanonicalName(), @@ -125,32 +139,83 @@ public class ProtocolCoderToolsetTest { testToolset(protocolFilter, jacksonToolset); } - private JsonProtocolFilter createFilterSet() { - List<FilterRule> filters = new ArrayList<>(); - filters.add(new FilterRule("first", ".*")); - filters.add(new FilterRule("second", "^blah.*")); - filters.add(new FilterRule("third", "^hello$")); + private void testToolset(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) { - return new JsonProtocolFilter(filters); + validateInitialization(protocolFilter, coderToolset); + + updateCoderFilterRule(coderToolset); + + addRemoveCoder(coderToolset); + + /* restore original filters */ + coderToolset.addCoder(Triple.class.getCanonicalName(), protocolFilter, 654321); + + Triple<String, String, String> triple = createTriple(); + + String tripleEncoded = encode(coderToolset, triple); + + decode(protocolFilter, coderToolset, triple, tripleEncoded); } - private void testToolset(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) { + private void decode(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset, + Triple<String, String, String> triple, String tripleEncoded) { - Assert.assertTrue("blah".equals(coderToolset.getControllerId())); - Assert.assertTrue(this.releaseId.getGroupId().equals(coderToolset.getGroupId())); - Assert.assertTrue(this.releaseId.getArtifactId().equals(coderToolset.getArtifactId())); - Assert.assertNull(coderToolset.getCustomCoder()); + Triple<String, String, String> tripleDecoded = null; + try { + tripleDecoded = + (Triple<String, String, String>) coderToolset.decode(tripleEncoded); + } catch(UnsupportedOperationException e){ + /* OK */ + logger.trace("Junit expected exception - decode does not pass filtering", e); + } - Assert.assertTrue(coderToolset.getCoders().size() == 1); + CoderFilters coderFilters = coderToolset.getCoder(Triple.class.getCanonicalName()); + Assert.assertTrue(coderFilters.getCodedClass() == Triple.class.getCanonicalName()); + Assert.assertTrue(coderFilters.getFilter() == protocolFilter); + Assert.assertTrue(coderFilters.getFilter().getRules("second").size() == 1); + Assert.assertTrue(coderFilters.getFilter().getRules("third").size() == 1); - CoderFilters coderFilters = coderToolset.getCoder("blah"); - Assert.assertTrue(coderFilters == null); + coderFilters.getFilter().getRules("second").get(0).setRegex("^v2$"); + coderFilters.getFilter().getRules("third").get(0).setRegex(".*v3.*"); - coderFilters = coderToolset.getCoder(Triple.class.getCanonicalName()); - Assert.assertNotNull(coderFilters); + tripleDecoded = + (Triple<String, String, String>) coderToolset.decode(tripleEncoded); - Assert.assertEquals(coderFilters.getFilter(), protocolFilter); + Assert.assertTrue(tripleDecoded.first().equals(triple.first())); + Assert.assertTrue(tripleDecoded.second().equals(triple.second())); + Assert.assertTrue(tripleDecoded.third().equals(triple.third())); + + coderFilters.getFilter().deleteRules("third"); + Assert.assertTrue(coderFilters.getFilter().getRules("third").isEmpty()); + + tripleDecoded = + (Triple<String, String, String>) coderToolset.decode(tripleEncoded); + + Assert.assertTrue(tripleDecoded.first().equals(triple.first())); + Assert.assertTrue(tripleDecoded.second().equals(triple.second())); + Assert.assertTrue(tripleDecoded.third().equals(triple.third())); + + coderFilters.getFilter().addRule("third", ".*v3.*"); + } + + private String encode(ProtocolCoderToolset coderToolset, Triple<String, String, String> triple) { + String tripleEncoded = coderToolset.encode(triple); + Assert.assertTrue(!tripleEncoded.isEmpty()); + return tripleEncoded; + } + private void addRemoveCoder(ProtocolCoderToolset coderToolset) { + List<FilterRule> filters = new ArrayList<>(); + filters.add(new FilterRule("second", ".*")); + + coderToolset.addCoder(this.getClass().getCanonicalName(), new JsonProtocolFilter(filters),654321); + Assert.assertTrue(coderToolset.getCoders().size() == 2); + + coderToolset.removeCoders(this.getClass().getCanonicalName()); + Assert.assertTrue(coderToolset.getCoders().size() == 1); + } + + private void updateCoderFilterRule(ProtocolCoderToolset coderToolset) { List<FilterRule> filters = new ArrayList<>(); filters.add(new FilterRule("third", ".*")); coderToolset.addCoder(Triple.class.getCanonicalName(), @@ -173,21 +238,28 @@ public class ProtocolCoderToolsetTest { Assert.assertTrue (".*".equals(coderToolset.getCoder(Triple.class.getCanonicalName()). getFilter().getRules("third").get(0).getRegex())); + } - coderToolset.addCoder("blah", new JsonProtocolFilter(filters),654321); - Assert.assertTrue(coderToolset.getCoders().size() == 2); + private void validateInitialization(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) { + Assert.assertTrue(CONTROLLER_ID.equals(coderToolset.getControllerId())); + Assert.assertTrue(this.releaseId.getGroupId().equals(coderToolset.getGroupId())); + Assert.assertTrue(this.releaseId.getArtifactId().equals(coderToolset.getArtifactId())); + Assert.assertNull(coderToolset.getCustomCoder()); - coderToolset.removeCoders("blah"); Assert.assertTrue(coderToolset.getCoders().size() == 1); - /* restore original filters */ - coderToolset.addCoder(Triple.class.getCanonicalName(), protocolFilter, 654321); + CoderFilters coderFilters = coderToolset.getCoder(CONTROLLER_ID); + Assert.assertTrue(coderFilters == null); - Triple<String, String, String> triple = - new Triple<>("v1", "v2", "v3"); + coderFilters = coderToolset.getCoder(Triple.class.getCanonicalName()); + Assert.assertNotNull(coderFilters); - String tripleEncoded = coderToolset.encode(triple); - Assert.assertTrue(!tripleEncoded.isEmpty()); + Assert.assertEquals(coderFilters.getFilter(), protocolFilter); + } + + private void createController() { + if (releaseId == null) + throw new IllegalStateException("no prereq artifact installed in maven repository"); Properties sinkConfig = new Properties(); sinkConfig.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, JUNIT_PROTOCOL_CODER_TOPIC); @@ -200,43 +272,18 @@ public class ProtocolCoderToolsetTest { droolsControllerConfig.put(PolicyProperties.RULES_VERSION, releaseId.getVersion()); droolsControllerConfig.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS + "." + JUNIT_PROTOCOL_CODER_TOPIC + PolicyProperties.PROPERTY_TOPIC_EVENTS_SUFFIX, - Triple.class.getCanonicalName()); + Triple.class.getCanonicalName()); DroolsController droolsController = DroolsController.factory.build(droolsControllerConfig, null, noopTopics); + } - Triple<String, String, String> tripleDecoded = null; - try { - tripleDecoded = - (Triple<String, String, String>) coderToolset.decode(tripleEncoded); - } catch(UnsupportedOperationException e){ - logger.trace("Junit expected exception - decode does not pass filtering", e); - } - - coderFilters = coderToolset.getCoder(Triple.class.getCanonicalName()); - Assert.assertTrue(coderFilters.getCodedClass() == Triple.class.getCanonicalName()); - Assert.assertTrue(coderFilters.getFilter() == protocolFilter); - Assert.assertTrue(coderFilters.getFilter().getRules("second").size() == 1); - Assert.assertTrue(coderFilters.getFilter().getRules("third").size() == 1); - - coderFilters.getFilter().getRules("second").get(0).setRegex("^v2$"); - coderFilters.getFilter().getRules("third").get(0).setRegex(".*v3.*"); - - tripleDecoded = - (Triple<String, String, String>) coderToolset.decode(tripleEncoded); - - Assert.assertTrue(tripleDecoded.first().equals(triple.first())); - Assert.assertTrue(tripleDecoded.second().equals(triple.second())); - Assert.assertTrue(tripleDecoded.third().equals(triple.third())); - - coderFilters.getFilter().deleteRules("third"); - Assert.assertTrue(coderFilters.getFilter().getRules("third").isEmpty()); - - tripleDecoded = - (Triple<String, String, String>) coderToolset.decode(tripleEncoded); + private JsonProtocolFilter createFilterSet() { + List<FilterRule> filters = new ArrayList<>(); + filters.add(new FilterRule("first", ".*")); + filters.add(new FilterRule("second", "^blah.*")); + filters.add(new FilterRule("third", "^hello$")); - Assert.assertTrue(tripleDecoded.first().equals(triple.first())); - Assert.assertTrue(tripleDecoded.second().equals(triple.second())); - Assert.assertTrue(tripleDecoded.third().equals(triple.third())); + return new JsonProtocolFilter(filters); } } |