Skip to content
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

Changes of pull requests #6 and #9 and additional examples #13

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,19 @@
<artifactId>geoapi-pending</artifactId>
<version>3.1-M04</version>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.76</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>


<dependency>
<groupId>com.github.erosb</groupId>
<artifactId>everit-json-schema</artifactId>
<version>1.14.1</version>
</dependency>

<dependency>
<groupId>org.geotoolkit</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.opengis.cite.eogeojson10;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.SpecVersionDetector;

public class BaseJsonSchemaValidatorTest {
public final int DEFAULT_BUFFER_SIZE = 8192;
private ObjectMapper mapper = new ObjectMapper();

//from https://github.com/networknt/json-schema-validator/blob/master/doc/quickstart.md

public JsonNode getJsonNodeFromClasspath(String name) throws IOException {
InputStream is1 = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
return mapper.readTree(is1);
}

public JsonNode getJsonNodeFromStringContent(String content) throws IOException {
return mapper.readTree(content);
}

public JsonNode getJsonNodeFromUrl(String url) throws IOException {
return mapper.readTree(new URL(url));
}

public JsonSchema getJsonSchemaFromClasspath(String name) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
return factory.getSchema(is);
}

public JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(schemaContent);
}

public JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(new URI(uri));
}

public JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(jsonNode);
}

// Automatically detect version for given JsonNode
public JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode));
return factory.getSchema(jsonNode);
}
// from https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
public String otherConvertInputStreamToString(InputStream is) throws IOException {

ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = is.read(buffer)) != -1) {
result.write(buffer, 0, length);
}



return result.toString("UTF-8");


}
}
11 changes: 0 additions & 11 deletions src/main/java/org/opengis/cite/eogeojson10/DataFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

import org.json.JSONObject;
import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -56,17 +55,7 @@ public void setTestSubject(String testSubject) {
this.testSubject = testSubject;
}

public JSONObject readJSONObjectFromFile(File filePath) throws IOException {

FileInputStream is = new FileInputStream(filePath);
try ( Scanner scanner = new Scanner(is,
StandardCharsets.UTF_8.toString())) {
scanner.useDelimiter("\\A");

return new JSONObject(scanner.hasNext() ? scanner.next() : "");
}

}

// from https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
public String convertInputStreamToString(InputStream is) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.opengis.cite.eogeojson10.acquisitioninformation;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;

