UNPKG

jmms

Version:

Jmms cli tools, Jmms is a java meta-micro-service framework

147 lines (123 loc) 4.53 kB
const _ = require('lodash'); const request = require('sync-request'); const swagger = require('swagger-parser'); const chai = require('chai'); const log = require('../log.js'); const spec = require('./spec'); const meta = require('./meta'); const opts = require('./opts'); const skipCrud = opts.options['skipCrud'] === true; const skipGen = opts.options['skipGen'] === true; const assert = chai.assert; const expect = chai.expect; const AssertionError = chai.AssertionError; log.info("\nRun tests..."); const testBaseUrl = opts.testUrl; const testStartUrl = testBaseUrl + '/start'; const testRunUrl = testBaseUrl + '/run'; log.debug("Start test context from '" + testStartUrl + "'"); const cid = JSON.parse(request('POST', testStartUrl).body).cid; log.debug("Context id : " + cid); //Validate spec it("Validate swagger spec", (done) => { swagger.parse(spec, (err, api) => { if(err) { log.error("Error validate swagger spec, " + err + "\n\n"); process.exit(1); } done(); }); }); const runTestCase = (test) => { const url = testRunUrl + '?cid=' + cid + '&tid=' + test.id; log.debug("Send request to '" + url + "' of test '" + test.name + "'..."); return JSON.parse(request('POST', url).body); } const runTestCases = (suite) => { if(suite.tests) { Object.keys(suite.tests).forEach((key) => { const test = suite.tests[key]; if(skipGen && test.generated == true) { return; } if(skipCrud && test.crud == true) { return; } var desc; if(!_.isEmpty(test.source)) { desc = test.title + ' (' + test.source + ')'; }else if(test.generated) { desc = test.title + ' (gen)'; } if(test.steps.length > 0) { describe(desc, () => { const testResult = runTestCase(test); for(var i=0;i<testResult.steps.length;i++) { const step = test.steps[i]; const stepResult = testResult.steps[i]; runTestStep(step, stepResult); } }); }else { it(desc , () => { const result = runTestCase(test); if(result.state == 'fail') { var message; if(result.steps && result.steps.length > 0) { message = getStepAssertMessage(result.steps[0]); }else { message = result.message; } throw new AssertionError(message); } }); } }); } if(suite.suites) { Object.keys(suite.suites).forEach((key) => { const child = suite.suites[key]; describe(child.title, () => { runTestCases(child); }); }); } } const runTestStep = (step, result) => { var stepDesc = '# ' + (result.desc ? result.desc : ("Step " + (i+1))); if(result.asserts && result.asserts.length > 0) { describe(stepDesc, () => { for(var j=0;j<result.asserts.length;j++) { const assertion = step.asserts[j]; const assertionResult = result.asserts[j]; const assertDesc = assertion.title ? assertion.title : assertion.expr; it(assertDesc, () => { if(assertionResult.state == 'fail') { var message = getStepAssertMessage(result); throw new AssertionError(message); } }); } }); }else { describe(stepDesc, () => { it("should success execute", () => { if(result.state == 'fail') { var message = getStepAssertMessage(result); throw new AssertionError(message); } }); }); } } const getStepAssertMessage = (result) => { var message = result.message; if(result.details) { message = message + " ->\n" + JSON.stringify(result.details, null, 2); if(result.exception) { message = message + "\n\n" + result.exception.stackTrace; } } return message; } runTestCases(meta);