\r
package org.onap.policy.drools.mdc.filters;\r
\r
+import com.att.aft.dme2.internal.apache.commons.lang3.StringUtils;\r
import java.util.ArrayList;\r
import java.util.Arrays;\r
import java.util.HashMap;\r
import java.util.List;\r
import java.util.Map;\r
+import lombok.Getter;\r
import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;\r
import org.slf4j.Logger;\r
import org.slf4j.LoggerFactory;\r
\r
private Map<String, FilterRule> rules = new HashMap<>();\r
\r
+ @Getter\r
public static class FilterRule {\r
private String mdcKey;\r
private List<String> paths;\r
this.paths = paths;\r
}\r
\r
- public String getMdcKey() {\r
- return mdcKey;\r
- }\r
-\r
- public List<String> getPaths() {\r
- return paths;\r
- }\r
-\r
protected void setMdcKey(String mdcKey) {\r
- if (mdcKey == null || mdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(mdcKey)) {\r
throw new IllegalArgumentException(MDC_KEY_ERROR);\r
}\r
this.mdcKey = mdcKey;\r
}\r
\r
protected void setPaths(List<String> paths) {\r
- if (paths == null || paths.isEmpty()) {\r
+ if (nullOrEmpty(paths)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
this.paths = paths;\r
}\r
\r
protected void addPaths(List<String> paths) {\r
- if (paths == null || paths.isEmpty()) {\r
+ if (nullOrEmpty(paths)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
this.paths.addAll(paths);\r
}\r
\r
protected void addPath(String path) {\r
- if (path == null || path.isEmpty()) {\r
+ if (StringUtils.isBlank(path)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
this.paths.add(path);\r
* @return the filter rule associated with the key\r
*/\r
protected FilterRule getFilterRule(String mdcKey) {\r
- if (mdcKey == null || mdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(mdcKey)) {\r
throw new IllegalArgumentException(MDC_KEY_ERROR);\r
}\r
return rules.get(mdcKey);\r
* @return the filter rule that was added for the topic\r
*/\r
protected FilterRule addFilterRule(String mdcKey, String path) {\r
- if (path == null || path.isEmpty()) {\r
+ if (StringUtils.isBlank(path)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
return addFilterRule(mdcKey, Arrays.asList(path));\r
* @return the filter rule that was added for the topic\r
*/\r
protected FilterRule addFilterRule(String mdcKey, List<String> paths) {\r
- if (mdcKey == null || mdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(mdcKey)) {\r
throw new IllegalArgumentException(MDC_KEY_ERROR);\r
}\r
\r
- if (paths == null || paths.isEmpty()) {\r
+ if (nullOrEmpty(paths)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
\r
return rule;\r
}\r
\r
+ private static boolean nullOrEmpty(List<String> paths) {\r
+ return paths == null || paths.isEmpty();\r
+ }\r
+\r
/**\r
* Modifies an existing filter rule by adding the specified path.\r
*\r
* @return the filter rule that was modified\r
*/\r
protected FilterRule modifyFilterRule(String mdcKey, String path) {\r
- if (path == null || path.isEmpty()) {\r
+ if (StringUtils.isBlank(path)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
return modifyFilterRule(mdcKey, Arrays.asList(path));\r
* @return the filter rule that was modified\r
*/\r
protected FilterRule modifyFilterRule(String mdcKey, List<String> paths) {\r
- if (mdcKey == null || mdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(mdcKey)) {\r
throw new IllegalArgumentException(MDC_KEY_ERROR);\r
}\r
\r
- if (paths == null || paths.isEmpty()) {\r
+ if (nullOrEmpty(paths)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
\r
* @return the filter rule that was modified\r
*/\r
protected FilterRule modifyFilterRule(String oldMdcKey, String newMdcKey, List<String> paths) {\r
- if (oldMdcKey == null || oldMdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(oldMdcKey)) {\r
throw new IllegalArgumentException("current mdcKey must be provided");\r
}\r
\r
- if (newMdcKey == null || newMdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(newMdcKey)) {\r
throw new IllegalArgumentException("new mdcKey must be provided");\r
}\r
\r
if (oldMdcKey.equals(newMdcKey)) {\r
throw new IllegalArgumentException("the old and new mdcKey are equivalent");\r
}\r
- if (paths == null || paths.isEmpty()) {\r
+ if (nullOrEmpty(paths)) {\r
throw new IllegalArgumentException(JSON_PATH_ERROR);\r
}\r
\r
* @return the filter rule that was deleted\r
*/\r
protected FilterRule deleteFilterRule(String mdcKey) {\r
- if (mdcKey == null || mdcKey.isEmpty()) {\r
+ if (StringUtils.isBlank(mdcKey)) {\r
throw new IllegalArgumentException(MDC_KEY_ERROR);\r
}\r
return rules.remove(mdcKey);\r
* Licensed under the Apache License, Version 2.0 (the "License");\r
* you may not use this file except in compliance with the License.\r
* You may obtain a copy of the License at\r
- * \r
+ *\r
* http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
+ *\r
* Unless required by applicable law or agreed to in writing, software\r
* distributed under the License is distributed on an "AS IS" BASIS,\r
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
\r
package org.onap.policy.drools.mdc.filters;\r
\r
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;\r
import static org.junit.Assert.assertEquals;\r
import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertTrue;\r
\r
+import java.util.ArrayList;\r
import java.util.Arrays;\r
+import java.util.Collections;\r
import java.util.List;\r
import java.util.Map;\r
-\r
import org.junit.Test;\r
import org.onap.policy.drools.mdc.filters.MdcTopicFilter.FilterRule;\r
\r
*/\r
@Test\r
public void multiFilterMultiPathTest() {\r
- String topicFilterProp = "requestID=$.requestID|$.body.request-id," \r
+ String topicFilterProp = "requestID=$.requestID|$.body.request-id,"\r
+ "closedLoopControlName=$.closedLoopControlName"\r
+ "|$.body.closedLoopControlName";\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.addFilterRule(null, "$.subRequestID");\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception for passing a null key and a list\r
* of paths.\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.addFilterRule("", "$.subRequestID");\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception for passing an empty key and\r
* a list of paths.\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.addFilterRule("requestID", "$.test");\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception for trying to add a filter with a key that\r
* already exists with a list of filters.\r
topicFilter.addFilterRule("requestID", Arrays.asList("$.test"));\r
}\r
\r
+ @Test\r
+ public void createFilterRuleExceptionTest() {\r
+ assertThatIllegalArgumentException().isThrownBy(() -> new MdcTopicFilter("invalid filter"))\r
+ .withMessage("could not parse filter rule");\r
+ }\r
+\r
/**\r
* Tests modifying a filter rule to add a new path.\r
*/\r
rule.getPaths());\r
}\r
\r
+ @Test\r
+ public void modifyFilterRuleMultiPathExceptionTest() {\r
+ MdcTopicFilter filter = new MdcTopicFilter("abc=$a.value");\r
+ assertThatIllegalArgumentException()\r
+ .isThrownBy(() -> filter.modifyFilterRule("def", "abc", Arrays.asList("$.b", "$.c")))\r
+ .withMessage("a filter rule already exists for key: abc");\r
+ }\r
+\r
/**\r
* Tests modifying a filter rule key.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule(null, "$.request-id");\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing a null key and\r
* a list of multiple paths.\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule(null, Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty key and\r
* a single path.\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("", "$.request-id");\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty key and\r
* a list of multiple paths.\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("", Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty string path.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("requestID", "");\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty list of paths.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("requestID", Arrays.asList());\r
}\r
- \r
+\r
/**\r
- * Tests throwing an exception when passing a key that is \r
+ * Tests throwing an exception when passing a key that is\r
* not in the filter rules map and a string path.\r
*/\r
@Test(expected = IllegalArgumentException.class)\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("request-id", "$.request-id");\r
}\r
- \r
+\r
/**\r
- * Tests throwing an exception when passing a key that is \r
+ * Tests throwing an exception when passing a key that is\r
* not in the filter rules map and a list of paths.\r
*/\r
@Test(expected = IllegalArgumentException.class)\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("request-id", Arrays.asList("$.request-id"));\r
}\r
- \r
- \r
+\r
+\r
/**\r
* Tests throwing an exception when passing a null oldKey.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule(null, "request-id", Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty oldKey.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("", "request-id", Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing a null newKey.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("requestID", null, Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty newKey.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("requestID", "", Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when the old and new key are the same.\r
*/\r
topicFilter.modifyFilterRule("requestID", "requestID",\r
Arrays.asList("$.request-id"));\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when passing an empty paths list.\r
*/\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.modifyFilterRule("requestID", "request-id", Arrays.asList());\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception when the old key doesn't exist\r
* in the rules map.\r
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
topicFilter.deleteFilterRule(null);\r
}\r
- \r
+\r
/**\r
* Tests throwing an exception if the key is empty.\r
*/\r
assertEquals("update", results.get("operation").get(0));\r
}\r
\r
+ @Test\r
+ public void findAllNotFoundTest() {\r
+ String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","\r
+ + "\"controllers\":[{\"name\":\"test-controller\","\r
+ + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","\r
+ + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";\r
+\r
+ String topicFilterProp = "requestID=$.requestID[3]";\r
+ MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
+\r
+ assertTrue(topicFilter.find(message).get("requestID").isEmpty());\r
+ }\r
+\r
/**\r
* Tests finding field matches for a filter rule corresponding to a topic.\r
*/\r
List<String> results = topicFilter.find(message, "requestID");\r
assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", results.get(0));\r
}\r
+\r
+ @Test\r
+ public void findNotFoundTest() {\r
+ String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","\r
+ + "\"controllers\":[{\"name\":\"test-controller\","\r
+ + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","\r
+ + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";\r
+\r
+ String topicFilterProp = "requestID=$.requestID[3]";\r
+ MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);\r
+\r
+ assertTrue(topicFilter.find(message, "requestID").isEmpty());\r
+ }\r
+\r
+ @Test\r
+ public void testFilterRuleStringString() {\r
+ FilterRule rule = new FilterRule("hello", "world");\r
+\r
+ assertEquals("hello", rule.getMdcKey());\r
+ assertEquals("[world]", rule.getPaths().toString());\r
+ }\r
+\r
+ @Test\r
+ public void testFilterRuleMdcKey() {\r
+ FilterRule rule = new FilterRule("abc", "def");\r
+\r
+ // check error cases first\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.setMdcKey(null))\r
+ .withMessage(MdcTopicFilter.MDC_KEY_ERROR);\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.setMdcKey(""))\r
+ .withMessage(MdcTopicFilter.MDC_KEY_ERROR);\r
+\r
+ // success cases\r
+ rule.setMdcKey("my-mdc-key");\r
+ assertEquals("my-mdc-key", rule.getMdcKey());\r
+ }\r
+\r
+ @Test\r
+ public void testFilterRulePaths() {\r
+ FilterRule rule = new FilterRule("abc", "def");\r
+\r
+ // check error cases first\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.setPaths(null))\r
+ .withMessage(MdcTopicFilter.JSON_PATH_ERROR);\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.setPaths(Collections.emptyList()))\r
+ .withMessage(MdcTopicFilter.JSON_PATH_ERROR);\r
+\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.addPaths(null))\r
+ .withMessage(MdcTopicFilter.JSON_PATH_ERROR);\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.addPaths(Collections.emptyList()))\r
+ .withMessage(MdcTopicFilter.JSON_PATH_ERROR);\r
+\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.addPath(null))\r
+ .withMessage(MdcTopicFilter.JSON_PATH_ERROR);\r
+ assertThatIllegalArgumentException().isThrownBy(() -> rule.addPath(""))\r
+ .withMessage(MdcTopicFilter.JSON_PATH_ERROR);\r
+\r
+ // success cases\r
+ rule.setPaths(new ArrayList<>(Arrays.asList("pathA", "pathB")));\r
+ assertEquals("[pathA, pathB]", rule.getPaths().toString());\r
+\r
+ rule.addPath("pathC");\r
+ assertEquals("[pathA, pathB, pathC]", rule.getPaths().toString());\r
+\r
+ rule.addPaths(Arrays.asList("pathD", "pathE"));\r
+ assertEquals("[pathA, pathB, pathC, pathD, pathE]", rule.getPaths().toString());\r
+ }\r
}\r