Skip to content

Commit

Permalink
Kotest support (#659)
Browse files Browse the repository at this point in the history
* Kotest tests started

* JSON path support

* Move original kotest to a separate file

* Type assert

* Better Kotest

* Nullability annotations

* Assert type chaining

* Boolean chaining

* Null assert

* Array chaining

* Array chaining

* Fix test

* Fix formatting

* Fix module name

* Clue test

* JSON object matcher

* Broken test

* Configuration helper

* KDoc

* Better test

* Another test

* Softly assertion

* Presence test

* KDoc
  • Loading branch information
lukas-krecan authored Sep 14, 2023
1 parent 8fe272f commit dae42c0
Show file tree
Hide file tree
Showing 11 changed files with 539 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Node convertToNode(Object source, String label, boolean lenient) {
return nullNode();
} else if (source instanceof Node) {
return (Node) source;
} else if (source instanceof String && ((String) source).trim().length() > 0) {
} else if (source instanceof String && !((String) source).trim().isEmpty()) {
return readValue((String) source, label, lenient);
} else if (source instanceof Reader) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package net.javacrumbs.jsonunit.core.internal;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -55,7 +58,7 @@ static Converter createDefaultConverter() {
List<NodeFactory> factories;
String property = System.getProperty(LIBRARIES_PROPERTY_NAME);

if (property != null && property.trim().length() > 0) {
if (property != null && !property.trim().isEmpty()) {
factories = createFactoriesSpecifiedInProperty(property);
} else {
factories = createDefaultFactories();
Expand Down Expand Up @@ -106,8 +109,8 @@ private static List<NodeFactory> createDefaultFactories() {
}
return factories;
}

Node convertToNode(Object source, String label, boolean lenient) {
@NotNull
Node convertToNode(@Nullable Object source, String label, boolean lenient) {
for (int i = 0; i < factories.size(); i++) {
NodeFactory factory = factories.get(i);
if (isLastFactory(i) || factory.isPreferredFor(source)) {
Expand All @@ -117,6 +120,7 @@ Node convertToNode(Object source, String label, boolean lenient) {
throw new IllegalStateException("Should not happen");
}

@NotNull
Node valueToNode(Object source) {
for (int i = 0; i < factories.size(); i++) {
NodeFactory factory = factories.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.Option;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.List;
Expand All @@ -41,19 +43,14 @@ public class JsonUtils {
* @param label label to be logged in case of error.
* @return
*/
public static Node convertToJson(Object source, String label) {
public static Node convertToJson(@Nullable Object source, String label) {
return convertToJson(source, label, false);
}

/**
* Converts object to JSON.
*
* @param source
* @param label label to be logged in case of error.
* @param lenient lenient parser used for expected values. Allows unquoted keys.
* @return
*/
public static Node convertToJson(Object source, String label, boolean lenient) {
public static Node convertToJson(@Nullable Object source, String label, boolean lenient) {
if (source instanceof JsonSource) {
return converter.convertToNode(((JsonSource) source).getJson(), label, lenient);
} else {
Expand All @@ -64,9 +61,8 @@ public static Node convertToJson(Object source, String label, boolean lenient) {

/**
* Converts value to Json node. It can be Map, String, null, or primitive. Should not be parsed, just converted.
* @param source
* @return
*/
@NotNull
public static Node valueToNode(Object source) {
if (source instanceof Node) {
return (Node) source;
Expand All @@ -77,22 +73,16 @@ public static Node valueToNode(Object source) {

/**
* Returns node with given path.
*
* @param root
* @param path
* @return
*/
@NotNull
public static Node getNode(Object root, String path) {
return getNode(root, Path.create(path));
}

/**
* Returns node with given path.
*
* @param root
* @param path
* @return
*/
@NotNull
public static Node getNode(Object root, Path path) {
return path.getNode(convertToJson(root, "actual"));
}
Expand All @@ -105,14 +95,17 @@ public static boolean nodeAbsent(Object json, Path path, Configuration configura
return nodeAbsent(json, path, configuration.getOptions().contains(Option.TREATING_NULL_AS_ABSENT));
}

@NotNull
public static Object jsonSource(Object json, String pathPrefix) {
return jsonSource(json, pathPrefix, emptyList());
}

@NotNull
public static Object jsonSource(Object json, String pathPrefix, List<String> matchingPaths) {
return new DefaultJsonSource(json, pathPrefix, matchingPaths);
}

@NotNull
public static String getPathPrefix(Object json) {
if (json instanceof JsonSource) {
return ((JsonSource) json).getPathPrefix();
Expand Down Expand Up @@ -255,5 +248,10 @@ public String getPathPrefix() {
public List<String> getMatchingPaths() {
return matchingPaths;
}

@Override
public String toString() {
return "JSON in path \"" + pathPrefix +"\"";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package net.javacrumbs.jsonunit.core.internal;

import org.jetbrains.annotations.Nullable;

interface NodeFactory {
/**
* Returns true if this factory is preferred for given source.
Expand All @@ -32,7 +34,7 @@ interface NodeFactory {
* @param lenient
* @return
*/
Node convertToNode(Object source, String label, boolean lenient);
Node convertToNode(@Nullable Object source, String label, boolean lenient);

/**
* Converts value to Json node. It can be Map, String, null, or primitive. Should not be parsed, just converted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Node getNode(Node root) {
}

private boolean isRoot() {
return path.length() == 0;
return path.isEmpty();
}

private static Node doStep(String step, Node startNode) {
Expand All @@ -139,7 +139,7 @@ private static Node doStep(String step, Node startNode) {
if (!matcher.matches()) {
startNode = startNode.get(step);
} else {
if (matcher.group(1).length() != 0) {
if (!matcher.group(1).isEmpty()) {
startNode = startNode.get(matcher.group(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.jayway.jsonpath.EvaluationListener;
import com.jayway.jsonpath.PathNotFoundException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,7 +38,8 @@ private JsonPathAdapter() {

}

public static Object inPath(@NotNull Object json, @NotNull String path) {
@NotNull
public static Object inPath(@Nullable Object json, @NotNull String path) {
String normalizedPath = fromBracketNotation(path);
try {
MatchRecordingListener recordingListener = new MatchRecordingListener();
Expand Down
144 changes: 144 additions & 0 deletions json-unit-kotest/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>json-unit-kotest</artifactId>

<description>
Kotest assertions
</description>
<properties>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<osgi.exportPackage>net.javacrumbs.jsonunit.kotest</osgi.exportPackage>
<kotest.version>5.7.2</kotest.version>
</properties>

<parent>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-parent</artifactId>
<version>3.1.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-json-path</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-shared-jvm</artifactId>
<version>${kotest.version}</version>
</dependency>

<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-json-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>
net.javacrumbs.jsonunit.kotest
</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit dae42c0

Please sign in to comment.