This commit is contained in:
Vishrut Shah 2017-04-17 16:33:10 -07:00
Родитель 3982aa7f03
Коммит 847f109948
4 изменённых файлов: 57 добавлений и 30 удалений

Просмотреть файл

@ -6,13 +6,18 @@
var validate = require('./lib/validate');
var utils = require('./lib/util/utils');
// Easy to use methods from validate.js
exports.getDocumentsFromCompositeSwagger = validate.getDocumentsFromCompositeSwagger;
exports.validateSpec = validate.validateSpec;
exports.validateCompositeSpec = validate.validateCompositeSpec;
exports.validateExamples = validate.validateExamples;
exports.validateExamplesInCompositeSpec = validate.validateExamplesInCompositeSpec;
exports.Validator = require('./lib/specValidator');
exports.log = require('./lib/util/logging');
exports.executePromisesSequentially = utils.executePromisesSequentially;
exports.resolveSpec = validate.resolveSpec;
exports.resolveCompositeSpec = validate.resolveCompositeSpec;
exports.resolveCompositeSpec = validate.resolveCompositeSpec;
// Classes
exports.Validator = require('./lib/validators/specValidator');
exports.LiveValidator = require('./lib/validators/liveValidator');
exports.SpecResolver = require('./lib/validators/specResolver');

Просмотреть файл

@ -21,8 +21,10 @@
*/
class PotentialOperationsResult {
constructor(operations, reason) {
this.operations = operations;
this.reason = reason;
this.operations = operations || [];
if (this.reason) {
this.reason = reason;
}
}
}

Просмотреть файл

