Skip to content

Move/add annotations and reformat code #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2024
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
9 changes: 6 additions & 3 deletions src/main/java/io/github/orangain/jsonmatch/JsonMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.orangain.jsonmatch.json.JsonUtil;
import io.github.orangain.jsonmatch.parser.JsonMatchPatternParser;
import io.github.orangain.jsonmatch.pattern.JsonPatternNode;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

Expand All @@ -16,7 +17,8 @@
public class JsonMatch {
/**
* Asserts that the actual JSON matches the pattern JSON.
* @param actualJson The actual JSON string.
*
* @param actualJson The actual JSON string.
* @param patternJson The pattern JSON string.
*/
public static void assertJsonMatches(String actualJson, String patternJson) {
Expand All @@ -28,11 +30,12 @@ public static void assertJsonMatches(String actualJson, String patternJson) {

/**
* Checks if the actual JSON matches the pattern JSON.
* @param actualJson The actual JSON string.
*
* @param actualJson The actual JSON string.
* @param patternJson The pattern JSON string.
* @return An {@link Optional} of {@link JsonMatchError} if the actual JSON does not match the pattern JSON.
*/
public static Optional<JsonMatchError> jsonMatches(String actualJson, String patternJson) {
public static @NotNull Optional<JsonMatchError> jsonMatches(String actualJson, String patternJson) {
ObjectMapper mapper = JsonUtil.getObjectMapper();
JsonNode actualTree;
JsonNode patternTree;
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/io/github/orangain/jsonmatch/JsonMatchError.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
*/
public class JsonMatchError {

private final String message;
private final String actual;
private final String expected;
private final @NotNull String message;
private final @NotNull String actual;
private final @NotNull String expected;

/**
* Constructor of the JSON match error.
* @param message The error message.
* @param actual The actual JSON string.
*
* @param message The error message.
* @param actual The actual JSON string.
* @param expected The expected JSON string.
*/
public JsonMatchError(@NotNull String message, @NotNull String actual, @NotNull String expected) {
Expand All @@ -25,25 +26,28 @@ public JsonMatchError(@NotNull String message, @NotNull String actual, @NotNull

/**
* Get the error message.
*
* @return The error message.
*/
public String getMessage() {
public @NotNull String getMessage() {
return message;
}

/**
* Get the actual JSON string.
*
* @return The actual JSON string.
*/
public String getActual() {
public @NotNull String getActual() {
return actual;
}

/**
* Get the expected JSON string.
*
* @return The expected JSON string.
*/
public String getExpected() {
public @NotNull String getExpected() {
return expected;
}
}
10 changes: 7 additions & 3 deletions src/main/java/io/github/orangain/jsonmatch/JsonStringAssert.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.orangain.jsonmatch;

import org.assertj.core.api.AbstractAssert;
import org.jetbrains.annotations.NotNull;

/**
* AssertJ assertion for JSON strings.
Expand All @@ -9,7 +10,8 @@ public class JsonStringAssert extends AbstractAssert<JsonStringAssert, String> {

/**
* Constructor of the JSON string assertion.
* @param s The JSON string to make assertions on.
*
* @param s The JSON string to make assertions on.
* @param selfType The class of the assertion.
*/
public JsonStringAssert(String s, Class<?> selfType) {
Expand All @@ -18,19 +20,21 @@ public JsonStringAssert(String s, Class<?> selfType) {

/**
* Creates a new instance of JsonStringAssert allowing to perform assertions on the provided JSON string.
*
* @param actual The JSON string to make assertions on.
* @return A new instance of JsonStringAssert.
*/
public static JsonStringAssert assertThat(String actual) {
public static @NotNull JsonStringAssert assertThat(String actual) {
return new JsonStringAssert(actual, JsonStringAssert.class);
}

/**
* Asserts that the JSON string matches the pattern JSON string.
*
* @param patternJson The pattern JSON string.
* @return This assertion object.
*/
public JsonStringAssert jsonMatches(String patternJson) {
public @NotNull JsonStringAssert jsonMatches(String patternJson) {
isNotNull();

JsonMatch.jsonMatches(actual, patternJson)
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/github/orangain/jsonmatch/json/JsonPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
* Simple subset of JSON path. It supports only root, array item, and object field.
*/
public class JsonPath {
private final String path;
private final @NotNull String path;

/**
* The root JSON path.
*/
public static JsonPath ROOT = new JsonPath("$");
public static @NotNull JsonPath ROOT = new JsonPath("$");

/**
* Constructor of the JSON path.
*
* @param path The JSON path string.
*/
public JsonPath(@NotNull String path) {
Expand All @@ -23,21 +24,21 @@ public JsonPath(@NotNull String path) {

/**
* Get the JSON path for the array item.
*
* @param index The index of the array item.
* @return The JSON path for the array item.
*/
@NotNull
public JsonPath arrayItem(int index) {
public @NotNull JsonPath arrayItem(int index) {
return new JsonPath(path + "[" + index + "]");
}

/**
* Get the JSON path for the object field.
*
* @param fieldName The name of the object field.
* @return The JSON path for the object field.
*/
@NotNull
public JsonPath objectField(@NotNull String fieldName) {
public @NotNull JsonPath objectField(@NotNull String fieldName) {
return new JsonPath(path + "." + fieldName);
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/github/orangain/jsonmatch/json/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* JSON utility class.
*/
public class JsonUtil {
private static ObjectMapper objectMapper;
private static @Nullable ObjectMapper objectMapper = null;

/**
* Get the singleton instance of the Jackson {@link ObjectMapper}.
*
* @return The singleton instance of the Jackson {@link ObjectMapper}.
*/
public static ObjectMapper getObjectMapper() {
public static @NotNull ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
Expand All @@ -22,10 +25,11 @@ public static ObjectMapper getObjectMapper() {

/**
* Convert an object to a JSON string. This method is unchecked exception version of {@link ObjectMapper#writeValueAsString(Object)}.
*
* @param value The object to convert to a JSON string.
* @return The JSON string.
*/
public static String toJsonString(Object value) {
public static @NotNull String toJsonString(Object value) {
try {
return getObjectMapper().writeValueAsString(value);
} catch (JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* Parser for JSON match patterns.
*/
public class JsonMatchPatternParser {
private static final Pattern ARRAY_PATTERN = Pattern.compile("\\A#\\[(\\d*)\\](.*)\\z");
private static final @NotNull Pattern ARRAY_PATTERN = Pattern.compile("\\A#\\[(\\d*)\\](.*)\\z");

/**
* Parse a JSON match pattern from a JSON node.
*
* @param jsonNode The JSON node to parse.
* @return The parsed JSON match pattern node.
*/
@NotNull
public static JsonPatternNode parse(@NotNull JsonNode jsonNode) {
public static @NotNull JsonPatternNode parse(@NotNull JsonNode jsonNode) {
if (jsonNode.isObject()) {
return parseObject(jsonNode);
} else if (jsonNode.isArray()) {
Expand All @@ -33,8 +33,7 @@ public static JsonPatternNode parse(@NotNull JsonNode jsonNode) {
}
}

@NotNull
private static JsonPatternNode parseObject(@NotNull JsonNode jsonNode) {
private static @NotNull JsonPatternNode parseObject(@NotNull JsonNode jsonNode) {
Map<String, JsonPatternNode> children = new HashMap<>();
for (Iterator<Map.Entry<String, JsonNode>> it = jsonNode.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> entry = it.next();
Expand All @@ -45,8 +44,7 @@ private static JsonPatternNode parseObject(@NotNull JsonNode jsonNode) {
return new ObjectLiteralPatternNode(jsonNode.toString(), children);
}

@NotNull
private static JsonPatternNode parseArray(@NotNull JsonNode jsonNode) {
private static @NotNull JsonPatternNode parseArray(@NotNull JsonNode jsonNode) {
List<JsonPatternNode> children = new ArrayList<>();
for (int i = 0; i < jsonNode.size(); i++) {
JsonNode childNode = jsonNode.get(i);
Expand All @@ -56,8 +54,7 @@ private static JsonPatternNode parseArray(@NotNull JsonNode jsonNode) {
return new ArrayLiteralPatternNode(jsonNode.toString(), children);
}

@NotNull
private static JsonPatternNode parseSimpleValue(@NotNull JsonNode jsonNode) {
private static @NotNull JsonPatternNode parseSimpleValue(@NotNull JsonNode jsonNode) {
if (jsonNode.isTextual()) {
JsonPatternNode parsed = parseMarkerOrNull(jsonNode.textValue());
if (parsed != null) {
Expand All @@ -68,8 +65,7 @@ private static JsonPatternNode parseSimpleValue(@NotNull JsonNode jsonNode) {
return new ValueLiteralPatternNode(jsonNode);
}

@Nullable
private static JsonPatternNode parseMarkerOrNull(@NotNull String value) {
private static @Nullable JsonPatternNode parseMarkerOrNull(@NotNull String value) {
if (!value.startsWith("#")) {
return null;
}
Expand Down Expand Up @@ -97,8 +93,7 @@ private static JsonPatternNode parseMarkerOrNull(@NotNull String value) {
}
}

@Nullable
private static JsonPatternNode parseValueMarkerOrNull(@NotNull String value) {
private static @Nullable JsonPatternNode parseValueMarkerOrNull(@NotNull String value) {
switch (value) {
case "#array":
return new ArrayMarkerPatternNode(JsonUtil.toJsonString(value));
Expand Down Expand Up @@ -127,8 +122,7 @@ private static JsonPatternNode parseValueMarkerOrNull(@NotNull String value) {
return parseArrayMarkerOrNull(value);
}

@Nullable
private static JsonPatternNode parseArrayMarkerOrNull(@NotNull String value) {
private static @Nullable JsonPatternNode parseArrayMarkerOrNull(@NotNull String value) {
Matcher arrayMatcher = ARRAY_PATTERN.matcher(value);
if (arrayMatcher.matches()) {
String length = arrayMatcher.group(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@
* JSON array pattern node that matches a JSON array with a fixed number of elements and specific patterns for each element.
*/
public class ArrayLiteralPatternNode extends ArrayPatternNode {
private final List<JsonPatternNode> expectedChildren;
private final @NotNull List<JsonPatternNode> expectedChildren;

/**
* Constructor of the JSON array pattern node.
* @param expected The string representation of the expected JSON array pattern.
*
* @param expected The string representation of the expected JSON array pattern.
* @param expectedChildren The expected patterns for each element of the array.
*/
public ArrayLiteralPatternNode(@NotNull String expected, List<JsonPatternNode> expectedChildren) {
public ArrayLiteralPatternNode(@NotNull String expected, @NotNull List<JsonPatternNode> expectedChildren) {
super(expected);
this.expectedChildren = expectedChildren;
}

@NotNull
@Override
protected Optional<JsonMatchErrorDetail> sizeMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
protected @NotNull Optional<JsonMatchErrorDetail> sizeMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
if (expectedChildren.size() != actualNode.size()) {
return Optional.of(error(jsonPath, actualNode, "actual and expected arrays are not the same size - " + actualNode.size() + ":" + expectedChildren.size()));
}
return Optional.empty();
}

@NotNull
@Override
protected Optional<JsonMatchErrorDetail> childrenMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
protected @NotNull Optional<JsonMatchErrorDetail> childrenMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
for (int i = 0; i < expectedChildren.size(); i++) {
Optional<JsonMatchErrorDetail> error = expectedChildren.get(i).matches(jsonPath.arrayItem(i), actualNode.get(i));
if (error.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public class ArrayMarkerPatternNode extends ArrayPatternNode {
/**
* The expected pattern for each element of the array. Null means any child pattern is allowed.
*/
private final JsonPatternNode expectedChildPattern;
private final @Nullable JsonPatternNode expectedChildPattern;

/**
* Constructor of the JSON array pattern node with no expected size or child pattern.
*
* @param expected The string representation of the expected JSON array pattern.
*/
public ArrayMarkerPatternNode(@NotNull String expected) {
Expand All @@ -30,7 +31,8 @@ public ArrayMarkerPatternNode(@NotNull String expected) {

/**
* Constructor of the JSON array pattern node with child pattern but no expected size.
* @param expected The string representation of the expected JSON array pattern.
*
* @param expected The string representation of the expected JSON array pattern.
* @param expectedChildPattern The expected pattern for each element of the array.
*/
public ArrayMarkerPatternNode(@NotNull String expected, @Nullable JsonPatternNode expectedChildPattern) {
Expand All @@ -39,8 +41,9 @@ public ArrayMarkerPatternNode(@NotNull String expected, @Nullable JsonPatternNod

/**
* Constructor of the JSON array pattern node with expected size and child pattern.
* @param expected The string representation of the expected JSON array pattern.
* @param expectedSize The expected size of the array.
*
* @param expected The string representation of the expected JSON array pattern.
* @param expectedSize The expected size of the array.
* @param expectedChildPattern The expected pattern for each element of the array.
*/
public ArrayMarkerPatternNode(@NotNull String expected, int expectedSize, @Nullable JsonPatternNode expectedChildPattern) {
Expand All @@ -49,19 +52,17 @@ public ArrayMarkerPatternNode(@NotNull String expected, int expectedSize, @Nulla
this.expectedChildPattern = expectedChildPattern;
}

@NotNull
@Override
protected Optional<JsonMatchErrorDetail> sizeMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
protected @NotNull Optional<JsonMatchErrorDetail> sizeMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
if (expectedSize >= 0 && expectedSize != actualNode.size()) {
return Optional.of(error(jsonPath, actualNode, "actual array length was: " + actualNode.size()));
}

return Optional.empty();
}

@NotNull
@Override
protected Optional<JsonMatchErrorDetail> childrenMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
protected @NotNull Optional<JsonMatchErrorDetail> childrenMatches(@NotNull JsonPath jsonPath, @NotNull JsonNode actualNode) {
if (expectedChildPattern == null) return Optional.empty();

for (int i = 0; i < actualNode.size(); i++) {
Expand Down
Loading