Skip to content

Commit

Permalink
test: Replace xml2js with fast-xml-parser (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
sravani1510 authored Sep 4, 2024
1 parent e0db8f3 commit d054166
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 455 deletions.
57 changes: 33 additions & 24 deletions test/functional/CommandCallFunctional.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT

const { expect } = require('chai');
const { parseString } = require('xml2js');
const { XMLParser } = require('fast-xml-parser');
const { CommandCall, Connection, ProgramCall } = require('../../lib/itoolkit');
const { config, printConfig } = require('./config');
const { isQSHSupported } = require('./checkVersion');
Expand All @@ -18,11 +18,11 @@ describe('CommandCall Functional Tests', function () {
connection.add(new CommandCall({ command: 'RTVJOBA USRLIBL(?) SYSLIBL(?)', type: 'cl' }));
connection.run((error, xmlOut) => {
expect(error).to.equal(null);
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.cmd[0].success[0]).to.include('+++ success RTVJOBA USRLIBL(?) SYSLIBL(?)');
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.cmd.success).to.include('+++ success RTVJOBA USRLIBL(?) SYSLIBL(?)');
done();
});
});
});
Expand All @@ -35,11 +35,12 @@ describe('CommandCall Functional Tests', function () {
expect(error).to.equal(null);
// xs does not return success property for sh or qsh command calls
// but on error sh or qsh node will not have any inner data
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.sh[0]._).to.match(/(System\sStatus\sInformation)/);
done();
});

const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.sh).to.match(/(System\sStatus\sInformation)/);
done();
});
});
});
Expand All @@ -53,20 +54,28 @@ describe('CommandCall Functional Tests', function () {
expect(error).to.equal(null);
// xs does not return success property for sh or qsh command calls
// but on error sh or qsh node will not have any inner data
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
const match = result.myscript.pgm[0].version[0].match(/\d\.\d\.\d/);
if (!match) {
throw Error('Unable to determine XMLSERVICE version');
}
if (!isQSHSupported(match[0])) {
// skip if QSH is unsupported
console.log(`XMLSERVICE version ${match[0]} does not support QSH`);
this.skip();
}
expect(result.myscript.qsh[0]._).to.match(/(System\sStatus\sInformation)/);

const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
const { version } = result.myscript.pgm;
const match = version.match(/\d\.\d\.\d/);

if (!match) {
done(new Error('Unable to determine XMLSERVICE version'));
return;
}

if (!isQSHSupported(match[0])) {
console.log(`XMLSERVICE version ${match[0]} does not support QSH`);
this.skip();
done();
});
return;
}
const qshContent = result.myscript.qsh;
expect(qshContent).to.match(/(System\sStatus\sInformation)/);

done();
});
});
});
Expand Down
30 changes: 17 additions & 13 deletions test/functional/ProgramCallFunctional.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT

const { expect } = require('chai');
const { parseString } = require('xml2js');
const { XMLParser } = require('fast-xml-parser');
const { ProgramCall, Connection } = require('../../lib/itoolkit');
const { config, printConfig } = require('./config');

Expand Down Expand Up @@ -57,11 +57,12 @@ describe('ProgramCall Functional Tests', function () {
connection.add(program);
connection.run((error, xmlOut) => {
expect(error).to.equal(null);
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.pgm[0].success[0]).to.include('+++ success QSYS QWCRSVAL');
done();
});

const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.pgm.success).to.include('+++ success QSYS QWCRSVAL');
done();
});
});
});
Expand All @@ -76,16 +77,19 @@ describe('ProgramCall Functional Tests', function () {

program.addParam({ type: '10A', varying: '4', value: 'Gill' });
const testValue = 'NEW_NAME';
program.addReturn('0', '20A', { varying: '4', name: testValue });
// program.addReturn('0', '20A', { varying: '4', name: testValue });
program.addReturn({
varying: '4', name: testValue, value: '0', type: '20A',
});
connection.add(program);
connection.run((error, xmlOut) => {
expect(error).to.equal(null);
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.pgm[0].success[0]).to.include('+++ success');
expect(result.myscript.pgm[0].return[0].data[0]._).to.equal('my name is Gill');
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.pgm.success).to.include('+++ success');
expect(result.myscript.pgm.return.data).to.equal('my name is Gill');
done();
});
});
});
Expand Down
55 changes: 30 additions & 25 deletions test/functional/deprecated/commandsFunctional.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable new-cap */

