change getMetaData function to be async and add tests

This commit is contained in:
umeshp7
2020-04-27 20:49:35 +05:30
parent babbddd331
commit 1e344ae7ba
3 changed files with 103 additions and 23 deletions

View File

@@ -18,13 +18,9 @@ module.exports = {
return schema.validationResult;
},
getMetaData: function (input) {
getMetaData: function (input, cb) {
var schema = new SchemaPack(input);
if (schema.metaData) {
return schema.metaData;
}
return schema.validationResult;
schema.getMetaData(cb);
},
mergeAndValidate: function (input, cb) {

View File

@@ -151,30 +151,51 @@ class SchemaPack {
}
this.openapi = specParseResult.openapi;
this.metaData = this.getMetaData(this.openapi);
this.validated = true;
this.validationResult = {
result: true,
meta: this.metaData
result: true
};
return this.validationResult;
}
getMetaData (openapi) {
if (!openapi) {
return {
result: false,
reason: 'Empty collection'
};
}
getMetaData (cb) {
let input = this.input;
return {
result: true,
output: [{
type: 'collection',
name: _.get(openapi, 'info.title', COLLECTION_NAME)
}]
};
// Return validation result if the schema is not validated.
if (input.type !== 'folder' && !this.validated) {
return cb(null, this.validationResult);
}
// if the schema is validated, return the meta data as required.
else if (this.validated) {
return cb(null, {
result: true,
name: _.get(this.openapi, 'info.title', COLLECTION_NAME),
output: [{
type: 'collection',
name: _.get(this.openapi, 'info.title', COLLECTION_NAME)
}]
});
}
else if (input.type === 'folder') {
this.mergeAndValidate((err, validationResult) => {
if (err) {
return cb(err);
}
if (!validationResult.result) {
return cb(null, validationResult);
}
return cb(null, {
result: true,
name: _.get(this.openapi, 'info.title', COLLECTION_NAME),
output: [{
type: 'collection',
name: _.get(this.openapi, 'info.title', COLLECTION_NAME)
}]
});
});
}
}
mergeAndValidate (cb) {

View File

@@ -377,6 +377,69 @@ describe('CONVERT FUNCTION TESTS ', function() {
});
});
it('Should return meta data from folder having one root file JSON', function(done) {
let folderPath = path.join(__dirname, '../data/petstore-separate'),
array = [
{ fileName: folderPath + '/common/Error.json' },
{ fileName: folderPath + '/spec/Pet.json' },
{ fileName: folderPath + '/spec/NewPet.json' },
{ fileName: folderPath + '/spec/parameters.json' },
{ fileName: folderPath + '/spec/swagger.json' }
];
Converter.getMetaData({ type: 'folder', data: array }, (err, status) => {
if (err) {
expect.fail(null, null, err);
}
if (status.result) {
expect(status.result).to.be.eq(true);
expect(status.name).to.be.equal('Swagger Petstore');
expect(status.output[0].name).to.be.equal('Swagger Petstore');
expect(status.output[0].type).to.be.equal('collection');
done();
}
else {
expect.fail(null, null, status.reason);
done();
}
});
});
it('Should return meta data from a valid file', function(done) {
var openapi = fs.readFileSync(testSpec, 'utf8');
Converter.getMetaData({ type: 'json', data: openapi }, (err, status) => {
if (err) {
expect.fail(null, null, err);
}
if (status.result) {
expect(status.result).to.be.eq(true);
expect(status.name).to.be.equal('Swagger Petstore');
expect(status.output[0].name).to.be.equal('Swagger Petstore');
expect(status.output[0].type).to.be.equal('collection');
done();
}
else {
expect.fail(null, null, status.reason);
done();
}
});
});
it('Should return validation result for an invalid file', function(done) {
var invalidNoInfo = path.join(__dirname, INVALID_OPENAPI_PATH + '/invalid-no-info.yaml'),
openapi = fs.readFileSync(invalidNoInfo, 'utf8');
Converter.getMetaData({ type: 'json', data: openapi }, (err, status) => {
if (err) {
expect.fail(null, null, err);
}
if (!status.result) {
expect(status.result).to.be.eq(false);
done();
}
else {
expect.fail(null, null, status.reason);
done();
}
});
});
it('Should return error for more than one root files', function(done) {
let folderPath = path.join(__dirname, '../data/multiFile_with_two_root'),
array = [