diff options
author | Peyton Puckett <peyton.puckett@att.com> | 2019-10-02 08:29:51 -0500 |
---|---|---|
committer | Peyton Puckett <peyton.puckett@att.com> | 2019-10-02 08:49:30 -0500 |
commit | b3819015a98f619b7cfbfc13ac1709f3e6dd3334 (patch) | |
tree | a9dafcd80c676204566e2894eedbfe3835d5916c /feature-mdc-filters/src/test | |
parent | 68b17b2a1ee2b6d1920cfc72f08dae25565bbe6a (diff) |
Remove windows carriage return
Remove windows CR from code in feature-controller-logging/
Remove windows CR from code in feature-mdc-filters/
Issue-ID: POLICY-2032
Change-Id: I9779f6907fb5a50ab6129969081c2228ce91d3a7
Signed-off-by: Peyton Puckett <peyton.puckett@att.com>
Diffstat (limited to 'feature-mdc-filters/src/test')
2 files changed, 830 insertions, 830 deletions
diff --git a/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java index 8efa96d6..51ff3e92 100755 --- a/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java +++ b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java @@ -1,169 +1,169 @@ -/*
- * ============LICENSE_START=======================================================
- * feature-mdc-filters
- * ================================================================================
- * Copyright (C) 2019 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.mdc.filters;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.util.List;
-import java.util.Properties;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
-import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
-import org.onap.policy.common.endpoints.event.comm.TopicSink;
-import org.onap.policy.common.endpoints.event.comm.TopicSource;
-import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
-import org.onap.policy.drools.system.PolicyController;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.slf4j.MDC;
-
-public class MdcFilterFeatureTest {
-
- /**
- * Logger.
- */
- private static final Logger logger = LoggerFactory.getLogger(MdcFilterFeatureTest.class);
-
- /**
- * Test topic names for mdc topic filters.
- */
- private static final String TEST_TOPIC_A = "org.onap.policy.test-topic-a";
- private static final String TEST_TOPIC_B = "org.onap.policy.test-topic-b";
-
- /**
- * The mock PolicyController to be used for the junits.
- */
- private PolicyController controller;
-
- /**
- * The mock properties to be used for the junits.
- */
- private Properties props;
-
- /**
- * An instance of the MdcFilterFeature.
- */
- private MdcFilterFeature mdcFilterFeature;
-
- /**
- * Sample json string to be logged.
- */
- private String message;
-
- /**
- * Setup.
- * @throws IOException thrown if onset.json file could not be read
- */
- @Before
- public void setUp() throws IOException {
- message = new String(Files.readAllBytes(Paths.get("src/test/resources/onset.json")));
-
- props = mockFeatureProperties();
- controller = mock(PolicyController.class);
-
- props.setProperty("dmaap.source.topics", TEST_TOPIC_A);
- props.setProperty("dmaap.source.topics." + TEST_TOPIC_A + ".servers", "http://testing123.com/");
- props.setProperty("noop.sink.topics", TEST_TOPIC_B);
-
- List<TopicSource> topicSources = TopicEndpointManager.getManager().addTopicSources(props);
- doReturn(topicSources).when(controller).getTopicSources();
-
- List<TopicSink> topicSinks = TopicEndpointManager.getManager().addTopicSinks(props);
- doReturn(topicSinks).when(controller).getTopicSinks();
-
- mdcFilterFeature = new MdcFilterFeatureImpl();
- mdcFilterFeature.afterCreate(controller);
- }
-
- /**
- * Tests extracting fields from a JSON message and place them in the MDC and
- * then clearing the MDC.
- */
- @Test
- public void mdcLogTest() {
- mdcFilterFeature.beforeLog(logger, EventType.IN,
- CommInfrastructure.DMAAP, TEST_TOPIC_A, message);
-
- assertEquals("8c1b8bd8-06f7-493f-8ed7-daaa4cc481bc", MDC.get("requestID"));
- assertEquals("CL-TEST", MDC.get("closedLoopControlName"));
-
- assertNotNull(MDC.getCopyOfContextMap());
-
- mdcFilterFeature.afterLog(logger, EventType.IN,
- CommInfrastructure.DMAAP, TEST_TOPIC_A, message);
-
- assertNull(MDC.getCopyOfContextMap());
- }
-
- /**
- * Tests that the feature does not search for fields in a JSON message
- * if there is not an MdcTopicFilter object for the generated key.
- */
- @Test
- public void noTopicFilterTest() {
- mdcFilterFeature.beforeLog(logger, EventType.OUT,
- CommInfrastructure.NOOP, "no-topic", message);
-
- assertEquals("OUT", MDC.get("networkEventType"));
- assertEquals("NOOP", MDC.get("networkProtocol"));
- assertEquals("no-topic", MDC.get("networkTopic"));
- assertNull(MDC.get("requestID"));
- }
-
- /**
- * Creates a simple properties map containing an mdc filter for a test
- * topic.
- *
- * @return a properties map with mdc filter properties.
- */
- private Properties mockFeatureProperties() {
- Properties props = new Properties();
-
- String key = "dmaap.source.topics." + TEST_TOPIC_A + ".mdcFilters";
- String value = "requestID=$.requestID,closedLoopControlName=$.closedLoopControlName";
- props.setProperty(key, value);
-
- return props;
- }
-
- /**
- * Subclass of MdcFilterFeature for junit usage.
- */
- private class MdcFilterFeatureImpl extends MdcFilterFeature {
-
- public MdcFilterFeatureImpl() {
- super();
- }
-
- @Override
- protected Properties getFeatureProps() {
- return props;
- }
- }
-}
+/* + * ============LICENSE_START======================================================= + * feature-mdc-filters + * ================================================================================ + * Copyright (C) 2019 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.mdc.filters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; +import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.drools.system.PolicyController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +public class MdcFilterFeatureTest { + + /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(MdcFilterFeatureTest.class); + + /** + * Test topic names for mdc topic filters. + */ + private static final String TEST_TOPIC_A = "org.onap.policy.test-topic-a"; + private static final String TEST_TOPIC_B = "org.onap.policy.test-topic-b"; + + /** + * The mock PolicyController to be used for the junits. + */ + private PolicyController controller; + + /** + * The mock properties to be used for the junits. + */ + private Properties props; + + /** + * An instance of the MdcFilterFeature. + */ + private MdcFilterFeature mdcFilterFeature; + + /** + * Sample json string to be logged. + */ + private String message; + + /** + * Setup. + * @throws IOException thrown if onset.json file could not be read + */ + @Before + public void setUp() throws IOException { + message = new String(Files.readAllBytes(Paths.get("src/test/resources/onset.json"))); + + props = mockFeatureProperties(); + controller = mock(PolicyController.class); + + props.setProperty("dmaap.source.topics", TEST_TOPIC_A); + props.setProperty("dmaap.source.topics." + TEST_TOPIC_A + ".servers", "http://testing123.com/"); + props.setProperty("noop.sink.topics", TEST_TOPIC_B); + + List<TopicSource> topicSources = TopicEndpointManager.getManager().addTopicSources(props); + doReturn(topicSources).when(controller).getTopicSources(); + + List<TopicSink> topicSinks = TopicEndpointManager.getManager().addTopicSinks(props); + doReturn(topicSinks).when(controller).getTopicSinks(); + + mdcFilterFeature = new MdcFilterFeatureImpl(); + mdcFilterFeature.afterCreate(controller); + } + + /** + * Tests extracting fields from a JSON message and place them in the MDC and + * then clearing the MDC. + */ + @Test + public void mdcLogTest() { + mdcFilterFeature.beforeLog(logger, EventType.IN, + CommInfrastructure.DMAAP, TEST_TOPIC_A, message); + + assertEquals("8c1b8bd8-06f7-493f-8ed7-daaa4cc481bc", MDC.get("requestID")); + assertEquals("CL-TEST", MDC.get("closedLoopControlName")); + + assertNotNull(MDC.getCopyOfContextMap()); + + mdcFilterFeature.afterLog(logger, EventType.IN, + CommInfrastructure.DMAAP, TEST_TOPIC_A, message); + + assertNull(MDC.getCopyOfContextMap()); + } + + /** + * Tests that the feature does not search for fields in a JSON message + * if there is not an MdcTopicFilter object for the generated key. + */ + @Test + public void noTopicFilterTest() { + mdcFilterFeature.beforeLog(logger, EventType.OUT, + CommInfrastructure.NOOP, "no-topic", message); + + assertEquals("OUT", MDC.get("networkEventType")); + assertEquals("NOOP", MDC.get("networkProtocol")); + assertEquals("no-topic", MDC.get("networkTopic")); + assertNull(MDC.get("requestID")); + } + + /** + * Creates a simple properties map containing an mdc filter for a test + * topic. + * + * @return a properties map with mdc filter properties. + */ + private Properties mockFeatureProperties() { + Properties props = new Properties(); + + String key = "dmaap.source.topics." + TEST_TOPIC_A + ".mdcFilters"; + String value = "requestID=$.requestID,closedLoopControlName=$.closedLoopControlName"; + props.setProperty(key, value); + + return props; + } + + /** + * Subclass of MdcFilterFeature for junit usage. + */ + private class MdcFilterFeatureImpl extends MdcFilterFeature { + + public MdcFilterFeatureImpl() { + super(); + } + + @Override + protected Properties getFeatureProps() { + return props; + } + } +} diff --git a/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java index 568a316b..7fd65a29 100755 --- a/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java +++ b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java @@ -1,661 +1,661 @@ -/*
- * ============LICENSE_START=======================================================
- * feature-mdc-filters
- * ================================================================================
- * Copyright (C) 2019 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.mdc.filters;
-
-import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import org.junit.Test;
-import org.onap.policy.drools.mdc.filters.MdcTopicFilter.FilterRule;
-
-public class MdcTopicFilterTest {
-
- /**
- * Test the simple case of having one filter rule for a key.
- */
- @Test
- public void singleFilterOnePathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.getFilterRule("requestID");
- assertEquals("requestID", rule.getMdcKey());
- assertEquals("[$.requestID]", rule.getPaths().toString());
- }
-
- /**
- * Tests having one filter rule with a set of potential paths to the key.
- */
- @Test
- public void singleFilterMultiPathTest() {
- String topicFilterProp = "requestID=$.requestID|$.request-id";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.getFilterRule("requestID");
- assertEquals("requestID", rule.getMdcKey());
- assertEquals(2, rule.getPaths().size());
- assertEquals("[$.requestID, $.request-id]", rule.getPaths().toString());
- }
-
- /**
- * Tests having two filter rules that each have one key/path pair.
- */
- @Test
- public void multiFilterSinglePathTest() {
- String topicFilterProp = "requestID=$.requestID,closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.getFilterRule("requestID");
- assertEquals("requestID", rule.getMdcKey());
- assertEquals(1, rule.getPaths().size());
- assertEquals("[$.requestID]", rule.getPaths().toString());
-
- FilterRule rule2 = topicFilter.getFilterRule("closedLoopControlName");
- assertEquals("closedLoopControlName", rule2.getMdcKey());
- assertEquals(1, rule2.getPaths().size());
- assertEquals("[$.closedLoopControlName]", rule2.getPaths().toString());
- }
-
- /**
- * Tests having two filter rules that each have two key/path pairs.
- */
- @Test
- public void multiFilterMultiPathTest() {
- String topicFilterProp = "requestID=$.requestID|$.body.request-id,"
- + "closedLoopControlName=$.closedLoopControlName"
- + "|$.body.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.getFilterRule("requestID");
- assertEquals("requestID", rule.getMdcKey());
- assertEquals(2, rule.getPaths().size());
- assertEquals("[$.requestID, $.body.request-id]", rule.getPaths().toString());
-
- FilterRule rule2 = topicFilter.getFilterRule("closedLoopControlName");
- assertEquals("closedLoopControlName", rule2.getMdcKey());
- assertEquals(2, rule2.getPaths().size());
- assertEquals("[$.closedLoopControlName, $.body.closedLoopControlName]", rule2.getPaths().toString());
- }
-
- /**
- * Tests that the regex split logic for '|' in the feature code doesn't
- * break parsing when "||" is used as a predicate in a JsonPath query.
- */
- @Test
- public void addOrPredicateFilterTest() {
- String topicFilterProp = "requestID=$.requestID||$.body.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- assertEquals(1, topicFilter.getFilterRule().size());
- assertEquals("requestID", topicFilter.getFilterRule("requestID").getMdcKey());
- assertEquals(Arrays.asList("$.requestID||$.body.requestID"), topicFilter
- .getFilterRule("requestID").getPaths());
- }
-
- /**
- * Tests getting all filter rules for a given topic.
- */
- @Test
- public void getAllFilterRulesTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID,"
- + "closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- assertEquals(3, topicFilter.getFilterRule().size());
- }
-
- /**
- * Tests getting a filter rule by its key.
- */
- @Test
- public void getFilterRuleTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID,"
- + "closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.getFilterRule("requestID");
- assertNotNull(rule);
- }
-
- /**
- * Tests throwing an exception for passing in a null key.
- */
- @Test(expected = IllegalArgumentException.class)
- public void getFilterRuleNullKeyTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.getFilterRule(null);
- }
-
- /**
- * Tests throwing an exception for passing in an empty key.
- */
- @Test(expected = IllegalArgumentException.class)
- public void getFilterRuleEmptyKeyTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.getFilterRule("");
- }
-
- /**
- * Tests adding a filter rule with a single path.
- */
- @Test
- public void addFilterRuleSinglePathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- String key = "subRequestID";
- String path = "$.subRequestID";
- FilterRule rule = topicFilter.addFilterRule(key, path);
- assertEquals(topicFilter.getFilterRule(key), rule);
- }
-
- /**
- * Tests adding a filter rule with multiple paths.
- */
- @Test
- public void addFilterRuleMultiPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- String key = "subRequestID";
- List<String> paths = Arrays.asList("$.subRequestID", "$.sub-request-id");
- FilterRule rule = topicFilter.addFilterRule(key, paths);
- assertEquals(topicFilter.getFilterRule(key), rule);
- }
-
- /**
- * Tests throwing an exception for passing a null key and a
- * single path.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addFilterRuleNullKeyStringPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule(null, "$.subRequestID");
- }
-
- /**
- * Tests throwing an exception for passing a null key and a list
- * of paths.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addFilterRuleNullKeyPathListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule(null, Arrays.asList("$.subRequestID"));
- }
-
- /**
- * Tests throwing an exception for passing an empty key and
- * a single path.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addFilterRuleEmptyKeyStringPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule("", "$.subRequestID");
- }
-
- /**
- * Tests throwing an exception for passing an empty key and
- * a list of paths.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addFilterRuleEmptyKeyPathListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule("", Arrays.asList("$.subRequestID"));
- }
-
- /**
- * Tests throwing an exception for passing an empty path string.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addFilterRuleEmptyPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule("subRequestID", "");
- }
-
- /**
- * Tests throwing an exception for passing an empty paths list.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addFilterRuleEmptyPathsTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule("subRequestID", Arrays.asList());
- }
-
- /**
- * Tests throwing an exception for trying to add a filter with a key that
- * already exists with a single filter.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addExistingFilterRuleStringTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule("requestID", "$.test");
- }
-
- /**
- * Tests throwing an exception for trying to add a filter with a key that
- * already exists with a list of filters.
- */
- @Test(expected = IllegalArgumentException.class)
- public void addExistingFilterRuleListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.addFilterRule("requestID", Arrays.asList("$.test"));
- }
-
- @Test
- public void createFilterRuleExceptionTest() {
- assertThatIllegalArgumentException().isThrownBy(() -> new MdcTopicFilter("invalid filter"))
- .withMessage("could not parse filter rule");
- }
-
- /**
- * Tests modifying a filter rule to add a new path.
- */
- @Test
- public void modifyFilterRuleSinglePathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.modifyFilterRule("requestID", "$.request-id");
- assertEquals(topicFilter.getFilterRule("requestID"), rule);
- assertEquals(Arrays.asList("$.requestID", "$.request-id"), rule.getPaths());
- }
-
- /**
- * Tests modifying a filter rule to add a list of new paths.
- */
- @Test
- public void modifyFilterRuleMultiPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.modifyFilterRule("requestID",
- Arrays.asList("$.request-id", "$.requestId"));
- assertEquals(topicFilter.getFilterRule("requestID"), rule);
- assertEquals(
- Arrays.asList("$.requestID", "$.request-id", "$.requestId"),
- rule.getPaths());
- }
-
- @Test
- public void modifyFilterRuleMultiPathExceptionTest() {
- MdcTopicFilter filter = new MdcTopicFilter("abc=$a.value");
- assertThatIllegalArgumentException()
- .isThrownBy(() -> filter.modifyFilterRule("def", "abc", Arrays.asList("$.b", "$.c")))
- .withMessage("a filter rule already exists for key: abc");
- }
-
- /**
- * Tests modifying a filter rule key.
- */
- @Test
- public void modifyFilterRuleKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- FilterRule rule = topicFilter.modifyFilterRule("requestID",
- "request-id", Arrays.asList("$.request-id"));
- assertEquals(topicFilter.getFilterRule("request-id"), rule);
- assertEquals("[$.request-id]", rule.getPaths().toString());
- }
-
- /**
- * Tests throwing an exception when passing a null key and
- * a single path.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleNullKeyStringPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule(null, "$.request-id");
- }
-
- /**
- * Tests throwing an exception when passing a null key and
- * a list of multiple paths.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleNullKeyPathListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule(null, Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when passing an empty key and
- * a single path.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyKeyStringPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("", "$.request-id");
- }
-
- /**
- * Tests throwing an exception when passing an empty key and
- * a list of multiple paths.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyKeyPathListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("", Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when passing an empty string path.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyPathStringTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("requestID", "");
- }
-
- /**
- * Tests throwing an exception when passing an empty list of paths.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyPathListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("requestID", Arrays.asList());
- }
-
- /**
- * Tests throwing an exception when passing a key that is
- * not in the filter rules map and a string path.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleMissingKeyStringPathTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("request-id", "$.request-id");
- }
-
- /**
- * Tests throwing an exception when passing a key that is
- * not in the filter rules map and a list of paths.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleMissingKeyPathListTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("request-id", Arrays.asList("$.request-id"));
- }
-
-
- /**
- * Tests throwing an exception when passing a null oldKey.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleNullOldKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule(null, "request-id", Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when passing an empty oldKey.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyOldKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("", "request-id", Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when passing a null newKey.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleNullNewKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("requestID", null, Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when passing an empty newKey.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyNewKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("requestID", "", Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when the old and new key are the same.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleSameKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("requestID", "requestID",
- Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests throwing an exception when passing an empty paths list.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleEmptyPathsTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("requestID", "request-id", Arrays.asList());
- }
-
- /**
- * Tests throwing an exception when the old key doesn't exist
- * in the rules map.
- */
- @Test(expected = IllegalArgumentException.class)
- public void modifyFilterRuleNonExistingOldKeyTest() {
- String topicFilterProp = "requestID=$.requestID";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.modifyFilterRule("request-id", "id", Arrays.asList("$.request-id"));
- }
-
- /**
- * Tests deleting all filter rules in the rules map.
- */
- @Test
- public void deleteAllFilterRulesTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID,"
- + "closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- assertEquals(3, topicFilter.getFilterRule().size());
- topicFilter.deleteFilterRule();
- assertEquals(0, topicFilter.getFilterRule().size());
- }
-
- /**
- * Tests deleting a single filter rule by its key from the rules map.
- */
- @Test
- public void deleteFilterRuleTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID,"
- + "closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- assertEquals(3, topicFilter.getFilterRule().size());
- topicFilter.deleteFilterRule("closedLoopControlName");
- assertEquals(2, topicFilter.getFilterRule().size());
- }
-
- /**
- * Tests throwing an exception if the key is null.
- */
- @Test(expected = IllegalArgumentException.class)
- public void deleteFilterRuleNullKeyTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID,"
- + "closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.deleteFilterRule(null);
- }
-
- /**
- * Tests throwing an exception if the key is empty.
- */
- @Test(expected = IllegalArgumentException.class)
- public void deleteFilterRuleEmptyKeyTest() {
- String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID,"
- + "closedLoopControlName=$.closedLoopControlName";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
- topicFilter.deleteFilterRule("");
- }
-
- /**
- * Tests finding all results for each filter rule corresponding to a topic.
- */
- @Test
- public void findAllTest() {
- String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","
- + "\"controllers\":[{\"name\":\"test-controller\","
- + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
- + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
-
- String topicFilterProp = "requestID=$.requestID,controllerName=$.controllers[0].name,"
- + "operation=$.controllers[0].operation";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- Map<String, List<String>> results = topicFilter.find(message);
- assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1",
- results.get("requestID").get(0));
- assertEquals("test-controller", results.get("controllerName").get(0));
- assertEquals("update", results.get("operation").get(0));
- }
-
- @Test
- public void findAllNotFoundTest() {
- String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","
- + "\"controllers\":[{\"name\":\"test-controller\","
- + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
- + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
-
- String topicFilterProp = "requestID=$.requestID[3]";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- assertTrue(topicFilter.find(message).get("requestID").isEmpty());
- }
-
- /**
- * Tests finding field matches for a filter rule corresponding to a topic.
- */
- @Test
- public void findTest() {
- String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","
- + "\"controllers\":[{\"name\":\"test-controller\","
- + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
- + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
-
- String topicFilterProp = "requestID=$.requestID,controllerName=$.controllers[0].name,"
- + "operation=$.controllers[0].operation";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- List<String> results = topicFilter.find(message, "requestID");
- assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", results.get(0));
- }
-
- @Test
- public void findNotFoundTest() {
- String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","
- + "\"controllers\":[{\"name\":\"test-controller\","
- + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
- + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
-
- String topicFilterProp = "requestID=$.requestID[3]";
- MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
-
- assertTrue(topicFilter.find(message, "requestID").isEmpty());
- }
-
- @Test
- public void testFilterRuleStringString() {
- FilterRule rule = new FilterRule("hello", "world");
-
- assertEquals("hello", rule.getMdcKey());
- assertEquals("[world]", rule.getPaths().toString());
- }
-
- @Test
- public void testFilterRuleMdcKey() {
- FilterRule rule = new FilterRule("abc", "def");
-
- // check error cases first
- assertThatIllegalArgumentException().isThrownBy(() -> rule.setMdcKey(null))
- .withMessage(MdcTopicFilter.MDC_KEY_ERROR);
- assertThatIllegalArgumentException().isThrownBy(() -> rule.setMdcKey(""))
- .withMessage(MdcTopicFilter.MDC_KEY_ERROR);
-
- // success cases
- rule.setMdcKey("my-mdc-key");
- assertEquals("my-mdc-key", rule.getMdcKey());
- }
-
- @Test
- public void testFilterRulePaths() {
- FilterRule rule = new FilterRule("abc", "def");
-
- // check error cases first
- assertThatIllegalArgumentException().isThrownBy(() -> rule.setPaths(null))
- .withMessage(MdcTopicFilter.JSON_PATH_ERROR);
- assertThatIllegalArgumentException().isThrownBy(() -> rule.setPaths(Collections.emptyList()))
- .withMessage(MdcTopicFilter.JSON_PATH_ERROR);
-
- assertThatIllegalArgumentException().isThrownBy(() -> rule.addPaths(null))
- .withMessage(MdcTopicFilter.JSON_PATH_ERROR);
- assertThatIllegalArgumentException().isThrownBy(() -> rule.addPaths(Collections.emptyList()))
- .withMessage(MdcTopicFilter.JSON_PATH_ERROR);
-
- assertThatIllegalArgumentException().isThrownBy(() -> rule.addPath(null))
- .withMessage(MdcTopicFilter.JSON_PATH_ERROR);
- assertThatIllegalArgumentException().isThrownBy(() -> rule.addPath(""))
- .withMessage(MdcTopicFilter.JSON_PATH_ERROR);
-
- // success cases
- rule.setPaths(new ArrayList<>(Arrays.asList("pathA", "pathB")));
- assertEquals("[pathA, pathB]", rule.getPaths().toString());
-
- rule.addPath("pathC");
- assertEquals("[pathA, pathB, pathC]", rule.getPaths().toString());
-
- rule.addPaths(Arrays.asList("pathD", "pathE"));
- assertEquals("[pathA, pathB, pathC, pathD, pathE]", rule.getPaths().toString());
- }
-}
+/* + * ============LICENSE_START======================================================= + * feature-mdc-filters + * ================================================================================ + * Copyright (C) 2019 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.mdc.filters; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.onap.policy.drools.mdc.filters.MdcTopicFilter.FilterRule; + +public class MdcTopicFilterTest { + + /** + * Test the simple case of having one filter rule for a key. + */ + @Test + public void singleFilterOnePathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals("[$.requestID]", rule.getPaths().toString()); + } + + /** + * Tests having one filter rule with a set of potential paths to the key. + */ + @Test + public void singleFilterMultiPathTest() { + String topicFilterProp = "requestID=$.requestID|$.request-id"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals(2, rule.getPaths().size()); + assertEquals("[$.requestID, $.request-id]", rule.getPaths().toString()); + } + + /** + * Tests having two filter rules that each have one key/path pair. + */ + @Test + public void multiFilterSinglePathTest() { + String topicFilterProp = "requestID=$.requestID,closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals(1, rule.getPaths().size()); + assertEquals("[$.requestID]", rule.getPaths().toString()); + + FilterRule rule2 = topicFilter.getFilterRule("closedLoopControlName"); + assertEquals("closedLoopControlName", rule2.getMdcKey()); + assertEquals(1, rule2.getPaths().size()); + assertEquals("[$.closedLoopControlName]", rule2.getPaths().toString()); + } + + /** + * Tests having two filter rules that each have two key/path pairs. + */ + @Test + public void multiFilterMultiPathTest() { + String topicFilterProp = "requestID=$.requestID|$.body.request-id," + + "closedLoopControlName=$.closedLoopControlName" + + "|$.body.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals(2, rule.getPaths().size()); + assertEquals("[$.requestID, $.body.request-id]", rule.getPaths().toString()); + + FilterRule rule2 = topicFilter.getFilterRule("closedLoopControlName"); + assertEquals("closedLoopControlName", rule2.getMdcKey()); + assertEquals(2, rule2.getPaths().size()); + assertEquals("[$.closedLoopControlName, $.body.closedLoopControlName]", rule2.getPaths().toString()); + } + + /** + * Tests that the regex split logic for '|' in the feature code doesn't + * break parsing when "||" is used as a predicate in a JsonPath query. + */ + @Test + public void addOrPredicateFilterTest() { + String topicFilterProp = "requestID=$.requestID||$.body.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + assertEquals(1, topicFilter.getFilterRule().size()); + assertEquals("requestID", topicFilter.getFilterRule("requestID").getMdcKey()); + assertEquals(Arrays.asList("$.requestID||$.body.requestID"), topicFilter + .getFilterRule("requestID").getPaths()); + } + + /** + * Tests getting all filter rules for a given topic. + */ + @Test + public void getAllFilterRulesTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + assertEquals(3, topicFilter.getFilterRule().size()); + } + + /** + * Tests getting a filter rule by its key. + */ + @Test + public void getFilterRuleTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertNotNull(rule); + } + + /** + * Tests throwing an exception for passing in a null key. + */ + @Test(expected = IllegalArgumentException.class) + public void getFilterRuleNullKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.getFilterRule(null); + } + + /** + * Tests throwing an exception for passing in an empty key. + */ + @Test(expected = IllegalArgumentException.class) + public void getFilterRuleEmptyKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.getFilterRule(""); + } + + /** + * Tests adding a filter rule with a single path. + */ + @Test + public void addFilterRuleSinglePathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + String key = "subRequestID"; + String path = "$.subRequestID"; + FilterRule rule = topicFilter.addFilterRule(key, path); + assertEquals(topicFilter.getFilterRule(key), rule); + } + + /** + * Tests adding a filter rule with multiple paths. + */ + @Test + public void addFilterRuleMultiPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + String key = "subRequestID"; + List<String> paths = Arrays.asList("$.subRequestID", "$.sub-request-id"); + FilterRule rule = topicFilter.addFilterRule(key, paths); + assertEquals(topicFilter.getFilterRule(key), rule); + } + + /** + * Tests throwing an exception for passing a null key and a + * single path. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleNullKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule(null, "$.subRequestID"); + } + + /** + * Tests throwing an exception for passing a null key and a list + * of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleNullKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule(null, Arrays.asList("$.subRequestID")); + } + + /** + * Tests throwing an exception for passing an empty key and + * a single path. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("", "$.subRequestID"); + } + + /** + * Tests throwing an exception for passing an empty key and + * a list of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("", Arrays.asList("$.subRequestID")); + } + + /** + * Tests throwing an exception for passing an empty path string. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("subRequestID", ""); + } + + /** + * Tests throwing an exception for passing an empty paths list. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyPathsTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("subRequestID", Arrays.asList()); + } + + /** + * Tests throwing an exception for trying to add a filter with a key that + * already exists with a single filter. + */ + @Test(expected = IllegalArgumentException.class) + public void addExistingFilterRuleStringTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("requestID", "$.test"); + } + + /** + * Tests throwing an exception for trying to add a filter with a key that + * already exists with a list of filters. + */ + @Test(expected = IllegalArgumentException.class) + public void addExistingFilterRuleListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("requestID", Arrays.asList("$.test")); + } + + @Test + public void createFilterRuleExceptionTest() { + assertThatIllegalArgumentException().isThrownBy(() -> new MdcTopicFilter("invalid filter")) + .withMessage("could not parse filter rule"); + } + + /** + * Tests modifying a filter rule to add a new path. + */ + @Test + public void modifyFilterRuleSinglePathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.modifyFilterRule("requestID", "$.request-id"); + assertEquals(topicFilter.getFilterRule("requestID"), rule); + assertEquals(Arrays.asList("$.requestID", "$.request-id"), rule.getPaths()); + } + + /** + * Tests modifying a filter rule to add a list of new paths. + */ + @Test + public void modifyFilterRuleMultiPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.modifyFilterRule("requestID", + Arrays.asList("$.request-id", "$.requestId")); + assertEquals(topicFilter.getFilterRule("requestID"), rule); + assertEquals( + Arrays.asList("$.requestID", "$.request-id", "$.requestId"), + rule.getPaths()); + } + + @Test + public void modifyFilterRuleMultiPathExceptionTest() { + MdcTopicFilter filter = new MdcTopicFilter("abc=$a.value"); + assertThatIllegalArgumentException() + .isThrownBy(() -> filter.modifyFilterRule("def", "abc", Arrays.asList("$.b", "$.c"))) + .withMessage("a filter rule already exists for key: abc"); + } + + /** + * Tests modifying a filter rule key. + */ + @Test + public void modifyFilterRuleKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.modifyFilterRule("requestID", + "request-id", Arrays.asList("$.request-id")); + assertEquals(topicFilter.getFilterRule("request-id"), rule); + assertEquals("[$.request-id]", rule.getPaths().toString()); + } + + /** + * Tests throwing an exception when passing a null key and + * a single path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule(null, "$.request-id"); + } + + /** + * Tests throwing an exception when passing a null key and + * a list of multiple paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule(null, Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty key and + * a single path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("", "$.request-id"); + } + + /** + * Tests throwing an exception when passing an empty key and + * a list of multiple paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty string path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyPathStringTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", ""); + } + + /** + * Tests throwing an exception when passing an empty list of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", Arrays.asList()); + } + + /** + * Tests throwing an exception when passing a key that is + * not in the filter rules map and a string path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleMissingKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("request-id", "$.request-id"); + } + + /** + * Tests throwing an exception when passing a key that is + * not in the filter rules map and a list of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleMissingKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("request-id", Arrays.asList("$.request-id")); + } + + + /** + * Tests throwing an exception when passing a null oldKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullOldKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule(null, "request-id", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty oldKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyOldKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("", "request-id", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing a null newKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullNewKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", null, Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty newKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyNewKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", "", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when the old and new key are the same. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleSameKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", "requestID", + Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty paths list. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyPathsTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", "request-id", Arrays.asList()); + } + + /** + * Tests throwing an exception when the old key doesn't exist + * in the rules map. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNonExistingOldKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("request-id", "id", Arrays.asList("$.request-id")); + } + + /** + * Tests deleting all filter rules in the rules map. + */ + @Test + public void deleteAllFilterRulesTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + assertEquals(3, topicFilter.getFilterRule().size()); + topicFilter.deleteFilterRule(); + assertEquals(0, topicFilter.getFilterRule().size()); + } + + /** + * Tests deleting a single filter rule by its key from the rules map. + */ + @Test + public void deleteFilterRuleTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + assertEquals(3, topicFilter.getFilterRule().size()); + topicFilter.deleteFilterRule("closedLoopControlName"); + assertEquals(2, topicFilter.getFilterRule().size()); + } + + /** + * Tests throwing an exception if the key is null. + */ + @Test(expected = IllegalArgumentException.class) + public void deleteFilterRuleNullKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.deleteFilterRule(null); + } + + /** + * Tests throwing an exception if the key is empty. + */ + @Test(expected = IllegalArgumentException.class) + public void deleteFilterRuleEmptyKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.deleteFilterRule(""); + } + + /** + * Tests finding all results for each filter rule corresponding to a topic. + */ + @Test + public void findAllTest() { + String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\"," + + "\"controllers\":[{\"name\":\"test-controller\"," + + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\"," + + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}"; + + String topicFilterProp = "requestID=$.requestID,controllerName=$.controllers[0].name," + + "operation=$.controllers[0].operation"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + Map<String, List<String>> results = topicFilter.find(message); + assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", + results.get("requestID").get(0)); + assertEquals("test-controller", results.get("controllerName").get(0)); + assertEquals("update", results.get("operation").get(0)); + } + + @Test + public void findAllNotFoundTest() { + String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\"," + + "\"controllers\":[{\"name\":\"test-controller\"," + + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\"," + + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}"; + + String topicFilterProp = "requestID=$.requestID[3]"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + assertTrue(topicFilter.find(message).get("requestID").isEmpty()); + } + + /** + * Tests finding field matches for a filter rule corresponding to a topic. + */ + @Test + public void findTest() { + String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\"," + + "\"controllers\":[{\"name\":\"test-controller\"," + + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\"," + + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}"; + + String topicFilterProp = "requestID=$.requestID,controllerName=$.controllers[0].name," + + "operation=$.controllers[0].operation"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + List<String> results = topicFilter.find(message, "requestID"); + assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", results.get(0)); + } + + @Test + public void findNotFoundTest() { + String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\"," + + "\"controllers\":[{\"name\":\"test-controller\"," + + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\"," + + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}"; + + String topicFilterProp = "requestID=$.requestID[3]"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + assertTrue(topicFilter.find(message, "requestID").isEmpty()); + } + + @Test + public void testFilterRuleStringString() { + FilterRule rule = new FilterRule("hello", "world"); + + assertEquals("hello", rule.getMdcKey()); + assertEquals("[world]", rule.getPaths().toString()); + } + + @Test + public void testFilterRuleMdcKey() { + FilterRule rule = new FilterRule("abc", "def"); + + // check error cases first + assertThatIllegalArgumentException().isThrownBy(() -> rule.setMdcKey(null)) + .withMessage(MdcTopicFilter.MDC_KEY_ERROR); + assertThatIllegalArgumentException().isThrownBy(() -> rule.setMdcKey("")) + .withMessage(MdcTopicFilter.MDC_KEY_ERROR); + + // success cases + rule.setMdcKey("my-mdc-key"); + assertEquals("my-mdc-key", rule.getMdcKey()); + } + + @Test + public void testFilterRulePaths() { + FilterRule rule = new FilterRule("abc", "def"); + + // check error cases first + assertThatIllegalArgumentException().isThrownBy(() -> rule.setPaths(null)) + .withMessage(MdcTopicFilter.JSON_PATH_ERROR); + assertThatIllegalArgumentException().isThrownBy(() -> rule.setPaths(Collections.emptyList())) + .withMessage(MdcTopicFilter.JSON_PATH_ERROR); + + assertThatIllegalArgumentException().isThrownBy(() -> rule.addPaths(null)) + .withMessage(MdcTopicFilter.JSON_PATH_ERROR); + assertThatIllegalArgumentException().isThrownBy(() -> rule.addPaths(Collections.emptyList())) + .withMessage(MdcTopicFilter.JSON_PATH_ERROR); + + assertThatIllegalArgumentException().isThrownBy(() -> rule.addPath(null)) + .withMessage(MdcTopicFilter.JSON_PATH_ERROR); + assertThatIllegalArgumentException().isThrownBy(() -> rule.addPath("")) + .withMessage(MdcTopicFilter.JSON_PATH_ERROR); + + // success cases + rule.setPaths(new ArrayList<>(Arrays.asList("pathA", "pathB"))); + assertEquals("[pathA, pathB]", rule.getPaths().toString()); + + rule.addPath("pathC"); + assertEquals("[pathA, pathB, pathC]", rule.getPaths().toString()); + + rule.addPaths(Arrays.asList("pathD", "pathE")); + assertEquals("[pathA, pathB, pathC, pathD, pathE]", rule.getPaths().toString()); + } +} |