const { expect } = require('chai');
const { parseString } = require('xml2js');
const { XMLParser } = require('fast-xml-parser');
const {
iCmd, iSh, iQsh, iConn, iPgm,
} = require('../../../lib/itoolkit');
Expand Down Expand Up @@ -41,11 +41,11 @@ describe('iSh, iCmd, iQsh, Functional Tests', function () {
const connection = new iConn(database, username, password, restOptions);
connection.add(iCmd('RTVJOBA USRLIBL(?) SYSLIBL(?)'));
connection.run((xmlOut) => {
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.cmd[0].success[0]).to.include('+++ success RTVJOBA USRLIBL(?) SYSLIBL(?)');
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.cmd.success).to.include('+++ success RTVJOBA USRLIBL(?) SYSLIBL(?)');
done();
});
});
});
Expand All @@ -57,11 +57,11 @@ describe('iSh, iCmd, iQsh, Functional Tests', function () {
connection.run((xmlOut) => {
// xs does not return success property for sh or qsh command calls
// but on error sh or qsh node will not have any inner data
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.sh[0]._).to.match(/(System\sStatus\sInformation)/);
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.sh).to.match(/(System\sStatus\sInformation)/);
done();
});
});
});
Expand All @@ -74,20 +74,25 @@ describe('iSh, iCmd, iQsh, Functional Tests', function () {
connection.run((xmlOut) => {
// xs does not return success property for sh or qsh command calls
// but on error sh or qsh node will not have any inner data
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
const match = result.myscript.pgm[0].version[0].match(/\d\.\d\.\d/);
if (!match) {
throw Error('Unable to determine XMLSERVICE version');
}
if (!isQSHSupported(match[0])) {
// skip if QSH is unsupported
console.log(`XMLSERVICE version ${match[0]} does not support QSH`);
this.skip();
}
expect(result.myscript.qsh[0]._).to.match(/(System\sStatus\sInformation)/);
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
const { version } = result.myscript.pgm;
const match = version.match(/\d\.\d\.\d/);

if (!match) {
done(new Error('Unable to determine XMLSERVICE version'));
return;
}

if (!isQSHSupported(match[0])) {
// skip if QSH is unsupported
console.log(`XMLSERVICE version ${match[0]} does not support QSH`);
this.skip();
}
const qshContent = result.myscript.qsh;
expect(qshContent).to.match(/(System\sStatus\sInformation)/);
done();
});
});
});
Expand Down
26 changes: 13 additions & 13 deletions test/functional/deprecated/iPgmFunctional.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable new-cap */

const { expect } = require('chai');
const { parseString } = require('xml2js');
const { XMLParser } = require('fast-xml-parser');
const { iPgm, iConn } = require('../../../lib/itoolkit');
const { config, printConfig } = require('../config');

Expand All @@ -31,7 +31,7 @@ if (config.transport === 'rest') {
describe('iPgm Functional Tests', function () {
before(function () {
printConfig();
});
});

describe('addParam', function () {
it('calls QWCRSVAL program checks if it ran successfully', function (done) {
Expand All @@ -56,11 +56,11 @@ describe('iPgm Functional Tests', function () {
program.addParam(this.errno, { io: 'both', len: 'rec2' });
connection.add(program);
connection.run((xmlOut) => {
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.pgm[0].success[0]).to.include('+++ success QSYS QWCRSVAL');
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.pgm.success).to.include('+++ success QSYS QWCRSVAL');
done();
});
});
});
Expand All @@ -78,12 +78,12 @@ describe('iPgm Functional Tests', function () {
program.addReturn('0', '20A', { varying: '4', name: testValue });
connection.add(program);
connection.run((xmlOut) => {
parseString(xmlOut, (parseError, result) => {
expect(parseError).to.equal(null);
expect(result.myscript.pgm[0].success[0]).to.include('+++ success');
expect(result.myscript.pgm[0].return[0].data[0]._).to.equal('my name is Gill');
done();
});
const parser = new XMLParser();
let result = parser.parse(xmlOut);
expect(Object.keys(result).length).gt(0);
expect(result.myscript.pgm.success).to.include('+++ success');
expect(result.myscript.pgm.return.data).to.equal('my name is Gill');
done();
});
});
});
Expand Down
Loading

0 comments on commit d054166

Please sign in to comment.