Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package org.citrusframework.json.schema;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

import com.networknt.schema.Schema;
Expand All @@ -28,6 +27,7 @@
import org.citrusframework.common.InitializingPhase;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.spi.Resource;
import org.citrusframework.spi.Resources.ByteArrayResource;

/**
* Adapter between the resource reference from the bean configuration and the usable {@link SimpleJsonSchema} for
Expand Down Expand Up @@ -65,23 +65,31 @@ public SimpleJsonSchema() {
*/
@Override
public void initialize() {
try {
schema = jsonSchemaFactory.getSchema(SchemaLocation.of(json.getURL().toString()));
} catch (Exception e) {
initializeFromFile();

if (json instanceof ByteArrayResource) {
initializeFromStream();
} else {
try {
// All other resources provide a URL, URL allows for loading or dependencies
schema = jsonSchemaFactory.getSchema(SchemaLocation.of(json.getURL().toString()));
} catch (Exception e) {
// If all fails, go for the stream
initializeFromStream();
}
}
}

/**
* Ensure backwards compatibility in any case, loading via SchemaLocation fails.
*/
private void initializeFromFile() {
try (FileInputStream fileInputStream = new FileInputStream(json.getFile())) {
schema = jsonSchemaFactory.getSchema(fileInputStream);
} catch (IOException e) {
private void initializeFromStream() {
try (InputStream inputStream = json.getInputStream()) {
schema = jsonSchemaFactory.getSchema(inputStream);
} catch (Exception e) {
throw new CitrusRuntimeException("Failed to load Json schema", e);
}
}

public Resource getJson() {
return json;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.citrusframework.json.schema;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.spi.Resource;
import org.citrusframework.spi.Resources.ByteArrayResource;
import org.citrusframework.spi.Resources.ClasspathResource;
import org.citrusframework.spi.Resources.FileSystemResource;
import org.citrusframework.spi.Resources.UrlResource;
import org.testng.annotations.Test;

public class SimpleJsonSchemaTest {

private static final String VALID_JSON_SCHEMA = """
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string" }
}
}
""";

private static final String INVALID_JSON_SCHEMA = "{ not valid json schema";

@Test
public void constructor_withResource_setsJsonField() {
Resource resource = createByteArrayResource(VALID_JSON_SCHEMA);

SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);

assertEquals(simpleJsonSchema.getJson(), resource);
assertNull(simpleJsonSchema.getSchema());
}

@Test
public void defaultConstructor_leavesFieldsUninitialized() {
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema();

assertNull(simpleJsonSchema.getJson());
assertNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withByteArrayResource_parsesSchema() {
ByteArrayResource resource = createByteArrayResource(VALID_JSON_SCHEMA);
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);

simpleJsonSchema.initialize();

assertNotNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withUrlResource_parsesSchema() throws Exception {
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/jsonSchema.json");
URL url = resource.getFile().toURI().toURL();
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(new UrlResource(url));

simpleJsonSchema.initialize();

assertNotNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withClasspathResource_parsesSchema() {
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/jsonSchema.json");
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);

simpleJsonSchema.initialize();

assertNotNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withFileSystemResource_parsesSchema() {
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/jsonSchema.json");
FileSystemResource fileSystemResource = new FileSystemResource(resource.getFile());
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(fileSystemResource);

simpleJsonSchema.initialize();

assertNotNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withJarUrlResource_parsesSchema() throws Exception {
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/test-schema.jar");

URL url = resource.getFile().toURI().toURL();
URL jarUrl = new URL("jar:"+url+"!/jsonschema.json");
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(new UrlResource(jarUrl));

simpleJsonSchema.initialize();

assertNotNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withUrlResourceFallback_usesInputStream() throws Exception {
UrlResource urlResource = new UrlResource(new URL("http://"+ UUID.randomUUID().toString())) {
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(VALID_JSON_SCHEMA.getBytes(StandardCharsets.UTF_8));
}
};

SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(urlResource);

simpleJsonSchema.initialize();

assertNotNull(simpleJsonSchema.getSchema());
}

@Test
public void initialize_withInvalidSchema_throwsCitrusRuntimeException() {
ByteArrayResource resource = createByteArrayResource(INVALID_JSON_SCHEMA);
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);

assertThrows(CitrusRuntimeException.class, simpleJsonSchema::initialize);
}

@Test
public void setJson_updatesJsonField() {
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema();
Resource resource = createByteArrayResource(VALID_JSON_SCHEMA);

simpleJsonSchema.setJson(resource);

assertEquals(simpleJsonSchema.getJson(), resource);
}

@Test
public void equals_sameInstance_returnsTrue() {
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema();

assertEquals(simpleJsonSchema, simpleJsonSchema);
}

private ByteArrayResource createByteArrayResource(String content) {
return new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string" }
}
}
Binary file not shown.
Loading