diff options
3 files changed, 157 insertions, 55 deletions
diff --git a/feature-mdc-filters/pom.xml b/feature-mdc-filters/pom.xml index 8d6af4bf..c3cd4eda 100755 --- a/feature-mdc-filters/pom.xml +++ b/feature-mdc-filters/pom.xml @@ -7,9 +7,9 @@ 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.
@@ -87,33 +87,39 @@ </build>
<dependencies>
-
+
<dependency>
<groupId>org.onap.policy.common</groupId>
<artifactId>policy-endpoints</artifactId>
<version>${policy.common.version}</version>
<scope>provided</scope>
</dependency>
-
+
<dependency>
<groupId>org.onap.policy.drools-pdp</groupId>
<artifactId>policy-management</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
-
+
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>
-
+
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
-
+
</dependencies>
</project>
diff --git a/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java b/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java index d0813a93..7aa0a920 100755 --- a/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java +++ b/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java @@ -20,11 +20,13 @@ package org.onap.policy.drools.mdc.filters;
+import com.att.aft.dme2.internal.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import lombok.Getter;
import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,6 +40,7 @@ public class MdcTopicFilter { private Map<String, FilterRule> rules = new HashMap<>();
+ @Getter
public static class FilterRule {
private String mdcKey;
private List<String> paths;
@@ -58,37 +61,29 @@ public class MdcTopicFilter { this.paths = paths;
}
- public String getMdcKey() {
- return mdcKey;
- }
-
- public List<String> getPaths() {
- return paths;
- }
-
protected void setMdcKey(String mdcKey) {
- if (mdcKey == null || mdcKey.isEmpty()) {
+ if (StringUtils.isBlank(mdcKey)) {
throw new IllegalArgumentException(MDC_KEY_ERROR);
}
this.mdcKey = mdcKey;
}
protected void setPaths(List<String> paths) {
- if (paths == null || paths.isEmpty()) {
+ if (nullOrEmpty(paths)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
this.paths = paths;
}
protected void addPaths(List<String> paths) {
- if (paths == null || paths.isEmpty()) {
+ if (nullOrEmpty(paths)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
this.paths.addAll(paths);
}
protected void addPath(String path) {
- if (path == null || path.isEmpty()) {
+ if (StringUtils.isBlank(path)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
this.paths.add(path);
@@ -130,7 +125,7 @@ public class MdcTopicFilter { * @return the filter rule associated with the key
*/
protected FilterRule getFilterRule(String mdcKey) {
- if (mdcKey == null || mdcKey.isEmpty()) {
+ if (StringUtils.isBlank(mdcKey)) {
throw new IllegalArgumentException(MDC_KEY_ERROR);
}
return rules.get(mdcKey);
@@ -144,7 +139,7 @@ public class MdcTopicFilter { * @return the filter rule that was added for the topic
*/
protected FilterRule addFilterRule(String mdcKey, String path) {
- if (path == null || path.isEmpty()) {
+ if (StringUtils.isBlank(path)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
return addFilterRule(mdcKey, Arrays.asList(path));
@@ -158,11 +153,11 @@ public class MdcTopicFilter { * @return the filter rule that was added for the topic
*/
protected FilterRule addFilterRule(String mdcKey, List<String> paths) {
- if (mdcKey == null || mdcKey.isEmpty()) {
+ if (StringUtils.isBlank(mdcKey)) {
throw new IllegalArgumentException(MDC_KEY_ERROR);
}
- if (paths == null || paths.isEmpty()) {
+ if (nullOrEmpty(paths)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
@@ -175,6 +170,10 @@ public class MdcTopicFilter { return rule;
}
+ private static boolean nullOrEmpty(List<String> paths) {
+ return paths == null || paths.isEmpty();
+ }
+
/**
* Modifies an existing filter rule by adding the specified path.
*
@@ -183,7 +182,7 @@ public class MdcTopicFilter { * @return the filter rule that was modified
*/
protected FilterRule modifyFilterRule(String mdcKey, String path) {
- if (path == null || path.isEmpty()) {
+ if (StringUtils.isBlank(path)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
return modifyFilterRule(mdcKey, Arrays.asList(path));
@@ -197,11 +196,11 @@ public class MdcTopicFilter { * @return the filter rule that was modified
*/
protected FilterRule modifyFilterRule(String mdcKey, List<String> paths) {
- if (mdcKey == null || mdcKey.isEmpty()) {
+ if (StringUtils.isBlank(mdcKey)) {
throw new IllegalArgumentException(MDC_KEY_ERROR);
}
- if (paths == null || paths.isEmpty()) {
+ if (nullOrEmpty(paths)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
@@ -224,18 +223,18 @@ public class MdcTopicFilter { * @return the filter rule that was modified
*/
protected FilterRule modifyFilterRule(String oldMdcKey, String newMdcKey, List<String> paths) {
- if (oldMdcKey == null || oldMdcKey.isEmpty()) {
+ if (StringUtils.isBlank(oldMdcKey)) {
throw new IllegalArgumentException("current mdcKey must be provided");
}
- if (newMdcKey == null || newMdcKey.isEmpty()) {
+ if (StringUtils.isBlank(newMdcKey)) {
throw new IllegalArgumentException("new mdcKey must be provided");
}
if (oldMdcKey.equals(newMdcKey)) {
throw new IllegalArgumentException("the old and new mdcKey are equivalent");
}
- if (paths == null || paths.isEmpty()) {
+ if (nullOrEmpty(paths)) {
throw new IllegalArgumentException(JSON_PATH_ERROR);
}
@@ -268,7 +267,7 @@ public class MdcTopicFilter { * @return the filter rule that was deleted
*/
protected FilterRule deleteFilterRule(String mdcKey) {
- if (mdcKey == null || mdcKey.isEmpty()) {
+ if (StringUtils.isBlank(mdcKey)) {
throw new IllegalArgumentException(MDC_KEY_ERROR);
}
return rules.remove(mdcKey);
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 2a5a875d..568a316b 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 @@ -7,9 +7,9 @@ * 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.
@@ -20,13 +20,16 @@ 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;
@@ -83,7 +86,7 @@ public class MdcTopicFilterTest { */
@Test
public void multiFilterMultiPathTest() {
- String topicFilterProp = "requestID=$.requestID|$.body.request-id,"
+ String topicFilterProp = "requestID=$.requestID|$.body.request-id,"
+ "closedLoopControlName=$.closedLoopControlName"
+ "|$.body.closedLoopControlName";
MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
@@ -195,7 +198,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.addFilterRule(null, "$.subRequestID");
}
-
+
/**
* Tests throwing an exception for passing a null key and a list
* of paths.
@@ -217,7 +220,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.addFilterRule("", "$.subRequestID");
}
-
+
/**
* Tests throwing an exception for passing an empty key and
* a list of paths.
@@ -259,7 +262,7 @@ public class MdcTopicFilterTest { 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.
@@ -271,6 +274,12 @@ public class MdcTopicFilterTest { 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.
*/
@@ -300,6 +309,14 @@ public class MdcTopicFilterTest { 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.
*/
@@ -324,7 +341,7 @@ public class MdcTopicFilterTest { 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.
@@ -335,7 +352,7 @@ public class MdcTopicFilterTest { 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.
@@ -346,7 +363,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("", "$.request-id");
}
-
+
/**
* Tests throwing an exception when passing an empty key and
* a list of multiple paths.
@@ -357,7 +374,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("", Arrays.asList("$.request-id"));
}
-
+
/**
* Tests throwing an exception when passing an empty string path.
*/
@@ -367,7 +384,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("requestID", "");
}
-
+
/**
* Tests throwing an exception when passing an empty list of paths.
*/
@@ -377,9 +394,9 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("requestID", Arrays.asList());
}
-
+
/**
- * Tests throwing an exception when passing a key that is
+ * Tests throwing an exception when passing a key that is
* not in the filter rules map and a string path.
*/
@Test(expected = IllegalArgumentException.class)
@@ -388,9 +405,9 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("request-id", "$.request-id");
}
-
+
/**
- * Tests throwing an exception when passing a key that is
+ * 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)
@@ -399,8 +416,8 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("request-id", Arrays.asList("$.request-id"));
}
-
-
+
+
/**
* Tests throwing an exception when passing a null oldKey.
*/
@@ -410,7 +427,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule(null, "request-id", Arrays.asList("$.request-id"));
}
-
+
/**
* Tests throwing an exception when passing an empty oldKey.
*/
@@ -420,7 +437,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("", "request-id", Arrays.asList("$.request-id"));
}
-
+
/**
* Tests throwing an exception when passing a null newKey.
*/
@@ -430,7 +447,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.modifyFilterRule("requestID", null, Arrays.asList("$.request-id"));
}
-
+
/**
* Tests throwing an exception when passing an empty newKey.
*/
@@ -440,7 +457,7 @@ public class MdcTopicFilterTest { 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.
*/
@@ -451,7 +468,7 @@ public class MdcTopicFilterTest { topicFilter.modifyFilterRule("requestID", "requestID",
Arrays.asList("$.request-id"));
}
-
+
/**
* Tests throwing an exception when passing an empty paths list.
*/
@@ -461,7 +478,7 @@ public class MdcTopicFilterTest { 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.
@@ -511,7 +528,7 @@ public class MdcTopicFilterTest { MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp);
topicFilter.deleteFilterRule(null);
}
-
+
/**
* Tests throwing an exception if the key is empty.
*/
@@ -544,6 +561,19 @@ public class MdcTopicFilterTest { 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.
*/
@@ -561,4 +591,71 @@ public class MdcTopicFilterTest { 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());
+ }
}
|