diff --git a/extensions/template/examples/collection.json b/extensions/template/examples/collection.json index 24f0b78d..d11eca1b 100644 --- a/extensions/template/examples/collection.json +++ b/extensions/template/examples/collection.json @@ -27,7 +27,12 @@ "assets": { "example": { "href": "https://example.com/examples/file.xyz", - "template:new_field": "test" + "template:new_field": "test", + "template:xyz": { + "x": 1, + "y": 2, + "z": 3 + } } }, "item_assets": { diff --git a/extensions/template/non-examples/collection.json b/extensions/template/non-examples/collection.json deleted file mode 100644 index 8776ed9d..00000000 --- a/extensions/template/non-examples/collection.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "stac_version": "1.0.0", - "stac_extensions": [ - "https://stac-extensions.github.io/item-assets/v1.0.0/schema.json", - "https://linz.github.io/stac/_STAC_VERSION_/template/schema.json" - ], - "type": "Collection", - "id": "collection", - "title": "A title", - "description": "This is a non-conformant STAC example using the template extension. The template:new_field is an integer rather than string.", - "license": "Apache-2.0", - "extent": { - "spatial": { - "bbox": [[172.9, 1.3, 173, 1.4]] - }, - "temporal": { - "interval": [["2015-06-23T00:00:00Z", null]] - } - }, - "template:new_field": 1234, - "template:xyz": { - "x": 1, - "y": 2, - "z": 3 - }, - "template:another_one": [1, 2, 3], - "assets": { - "example": { - "href": "https://example.com/examples/file.xyz", - "template:new_field": "test" - } - }, - "item_assets": { - "data": { - "roles": ["data"], - "template:new_field": "test" - } - }, - "summaries": { - "datetime": { - "minimum": "2015-06-23T00:00:00Z", - "maximum": "2019-07-10T13:44:56Z" - } - }, - "links": [ - { - "href": "https://example.com/examples/collection.json", - "rel": "self" - }, - { - "href": "https://example.com/examples/item.json", - "rel": "item" - } - ] -} diff --git a/extensions/template/non-examples/item.json b/extensions/template/non-examples/item.json deleted file mode 100644 index 135c62fe..00000000 --- a/extensions/template/non-examples/item.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "stac_version": "1.0.0", - "stac_extensions": ["https://linz.github.io/stac/_STAC_VERSION_/template/schema.json"], - "type": "Feature", - "description": "This is a non-conformant STAC example using the template extension. It is missing the mandatory template:new_field.", - "id": "item", - "bbox": [172.9, 1.3, 173, 1.4], - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [172.9, 1.3], - [173, 1.3], - [173, 1.4], - [172.9, 1.4], - [172.9, 1.3] - ] - ] - }, - "properties": { - "datetime": "2020-12-11T22:38:32Z", - "template:xyz": { - "x": 1, - "y": 2, - "z": 3 - }, - "template:another_one": [1, 2, 3] - }, - "links": [ - { - "href": "https://example.com/examples/item.json", - "rel": "self" - } - ], - "assets": { - "data": { - "href": "https://example.com/examples/file.xyz" - } - } -} diff --git a/extensions/template/tests/template_collection.test.js b/extensions/template/tests/template_collection.test.js new file mode 100644 index 00000000..3b550f24 --- /dev/null +++ b/extensions/template/tests/template_collection.test.js @@ -0,0 +1,47 @@ +import o from 'ospec'; +import Ajv from 'ajv'; +import { dirname, join } from 'path'; +import { fileURLToPath } from 'url'; +import { promises as fs } from 'fs'; +import { AjvOptions } from '../../validation.js'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const schemaPath = join(__dirname, '..', 'schema.json'); +const exampleCollectionPath = join(__dirname, '..', 'examples/collection.json'); + +o.spec('template-collection', () => { + o.specTimeout(20000); + let validate; + const ajv = new Ajv(AjvOptions); + + o.before(async () => { + const data = JSON.parse(await fs.readFile(schemaPath)); + validate = await ajv.compileAsync(data); + }); + + o('Collection should pass validation', async () => { + // given + const templateCollectionExample = JSON.parse(await fs.readFile(exampleCollectionPath)); + + // when + let valid = validate(templateCollectionExample); + + // then + o(valid).equals(true)(JSON.stringify(validate.errors, null, 2)); + }); + + o("Collection without mandatory 'y' field in the 'template:xyz object should fail validation", async () => { + // given + const templateCollectionExample = JSON.parse(await fs.readFile(exampleCollectionPath)); + delete templateCollectionExample['assets']['example']['template:xyz']['y']; + + // when + let valid = validate(templateCollectionExample); + + // then + o(valid).equals(false); + o(validate.errors.some((error) => error.message === "should have required property 'y'")).equals(true)( + JSON.stringify(validate.errors), + ); + }); +}); diff --git a/extensions/template/tests/template_item.test.js b/extensions/template/tests/template_item.test.js new file mode 100644 index 00000000..cbd4e0b0 --- /dev/null +++ b/extensions/template/tests/template_item.test.js @@ -0,0 +1,46 @@ +import o from 'ospec'; +import Ajv from 'ajv'; +import { dirname, join } from 'path'; +import { fileURLToPath } from 'url'; +import { promises as fs } from 'fs'; +import { AjvOptions } from '../../validation.js'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const schemaPath = join(__dirname, '..', 'schema.json'); +const exampleItemPath = join(__dirname, '..', 'examples/item.json'); + +o.spec('template-item', () => { + o.specTimeout(20000); + let validate; + const ajv = new Ajv(AjvOptions); + + o.before(async () => { + const data = JSON.parse(await fs.readFile(schemaPath)); + validate = await ajv.compileAsync(data); + }); + + o('Item should pass validation', async () => { + // given + const templateItemExample = JSON.parse(await fs.readFile(exampleItemPath)); + + // when + let valid = validate(templateItemExample); + + // then + o(valid).equals(true)(JSON.stringify(validate.errors, null, 2)); + }); + + o("Item without mandatory 'template:new_field' property should fail validation", async () => { + // given + const templateItemExample = JSON.parse(await fs.readFile(exampleItemPath)); + delete templateItemExample.properties['template:new_field']; + // when + let valid = validate(templateItemExample); + + // then + o(valid).equals(false); + o( + validate.errors.some((error) => error.message === "should have required property '['template:new_field']'"), + ).equals(true)(JSON.stringify(validate.errors)); + }); +});