Skip to content

Commit

Permalink
Added bbox and crs schema validation
Browse files Browse the repository at this point in the history
modified bbox.json schema to verify array of 4 items.
  • Loading branch information
rafael.lopez.torres committed Nov 6, 2018
1 parent 91ab7c8 commit 1a12bed
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ luego incluir la dependencia en tu proyecto
<dependency>
<groupId>org.geowe.geojson</groupId>
<artifactId>geojson-validator</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>0.0.4-SNAPSHOT</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.geowe.geojson</groupId>
<artifactId>geojson-validator</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>0.0.4-SNAPSHOT</version>

<name>Geojson Validator</name>
<description>Utility to validate Geojson schema and geometry</description>
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/org/geowe/geojson/validation/GeojsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public class GeojsonValidator {
* @throws IOException
* @throws ProcessingException
*/
public Optional<ProcessingReport> validateSchema(String geojson) throws ProcessingException, IOException {
public Optional<ProcessingReport> validateSchema(String geojson)
throws ProcessingException, IOException {

return validate(geojson,
new GeojsonSchemaProvider().getJsonSchema(GeojsonSchemaProvider.Schema.GEOJSON_SCHEMA));
return validate(geojson, new GeojsonSchemaProvider()
.getJsonSchema(GeojsonSchemaProvider.Schema.GEOJSON_SCHEMA));
}

/**
Expand All @@ -64,12 +65,43 @@ public Optional<ProcessingReport> validateSchema(String geojson) throws Processi
* @throws ProcessingException
* @throws IOException
*/
public Optional<ProcessingReport> validateGeometrySchema(String geojson) throws ProcessingException, IOException {
public Optional<ProcessingReport> validateGeometrySchema(String geojson)
throws ProcessingException, IOException {

return validate(geojson,
new GeojsonSchemaProvider().getJsonSchema(GeojsonSchemaProvider.Schema.GEOMETRY_SCHEMA));
return validate(geojson, new GeojsonSchemaProvider()
.getJsonSchema(GeojsonSchemaProvider.Schema.GEOMETRY_SCHEMA));
}

/**
* Validate geoJSON optional bbox schema according to resources/schemas/bbox.json
*
* @param geojson : bbox geojson
* @return Optional<ProcessingReport> : validation report
* @throws ProcessingException
* @throws IOException
*/
public Optional<ProcessingReport> validateBboxSchema(String geojson)
throws ProcessingException, IOException {

return validate(geojson, new GeojsonSchemaProvider()
.getJsonSchema(GeojsonSchemaProvider.Schema.BBOX_SCHEMA));
}

/**
* Validate geoJSON optional crs schema (not included in RFC) according to resources/schemas/crs.json
*
* @param geojson : crs geojson
* @return Optional<ProcessingReport> : validation report
* @throws ProcessingException
* @throws IOException
*/
public Optional<ProcessingReport> validateCrsSchema(String geojson)
throws ProcessingException, IOException {

return validate(geojson, new GeojsonSchemaProvider()
.getJsonSchema(GeojsonSchemaProvider.Schema.CRS_SCHEMA));
}

private Optional<ProcessingReport> validate(String geojson, JsonSchema jsonSchema)
throws ProcessingException, IOException {
JsonNode geoJsonNode = objectMapper.readTree(geojson);
Expand Down
31 changes: 25 additions & 6 deletions src/main/resources/schemas/bbox.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://json-schema.org/geojson/bbox.json#",
"description": "A bounding box as defined by GeoJSON",
"type": "array",
"items": { "type": "number" }
}
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://json-schema.org/geojson/bbox.json#",
"description": "A bounding box as defined by GeoJSON",
"type": [
"object",
"null"
],
"required": [
"bbox"
],
"properties": {
"bbox": {
"id": "#/properties/bbox",
"type": "array",
"minItems": 4,
"maxItems": 4,
"title": "The Bbox Schema",
"items": {
"type": "number",
"title": "The Items Schema"
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,52 @@ public void Given_validFeatureGeojson_When_validate_Expect_success() {
LOG.error("Validation fails: " + e);
}
}

@Test
public void Given_validBboxGeojson_When_validate_Expect_success() {
GeojsonValidator validator = new GeojsonValidator();
try {
Assert.assertTrue(validator.validateBboxSchema(TestDataProvider.VALID_BBOX_GEOJSON).get().isSuccess());
} catch (IOException | ProcessingException e) {
LOG.error("Validation fails: " + e);
}
}

@Test
public void Given_invalidBboxGeojson_When_validate_Expect_fails() {
GeojsonValidator validator = new GeojsonValidator();
try {
ProcessingReport validationResult = validator.validateBboxSchema(TestDataProvider.INVALID_BBOX_GEOJSON)
.get();
LOG.info(validationResult.toString());
Assert.assertFalse(validationResult.isSuccess());
} catch (IOException | ProcessingException e) {
LOG.error("Validation fails: " + e);
}
}

@Test
public void Given_validCrsGeojson_When_validate_Expect_success() {
GeojsonValidator validator = new GeojsonValidator();
try {
ProcessingReport validationResult = validator.validateCrsSchema(TestDataProvider.VALID_CRS_GEOJSON).get();
LOG.info(validationResult.toString());
Assert.assertTrue(validationResult.isSuccess());
} catch (IOException | ProcessingException e) {
LOG.error("Validation fails: " + e);
}
}

@Test
public void Given_invalidCrsGeojson_When_validate_Expect_fails() {
GeojsonValidator validator = new GeojsonValidator();
try {
ProcessingReport validationResult = validator.validateCrsSchema(TestDataProvider.INVALID_CRS_GEOJSON)
.get();
LOG.info(validationResult.toString());
Assert.assertFalse(validationResult.isSuccess());
} catch (IOException | ProcessingException e) {
LOG.error("Validation fails: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ public class TestDataProvider {
public static final String VALID_POINT_GEOJSON = "{\"type\":\"Point\",\"coordinates\":[101.3818359375,-0.3790255558308115]}";
public static final String VALID_LINESTRING_GEOJSON = "{\"type\":\"LineString\",\"coordinates\":[[101.38170182704926,-0.37865273692742313],[101.38204783201218,-0.37883512315554363],[101.38206660747528,-0.3791006560396358],[101.38178765773773,-0.37937155321600796],[101.38165354728699,-0.3792454921547884]]}";
public static final String INVALID_POLYGON_GEOJSON = "{\"type\":\"Polygon\",\"coordinates\":[[[101.38203978538513,-0.37824236790011306],[101.38233482837676,-0.3783147859651637],[101.38202100992203,-0.3785856831661152],[101.38224631547928,-0.3787385657411375],[101.38234555721283,-0.37850790080234714],[101.38203978538513,-0.37824236790011306]]]}";

public static final String VALID_BBOX_GEOJSON = "{\"bbox\":[-180,-90,180,90]}";
public static final String INVALID_BBOX_GEOJSON = "{\"bbox\":[-180,-90,180]}";

public static final String VALID_CRS_GEOJSON = "{\"type\":\"name\",\"properties\":{\"name\":\"urn:ogc:def:crs:OGC:1.3:CRS84\"}}";
public static final String INVALID_CRS_GEOJSON = "{\"typ-e\":\"name\",\"properties\":{\"name\":\"urn:ogc:def:crs:OGC:1.3:CRS84\"}}";
}

0 comments on commit 1a12bed

Please sign in to comment.