Skip to content

Commit

Permalink
feat: add methods and tests for serverVariables (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkocot authored Apr 21, 2022
1 parent 9d3538f commit 382ab29
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 45 deletions.
26 changes: 26 additions & 0 deletions lib/models/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ChannelParameter = require('./channel-parameter');
const CorrelationId = require('./correlation-id');
const OperationTrait = require('./operation-trait');
const MessageTrait = require('./message-trait');
const ServerVariable = require('./server-variable');

const MixinSpecificationExtensions = require('../mixins/specification-extensions');

Expand Down Expand Up @@ -219,6 +220,31 @@ class Components extends Base {
messageTrait(name) {
return getMapValueOfType(this._json.messageTraits, name, MessageTrait);
}

/**
*
* @returns {Object<string, ServerVariable>}
*/
serverVariables() {
return createMapOfType(this._json.serverVariables, ServerVariable);
}

/**
*
* @returns {boolean}
*/
hasServerVariables() {
return !!this._json.serverVariables;
}

/**
*
* @param {string} name - Name of the server variable.
* @returns {ServerVariable}
*/
serverVariable(name) {
return getMapValueOfType(this._json.serverVariables, name, ServerVariable);
}
}

module.exports = mix(Components, MixinSpecificationExtensions);
88 changes: 44 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.6",
"@asyncapi/specs": "v2.14.0-2022-04-release.1",
"@asyncapi/specs": "^v2.14.0-2022-04-release.2",
"@fmvilas/pseudo-yaml-ast": "^0.3.1",
"ajv": "^6.10.1",
"js-yaml": "^3.13.1",
Expand Down
51 changes: 51 additions & 0 deletions test/models/components_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,55 @@ describe('Components', function() {
assertMixinSpecificationExtensionsInheritance(Components);
});
});

describe('#serverVariables()', function() {
it('should return a map of ServerVariable objects', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(typeof d.serverVariables()).to.be.equal('object');
expect(d.serverVariables().test1.constructor.name).to.equal('ServerVariable');
expect(d.serverVariables().test1.json()).to.equal(doc.serverVariables.test1);
expect(d.serverVariables().test2.constructor.name).to.equal('ServerVariable');
expect(d.serverVariables().test2.json()).to.equal(doc.serverVariables.test2);
});

it('should return an empty object if the components field has no defined serverVariables', function() {
const doc = {};
const d = new Components(doc);
expect(typeof d.serverVariables()).to.be.equal('object');
expect(d.serverVariables()).to.deep.equal({});
});
});

describe('#hasServerVariables()', function() {
it('should return a boolean indicating if the components field has serverVariables', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const docNoSchemas = { servers: {} };
const d = new Components(doc);
const d2 = new Components(docNoSchemas);
expect(d.hasServerVariables()).to.equal(true);
expect(d2.hasServerVariables()).to.equal(false);
});
});

describe('#serverVariable()', function() {
it('should return a specific serverVariable object', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(d.serverVariable('test1').constructor.name).to.equal('ServerVariable');
expect(d.serverVariable('test1').json()).to.equal(doc.serverVariables.test1);
});

it('should return null if a serverVariable name is not provided', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(d.serverVariable()).to.equal(null);
});

it('should return null if a serverVariable name is not found', function() {
const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } };
const d = new Components(doc);
expect(d.serverVariable('not found')).to.equal(null);
});
});
});

0 comments on commit 382ab29

Please sign in to comment.