Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
subeeshcbabu committed Jun 17, 2016
1 parent 873a3c3 commit 5829f39
Show file tree
Hide file tree
Showing 10 changed files with 1,352 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"rules": {
"indent": [
2,
4
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
},
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended"
}
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.orig

work
build
pids
logs
results
coverage
lib-cov
html-report
xunit.xml
node_modules
npm-debug.log

.project
.idea
.settings
.iml
*.sublime-workspace
*.sublime-project

.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
.AppleDouble
.LSOverride
.Spotlight-V100
.Trashes

test/temp
51 changes: 51 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Automatically ignored per:
# https://www.npmjs.org/doc/developers.html#Keeping-files-out-of-your-package
#
# .*.swp
# ._*
# .DS_Store
# .git
# .hg
# .lock-wscript
# .svn
# .wafpickle-*
# CVS
# npm-debug.log
# node_modules

*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.orig

work
build
test
pids
logs
results
coverage
lib-cov
html-report
xunit.xml

.project
.idea
.settings
.iml
*.sublime-workspace
*.sublime-project

ehthumbs.db
Icon?
Thumbs.db
.AppleDouble
.LSOverride
.Spotlight-V100
.Trashes

test/temp
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# swagmock
Mock data generator for swagger api

~THIS MODULE IS UNDER ACTIVE DEVELOPMENT~
15 changes: 15 additions & 0 deletions lib/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
var Moment = require('moment');

module.exports = {
'date': dateMock,
'date-time': dataTimeMock
};

function dateMock() {
return Moment().format('YYYY-MM-DD');
}

function dataTimeMock() {
return Moment().toISOString();
}
139 changes: 139 additions & 0 deletions lib/generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'use strict';
var Chance = require('chance').Chance();
var Format = require('./format');

var Generators = module.exports = {
object: objectMock,
array: arrayMock,
string: stringMock,
integer: integerMock,
number: numberMock,
boolean: booleanMock,
mock: mock
};

function mock(schema) {
var mock;
if (schema) {
var type = schema.type;
var example = schema.examples || schema.example;
/**
* Use examples as Mock if provided
*/
if (example) {
mock = example;
} else if (type) {
/**
* Get the mock generator from the `type` of the schema
*/
var generator = Generators[type];
if (generator) {
mock = generator.call(null, schema);
}
}
}
return mock;
}

function objectMock(schema) {
var mockObj = {};
var props = schema.properties;
if (props) {
Object.keys(props).forEach(function (key) {
mockObj[key] = mock(props[key]);
});
/**
* In the absense of `properties`, check if `additionalProperties` is defined or not.
* (If additionalProperties is an object, that object is a schema that will be used to validate
* any additional properties not listed in properties.)
*
* If present, use this to generate mocks.
*/
} else if (schema.additionalProperties) {
//Create a random property
mockObj[Chance.word()] = mock(schema.additionalProperties);
}
return mockObj;
}

function arrayMock(schema) {
var items = schema.items;
var min = 0;
var max = 1;
var arr = [];

if (items) {
if (schema.minItems) {
min = schema.minItems;
}
if (schema.maxItems) {
max = schema.maxItems;
}
for (; min <= max; min++) {
arr.push(mock(items));
}
}
return arr;
}

function integerMock(schema) {
var opts = {};
if (schema.minimum) {
opts.min = schema.minimum;
}
if (schema.maximum) {
opts.max = schema.maximum;
}
return Chance.integer(opts);
}

function numberMock(schema) {
var opts = {};
if (schema.minimum) {
opts.min = schema.minimum;
}
if (schema.maximum) {
opts.max = schema.maximum;
}
return Chance.floating(opts);
}

function booleanMock() {
return Chance.bool();
}

function stringMock(schema) {
var mockStr;
var opts = {};
var minLength = schema.minLength || 1;
var maxLength = schema.maxLength || minLength + 10;
opts.min = minLength;
opts.max = maxLength;

if (schema.enum && schema.enum.length > 0) {
/**
* If `enum` is defined for the property
*/
mockStr = enumMock(schema);
} else if(Format[schema.format]) {
/**
* If a `format` is defined for the property
*/
mockStr = Format[schema.format].call(null, schema);
} else {
mockStr = Chance.string({
length: Chance.integer(opts)
});
}

return mockStr;
}

function enumMock(schema) {
var len = schema.enum.length;
var opts = {
min: 0,
max: len - 1
};
return schema.enum[Chance.integer(opts)];
}
65 changes: 65 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';
var Parser = require('swagger-parser');
var Generators = require('./generators');

module.exports = function mockGen(apiPath, options, callback) {
options = options || {};

Parser.validate(apiPath, function (error, api) {
if (error) {
callback(error);
return;
}
callback(null, mockSchema(api, options));
});
};

function mockSchema(api, options) {
var mock = {};
var paths = api.paths;
if (paths) {
var pathObj = paths[options.path];
if (pathObj) {
//Found the requested path
var opsObj = pathObj[options.operation];
if (opsObj) {
//Found the operation
//Mock response
if (options.mockResponse) {
mock.response = mockResponse(opsObj, options);
}
//Mock Parameters
if (options.mockParams) {
mock.params = mockParameters(opsObj, options);
}
}
}
}
return mock;
}

function mockResponse(opsObj, options) {
var mockResp;
var response = opsObj.responses[options.response];
if (response) {
//Found the response
var schema = response.schema;
if (schema) {
mockResp = Generators.mock(schema);
}
}
return mockResp;
}

function mockParameters(opsObj, options) {
var mockParam;
var parameter = opsObj.parameters[options.parameter];
if (parameter) {
//Found the parameter
var schema = parameter.schema;
if (schema) {
mockParam = Generators.mock(schema);
}
}
return mockParam;
}
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "swagmock",
"version": "0.0.1",
"description": "Mock data generator for swagger api",
"main": "lib/index.js",
"scripts": {
"lint": "eslint lib",
"test": "mocha tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/subeeshcbabu/swagmock.git"
},
"keywords": [
"swagger",
"openapi",
"mock",
"data",
"generator",
"swaggermock"
],
"author": "subeesh chothendavida <subeeshcbabu@yahoo.co.in>",
"license": "MIT",
"bugs": {
"url": "https://github.com/subeeshcbabu/swagmock/issues"
},
"homepage": "https://github.com/subeeshcbabu/swagmock#readme",
"dependencies": {
"chance": "^1.0.3",
"moment": "^2.13.0",
"swagger-parser": "^3.4.1"
},
"devDependencies": {
"eslint": "^2.13.0",
"mocha": "^2.5.3"
}
}
Loading

0 comments on commit 5829f39

Please sign in to comment.