Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static Match fromJson(Map<String, Object> match, String tag)
} else {
return new MatchFalse(tag);
}
} else if (match.containsKey("not")) {
Map<String, Object> inner = (Map<String, Object>) match.get("not");
var notMatch = MatchBuilder.fromJson(inner, tag);
return new MatchNot(notMatch, tag);
}

throw new InvalidMatchConfiguration(
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> configFalse =
Map.of(
"not",
Map.of(
"and",
List.of(
Map.of(
"jsonpath",
"$.telemetry_data.encoding_path",
"value",
"openconfig-interfaces:interfaces"))));
Map<String, Object> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}