Skip to content

Commit

Permalink
Issue/1730 refactor input file cli (#105)
Browse files Browse the repository at this point in the history
* Refactored input file logic in CLI

* Bumped up the version and postponed the audits

* Added the extra summary information in CLI report
  • Loading branch information
vijayg10 authored Oct 1, 2020
1 parent a92ef7a commit adb7800
Show file tree
Hide file tree
Showing 11 changed files with 16,134 additions and 16,061 deletions.
32,038 changes: 16,019 additions & 16,019 deletions audit-resolve.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ml-testing-toolkit",
"description": "Testing Toolkit for Mojaloop implementations",
"version": "11.2.2",
"version": "11.2.3",
"license": "Apache-2.0",
"author": "Vijaya Kumar Guthi, ModusBox Inc.",
"contributors": [
Expand Down Expand Up @@ -116,7 +116,8 @@
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.0",
"uuid": "8.1.0",
"uuid4": "1.1.4"
"uuid4": "1.1.4",
"ml-testing-toolkit-shared-lib": "11.0.0"
},
"devDependencies": {
"@types/jest": "24.0.22",
Expand Down
6 changes: 6 additions & 0 deletions spec_files/reports/templates/newman/html_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ <h5 class="card-title text-uppercase text-white text-center bg-info">Runtime Inf
{{#if runtimeInformation}}
<span><i class="fas fa-stopwatch"></i></span><strong> Total run duration:</strong> {{runtimeInformation.runDurationMs}} ms<br>
<span><i class="fas fa-stopwatch"></i></span><strong> Average response time:</strong> {{runtimeInformation.avgResponseTime}}<br>
{{/if}}
{{#if extraRuntimeInformation}}
{{#each extraRuntimeInformation}}
<span><i class="fas fa-arrow-right"></i></span><strong> {{key}}:</strong> {{value}}<br>
{{/each}}

{{/if}}
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/cli_client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ commander
.option('--report-auto-filename-enable <reportAutoFilenameEnable>', 'default: false, if true the file name will be generated by the backend')
.option('--report-target <reportTarget>', 'default: "file://<file_name_genrated_by_backend>" --- supported targets: "file://path_to_file", "s3://<bucket_name>[/<file_path>]"')
.option('--slack-webhook-url <slackWebhookUrl>', 'default: "Disabled" --- supported formats: "https://....."')
.option('--extra-summary-information <Comma separated values in the format key:value>', 'default: none --- example: "Testcase Name:something,Environment:Dev1"')
.on('--help', () => { // Extra information on help message
console.log('')
console.log(' *** If the option report-target is set to use AWS S3 service, the variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION) should be passed in environment')
Expand Down
10 changes: 9 additions & 1 deletion src/cli_client/extras/slack-broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ const generateSlackBlocks = (progress) => {
}
}
}
let extramSummaryText = ''
if (config.extraSummaryInformation) {
const extraSummaryInformationArr = config.extraSummaryInformation.split(',')
extraSummaryInformationArr.forEach(info => {
const infoArr = info.split(':')
extramSummaryText += infoArr[0] + ': `' + infoArr[1] + '`\n'
})
}
slackBlocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: '*Test Result:*' + ((totalAssertionsCount === totalPassedAssertionsCount) ? ' `PASSED` ' : ' `FAILED` ') + '\n' + summaryText
text: '*Test Result:*' + ((totalAssertionsCount === totalPassedAssertionsCount) ? ' `PASSED` ' : ' `FAILED` ') + '\n' + extramSummaryText + summaryText
},
...additionalParams
})
Expand Down
40 changes: 3 additions & 37 deletions src/cli_client/modes/outbound.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,18 @@ const fs = require('fs')
const { promisify } = require('util')
const objectStore = require('../objectStore')
const slackBroadcast = require('../extras/slack-broadcast')
const TemplateGenerator = require('../utils/templateGenerator')

const bar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic)

const sendTemplate = async () => {
const config = objectStore.get('config')
try {
const readFileAsync = promisify(fs.readFile)
const readFilesAsync = promisify(fs.readdir)
const outboundRequestID = Math.random().toString(36).substring(7)
const collections = []
const inputFiles = config.inputFiles.split(',')
for (let i = 0; i < inputFiles.length; i++) {
try {
if (inputFiles[i].endsWith('.json')) {
collections.push(JSON.parse(await readFileAsync(inputFiles[i], 'utf8')))
} else {
const files = await readFilesAsync(inputFiles[i])
for (let j = 0; j < files.length; j++) {
collections.push(JSON.parse(await readFileAsync(`${inputFiles[i]}/${files[j]}`, 'utf8')))
}
}
} catch (err) {
console.log(err)
}
}

const template = {
inputValues: JSON.parse(await readFileAsync(config.environmentFile, 'utf8')).inputValues,
test_cases: []
}
if (collections.length > 1) {
template.name = 'multi'
let index = 1
collections.forEach(collection => {
collection.test_cases.forEach(testCase => {
const { id, ...remainingTestCaseProps } = testCase
template.test_cases.push({
id: index++,
...remainingTestCaseProps
})
})
})
} else {
template.name = collections[0].name
template.test_cases = collections[0].test_cases
}
const template = await TemplateGenerator.generateTemplate(inputFiles)
template.inputValues = JSON.parse(await readFileAsync(config.environmentFile, 'utf8')).inputValues

let totalRequests = 0
template.test_cases.forEach(testCase => {
Expand Down
3 changes: 2 additions & 1 deletion src/cli_client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const cli = (commander) => {
slackWebhookUrl: commander.slackWebhookUrl || configFile.slackWebhookUrl,
slackPassedImage: configFile.slackPassedImage,
slackFailedImage: configFile.slackFailedImage,
baseURL: commander.baseUrl || configFile.baseURL
baseURL: commander.baseUrl || configFile.baseURL,
extraSummaryInformation: commander.extraSummaryInformation || configFile.extraSummaryInformation
}

objectStore.set('config', config)
Expand Down
9 changes: 9 additions & 0 deletions src/cli_client/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
******/
const fStr = require('node-strings')
const Table = require('cli-table3')
const objectStore = require('../objectStore')

const outbound = (progress) => {
let totalAssertionsCount = 0
Expand All @@ -49,6 +50,14 @@ const outbound = (progress) => {
})
})
console.log(fStr.yellow(testCasesTag))
const config = objectStore.get('config')
if (config.extraSummaryInformation) {
const extraSummaryInformationArr = config.extraSummaryInformation.split(',')
extraSummaryInformationArr.forEach(info => {
const infoArr = info.split(':')
console.log(infoArr[0] + ':' + fStr.yellow(infoArr[1]))
})
}
const summary = new Table()
summary.push(
[{ colSpan: 2, content: 'SUMMARY', hAlign: 'center' }],
Expand Down
10 changes: 10 additions & 0 deletions src/cli_client/utils/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const outbound = async (data) => {
break
case 'html':
case 'printhtml': {
if (config.extraSummaryInformation) {
const extraSummaryInformationArr = config.extraSummaryInformation.split(',')
data.extraRuntimeInformation = extraSummaryInformationArr.map(info => {
const infoArr = info.split(':')
return {
key: infoArr[0],
value: infoArr[1]
}
})
}
const response = await axios.post(`${config.baseURL}/api/reports/testcase/${config.reportFormat}`, data, { headers: { 'Content-Type': 'application/json' } })
reportData = response.data
const disposition = response.headers['content-disposition']
Expand Down
63 changes: 63 additions & 0 deletions src/cli_client/utils/templateGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { FolderParser } = require('ml-testing-toolkit-shared-lib')
const { readFileAsync, readRecursiveAsync, fileStatAsync } = require('../../lib/utils')

const getFileData = async (fileToRead, fileStat) => {
try {
const content = await readFileAsync(fileToRead, 'utf8')
const fileContent = JSON.parse(content)
return {
name: fileToRead,
path: fileToRead,
size: fileStat.size,
modified: '' + fileStat.mtime,
content: fileContent
}
} catch (err) {
console.log(err.message)
return null
}
}
const getFolderRawData = async (folderItem) => {
var importFolderRawData = []
const stat = await fileStatAsync(folderItem)
if (stat.isFile() && folderItem.endsWith('.json')) {
const fileItemData = await getFileData(folderItem, stat)
if (fileItemData) {
importFolderRawData.push(fileItemData)
}
} else if (stat.isDirectory()) {
const fileList = await readRecursiveAsync(folderItem)
for (var j = 0; j < fileList.length; j++) {
const fileItemData = await getFileData(fileList[j], stat)
if (fileItemData) {
importFolderRawData.push(fileItemData)
}
}
}
importFolderRawData.sort((a, b) => a.path.localeCompare(b.path))
return importFolderRawData
}

const generateTemplate = async (fileList) => {
try {
var testCases = []
for (var i = 0; i < fileList.length; i++) {
const importFolderRawData = await getFolderRawData(fileList[i])
const folderData = FolderParser.getFolderData(importFolderRawData)
const folderTestCases = FolderParser.getTestCases(folderData)
testCases = testCases.concat(folderTestCases)
}
FolderParser.sequenceTestCases(testCases)
const template = {}
template.test_cases = testCases
template.name = 'multi'
return template
} catch (err) {
console.log(err)
return null
}
}

module.exports = {
generateTemplate
}

0 comments on commit adb7800

Please sign in to comment.