Skip to content

Commit

Permalink
test(definitions): license
Browse files Browse the repository at this point in the history
check License Json Schema

asyncapi#539
  • Loading branch information
Pakisan committed May 21, 2024
1 parent 4d4124a commit f13ea3e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/definitions/3.0.0/license/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/definitions/3.0.0/license/extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Apache License 2.0",
"url": "http://www.apache.org/licenses/",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
64 changes: 64 additions & 0 deletions test/definitions/3.0.0/license/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Ajv = require('ajv');
const assert = require('assert');
const addFormats = require('ajv-formats');
const fs = require('fs');

const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: '$id',
logger: false,
validateFormats: true,
strict: false,
});
addFormats(ajv);

const infoJsonSchema = require('../../../../definitions/3.0.0/license.json');
const validator = ajv
.addMetaSchema(require('../../../../definitions/3.0.0/schema.json'))
.addSchema(require('../../../../definitions/3.0.0/specificationExtension.json'))
.compile(infoJsonSchema);

describe('Contact', () => {
it('empty', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'License with empty body is not valid');
assert(validator.errors[0].message === 'must have required property \'name\'');
assert(validator.errors.length === 1);
});

it('without required properties', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/without required properties.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'License without required properties is not valid');
assert(validator.errors[0].message === 'must have required property \'name\'');
assert(validator.errors.length === 1);
});

it('only required properties', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/only required properties.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, 'License is valid with only required properties');
});

it('extended', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, 'License can be extended');
});

it('wrongly extended', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'License is not valid when was wrongly extended');
assert(validator.errors[0].message === 'must NOT have additional properties');
assert(validator.errors[0].params.additionalProperty === 'ext-number');
assert(validator.errors.length === 1);
});
});
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/license/only required properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Apache License 2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"url": "http://www.apache.org/licenses/",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
10 changes: 10 additions & 0 deletions test/definitions/3.0.0/license/wrongly extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Apache License 2.0",
"url": "http://www.apache.org/licenses/",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
},
"ext-number": 1
}
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const path = require('path');

describe('AsyncAPI: 3.0.0', () => {
require('./definitions/3.0.0/info/info.js');
require('./definitions/3.0.0/contact/contact.js')
require('./definitions/3.0.0/contact/contact.js');
require('./definitions/3.0.0/license/license.js');
});

describe('AsyncAPI', () => {
Expand Down

0 comments on commit f13ea3e

Please sign in to comment.