From d7d21069ca234a866b4e6a1205f3153ad5918e89 Mon Sep 17 00:00:00 2001 From: Pavel Bodiachevskii Date: Thu, 23 May 2024 22:08:19 +0400 Subject: [PATCH] test(definitions): add example test https://github.com/asyncapi/spec-json-schemas/issues/539 --- .../3.0.0/models/info/contact/contact.js | 12 +++++-- .../3.0.0/models/info/contact/example.json | 5 +++ test/definitions/3.0.0/models/info/index.js | 4 +-- .../models/info/info extensions/example.json | 4 +++ .../info/info extensions/info-extension.js | 12 +++++-- .../3.0.0/models/info/{ => info}/empty.json | 0 .../3.0.0/models/info/info/example.json | 29 +++++++++++++++ .../models/info/{ => info}/extended.json | 0 .../3.0.0/models/info/{ => info}/info.js | 36 +++++++++++-------- .../{ => info}/only required properties.json | 0 .../without required properties.json | 0 .../info/{ => info}/wrongly extended.json | 0 .../3.0.0/models/info/license/example.json | 4 +++ .../3.0.0/models/info/license/license.js | 8 +++++ .../3.0.0/models/reference/example.json | 3 ++ .../3.0.0/models/reference/reference.js | 8 +++++ .../definitions/3.0.0/models/tag/example.json | 8 +++++ test/definitions/3.0.0/models/tag/tag.js | 8 +++++ 18 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 test/definitions/3.0.0/models/info/contact/example.json create mode 100644 test/definitions/3.0.0/models/info/info extensions/example.json rename test/definitions/3.0.0/models/info/{ => info}/empty.json (100%) create mode 100644 test/definitions/3.0.0/models/info/info/example.json rename test/definitions/3.0.0/models/info/{ => info}/extended.json (100%) rename test/definitions/3.0.0/models/info/{ => info}/info.js (64%) rename test/definitions/3.0.0/models/info/{ => info}/only required properties.json (100%) rename test/definitions/3.0.0/models/info/{ => info}/without required properties.json (100%) rename test/definitions/3.0.0/models/info/{ => info}/wrongly extended.json (100%) create mode 100644 test/definitions/3.0.0/models/info/license/example.json create mode 100644 test/definitions/3.0.0/models/reference/example.json create mode 100644 test/definitions/3.0.0/models/tag/example.json diff --git a/test/definitions/3.0.0/models/info/contact/contact.js b/test/definitions/3.0.0/models/info/contact/contact.js index ea730c6e..1eb5cf32 100644 --- a/test/definitions/3.0.0/models/info/contact/contact.js +++ b/test/definitions/3.0.0/models/info/contact/contact.js @@ -13,13 +13,21 @@ const ajv = new Ajv({ }); addFormats(ajv); -const infoJsonSchema = require('../../../../../../definitions/3.0.0/contact.json'); +const jsonSchemaName = 'Contact'; +const jsonSchema = require('../../../../../../definitions/3.0.0/contact.json'); const validator = ajv .addMetaSchema(require('../../../../../../definitions/3.0.0/schema.json')) .addSchema(require('../../../../../../definitions/3.0.0/specificationExtension.json')) - .compile(infoJsonSchema); + .compile(jsonSchema); describe('Contact', () => { + it('example', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, `${jsonSchemaName} example MUST be valid`); + }); + it('empty', () => { const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); const validationResult = validator(info); diff --git a/test/definitions/3.0.0/models/info/contact/example.json b/test/definitions/3.0.0/models/info/contact/example.json new file mode 100644 index 00000000..31cd246a --- /dev/null +++ b/test/definitions/3.0.0/models/info/contact/example.json @@ -0,0 +1,5 @@ +{ + "name": "AsyncApi", + "url": "https://www.asyncapi.com", + "email": "java@asyncapi.com" +} diff --git a/test/definitions/3.0.0/models/info/index.js b/test/definitions/3.0.0/models/info/index.js index d337805d..bbb6a641 100644 --- a/test/definitions/3.0.0/models/info/index.js +++ b/test/definitions/3.0.0/models/info/index.js @@ -1,5 +1,5 @@ -describe('Info Object', () => { - require('./info.js'); +describe('Info Test Suite', () => { + require('./info/info.js'); require('./info extensions/info-extension.js'); require('./contact/contact.js'); }); diff --git a/test/definitions/3.0.0/models/info/info extensions/example.json b/test/definitions/3.0.0/models/info/info extensions/example.json new file mode 100644 index 00000000..331d9f15 --- /dev/null +++ b/test/definitions/3.0.0/models/info/info extensions/example.json @@ -0,0 +1,4 @@ +{ + "x-x": "PBodiachevskii", + "x-linkedin": "https://www.linkedin.com/company/asyncapi/" +} diff --git a/test/definitions/3.0.0/models/info/info extensions/info-extension.js b/test/definitions/3.0.0/models/info/info extensions/info-extension.js index dc4f4a34..d39c2205 100644 --- a/test/definitions/3.0.0/models/info/info extensions/info-extension.js +++ b/test/definitions/3.0.0/models/info/info extensions/info-extension.js @@ -13,14 +13,22 @@ const ajv = new Ajv({ }); addFormats(ajv); -const infoJsonSchema = require('../../../../../../definitions/3.0.0/infoExtensions.json'); +const jsonSchemaName = 'Info Extensions'; +const jsonSchema = require('../../../../../../definitions/3.0.0/infoExtensions.json'); const validator = ajv .addMetaSchema(require('../../../../../../definitions/3.0.0/schema.json')) .addSchema(require('../../../../../../extensions/linkedin/0.1.0/schema.json')) .addSchema(require('../../../../../../extensions/x/0.1.0/schema.json')) - .compile(infoJsonSchema); + .compile(jsonSchema); describe('InfoExtensions', () => { + it('example', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, `${jsonSchemaName} example MUST be valid`); + }); + it('empty', () => { const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); const validationResult = validator(info); diff --git a/test/definitions/3.0.0/models/info/empty.json b/test/definitions/3.0.0/models/info/info/empty.json similarity index 100% rename from test/definitions/3.0.0/models/info/empty.json rename to test/definitions/3.0.0/models/info/info/empty.json diff --git a/test/definitions/3.0.0/models/info/info/example.json b/test/definitions/3.0.0/models/info/info/example.json new file mode 100644 index 00000000..8231edb9 --- /dev/null +++ b/test/definitions/3.0.0/models/info/info/example.json @@ -0,0 +1,29 @@ +{ + "title": "AsyncApi sample", + "version": "2.0", + "description": "short description", + "termsOfService": "https://www.asyncapi.com/about/", + "contact": { + "name": "AsyncApi", + "url": "https://www.asyncapi.com", + "email": "java@asyncapi.com" + }, + "license": { + "name": "Apache License 2.0", + "url": "http://www.apache.org/licenses/" + }, + "tags": [ + { + "name": "user", + "description": "User-related messages", + "externalDocs": { + "description" : "Find more info here", + "url" : "https://example.com" + } + } + ], + "externalDocs": { + "description": "Find more info here", + "url": "https://example.com" + } +} diff --git a/test/definitions/3.0.0/models/info/extended.json b/test/definitions/3.0.0/models/info/info/extended.json similarity index 100% rename from test/definitions/3.0.0/models/info/extended.json rename to test/definitions/3.0.0/models/info/info/extended.json diff --git a/test/definitions/3.0.0/models/info/info.js b/test/definitions/3.0.0/models/info/info/info.js similarity index 64% rename from test/definitions/3.0.0/models/info/info.js rename to test/definitions/3.0.0/models/info/info/info.js index 3bfcef46..f03cbed7 100644 --- a/test/definitions/3.0.0/models/info/info.js +++ b/test/definitions/3.0.0/models/info/info/info.js @@ -13,23 +13,31 @@ const ajv = new Ajv({ }); addFormats(ajv); -const infoJsonSchema = require('../../../../../definitions/3.0.0/info.json'); +const jsonSchemaName = 'Info'; +const jsonSchema = require('../../../../../../definitions/3.0.0/info.json'); const validator = ajv - .addMetaSchema(require('../../../../../definitions/3.0.0/schema.json')) - .addSchema(require('../../../../../definitions/3.0.0/infoExtensions.json')) - .addSchema(require('../../../../../definitions/3.0.0/contact.json')) - .addSchema(require('../../../../../definitions/3.0.0/license.json')) - .addSchema(require('../../../../../examples/3.0.0/ReferenceObject.json')) - .addSchema(require('../../../../../definitions/3.0.0/ReferenceObject.json')) - .addSchema(require('../../../../../definitions/3.0.0/Reference.json')) - .addSchema(require('../../../../../definitions/3.0.0/tag.json')) - .addSchema(require('../../../../../definitions/3.0.0/externalDocs.json')) - .addSchema(require('../../../../../definitions/3.0.0/specificationExtension.json')) - .addSchema(require('../../../../../extensions/linkedin/0.1.0/schema.json')) - .addSchema(require('../../../../../extensions/x/0.1.0/schema.json')) - .compile(infoJsonSchema); + .addMetaSchema(require('../../../../../../definitions/3.0.0/schema.json')) + .addSchema(require('../../../../../../definitions/3.0.0/infoExtensions.json')) + .addSchema(require('../../../../../../definitions/3.0.0/contact.json')) + .addSchema(require('../../../../../../definitions/3.0.0/license.json')) + .addSchema(require('../../../../../../examples/3.0.0/ReferenceObject.json')) + .addSchema(require('../../../../../../definitions/3.0.0/ReferenceObject.json')) + .addSchema(require('../../../../../../definitions/3.0.0/Reference.json')) + .addSchema(require('../../../../../../definitions/3.0.0/tag.json')) + .addSchema(require('../../../../../../definitions/3.0.0/externalDocs.json')) + .addSchema(require('../../../../../../definitions/3.0.0/specificationExtension.json')) + .addSchema(require('../../../../../../extensions/linkedin/0.1.0/schema.json')) + .addSchema(require('../../../../../../extensions/x/0.1.0/schema.json')) + .compile(jsonSchema); describe('Info', () => { + it('example', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, `${jsonSchemaName} example MUST be valid`); + }); + it('empty', () => { const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); const validationResult = validator(info); diff --git a/test/definitions/3.0.0/models/info/only required properties.json b/test/definitions/3.0.0/models/info/info/only required properties.json similarity index 100% rename from test/definitions/3.0.0/models/info/only required properties.json rename to test/definitions/3.0.0/models/info/info/only required properties.json diff --git a/test/definitions/3.0.0/models/info/without required properties.json b/test/definitions/3.0.0/models/info/info/without required properties.json similarity index 100% rename from test/definitions/3.0.0/models/info/without required properties.json rename to test/definitions/3.0.0/models/info/info/without required properties.json diff --git a/test/definitions/3.0.0/models/info/wrongly extended.json b/test/definitions/3.0.0/models/info/info/wrongly extended.json similarity index 100% rename from test/definitions/3.0.0/models/info/wrongly extended.json rename to test/definitions/3.0.0/models/info/info/wrongly extended.json diff --git a/test/definitions/3.0.0/models/info/license/example.json b/test/definitions/3.0.0/models/info/license/example.json new file mode 100644 index 00000000..61436164 --- /dev/null +++ b/test/definitions/3.0.0/models/info/license/example.json @@ -0,0 +1,4 @@ +{ + "name": "Apache License 2.0", + "url": "http://www.apache.org/licenses/" +} diff --git a/test/definitions/3.0.0/models/info/license/license.js b/test/definitions/3.0.0/models/info/license/license.js index 9493f08c..57302d2e 100644 --- a/test/definitions/3.0.0/models/info/license/license.js +++ b/test/definitions/3.0.0/models/info/license/license.js @@ -13,6 +13,7 @@ const ajv = new Ajv({ }); addFormats(ajv); +const jsonSchemaName = 'License'; const infoJsonSchema = require('../../../../../../definitions/3.0.0/license.json'); const validator = ajv .addMetaSchema(require('../../../../../../definitions/3.0.0/schema.json')) @@ -20,6 +21,13 @@ const validator = ajv .compile(infoJsonSchema); describe('License', () => { + it('example', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, `${jsonSchemaName} example MUST be valid`); + }); + it('empty', () => { const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); const validationResult = validator(info); diff --git a/test/definitions/3.0.0/models/reference/example.json b/test/definitions/3.0.0/models/reference/example.json new file mode 100644 index 00000000..fd5bb0d1 --- /dev/null +++ b/test/definitions/3.0.0/models/reference/example.json @@ -0,0 +1,3 @@ +{ + "$ref": "#/components/schemas/user" +} diff --git a/test/definitions/3.0.0/models/reference/reference.js b/test/definitions/3.0.0/models/reference/reference.js index 5823f39e..2610f67e 100644 --- a/test/definitions/3.0.0/models/reference/reference.js +++ b/test/definitions/3.0.0/models/reference/reference.js @@ -13,6 +13,7 @@ const ajv = new Ajv({ }); addFormats(ajv); +const jsonSchemaName = 'Reference'; const infoJsonSchema = require('../../../../../definitions/3.0.0/Reference.json'); const validator = ajv .addMetaSchema(require('../../../../../definitions/3.0.0/schema.json')) @@ -20,6 +21,13 @@ const validator = ajv .compile(infoJsonSchema); describe('Reference', () => { + it('example', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, `${jsonSchemaName} example MUST be valid`); + }); + it('empty', () => { const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); const validationResult = validator(info); diff --git a/test/definitions/3.0.0/models/tag/example.json b/test/definitions/3.0.0/models/tag/example.json new file mode 100644 index 00000000..aa06e030 --- /dev/null +++ b/test/definitions/3.0.0/models/tag/example.json @@ -0,0 +1,8 @@ +{ + "name": "user", + "description": "User-related messages", + "externalDocs": { + "description" : "Find more info here", + "url" : "https://example.com" + } +} diff --git a/test/definitions/3.0.0/models/tag/tag.js b/test/definitions/3.0.0/models/tag/tag.js index acde4320..e55ead00 100644 --- a/test/definitions/3.0.0/models/tag/tag.js +++ b/test/definitions/3.0.0/models/tag/tag.js @@ -13,6 +13,7 @@ const ajv = new Ajv({ }); addFormats(ajv); +const jsonSchemaName = 'Tag'; const infoJsonSchema = require('../../../../../definitions/3.0.0/tag.json'); const validator = ajv .addMetaSchema(require('../../../../../definitions/3.0.0/schema.json')) @@ -23,6 +24,13 @@ const validator = ajv .compile(infoJsonSchema); describe('Reference', () => { + it('example', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, `${jsonSchemaName} example MUST be valid`); + }); + it('empty', () => { const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); const validationResult = validator(info);