Skip to content

Commit 08c7e54

Browse files
authored
Merge pull request #37 from orangain/refactor-classes
Add javadocs and fix visibility
2 parents 69eb39e + 98d267e commit 08c7e54

25 files changed

+330
-9
lines changed

src/main/java/io/github/orangain/jsonmatch/JsonMatch.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,28 @@
77

88
import java.util.Optional;
99

10-
10+
/**
11+
* JSON matcher that compares actual JSON with a pattern JSON.
12+
*/
1113
public class JsonMatch {
14+
/**
15+
* Asserts that the actual JSON matches the pattern JSON.
16+
* @param actualJson The actual JSON string.
17+
* @param patternJson The pattern JSON string.
18+
*/
1219
public static void assertJsonMatches(String actualJson, String patternJson) {
1320
jsonMatches(actualJson, patternJson)
1421
.ifPresent(error -> {
1522
throw new AssertionError(String.format("%s%nexpected:<%s> but was:<%s>", error.getMessage(), error.getExpected(), error.getActual()));
1623
});
1724
}
1825

26+
/**
27+
* Checks if the actual JSON matches the pattern JSON.
28+
* @param actualJson The actual JSON string.
29+
* @param patternJson The pattern JSON string.
30+
* @return An {@link Optional} of {@link JsonMatchError} if the actual JSON does not match the pattern JSON.
31+
*/
1932
public static Optional<JsonMatchError> jsonMatches(String actualJson, String patternJson) {
2033
ObjectMapper mapper = JsonUtil.getObjectMapper();
2134
JsonNode actualTree;

src/main/java/io/github/orangain/jsonmatch/JsonMatchError.java

+21
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,47 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
/**
6+
* JSON match error.
7+
*/
58
public class JsonMatchError {
69

710
private final String message;
811
private final String actual;
912
private final String expected;
1013

14+
/**
15+
* Constructor of the JSON match error.
16+
* @param message The error message.
17+
* @param actual The actual JSON string.
18+
* @param expected The expected JSON string.
19+
*/
1120
public JsonMatchError(@NotNull String message, @NotNull String actual, @NotNull String expected) {
1221
this.message = message;
1322
this.actual = actual;
1423
this.expected = expected;
1524
}
1625

26+
/**
27+
* Get the error message.
28+
* @return The error message.
29+
*/
1730
public String getMessage() {
1831
return message;
1932
}
2033

34+
/**
35+
* Get the actual JSON string.
36+
* @return The actual JSON string.
37+
*/
2138
public String getActual() {
2239
return actual;
2340
}
2441

42+
/**
43+
* Get the expected JSON string.
44+
* @return The expected JSON string.
45+
*/
2546
public String getExpected() {
2647
return expected;
2748
}

src/main/java/io/github/orangain/jsonmatch/JsonMatchErrorDetail.java

+10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
/**
6+
* JSON match error detail.
7+
*/
58
public class JsonMatchErrorDetail {
69
private final JsonPath path;
710
private final String reason;
811
private final String actual;
912
private final String expected;
1013

14+
/**
15+
* Constructor of the JSON match error detail.
16+
* @param path The JSON path where the error occurred.
17+
* @param actual The actual JSON string.
18+
* @param expected The expected JSON string.
19+
* @param reason The reason for the error.
20+
*/
1121
public JsonMatchErrorDetail(@NotNull JsonPath path, @NotNull String actual, @NotNull String expected, @NotNull String reason) {
1222
this.path = path;
1323
this.actual = actual;

src/main/java/io/github/orangain/jsonmatch/JsonMatchPatternParser.java

+8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
import java.util.regex.Matcher;
1111
import java.util.regex.Pattern;
1212

13+
/**
14+
* Parser for JSON match patterns.
15+
*/
1316
public class JsonMatchPatternParser {
1417
private static final Pattern ARRAY_PATTERN = Pattern.compile("\\A#\\[(\\d*)\\](.*)\\z");
1518

19+
/**
20+
* Parse a JSON match pattern from a JSON node.
21+
* @param jsonNode The JSON node to parse.
22+
* @return The parsed JSON match pattern node.
23+
*/
1624
@NotNull
1725
public static JsonPatternNode parse(@NotNull JsonNode jsonNode) {
1826
if (jsonNode.isObject()) {

src/main/java/io/github/orangain/jsonmatch/JsonPath.java

+20
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,40 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
/**
6+
* Simple subset of JSON path. It supports only root, array item, and object field.
7+
*/
58
public class JsonPath {
69
private final String path;
710

11+
/**
12+
* The root JSON path.
13+
*/
814
public static JsonPath ROOT = new JsonPath("$");
915

16+
/**
17+
* Constructor of the JSON path.
18+
* @param path The JSON path string.
19+
*/
1020
public JsonPath(@NotNull String path) {
1121
this.path = path;
1222
}
1323

24+
/**
25+
* Get the JSON path for the array item.
26+
* @param index The index of the array item.
27+
* @return The JSON path for the array item.
28+
*/
1429
@NotNull
1530
public JsonPath arrayItem(int index) {
1631
return new JsonPath(path + "[" + index + "]");
1732
}
1833

34+
/**
35+
* Get the JSON path for the object field.
36+
* @param fieldName The name of the object field.
37+
* @return The JSON path for the object field.
38+
*/
1939
@NotNull
2040
public JsonPath objectField(@NotNull String fieldName) {
2141
return new JsonPath(path + "." + fieldName);

src/main/java/io/github/orangain/jsonmatch/JsonStringAssert.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@
22

33
import org.assertj.core.api.AbstractAssert;
44

5-
5+
/**
6+
* AssertJ assertion for JSON strings.
7+
*/
68
public class JsonStringAssert extends AbstractAssert<JsonStringAssert, String> {
79

10+
/**
11+
* Constructor of the JSON string assertion.
12+
* @param s The JSON string to make assertions on.
13+
* @param selfType The class of the assertion.
14+
*/
815
public JsonStringAssert(String s, Class<?> selfType) {
916
super(s, selfType);
1017
}
1118

19+
/**
20+
* Creates a new instance of JsonStringAssert allowing to perform assertions on the provided JSON string.
21+
* @param actual The JSON string to make assertions on.
22+
* @return A new instance of JsonStringAssert.
23+
*/
1224
public static JsonStringAssert assertThat(String actual) {
1325
return new JsonStringAssert(actual, JsonStringAssert.class);
1426
}
1527

28+
/**
29+
* Asserts that the JSON string matches the pattern JSON string.
30+
* @param patternJson The pattern JSON string.
31+
* @return This assertion object.
32+
*/
1633
public JsonStringAssert jsonMatches(String patternJson) {
1734
isNotNull();
1835

src/main/java/io/github/orangain/jsonmatch/JsonUtil.java

+12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55

6+
/**
7+
* JSON utility class.
8+
*/
69
public class JsonUtil {
710
private static ObjectMapper objectMapper;
811

12+
/**
13+
* Get the singleton instance of the Jackson {@link ObjectMapper}.
14+
* @return The singleton instance of the Jackson {@link ObjectMapper}.
15+
*/
916
static ObjectMapper getObjectMapper() {
1017
if (objectMapper == null) {
1118
objectMapper = new ObjectMapper();
1219
}
1320
return objectMapper;
1421
}
1522

23+
/**
24+
* Convert an object to a JSON string. This method is unchecked exception version of {@link ObjectMapper#writeValueAsString(Object)}.
25+
* @param value The object to convert to a JSON string.
26+
* @return The JSON string.
27+
*/
1628
public static String toJsonString(Object value) {
1729
try {
1830
return getObjectMapper().writeValueAsString(value);

src/main/java/io/github/orangain/jsonmatch/pattern/ArrayLiteralPatternNode.java

+8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
import java.util.List;
99
import java.util.Optional;
1010

11+
/**
12+
* JSON array pattern node that matches a JSON array with a fixed number of elements and specific patterns for each element.
13+
*/
1114
public class ArrayLiteralPatternNode extends ArrayPatternNode {
1215
private final List<JsonPatternNode> expectedChildren;
1316

17+
/**
18+
* Constructor of the JSON array pattern node.
19+
* @param expected The string representation of the expected JSON array pattern.
20+
* @param expectedChildren The expected patterns for each element of the array.
21+
*/
1422
public ArrayLiteralPatternNode(@NotNull String expected, List<JsonPatternNode> expectedChildren) {
1523
super(expected);
1624
this.expectedChildren = expectedChildren;

src/main/java/io/github/orangain/jsonmatch/pattern/ArrayMarkerPatternNode.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,42 @@
88

99
import java.util.Optional;
1010

11+
/**
12+
* JSON array pattern node that matches a JSON array. The pattern is represented by the "#array" marker.
13+
*/
1114
public class ArrayMarkerPatternNode extends ArrayPatternNode {
12-
private final int expectedSize; // -1 when any size is allowed
13-
private final JsonPatternNode expectedChildPattern; // null when any item is allowed
15+
/**
16+
* The expected size of the array. -1 means any size is allowed.
17+
*/
18+
private final int expectedSize;
19+
/**
20+
* The expected pattern for each element of the array. Null means any child pattern is allowed.
21+
*/
22+
private final JsonPatternNode expectedChildPattern;
1423

24+
/**
25+
* Constructor of the JSON array pattern node with no expected size or child pattern.
26+
* @param expected The string representation of the expected JSON array pattern.
27+
*/
1528
public ArrayMarkerPatternNode(@NotNull String expected) {
1629
this(expected, null);
1730
}
1831

32+
/**
33+
* Constructor of the JSON array pattern node with child pattern but no expected size.
34+
* @param expected The string representation of the expected JSON array pattern.
35+
* @param expectedChildPattern The expected pattern for each element of the array.
36+
*/
1937
public ArrayMarkerPatternNode(@NotNull String expected, @Nullable JsonPatternNode expectedChildPattern) {
2038
this(expected, -1, expectedChildPattern);
2139
}
2240

41+
/**
42+
* Constructor of the JSON array pattern node with expected size and child pattern.
43+
* @param expected The string representation of the expected JSON array pattern.
44+
* @param expectedSize The expected size of the array.
45+
* @param expectedChildPattern The expected pattern for each element of the array.
46+
*/
2347
public ArrayMarkerPatternNode(@NotNull String expected, int expectedSize, @Nullable JsonPatternNode expectedChildPattern) {
2448
super(expected);
2549
this.expectedSize = expectedSize;

src/main/java/io/github/orangain/jsonmatch/pattern/ArrayPatternNode.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
import java.util.Optional;
99

10+
/**
11+
* Base class for JSON array pattern nodes.
12+
*/
1013
public abstract class ArrayPatternNode extends JsonPatternNode {
11-
public ArrayPatternNode(@NotNull String expected) {
14+
/**
15+
* Constructor of the JSON array pattern node.
16+
* @param expected The string representation of the expected JSON array pattern.
17+
*/
18+
protected ArrayPatternNode(@NotNull String expected) {
1219
super(expected);
1320
}
1421

15-
1622
@NotNull
1723
@Override
1824
public Optional<JsonMatchErrorDetail> matches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
@@ -28,9 +34,21 @@ public Optional<JsonMatchErrorDetail> matches(@NotNull JsonPath jsonPath, @NotNu
2834
return childrenMatches(jsonPath, actualNode);
2935
}
3036

37+
/**
38+
* Checks if the size of the actual JSON array node matches the pattern node.
39+
* @param jsonPath The JSON path to the actual JSON node.
40+
* @param actualNode The actual JSON node.
41+
* @return An empty optional if the size matches, or an error detail if it does not match.
42+
*/
3143
@NotNull
3244
protected abstract Optional<JsonMatchErrorDetail> sizeMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode);
3345

46+
/**
47+
* Checks if the children of the actual JSON array node matches the pattern node.
48+
* @param jsonPath The JSON path to the actual JSON node.
49+
* @param actualNode The actual JSON node.
50+
* @return An empty optional if the children matches, or an error detail if they do not match.
51+
*/
3452
@NotNull
3553
protected abstract Optional<JsonMatchErrorDetail> childrenMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode);
3654
}

src/main/java/io/github/orangain/jsonmatch/pattern/JsonPatternNode.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,48 @@
77

88
import java.util.Optional;
99

10+
/**
11+
* Base class for JSON pattern nodes.
12+
*/
1013
public abstract class JsonPatternNode {
14+
/**
15+
* The string representation of the expected JSON pattern.
16+
*/
1117
private final String expected;
1218

13-
public JsonPatternNode(@NotNull String expected) {
19+
/**
20+
* Constructor of the JSON pattern node.
21+
* @param expected The string representation of the expected JSON pattern.
22+
*/
23+
protected JsonPatternNode(@NotNull String expected) {
1424
this.expected = expected;
1525
}
1626

27+
/**
28+
* Matches the JSON pattern node against the actual JSON node.
29+
* @param path The JSON path to the actual JSON node.
30+
* @param actualNode The actual JSON node.
31+
* @return An empty optional if the actual JSON node matches the pattern node, or an error detail if it does not match.
32+
*/
1733
@NotNull
1834
public abstract Optional<JsonMatchErrorDetail> matches(@NotNull JsonPath path, @NotNull JsonNode actualNode);
1935

36+
/**
37+
* Creates an error detail.
38+
* @param path The JSON path to the actual JSON node.
39+
* @param actualNode The actual JSON node.
40+
* @param reason The reason why the actual JSON node does not match the pattern node.
41+
* @return An error detail.
42+
*/
2043
@NotNull
2144
protected JsonMatchErrorDetail error(@NotNull JsonPath path, @NotNull JsonNode actualNode, @NotNull String reason) {
2245
return new JsonMatchErrorDetail(path, actualNode.toString(), expected, reason);
2346
}
2447

48+
/**
49+
* Returns if the pattern node can be missing.
50+
* @return True if the pattern node can be missing, false otherwise.
51+
*/
2552
protected boolean canBeMissing() {
2653
return false;
2754
}

src/main/java/io/github/orangain/jsonmatch/pattern/ObjectLiteralPatternNode.java

+8
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
import java.util.Set;
1313
import java.util.stream.Collectors;
1414

15+
/**
16+
* JSON object pattern node that matches a JSON object with fixed key-value pairs.
17+
*/
1518
public class ObjectLiteralPatternNode extends ObjectPatternNode {
1619
private final Map<String, JsonPatternNode> expectedChildren;
1720

21+
/**
22+
* Constructor of the JSON object pattern node.
23+
* @param expected The string representation of the expected JSON object pattern.
24+
* @param expectedChildren The expected pairs of key and value pattern node.
25+
*/
1826
public ObjectLiteralPatternNode(@NotNull String expected, @NotNull Map<String, JsonPatternNode> expectedChildren) {
1927
super(expected);
2028
this.expectedChildren = expectedChildren;

0 commit comments

Comments
 (0)