Skip to content

Commit 133aa53

Browse files
cuihengchHeng Cui
andauthored
Feature/match support (#22)
Match support Co-authored-by: Heng Cui <Heng.Cui@swisscom.com>
1 parent fbf8020 commit 133aa53

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

json-transformation/src/main/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public static Match fromJson(Map<String, Object> match, String tag)
4343
} else {
4444
return new MatchFalse(tag);
4545
}
46+
} else if (match.containsKey("not")) {
47+
Map<String, Object> inner = (Map<String, Object>) match.get("not");
48+
var notMatch = MatchBuilder.fromJson(inner, tag);
49+
return new MatchNot(notMatch, tag);
4650
}
4751

4852
throw new InvalidMatchConfiguration(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.swisscom.daisy.cosmos.candyfloss.transformations.match;
2+
3+
import com.jayway.jsonpath.Configuration;
4+
import com.jayway.jsonpath.DocumentContext;
5+
import com.jayway.jsonpath.JsonPath;
6+
import com.jayway.jsonpath.Option;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
10+
/***
11+
* A match that always returns true
12+
*/
13+
@Getter
14+
@AllArgsConstructor
15+
public class MatchNot implements Match {
16+
private final Match innerMatch;
17+
private static final Configuration configuration =
18+
Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
19+
20+
private final String tag;
21+
22+
@Override
23+
public boolean match(Object jsonObject) {
24+
DocumentContext context = JsonPath.using(configuration).parse(jsonObject);
25+
return !this.innerMatch.matchContext(context);
26+
}
27+
28+
@Override
29+
public boolean matchContext(DocumentContext context) {
30+
return !this.innerMatch.matchContext(context);
31+
}
32+
}

json-transformation/src/test/java/com/swisscom/daisy/cosmos/candyfloss/transformations/match/MatchBuilderTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,34 @@ public void testMatchOr() throws IOException, InvalidMatchConfiguration {
8888
assertTrue(matchTrue.match(input));
8989
assertFalse(matchFalse.match(input));
9090
}
91+
92+
@Test
93+
public void testMatchNot() throws IOException, InvalidMatchConfiguration {
94+
Map<String, Object> configFalse =
95+
Map.of(
96+
"not",
97+
Map.of(
98+
"and",
99+
List.of(
100+
Map.of(
101+
"jsonpath",
102+
"$.telemetry_data.encoding_path",
103+
"value",
104+
"openconfig-interfaces:interfaces"))));
105+
Map<String, Object> configTrue =
106+
Map.of(
107+
"not",
108+
Map.of(
109+
"jsonpath",
110+
"$.telemetry_data.encoding_path1",
111+
"value",
112+
"openconfig-interfaces:interfaces"));
113+
var matchTrue = MatchBuilder.fromJson(configFalse, "t1");
114+
var matchFalse = MatchBuilder.fromJson(configTrue, "t1");
115+
var input =
116+
JsonUtil.readJson(
117+
getClass().getClassLoader().getResource("openconfig-interfaces/input.json"));
118+
assertFalse(matchTrue.match(input));
119+
assertTrue(matchFalse.match(input));
120+
}
91121
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.swisscom.daisy.cosmos.candyfloss.transformations.match;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.jayway.jsonpath.JsonPath;
7+
import com.swisscom.daisy.cosmos.candyfloss.testutils.JsonUtil;
8+
import java.io.IOException;
9+
import java.util.List;
10+
import org.junit.jupiter.api.Test;
11+
12+
class MatchNotTest {
13+
@Test
14+
public void testMatch() throws IOException {
15+
var jsonPath1 = JsonPath.compile("$.telemetry_data.encoding_path");
16+
var jsonPath2 = JsonPath.compile("$.telemetry_data.encoding_path_no_exist");
17+
var matchTrue = new MatchJsonPathExists(jsonPath1, "t1");
18+
var matchFalse = new MatchJsonPathExists(jsonPath2, "t1");
19+
var input =
20+
JsonUtil.readJson(
21+
getClass().getClassLoader().getResource("openconfig-interfaces/input.json"));
22+
23+
var matchNotFalse = new MatchNot(matchTrue, "t1");
24+
var matchNotTrue = new MatchNot(matchFalse, "t1");
25+
26+
assertTrue(matchTrue.match(input));
27+
assertFalse(matchFalse.match(input));
28+
assertFalse(matchNotFalse.match(input));
29+
assertTrue(matchNotTrue.match(input));
30+
}
31+
32+
@Test
33+
public void testMatchComplex() throws IOException {
34+
var matchTrue = new MatchOr(List.of(new MatchTrue("t1"), new MatchFalse("t1")), "t1");
35+
var matchFalse = new MatchOr(List.of(new MatchFalse("t1"), new MatchFalse("t1")), "t1");
36+
var matchNotFalse = new MatchNot(matchTrue, "t1");
37+
var matchNotTrue = new MatchNot(matchFalse, "t1");
38+
var input =
39+
JsonUtil.readJson(
40+
getClass().getClassLoader().getResource("openconfig-interfaces/input.json"));
41+
assertTrue(matchNotTrue.match(input));
42+
assertFalse(matchNotFalse.match(input));
43+
}
44+
}

0 commit comments

Comments
 (0)