@ -220,7 +220,7 @@ class LiveValidator {
let liveValidationError;
if (path === null || path === undefined) {
msg = `Could not find path from requestUrl: "${requestUrl}".`;
liveValidationError = models.LiveValidationError(Constants.ErrorCodes.PathNotFoundInRequestUrl, msg);
liveValidationError = new models.LiveValidationError(Constants.ErrorCodes.PathNotFoundInRequestUrl, msg);
result = new models.PotentialOperationsResult(potentialOperations, liveValidationError);
return result;
}
@ -342,6 +342,7 @@ class LiveValidator {
* @param {object} requestResponseObj - The wrapper that constains the live request and response
* @param {object} requestResponseObj.liveRequest - The live request
* @param {object} requestResponseObj.liveResponse - The live response
* @returns {object} validationResult - Validation result for given input
*/
validateLiveRequestResponse(requestResponseObj) {
let self = this;
@ -362,6 +363,8 @@ class LiveValidator {
return validationResult;
}
try {
// We are using this to validate the payload as per the definitions in swagger.
// We do not need the serialized output from ms-rest.
let mapper = new models.RequestResponse().mapper();
msRest.models = models;
msRest.serialize(mapper, requestResponseObj, 'requestResponseObj');
@ -403,20 +406,36 @@ class LiveValidator {
operationId: operation.operationId,
apiVersion: currentApiVersion
};
validationResult.requestValidationResult.operationInfo = basicOperationInfo;
let reqResult = operation.validateRequest(request);
validationResult.requestValidationResult.errors = reqResult;
log.debug('Request Validation Result');
log.debug(reqResult);
let resResult = operation.validateResponse(response);
validationResult.responseValidationResult.operationInfo = basicOperationInfo;
validationResult.responseValidationResult.errors = resResult;
log.debug('Response Validation Result');
log.debug(resResult);
if (reqResult.errors && !reqResult.errors.length) {
validationResult.requestValidationResult.operationInfo = [basicOperationInfo];
validationResult.responseValidationResult.operationInfo = [basicOperationInfo];
let reqResult;
try {
reqResult = operation.validateRequest(request);
validationResult.requestValidationResult.errors = reqResult.errors || [];
log.debug('Request Validation Result');
log.debug(reqResult);
} catch (reqValidationError) {
let msg = `An error occurred while validating the live request for operation "${operation.operationId}". ` +
`The error is:\n ${util.inspect(requestValidationError, { depth: null })}`;
let err = new models.LiveValidationError(Constants.ErrorCodes.RequestValidationError, msg);
validationResult.requestValidationResult.errors = [err];
}
let resResult;
try {
resResult = operation.validateResponse(response);
validationResult.responseValidationResult.errors = resResult.errors || [];
log.debug('Response Validation Result');
log.debug(resResult);
} catch (resValidationError) {
let msg = `An error occurred while validating the live response for operation "${operation.operationId}". ` +
`The error is:\n ${util.inspect(responseValidationError, { depth: null })}`;
let err = new models.LiveValidationError(Constants.ErrorCodes.ResponseValidationError, msg);
validationResult.responseValidationResult.errors = [err];
}
if (reqResult && reqResult.errors && Array.isArray(reqResult.errors) && !reqResult.errors.length) {
validationResult.requestValidationResult.successfulRequest = true;
}
if (resResult.errors && !resResult.errors.length) {
if (resResult && resResult.errors && Array.isArray(resResult.errors) && !resResult.errors.length) {
validationResult.responseValidationResult.successfulResponse = true;
}
}
@ -426,8 +445,8 @@ class LiveValidator {
let msg = `Found multiple matching operations with operationIds "${operationIds}" ` +
`for request url "${request.url}" with HTTP Method "${request.method}".`;
log.debug(msg);
let e = new models.LiveValidationError(Constants.ErrorCodes.MultipleOperationsFound, msg);
validationResult.errors = [e];
let err = new models.LiveValidationError(Constants.ErrorCodes.MultipleOperationsFound, msg);
validationResult.errors = [err];
}
return validationResult;

Просмотреть файл

@ -201,9 +201,9 @@ class SpecValidator {
let warnings = ValidationResponse.sanitizeWarnings(validationResult.warnings);
if (warnings && warnings.length) {
self.specValidationResult.validateSpec.warnings = warnings;
log.warn(Constants.Warnings);
log.warn('--------');
log.warn(util.inspect(warnings));
log.debug(Constants.Warnings);
log.debug('--------');
log.debug(util.inspect(warnings));
}
}
}
@ -305,7 +305,7 @@ class SpecValidator {
log.error(`${msg}:\n`, e);
} else if (requestValidationWarnings) {
operationResult.request.warning = requestValidationWarnings;
log.warn(`${msg}:\n`, requestValidationWarnings);
log.debug(`${msg}:\n`, requestValidationWarnings);
} else {
operationResult.request.isValid = true;
operationResult.request.result = msg;
@ -326,7 +326,7 @@ class SpecValidator {
log.error(`${msg}:\n`, e);
} else if (responseValidationWarnings) {
operationResult.responses[responseStatusCode].warning = responseValidationWarnings;
log.warn(`${msg}:\n`, responseValidationWarnings);
log.debug(`${msg}:\n`, responseValidationWarnings);
} else {
operationResult.responses[responseStatusCode].isValid = true;
operationResult.responses[responseStatusCode].result = msg;
@ -504,12 +504,13 @@ class SpecValidator {
resultScenarios[scenario] = {};
resultScenarios[scenario].requestValidation = self.validateRequest(operation, xmsExample.parameters);
resultScenarios[scenario].responseValidation = self.validateXmsExampleResponses(operation, xmsExample.responses);
let fileName = path.join('/Users/amarz/sdk/openapi-validation-tools/test/swaggers/arm-storage/2016-01-01/live',
`${operation.operationId}.json`);
let data = {
request: self.sampleRequest,
response: self.sampleResponse
}
// TODO: Usefult for creating live request and response from x-ms-examples
// let fileName = path.join('/Users/amarz/sdk/openapi-validation-tools/test/swaggers/arm-storage/2016-01-01/live',
// `${operation.operationId}.json`);
// let data = {
// request: self.sampleRequest,
// response: self.sampleResponse
// }
//fs.writeFileSync(fileName, JSON.stringify(data, null, 2), { encoding: 'utf8' });
}
} else {