Adding tests to convertion in swagger 20

This commit is contained in:
Erik Mendoza
2022-04-14 14:33:56 -05:00
parent 6078a961f9
commit 0f213a5168
15 changed files with 229 additions and 16 deletions

View File

@@ -1,5 +1,4 @@
const Swagger2OpenAPI = require('swagger2openapi'), const Swagger2OpenAPI = require('swagger2openapi');
_ = require('lodash');
module.exports = { module.exports = {
convertSwaggerToOpenapi: function(parsedSwagger) { convertSwaggerToOpenapi: function(parsedSwagger) {

View File

@@ -5,7 +5,9 @@ var expect = require('chai').expect,
_ = require('lodash'), _ = require('lodash'),
async = require('async'), async = require('async'),
VALID_OPENAPI_PATH = '../data/valid_openapi', VALID_OPENAPI_PATH = '../data/valid_openapi',
INVALID_OPENAPI_PATH = '../data/invalid_openapi'; INVALID_OPENAPI_PATH = '../data/invalid_openapi',
SWAGGER_20_FOLDER_YAML = '../data/valid_swagger/yaml/',
SWAGGER_20_FOLDER_JSON = '../data/valid_swagger/json/';
describe('CONVERT FUNCTION TESTS ', function() { describe('CONVERT FUNCTION TESTS ', function() {
// these two covers remaining part of util.js // these two covers remaining part of util.js
@@ -1094,6 +1096,122 @@ describe('CONVERT FUNCTION TESTS ', function() {
done(); done();
}); });
}); });
describe('Converting swagger 2.0 files', function() {
it('should convert path paramters to postman-compatible paramters', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger2-with-params.json'),
input = {
type: 'file',
data: fileData
};
Converter.convert(input, {}, function(err, convertResult) {
expect(err).to.be.null;
// Make sure that path params are updated and their respective default values
convertResult.output.forEach(function(element) {
expect(element.type).to.equal('collection');
expect(element.data.item[0].request.url.path.indexOf(':ownerId') > -1).to.equal(true);
expect(element.data.item[0].request.url.path.indexOf(':petId') > -1).to.equal(true);
let thisVar = element.data.item[0].request.url.variable[0];
expect(thisVar.type).to.equal('any');
expect(thisVar.value).to.equal('42');
expect(thisVar.key).to.equal('ownerId');
});
done();
});
});
it('Should convert a swagger document with YAML anchors', function(done) {
const fileData = fs.readFileSync(path.join(__dirname, SWAGGER_20_FOLDER_YAML, 'yaml_anchor.yaml'), 'utf8'),
input = {
type: 'string',
data: fileData
};
Converter.convert(input, {}, (error, result) => {
expect(error).to.be.null;
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.have.equal('collection');
expect(result.output[0].data).to.have.property('info');
expect(result.output[0].data).to.have.property('item');
});
done();
});
it('must read values consumes', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger_aws_2.json'),
input = {
type: 'file',
data: fileData
};
Converter.convert(input, { requestName: 'url' }, (err, convertResult) => {
expect(err).to.be.null;
// Make sure that consumes and produces are processed
convertResult.output.forEach(function(element) {
expect(element.type).to.equal('collection');
expect(JSON.stringify(element.data.item[0].request.header[0])).to
.equal('{"key":"Content-Type","value":"application/json"}');
});
done();
});
});
it('should convert a swagger object which only have a root path.', function(done) {
const fileData = JSON.parse(
fs.readFileSync(path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger3.json'), 'utf8')
),
input = {
type: 'json',
data: fileData
};
Converter.convert(input, {}, (err, result) => {
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.have.equal('collection');
expect(result.output[0].data).to.have.property('info');
expect(result.output[0].data).to.have.property('item');
done();
});
});
it('should name the requests based on requestNameSource parameter, value=`URL`', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger3.json'),
input = {
type: 'file',
data: fileData
};
Converter.convert(input, { requestNameSource: 'URL' }, (err, convertResult) => {
let request = convertResult.output[0].data.item[0].request;
expect(err).to.be.null;
expect(request.name).to.equal('{{baseUrl}}/');
done();
});
});
it('should name the requests based on requestNameSource parameter, value=`Fallback`', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger3.json'),
input = {
type: 'file',
data: fileData
};
Converter.convert(input, { requestNameSource: 'Fallback' }, (err, convertResult) => {
let request = convertResult.output[0].data.item[0].request;
expect(err).to.be.null;
expect(request.name).to.equal('List API versions');
done();
});
});
});
}); });
describe('requestNameSource option', function() { describe('requestNameSource option', function() {

View File

@@ -1,40 +1,40 @@
const { expect } = require('chai'), const { expect } = require('chai'),
concreteUtils = require('../../../lib/swaggerUtils/schemaUtilsSwagger'), concreteUtils = require('../../../lib/swaggerUtils/schemaUtilsSwagger'),
fs = require('fs'), fs = require('fs'),
valid31xFolder = './test/data/valid_swagger', validSwaggerFolder = './test/data/valid_swagger',
invalid31xFolder = './test/data/invalid_swagger'; invalidSwaggerFolder = './test/data/invalid_swagger';
describe('parseSpec method', function () { describe('parseSpec method', function () {
it('should return true and a parsed specification', function () { it('should return true and a parsed specification', function () {
let fileContent = fs.readFileSync(valid31xFolder + '/sampleswagger.json', 'utf8'); let fileContent = fs.readFileSync(validSwaggerFolder + '/json/sampleswagger.json', 'utf8');
const parsedSpec = concreteUtils.parseSpec(fileContent, {}); const parsedSpec = concreteUtils.parseSpec(fileContent, {});
expect(parsedSpec.result).to.be.true; expect(parsedSpec.result).to.be.true;
expect(parsedSpec.openapi.swagger).to.equal('2.0'); expect(parsedSpec.openapi.swagger).to.equal('2.0');
}); });
it('should return false and info must have a title message', function () { it('should return false and info must have a title message', function () {
let fileContent = fs.readFileSync(invalid31xFolder + '/invalid_no_info_title.json', 'utf8'); let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_no_info_title.json', 'utf8');
const parsedSpec = concreteUtils.parseSpec(fileContent, {}); const parsedSpec = concreteUtils.parseSpec(fileContent, {});
expect(parsedSpec.result).to.be.false; expect(parsedSpec.result).to.be.false;
expect(parsedSpec.reason).to.equal('The info property must have title and version defined'); expect(parsedSpec.reason).to.equal('The info property must have title and version defined');
}); });
it('should return false and swagger must have info object message', function () { it('should return false and swagger must have info object message', function () {
let fileContent = fs.readFileSync(invalid31xFolder + '/invalid_no_info.json', 'utf8'); let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_no_info.json', 'utf8');
const parsedSpec = concreteUtils.parseSpec(fileContent, {}); const parsedSpec = concreteUtils.parseSpec(fileContent, {});
expect(parsedSpec.result).to.be.false; expect(parsedSpec.result).to.be.false;
expect(parsedSpec.reason).to.equal('The Swagger object must have an \"info\" property'); expect(parsedSpec.reason).to.equal('The Swagger object must have an \"info\" property');
}); });
it('should return false and invalid version message', function () { it('should return false and invalid version message', function () {
let fileContent = fs.readFileSync(invalid31xFolder + '/invalid_wrong_swagger_version.json', 'utf8'); let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_wrong_swagger_version.json', 'utf8');
const parsedSpec = concreteUtils.parseSpec(fileContent, {}); const parsedSpec = concreteUtils.parseSpec(fileContent, {});
expect(parsedSpec.result).to.be.false; expect(parsedSpec.result).to.be.false;
expect(parsedSpec.reason).to.equal('The Swagger object must have the \"swagger\" property set to 2.0'); expect(parsedSpec.reason).to.equal('The Swagger object must have the \"swagger\" property set to 2.0');
}); });
it('should return false and no paths message', function () { it('should return false and no paths message', function () {
let fileContent = fs.readFileSync(invalid31xFolder + '/invalid_no_paths.json', 'utf8'); let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_no_paths.json', 'utf8');
const parsedSpec = concreteUtils.parseSpec(fileContent, {}); const parsedSpec = concreteUtils.parseSpec(fileContent, {});
expect(parsedSpec.result).to.be.false; expect(parsedSpec.result).to.be.false;
expect(parsedSpec.reason).to.equal('The Swagger object must have a "paths" property'); expect(parsedSpec.reason).to.equal('The Swagger object must have a "paths" property');

View File

@@ -2,11 +2,12 @@ const SchemaPack = require('../..').SchemaPack,
expect = require('chai').expect, expect = require('chai').expect,
fs = require('fs'), fs = require('fs'),
path = require('path'), path = require('path'),
SWAGGER_20_FOLDER = '../data/valid_swagger'; SWAGGER_20_FOLDER_YAML = '../data/valid_swagger/yaml/',
SWAGGER_20_FOLDER_JSON = '../data/valid_swagger/json/';
describe('SchemaPack instance creation', function() { describe('SchemaPack instance creation', function() {
it('Should create an instance of SchemaPack when input is a string', function() { it('Should create an instance of SchemaPack when input is a string', function() {
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER + '/sampleswagger.json'), const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON + '/sampleswagger.json'),
fileData = fs.readFileSync(fileSource, 'utf8'), fileData = fs.readFileSync(fileSource, 'utf8'),
input = { input = {
type: 'string', type: 'string',
@@ -17,7 +18,7 @@ describe('SchemaPack instance creation', function() {
}); });
it('Should create an instance of SchemaPack when input is a file', function() { it('Should create an instance of SchemaPack when input is a file', function() {
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER + '/sampleswagger.json'), const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON + '/sampleswagger.json'),
input = { input = {
type: 'file', type: 'file',
data: fileSource data: fileSource
@@ -29,7 +30,7 @@ describe('SchemaPack instance creation', function() {
describe('getMetaData method', function() { describe('getMetaData method', function() {
it('Should return the provided input metadata', function() { it('Should return the provided input metadata', function() {
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER + '/sampleswagger.json'), const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON + 'sampleswagger.json'),
input = { input = {
type: 'file', type: 'file',
data: fileSource data: fileSource
@@ -44,8 +45,8 @@ describe('getMetaData method', function() {
}); });
describe('Convert method', function() { describe('Convert method', function() {
it('Should convert a basic example', function() { it('Should convert an example file from: ', function(done) {
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER + '/sampleswagger.json'), const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'sampleswagger.json'),
fileData = fs.readFileSync(fileSource, 'utf8'), fileData = fs.readFileSync(fileSource, 'utf8'),
input = { input = {
type: 'string', type: 'string',
@@ -54,6 +55,101 @@ describe('Convert method', function() {
schemapack = new SchemaPack(input); schemapack = new SchemaPack(input);
schemapack.convert((error, result) => { schemapack.convert((error, result) => {
expect(error).to.be.null; expect(error).to.be.null;
expect(result.result).to.be.true;
});
done();
});
it('Should convert a swagger document with YAML anchors', function(done) {
const fileData = fs.readFileSync(path.join(__dirname, SWAGGER_20_FOLDER_YAML, 'yaml_anchor.yaml'), 'utf8'),
input = {
type: 'string',
data: fileData
},
schemapack = new SchemaPack(input);
schemapack.convert((error, result) => {
expect(error).to.be.null;
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.have.equal('collection');
expect(result.output[0].data).to.have.property('info');
expect(result.output[0].data).to.have.property('item');
});
done();
});
it('must read values consumes', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger_aws_2.json'),
input = {
type: 'file',
data: fileData
},
schemapack = new SchemaPack(input, { requestName: 'url' });
schemapack.convert((err, convertResult) => {
expect(err).to.be.null;
// Make sure that consumes and produces are processed
convertResult.output.forEach(function(element) {
expect(element.type).to.equal('collection');
expect(JSON.stringify(element.data.item[0].request.header[0])).to
.equal('{"key":"Content-Type","value":"application/json"}');
});
done();
});
});
it('should convert a swagger object which only have a root path.', function(done) {
const fileData = JSON.parse(
fs.readFileSync(path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger3.json'), 'utf8')
),
input = {
type: 'json',
data: fileData
},
schemapack = new SchemaPack(input, {});
schemapack.convert((err, result) => {
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.have.equal('collection');
expect(result.output[0].data).to.have.property('info');
expect(result.output[0].data).to.have.property('item');
done();
});
});
it('should name the requests based on requestNameSource parameter, value=`URL`', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger3.json'),
input = {
type: 'file',
data: fileData
},
schemapack = new SchemaPack(input, { requestNameSource: 'URL' });
schemapack.convert((err, convertResult) => {
let request = convertResult.output[0].data.item[0].request;
expect(err).to.be.null;
expect(request.name).to.equal('{{baseUrl}}/');
done();
});
});
it('should name the requests based on requestNameSource parameter, value=`Fallback`', function (done) {
const fileData = path.join(__dirname, SWAGGER_20_FOLDER_JSON, 'swagger3.json'),
input = {
type: 'file',
data: fileData
},
schemapack = new SchemaPack(input, { requestNameSource: 'Fallback' });
schemapack.convert((err, convertResult) => {
let request = convertResult.output[0].data.item[0].request;
expect(err).to.be.null;
expect(request.name).to.equal('List API versions');
done();
}); });
}); });
}); });