Skip to content

Commit

Permalink
TDD Done
Browse files Browse the repository at this point in the history
  • Loading branch information
sathishk committed Jan 31, 2024
1 parent d2ab9f7 commit 500b362
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 73 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
</dependencyManagement>

<dependencies>


<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -128,7 +136,11 @@
<version>4.33.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/techatpark/sjson/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.io.Reader;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;

import javax.validation.ConstraintViolation;

/**
* Represents a JSON schema document. Provides functionality to serialize
* Java objects to JSON strings.
Expand All @@ -19,6 +22,17 @@ public JsonSchema() {
// Constructor logic if any
}

/**
* Validate the given root Json, starting at the root of the data path.
*
* @param reader the root node
* @return A list of ValidationMessage if there is any validation error,
* or an empty list if there is no error.
*/
public Set<ConstraintViolation> validate(final Reader reader) {
throw new UnsupportedOperationException("Not yet implemented");
}

/**
* Reads JSON from a Reader and converts it into a Java Object.
* This method is not yet implemented.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
module json.parser {
requires java.base;
requires java.validation;
opens com.techatpark.sjson.util;
opens com.techatpark.sjson.schema.generator;
opens com.techatpark.sjson.schema;
Expand Down
46 changes: 40 additions & 6 deletions src/test/java/com/techatpark/sjson/schema/JsonSchemaTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
package com.techatpark.sjson.schema;

import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import com.techatpark.sjson.util.TestUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

class JsonSchemaTest {

@Test
void read() {
public final ObjectMapper mapper = new ObjectMapper();

@ParameterizedTest
@MethodSource("jsonSchemaFilesProvider")
@Disabled
void read(final File schemaFile) throws IOException {
File dataFile = new File(new File(schemaFile.getParentFile().getParentFile(),"samples")
,schemaFile.getName());
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
JsonSchema jsonSchema = factory.getSchema(new FileInputStream(schemaFile));
JsonNode jsonNode = mapper.readTree(new FileReader(dataFile));
Set<ValidationMessage> expectedErrors = jsonSchema.validate(jsonNode);
Set<ValidationMessage> actualErrors = jsonSchema.validate(jsonNode);
Assertions.assertEquals(expectedErrors.size(), actualErrors.size());
}

@Test
void jsonText() {
/**
* Provides paths to JSON files for parameterized tests.
*
* @return Stream of paths to JSON files
* @throws IOException if there is an issue listing files
*/
private static Set<File> jsonSchemaFilesProvider() throws IOException {
return TestUtil.getJSONSchemaFiles();
}

}
24 changes: 21 additions & 3 deletions src/test/java/com/techatpark/sjson/util/TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.techatpark.sjson.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -10,18 +11,35 @@

public class TestUtil {

private static String baseFolder = System.getenv("SJSON_LOCAL_DIR") == null ? "src/test/resources" :
System.getenv("SJSON_LOCAL_DIR");

/**
* Utility to get Json Files from Test Resources directory.
* @return Set of Paths
* @throws IOException
*/
public static Set<Path> getJSONFiles() throws IOException {
String baseFolder = System.getenv("SJSON_LOCAL_DIR") == null ? "src/test/resources/samples" :
System.getenv("SJSON_LOCAL_DIR");
try (Stream<Path> stream = Files.list(Paths.get(baseFolder))) {

try (Stream<Path> stream = Files.list(new File(baseFolder, "samples").toPath())) {
return stream
.filter(file -> !Files.isDirectory(file))
.collect(Collectors.toSet());
}
}

/**
* Utility to get Json Schema Files from Test Resources directory.
* @return Set of Paths
* @throws IOException
*/
public static Set<File> getJSONSchemaFiles() throws IOException {

try (Stream<Path> stream = Files.list(new File(baseFolder, "schemas").toPath())) {
return stream
.filter(path -> !Files.isDirectory(path))
.map(Path::toFile)
.collect(Collectors.toSet());
}
}
}
5 changes: 5 additions & 0 deletions src/test/resources/samples/product.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 1,
"name": "Lampshade",
"price": 6
}
69 changes: 16 additions & 53 deletions src/test/resources/schemas/product.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,22 @@
{
"$schema": "https://json-schema.org/draft/-/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number",
"exclusiveMinimum":
},
"tags": {
"description": "Tags for the product",
"type": "array",
"items": {
"type": "string"
},
"minItems":,
"uniqueItems": true
},
"dimensions": {
"type": "object",
"properties": {
"length": {
"type": "number"
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from the catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"width": {
"type": "number"
"name": {
"description": "Name of the product",
"type": "string"
},
"height": {
"type": "number"
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
}
},
"required": [
"length",
"width",
"height"
]
},
"warehouseLocation": {
"description": "Coordinates of the warehouse where the product is located.",
"$ref": "https://example.com/geographical-location.schema.json"
}
},
"required": [
"productId",
"productName",
"price"
]
"required": ["id", "name", "price"]
}
10 changes: 0 additions & 10 deletions src/test/resources/schemas/sample-product.json

This file was deleted.

0 comments on commit 500b362

Please sign in to comment.