import org.opengis.cite.eogeojson10.BaseJsonSchemaValidatorTest;
import org.opengis.cite.eogeojson10.DataFixture;
import org.testng.Assert;
import org.testng.SkipException;
Expand All @@ -18,12 +21,14 @@ public class AquisitionInformationConfClassTests extends DataFixture{
public void validateAquisitionInformation() throws IOException{
boolean valid = true;
StringBuffer sb = new StringBuffer();

BaseJsonSchemaValidatorTest tester = new BaseJsonSchemaValidatorTest();
JsonNode jo = tester.getJsonNodeFromStringContent(tester.otherConvertInputStreamToString(new FileInputStream(new File(testSubject))));

JSONObject jo = readJSONObjectFromFile(new File(testSubject));


if(jo.has("properties")) {

JSONObject propertiesJO = jo.getJSONObject("properties");
JsonNode propertiesJO = jo.get("properties");
if(propertiesJO.has("acquisitionInformation")) {
//do nothing
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.opengis.cite.eogeojson10.acquisitionparameters;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import org.opengis.cite.eogeojson10.BaseJsonSchemaValidatorTest;
import org.opengis.cite.eogeojson10.DataFixture;
import org.testng.Assert;
import org.testng.SkipException;
Expand All @@ -19,18 +22,21 @@ public class AquisitionParametersConfClassTests extends DataFixture{
public void validateAquisitionParameters() throws IOException{
boolean valid = true;
StringBuffer sb = new StringBuffer();

JSONObject jo = readJSONObjectFromFile(new File(testSubject));

if(jo.has("properties")) {

JSONObject propertiesJO = jo.getJSONObject("properties");

BaseJsonSchemaValidatorTest tester = new BaseJsonSchemaValidatorTest();
JsonNode jo = tester.getJsonNodeFromStringContent(tester.otherConvertInputStreamToString(new FileInputStream(new File(testSubject))));


if(jo.has("properties")) {

JsonNode propertiesJO = jo.get("properties");
if(propertiesJO.has("acquisitionInformation")) {

//-------------------
JSONArray acquisitionInformationJO = propertiesJO.getJSONArray("acquisitionInformation");
for(int i=0; i < acquisitionInformationJO.length(); i++) {
JSONObject acquisitionInformationJOItem = (JSONObject) acquisitionInformationJO.get(i);

ArrayNode acquisitionInformationJO = (ArrayNode) propertiesJO.get("acquisitionInformation");
for(int i=0; i < acquisitionInformationJO.size(); i++) {
JsonNode acquisitionInformationJOItem = acquisitionInformationJO.get(i);
if(acquisitionInformationJOItem.has("acquisitionParameters")) {
//do nothing
}
Expand Down
92 changes: 56 additions & 36 deletions src/main/java/org/opengis/cite/eogeojson10/core/CoreTests.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.opengis.cite.eogeojson10.core;

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.ValidationMessage;


import org.opengis.cite.eogeojson10.BaseJsonSchemaValidatorTest;
import org.opengis.cite.eogeojson10.DataFixture;
import org.opengis.cite.eogeojson10.ErrorMessage;
import org.opengis.cite.eogeojson10.ErrorMessageKeys;
Expand All @@ -12,9 +14,7 @@
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;

import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.SkipException;
Expand Down Expand Up @@ -64,72 +64,92 @@ public void validateEOMetadataGeoJSONValidPerSchema(){


boolean valid = false;
InputStream inputStream = getClass()

JsonSchema schema = null;
BaseJsonSchemaValidatorTest tester = new BaseJsonSchemaValidatorTest();

StringBuffer sb = new StringBuffer();

InputStream inputStream = tester.getClass()
.getResourceAsStream(schemaToApply);


Schema schema = null;
//------Test the Feature


try {
JSONObject rawSchema = new JSONObject(convertInputStreamToString(inputStream));
schema = SchemaLoader.load(rawSchema);

schema.validate(readJSONObjectFromFile(new File(testSubject))); // throws a ValidationException if this object is invalid

valid = true;
}
catch(Exception ex) {
errorMessage.append("Validation of single feature document failed because "+ex.getMessage()+"\n");

valid = false;
}
JsonNode schemaNode = tester.getJsonNodeFromStringContent(tester.otherConvertInputStreamToString(inputStream));
schema = tester.getJsonSchemaFromJsonNodeAutomaticVersion(schemaNode);

schema.initializeValidators();

JsonNode node = tester.getJsonNodeFromStringContent(tester.otherConvertInputStreamToString(new FileInputStream(testSubject)));
Set<ValidationMessage> errors = schema.validate(node);



Iterator it = errors.iterator();
while(it.hasNext())
{
valid = false;
String errorText = " "+it.next();
sb.append(errorText+".\n");
errorMessage.append(errorText+".\n");

}

if(valid==false) {
Assert.assertTrue(valid,
"Validation failed. "+errorMessage.toString()+ " . ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


///---



//------Test the Feature Collection
JSONObject jo = null;

JsonNode jo = null;
boolean validCol = false;
try {

jo = readJSONObjectFromFile(new File(collectionTestSubject));



jo = tester.getJsonNodeFromStringContent(tester.otherConvertInputStreamToString(new FileInputStream(collectionTestSubject)));


if(jo.has("type")) {

if(jo.get("type").equals("FeatureCollection")) {
schema.validate(jo); // throws a ValidationException if this object is invalid
if(jo.get("type").textValue().equals("FeatureCollection")) {
Set<ValidationMessage> errors = schema.validate(jo); // throws a ValidationException if this object is invalid
if(errors.size()>0) errorMessage.append("CoreTests validation error -> "+errors.toString()+".");
validCol = true;

}
else {

validCol = false;
errorMessage.append("Validation of feature collection did not have type property value that equals 'FeatureCollection'\n");
}
}
else {

validCol = false;
errorMessage.append("Validation of feature collection did not have type property\n");
}

}
catch(Exception ex)
{

errorMessage.append("Validation of Feature Collection failed because "+ex.getMessage()+"\n");

validCol = false;
}

if(validCol==false) {
if(validCol==false && valid==false) {

Assert.assertTrue(validCol,
"Validation failed. "+errorMessage.toString());
}

}




Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.opengis.cite.eogeojson10.dataidentification;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;

import org.opengis.cite.eogeojson10.BaseJsonSchemaValidatorTest;
import org.opengis.cite.eogeojson10.DataFixture;
import org.testng.Assert;
import org.testng.SkipException;
Expand All @@ -19,12 +22,14 @@ public void validateDataIdentification() throws IOException{
boolean valid = true;
StringBuffer sb = new StringBuffer();
String[] mandatoryFields = {"title","identifier","date"};

JSONObject jo = readJSONObjectFromFile(new File(testSubject));

BaseJsonSchemaValidatorTest tester = new BaseJsonSchemaValidatorTest();
JsonNode jo = tester.getJsonNodeFromStringContent(tester.otherConvertInputStreamToString(new FileInputStream(new File(testSubject))));


if(jo.has("properties")) {

JSONObject propertiesJO = jo.getJSONObject("properties");
JsonNode propertiesJO = jo.get("properties");
for(String prop:mandatoryFields)
{
if(propertiesJO.has(prop)!=true) {
Expand Down
Loading