Skip to content

Add Unit and Functional Tests #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/iprod.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,25 @@ class iProd {
];
if(!cb) { // If we do not get the third param,
if(option) { // If we get two params,
if(xt.getClass(option) == "Function") // If it is a function,
if(xt.getClass(option) == "Function"){ // If it is a function,
cb = option; // then it is the callback.
option = "0000";
}
}
else { // If we have only one param,
option = "0000"; // then use *BASE as default.
}

}
if(option < 0 || option > 99)
option = "0000";
else if(option < 10)
option = "000" + option;
else option = "00" + option;

if(Number.isInteger(option)) {
if(option < 0 || option > 99)
option = "0000";
else if(option < 10)
option = "000" + option;
else
option = "00" + option;
}

let ProdInfo = [
[prodID, "7A"],
["*ONLY", "6A"],
Expand Down
19 changes: 14 additions & 5 deletions lib/itoolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,14 @@ const xmlToJson = (xml) => {
let sucFlag = sqlData[i].match(successReg);
if(sucFlag && sucFlag.length > 0) {
rs.success = true;
if(sucFlag.length > 1)
rs.stmt = sucFlag[1];
if(sucFlag.length > 1){
if(sucFlag[0].includes('![CDATA')){
let fixed = sucFlag[1].replace(/]]>/, '');
rs.stmt = fixed;
} else{
rs.stmt = sucFlag[1];
}
}
}
else {
rs.success = false;
Expand Down Expand Up @@ -224,7 +230,7 @@ class iConn {
}
// setTimeout() override the default timeout value for sync mode.
setTimeout(seconds) {
if(__getClass(flag) == "Number")
if(__getClass(seconds) == "Number")
this.timeout = seconds * 1000;
}
// debug() get current verbose mode or enable/disable verbose mode for debugging.
Expand Down Expand Up @@ -535,11 +541,14 @@ class iSql {
}

rowCount(options) {
this.xml += i_xml.iXmlNodeSqlRowCount(options.action, options.error);
if(options && options.error)
this.xml += i_xml.iXmlNodeSqlRowCount(options.error);
else
this.xml += i_xml.iXmlNodeSqlRowCount();
}

count(options) {
this.xml += i_xml.iXmlNodeSqlRowCount(options.desc, options.error);
this.xml += i_xml.iXmlNodeSqlCount(options.desc, options.error);
}

describe(options) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ixml.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ const iXmlNodeSqlConnect = (db, uid, pwd, option_label) => {
}

const iXmlNodeSqlOptions = (options) => {
const iXml = iXmlNodeOpen(I_XML_NODE_SQL_OPTIONS_OPEN);
let iXml = iXmlNodeOpen(I_XML_NODE_SQL_OPTIONS_OPEN);
for(const i in options)
iXml += iXmlAttrDefault(options[i].desc, options[i].value, I_XML_ATTR_VALUE_OPTIONAL);
return iXml + I_XML_NODE_CLOSE + I_XML_NODE_SQL_OPTIONS_CLOSE;
Expand Down Expand Up @@ -332,7 +332,7 @@ const iXmlNodeSqlExecuteClose = () => {

const iXmlNodeSqlParmOpen = (xio) => {
return iXmlNodeOpen(I_XML_NODE_PARM_OPEN)
+ iXmlAttrDefault(I_XML_ATTR_KEY_NAME,xio,I_XML_ATTR_VALUE_OPTIONAL)
+ iXmlAttrDefault(I_XML_ATTR_KEY_IO,xio,I_XML_ATTR_VALUE_OPTIONAL)
+ I_XML_NODE_CLOSE;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable new-cap */
const { iConn } = require('./itoolkit.js');

function returnTransports(opt) {
const transports = [{ name: 'idb', me: new iConn(opt.database, opt.user, opt.password) },
{ name: 'rest', me: new iConn(opt.database, opt.user, opt.password, opt) },
];

return transports;
}

module.exports = { returnTransports };
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
],
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0",
"sinon": "^7.2.3"
},
"optionalDependencies": {
"idb-connector": "^1.1.8",
"mocha": "^5.2.0"
"idb-pconnector": "^1.0.2"
}
}
89 changes: 89 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Node.js Toolkit Tests

Ensure dependencies are installed

From the root of the project run: `npm install`

***NOTE***

Some tests require creating libraries, objects, tables, etc.

A before hook is setup to check for theses objects and create if needed.

These hooks are ran with `idb-pconnector` which requires to be run on IBM i.

In any case, the functional tests test for both transports Db2 and REST.

Using Db2 transport requires `idb-connector` which only runs on IBM i systems.

Tests using these hooks will fail on non IBM i systems.

# Running Tests

From the project root

`npm test test/foo`

where foo is the name of subdir such as `unit` or or individual test file.

***NOTE***

If you experience timeout issue with network calls add

`"test": "./node_modules/mocha/bin/mocha" --timeout Xs `

within `package.json` file, where X is the number of seconds before timeout

# Setup Rest interface

- add to the default apache server conf: `/www/apachedft/conf/httpd.conf`

```
ScriptAlias /cgi-bin/ /QSYS.LIB/QXMLSERVc .LIB/
<Directory /QSYS.LIB/QXMLSERV.LIB/>
AllowOverride None
order allow,deny
allow from all
SetHandler cgi-script
Options +ExecCGI
</Directory>

```

- start the server

` STRTCPSVR SERVER(*HTTP) HTTPSVR(APACHEDFT)`

- go to `http://HOSTNAME/cgi-bin/xmlcgi.pgm`

you should see an XML document

- when finished testing you can shutdown server with

` ENDTCPSVR SERVER(*HTTP) HTTPSVR(APACHEDFT)`


# Configuring Tests
Each functional test contains an config object that is used to create connections

It is recommend to setup environment variables for these configurations

Instead of hard coding credentials with the test file.

you can set environment varaibales with `export KEY='value'`

---
- user `TKUSER` defaults to ''

- password `TKPASS` defaults to ''

- database `TKDB` defaults to `*LOCAL`

For Rest Tests
---
- HOST `TKHOST` defaults to `localhost`

- PORT `TKPORT` defaults to `80`

- PATH `TKPATH` defaults to `/cgi-bin/xmlcgi.pgm`

97 changes: 97 additions & 0 deletions test/functional/commandsFunctional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* eslint-env mocha */

const { expect } = require('chai');
const {
iCmd, iSh, iQsh, xmlToJson,
} = require('../../lib/itoolkit');

// Set Env variables or set values here.
const opt = {
database: process.env.TKDB || '*LOCAL',
user: process.env.TKUSER || '',
password: process.env.TKPASS || '',
host: process.env.TKHOST || 'localhost',
port: process.env.TKPORT || 80,
path: process.env.TKPATH || '/cgi-bin/xmlcgi.pgm',
};

const { returnTransports } = require('../../lib/utils');

const transports = returnTransports(opt);

describe('iSh, iCmd, iQsh, Functional Tests', () => {
describe('iCmd()', () => {
transports.forEach((transport) => {
it(`calls CL command using ${transport.name} transport`, (done) => {
const connection = transport.me;
connection.add(iCmd('RTVJOBA USRLIBL(?) SYSLIBL(?)'));
connection.run((xmlOut) => {
const results = xmlToJson(xmlOut);
results.forEach((result) => {
expect(result.success).to.equal(true);
});
done();
});
});
});
});

describe('iSh()', () => {
transports.forEach((transport) => {
it(`calls PASE shell command using ${transport.name} transport`, (done) => {
const connection = transport.me;

connection.add(iSh('system -i wrksyssts'));
connection.run((xmlOut) => {
const results = xmlToJson(xmlOut);
// xs does not return success property for iSh or iQsh
// but on error data property = '\n'
// so lets base success on contents of data.
results.forEach((result) => {
expect(result.data).not.to.equal('\n');
expect(result.data).to.match(/(System\sStatus\sInformation)/);
});
done();
});
});
});
});

describe('iQsh()', () => {
transports.forEach((transport) => {
it(`calls QSH command using ${transport.name} transport`, (done) => {
const connection = transport.me;
connection.add(iQsh('system wrksyssts'));
connection.run((xmlOut) => {
const results = xmlToJson(xmlOut);
// xs does not return success property for iSh or iQsh
// but on error data property = '\n'
// so lets base success on contents of data.
results.forEach((result) => {
expect(result.data).not.to.equal('\n');
expect(result.data).to.match(/(System\sStatus\sInformation)/);
});
done();
});
});
});
});
});
Loading