diff --git a/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilder.java b/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilder.java index 1aaceb3..28622e0 100644 --- a/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilder.java +++ b/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilder.java @@ -43,6 +43,10 @@ public static Match fromJson(Map match, String tag) } else { return new MatchFalse(tag); } + } else if (match.containsKey("not")) { + Map inner = (Map) match.get("not"); + var notMatch = MatchBuilder.fromJson(inner, tag); + return new MatchNot(notMatch, tag); } throw new InvalidMatchConfiguration( diff --git a/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchNot.java b/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchNot.java new file mode 100644 index 0000000..c567014 --- /dev/null +++ b/json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchNot.java @@ -0,0 +1,32 @@ +package com.swisscom.daisy.cosmos.candyfloss.transformations.match; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; +import lombok.AllArgsConstructor; +import lombok.Getter; + +/*** + * A match that always returns true + */ +@Getter +@AllArgsConstructor +public class MatchNot implements Match { + private final Match innerMatch; + private static final Configuration configuration = + Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); + + private final String tag; + + @Override + public boolean match(Object jsonObject) { + DocumentContext context = JsonPath.using(configuration).parse(jsonObject); + return !this.innerMatch.matchContext(context); + } + + @Override + public boolean matchContext(DocumentContext context) { + return !this.innerMatch.matchContext(context); + } +} diff --git a/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilderTest.java b/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilderTest.java index a7afc3c..5b67850 100644 --- a/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilderTest.java +++ b/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilderTest.java @@ -88,4 +88,34 @@ public void testMatchOr() throws IOException, InvalidMatchConfiguration { assertTrue(matchTrue.match(input)); assertFalse(matchFalse.match(input)); } + + @Test + public void testMatchNot() throws IOException, InvalidMatchConfiguration { + Map configFalse = + Map.of( + "not", + Map.of( + "and", + List.of( + Map.of( + "jsonpath", + "$.telemetry_data.encoding_path", + "value", + "openconfig-interfaces:interfaces")))); + Map configTrue = + Map.of( + "not", + Map.of( + "jsonpath", + "$.telemetry_data.encoding_path1", + "value", + "openconfig-interfaces:interfaces")); + var matchTrue = MatchBuilder.fromJson(configFalse, "t1"); + var matchFalse = MatchBuilder.fromJson(configTrue, "t1"); + var input = + JsonUtil.readJson( + getClass().getClassLoader().getResource("openconfig-interfaces/input.json")); + assertFalse(matchTrue.match(input)); + assertTrue(matchFalse.match(input)); + } } diff --git a/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchNotTest.java b/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchNotTest.java new file mode 100644 index 0000000..a9553a7 --- /dev/null +++ b/json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchNotTest.java @@ -0,0 +1,44 @@ +package com.swisscom.daisy.cosmos.candyfloss.transformations.match; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.jayway.jsonpath.JsonPath; +import com.swisscom.daisy.cosmos.candyfloss.testutils.JsonUtil; +import java.io.IOException; +import java.util.List; +import org.junit.jupiter.api.Test; + +class MatchNotTest { + @Test + public void testMatch() throws IOException { + var jsonPath1 = JsonPath.compile("$.telemetry_data.encoding_path"); + var jsonPath2 = JsonPath.compile("$.telemetry_data.encoding_path_no_exist"); + var matchTrue = new MatchJsonPathExists(jsonPath1, "t1"); + var matchFalse = new MatchJsonPathExists(jsonPath2, "t1"); + var input = + JsonUtil.readJson( + getClass().getClassLoader().getResource("openconfig-interfaces/input.json")); + + var matchNotFalse = new MatchNot(matchTrue, "t1"); + var matchNotTrue = new MatchNot(matchFalse, "t1"); + + assertTrue(matchTrue.match(input)); + assertFalse(matchFalse.match(input)); + assertFalse(matchNotFalse.match(input)); + assertTrue(matchNotTrue.match(input)); + } + + @Test + public void testMatchComplex() throws IOException { + var matchTrue = new MatchOr(List.of(new MatchTrue("t1"), new MatchFalse("t1")), "t1"); + var matchFalse = new MatchOr(List.of(new MatchFalse("t1"), new MatchFalse("t1")), "t1"); + var matchNotFalse = new MatchNot(matchTrue, "t1"); + var matchNotTrue = new MatchNot(matchFalse, "t1"); + var input = + JsonUtil.readJson( + getClass().getClassLoader().getResource("openconfig-interfaces/input.json")); + assertTrue(matchNotTrue.match(input)); + assertFalse(matchNotFalse.match(input)); + } +}