From 5fe8de333ec374502f88843adc3313552ead0e58 Mon Sep 17 00:00:00 2001 From: Denis Antonenko Date: Wed, 21 Feb 2018 12:42:51 +0200 Subject: [PATCH] Integration tests & fixes --- package.json | 4 +- src/models/Configuration.js | 10 - src/models/Device.js | 16 +- src/models/query/PluginRegisterQuery.js | 3 +- test/integration/config.json | 16 ++ .../controllers/ConfigurationAPI.spec.js | 66 +++++ test/integration/controllers/Device.spec.js | 118 ++++++++ .../controllers/DeviceCommandAPI.spec.js | 185 ++++++++++++ .../controllers/DeviceNotificationAPI.spec.js | 135 +++++++++ .../controllers/DeviceTypeAPI.spec.js | 130 +++++++++ .../controllers/NetworkAPI.spec.js | 129 +++++++++ .../integration/controllers/PluginAPI.spec.js | 132 +++++++++ .../controllers/ServerInfoAPI.spec.js | 61 ++++ test/integration/controllers/TokenAPI.spec.js | 96 +++++++ test/integration/controllers/UserAPI.spec.js | 269 ++++++++++++++++++ .../controllers/ConfigurationAPI.http.spec.js | 3 +- test/unit/controllers/DeviceAPI.http.spec.js | 2 +- test/unit/controllers/DeviceAPI.ws.spec.js | 2 +- ....js => DeviceNotificationAPI.http.spec.js} | 0 ...ec.js => DeviceNotificationAPI.ws.spec.js} | 0 test/unit/models/Configuration.spec.js | 3 +- test/unit/models/Device.spec.js | 2 +- 22 files changed, 1354 insertions(+), 28 deletions(-) create mode 100644 test/integration/config.json create mode 100644 test/integration/controllers/ConfigurationAPI.spec.js create mode 100644 test/integration/controllers/Device.spec.js create mode 100644 test/integration/controllers/DeviceCommandAPI.spec.js create mode 100644 test/integration/controllers/DeviceNotificationAPI.spec.js create mode 100644 test/integration/controllers/DeviceTypeAPI.spec.js create mode 100644 test/integration/controllers/NetworkAPI.spec.js create mode 100644 test/integration/controllers/PluginAPI.spec.js create mode 100644 test/integration/controllers/ServerInfoAPI.spec.js create mode 100644 test/integration/controllers/TokenAPI.spec.js create mode 100644 test/integration/controllers/UserAPI.spec.js rename test/unit/controllers/{NotificationAPI.http.spec.js => DeviceNotificationAPI.http.spec.js} (100%) rename test/unit/controllers/{NotificationAPI.ws.spec.js => DeviceNotificationAPI.ws.spec.js} (100%) diff --git a/package.json b/package.json index ef7f6da..d99288b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "index.js", "scripts": { "build": "node build.js", - "test": "node_modules/.bin/mocha test/unit/**/**.spec.js --exit" + "test": "npm run unitTest & npm run integrationTest", + "unitTest": "node_modules/.bin/mocha test/unit/**/**.spec.js --exit", + "integrationTest": "node_modules/.bin/mocha test/integration/**/**.spec.js --timeout 10000 --exit" }, "repository": { "type": "git", diff --git a/src/models/Configuration.js b/src/models/Configuration.js index fb67d27..fd124b7 100644 --- a/src/models/Configuration.js +++ b/src/models/Configuration.js @@ -11,14 +11,12 @@ class Configuration extends BaseModel { * @param {Object} options - model options object * @param {string} options.name - Configuration parameter name. * @param {string} options.value - Configuration parameter value. - * @param {number} options.entityVersion - Specifies the version field or property of an entity class. */ constructor({ name, value, entityVersion } = {}) { super(); this.name = name; this.value = value; - this.entityVersion = entityVersion; } get name() { @@ -37,14 +35,6 @@ class Configuration extends BaseModel { this._value = value; } - get entityVersion() { - return this._entityVersion; - } - - set entityVersion(value) { - this._entityVersion = value; - } - /** * Returns instance as a plain JS object * @returns {Object} diff --git a/src/models/Device.js b/src/models/Device.js index f9eb193..0d29a94 100644 --- a/src/models/Device.js +++ b/src/models/Device.js @@ -14,9 +14,9 @@ class Device extends BaseModel { * @param {object} options.data - Device data, a JSON object with an arbitrary structure * @param {number} options.networkId - Associated network id * @param {number} options.deviceTypeId - Associated deviceType id - * @param {boolean} options.blocked - Indicates whether device is blocked + * @param {boolean} options.isBlocked - Indicates whether device is isBlocked */ - constructor({ id, name, data, networkId, deviceTypeId, blocked } = {}) { + constructor({ id, name, data, networkId, deviceTypeId, isBlocked } = {}) { super(); this.id = id; @@ -24,7 +24,7 @@ class Device extends BaseModel { this.data = data; this.networkId = networkId; this.deviceTypeId = deviceTypeId; - this.blocked = blocked; + this.isBlocked = isBlocked; } get id() { @@ -67,12 +67,12 @@ class Device extends BaseModel { this._deviceTypeId = value; } - get blocked() { - return this._blocked; + get isBlocked() { + return this._isBlocked; } - set blocked(value) { - this._blocked = value; + set isBlocked(value) { + this._isBlocked = value; } /** @@ -86,7 +86,7 @@ class Device extends BaseModel { data: this.data, networkId: this.networkId, deviceTypeId: this.deviceTypeId, - blocked: this.blocked + isBlocked: this.isBlocked }; } } diff --git a/src/models/query/PluginRegisterQuery.js b/src/models/query/PluginRegisterQuery.js index a485b62..29c5b59 100644 --- a/src/models/query/PluginRegisterQuery.js +++ b/src/models/query/PluginRegisterQuery.js @@ -17,9 +17,8 @@ class PluginRegisterQuery extends BaseModel { * @param {boolean} [options.returnUpdatedCommands] - Checks if updated commands should be returned * @param {boolean} [options.returnNotifications] - Checks if commands should be returned */ - constructor({ deviceId, networkIds, deviceTypeIds, names, returnCommands, returnUpdatedCommands, returnNotifications } = {}) { + constructor({ deviceId, networkIds, deviceTypeIds, names, returnCommands = true, returnUpdatedCommands = false, returnNotifications = false } = {}) { super(); - this.deviceId = deviceId; this.networkIds = networkIds; this.deviceTypeIds = deviceTypeIds; diff --git a/test/integration/config.json b/test/integration/config.json new file mode 100644 index 0000000..dab5212 --- /dev/null +++ b/test/integration/config.json @@ -0,0 +1,16 @@ +{ + "server": { + "http": { + "login": "dhadmin", + "password": "dhadmin_#911", + "mainServiceURL": "http://localhost:8080/dh/rest", + "authServiceURL": "http://localhost:8090/dh/rest", + "pluginServiceURL": "http://localhost:8110/dh/rest" + }, + "ws": { + "login": "dhadmin", + "password": "dhadmin_#911", + "mainServiceURL": "ws://localhost:8080/dh/websocket" + } + } +} \ No newline at end of file diff --git a/test/integration/controllers/ConfigurationAPI.spec.js b/test/integration/controllers/ConfigurationAPI.spec.js new file mode 100644 index 0000000..d4bf98a --- /dev/null +++ b/test/integration/controllers/ConfigurationAPI.spec.js @@ -0,0 +1,66 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testConfigurations = [ + { + name: 'myTestName', + value: 'string' + }, + { + name: 'myTestName2', + value: 'string' + } +]; + +describe('ConfigurationAPI', () => { + + before(done => { + // Configaratuion DeviceHive + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('ConfigurationAPI.put()', done => { + + // Configurating Configaration model + const configuration = new DeviceHive.models.Configuration(testConfigurations[0]); + const configuration2 = new DeviceHive.models.Configuration(testConfigurations[1]); + + Promise.all([httpDeviceHive.configuration.put(configuration), wsDeviceHive.configuration.put(configuration2)]) + .then(() => done()) + .catch(done); + }); + + + it('ConfigurationAPI.get()', done => { + + Promise.all([httpDeviceHive.configuration.get(testConfigurations[0].name), wsDeviceHive.configuration.get(testConfigurations[1].name)]) + .then(dataAll => { + for (const key in dataAll) { + assert.isObject(dataAll[key]) + assert.include(dataAll[key], testConfigurations[key]); + } + }) + .then(done) + .catch(done); + }); + + + it('ConfigurationAPI.delete()', done => { + + Promise.all([httpDeviceHive.configuration.delete(testConfigurations[0].name), wsDeviceHive.configuration.delete(testConfigurations[1].name)]) + .then(() => done()) + .catch(done); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/Device.spec.js b/test/integration/controllers/Device.spec.js new file mode 100644 index 0000000..72b4512 --- /dev/null +++ b/test/integration/controllers/Device.spec.js @@ -0,0 +1,118 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testDevices = [ + { + id: 'myTestId', + name: 'myTestName', + networkId: 1, + deviceTypeId: 1, + isBlocked: false + }, { + id: 'myTestId2', + name: 'myTestName', + networkId: 1, + deviceTypeId: 1, + isBlocked: false + } +]; + +describe('DeviceAPI', () => { + + before(done => { + // Configaratuion DeviceHive + + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('DeviceAPI.add()', done => { + + // Configurating Device model + const Device = DeviceHive.models.Device; + + const device = new Device(testDevices[0]); + const device2 = new Device(testDevices[1]); + + Promise.all([httpDeviceHive.device.add(device), wsDeviceHive.device.add(device2)]) + .then(() => done()) + .catch(done); + }); + + + it('DeviceAPI.get()', done => { + + const expected = { + deviceId: testDevices[0].id + }; + + Promise.all([httpDeviceHive.device.get(expected.deviceId), wsDeviceHive.device.get(expected.deviceId)]) + .then(dataAll => { + for (const data of dataAll) { + assert.isObject(data) + assert.include(data, testDevices[0]); + } + }) + .then(done) + .catch(done); + }); + + + it('DeviceAPI.list()', done => { + + // Configurating Device List query + const deviceListQuery = new DeviceHive.models.query.DeviceListQuery({ + networkId: 1 + }); + + + Promise.all([httpDeviceHive.device.list(deviceListQuery), wsDeviceHive.device.list(deviceListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + for (const device of data) { + assert.containsAllKeys(device, Object.keys(testDevices[0])); + } + } + }) + .then(done) + .catch(done); + + }); + + + it('DeviceAPI.count()', done => { + + // Configurating Device List query + const deviceListQuery = new DeviceHive.models.query.DeviceCountQuery({ + networkId: '1' + }); + + Promise.all([httpDeviceHive.device.count(deviceListQuery), wsDeviceHive.device.count(deviceListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + assert.property(data, 'count'); + } + }) + .then(done) + .catch(done); + }); + + + it('DeviceAPI.delete()', done => { + + Promise.all([httpDeviceHive.device.delete(testDevices[0].id), wsDeviceHive.device.delete(testDevices[1].id)]) + .then(() => done()) + .catch(done); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/DeviceCommandAPI.spec.js b/test/integration/controllers/DeviceCommandAPI.spec.js new file mode 100644 index 0000000..f139351 --- /dev/null +++ b/test/integration/controllers/DeviceCommandAPI.spec.js @@ -0,0 +1,185 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + +const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be2'; + +const testDeviceCommands = [ + { + deviceId, + command: 'command', + timestamp: new Date().toISOString(), + lastUpdated: new Date().toISOString(), + userId: 1, + networkId: 1, + parameters: { + jsonString: 'jsonString' + }, + lifetime: 0, + status: 'status', + result: { + jsonString: 'jsonString' + } + }, + { + deviceId, + command: 'command2', + timestamp: new Date().toISOString(), + lastUpdated: new Date().toISOString(), + userId: 1, + networkId: 1, + parameters: { + jsonString: 'jsonString' + }, + lifetime: 0, + status: 'status', + result: { + jsonString: 'jsonString' + } + } +]; + +describe('DeviceCommandAPI', () => { + + before(done => { + // Configaratuion DeviceHive + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('DeviceCommandAPI.insert()', done => { + const command = new DeviceHive.models.Command(testDeviceCommands[0]); + const command2 = new DeviceHive.models.Command(testDeviceCommands[1]); + + Promise.all([httpDeviceHive.command.insert(deviceId, command), wsDeviceHive.command.insert(deviceId, command2)]) + .then(() => done()) + .catch(done); + }); + + + it('DeviceCommandAPI.list()', done => { + + // Configurating Device List query + const commandListQuery = new DeviceHive.models.query.CommandListQuery({ + deviceId, + command: 'command', + status: 'status', + sortField: 'id', + sortOrder: 'id', + take: 2, + skip: 0 + }); + + Promise.all([httpDeviceHive.command.list(commandListQuery), wsDeviceHive.command.list(commandListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + for (const deviceCommandKey in data) { + testDeviceCommands[deviceCommandKey].id = data[deviceCommandKey].id; + testDeviceCommands[deviceCommandKey].timestamp = data[deviceCommandKey].timestamp; + testDeviceCommands[deviceCommandKey].lastUpdated = data[deviceCommandKey].lastUpdated; + assert.containsAllKeys(data[deviceCommandKey], Object.keys(testDeviceCommands[0])); + } + } + }) + .then(done) + .catch(done); + }); + + + it('DeviceCommand API.get()', done => { + + + Promise.all([httpDeviceHive.command.get(deviceId, testDeviceCommands[0].id), wsDeviceHive.command.get(deviceId, testDeviceCommands[0].id)]) + .then(dataAll => { + const expected = testDeviceCommands[0]; + for (const data of dataAll) { + assert.isObject(data); + assert.deepInclude(data, expected); + } + }) + .then(done) + .catch(done); + }); + + + it('DeviceCommandAPI.update()', done => { + + const command = new DeviceHive.models.Command(testDeviceCommands[0], testDeviceCommands[0]); + const command2 = new DeviceHive.models.Command(testDeviceCommands[0], testDeviceCommands[1]); + + Promise.all([httpDeviceHive.command.update(command), wsDeviceHive.command.update(command2)]) + .then(() => done()) + .catch(done); + }); + + + it('DeviceCommandAPI.poll()', done => { + + // Configurating Command List query + const commandPollQuery = new DeviceHive.models.query.CommandPollQuery({ + deviceId, + returnUpdatedCommands: true, + limit: 1, + waitTimeout: 1 + }); + + httpDeviceHive.command.poll(commandPollQuery) + .then(() => done()) + .catch(done); + + // emit command + setTimeout(() => { + const command = new DeviceHive.models.Command(testDeviceCommands[0]); + httpDeviceHive.command.insert(deviceId, command); + }, 50); + }); + + + it('DeviceCommandAPI.pollMany()', done => { + + const commandPollManyQuery = new DeviceHive.models.query.CommandPollManyQuery({ + deviceIds: deviceId + }); + + httpDeviceHive.command.pollMany(commandPollManyQuery) + .then(() => done()) + .catch(done); + + // emit command + setTimeout(() => { + const command = new DeviceHive.models.Command(testDeviceCommands[0]); + httpDeviceHive.command.insert(deviceId, command); + }, 50); + }); + + + it('DeviceCommandAPI.wait()', done => { + + // TODO + done(); + + // Configurating Command List query + // const commandWaitQuery = new DeviceHive.models.query.CommandWaitQuery({ + // deviceId, + // commandId: testDeviceCommands[0].id, + // waitTimeout: 1 + // }); + + // httpDeviceHive.command.wait(commandWaitQuery.deviceId, commandWaitQuery.commandId, commandWaitQuery) + // .then(() => done()) + // .catch(done); + + // // emit command + // const command = new DeviceHive.models.Command(testDeviceCommands[0]); + // httpDeviceHive.command.insert(deviceId, command); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/DeviceNotificationAPI.spec.js b/test/integration/controllers/DeviceNotificationAPI.spec.js new file mode 100644 index 0000000..fb97e36 --- /dev/null +++ b/test/integration/controllers/DeviceNotificationAPI.spec.js @@ -0,0 +1,135 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + +const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be2'; + +const testDeviceNotifications = [ + { + deviceId, + notification: 'notification', + timestamp: new Date().toISOString(), + parameters: { + jsonString: 'jsonString' + } + }, + { + deviceId, + notification: 'notification', + timestamp: new Date().toISOString(), + parameters: { + jsonString: 'jsonString' + } + }, +]; + +describe('NotificationAPI', () => { + + before(done => { + // Configaratuion DeviceHive + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('NotificationAPI.insert()', done => { + + const notification = new DeviceHive.models.Notification(testDeviceNotifications[0]); + const notification2 = new DeviceHive.models.Notification(testDeviceNotifications[1]); + + Promise.all([httpDeviceHive.notification.insert(deviceId, notification), wsDeviceHive.notification.insert(deviceId, notification2)]) + .then(() => done()) + .catch(done); + }); + + + it('NotificationAPI.list()', done => { + + // Configurating Device List query + const notificationListQuery = new DeviceHive.models.query.NotificationListQuery({ + deviceId, + notification: 'notification', + status: 'status', + sortField: 'id', + sortOrder: 'id', + take: 2, + skip: 0 + }); + + Promise.all([httpDeviceHive.notification.list(notificationListQuery), wsDeviceHive.notification.list(notificationListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + for (const deviceNotificationKey in data) { + testDeviceNotifications[deviceNotificationKey].id = data[deviceNotificationKey].id; + testDeviceNotifications[deviceNotificationKey].timestamp = data[deviceNotificationKey].timestamp; + assert.containsAllKeys(data[deviceNotificationKey], Object.keys(testDeviceNotifications[0])); + } + } + }) + .then(done) + .catch(done); + }); + + + it('NotificationAPI.get()', done => { + + Promise.all([httpDeviceHive.notification.get(deviceId, testDeviceNotifications[0].id), wsDeviceHive.notification.get(deviceId, testDeviceNotifications[0].id)]) + .then(dataAll => { + const expected = testDeviceNotifications[0]; + for (const data of dataAll) { + assert.isObject(data); + assert.deepInclude(data, expected); + } + }) + .then(done) + .catch(done); + }); + + + it('NotificationAPI.poll()', done => { + + // Configurating Notification List query + const notificationPollQuery = new DeviceHive.models.query.NotificationPollQuery({ + deviceId, + returnUpdatedNotifications: true, + limit: 1, + waitTimeout: 1 + }); + + httpDeviceHive.notification.poll(notificationPollQuery) + .then(() => done()) + .catch(done); + + // emit notification + setTimeout(() => { + const notification = new DeviceHive.models.Notification(testDeviceNotifications[0]); + httpDeviceHive.notification.insert(deviceId, notification); + }, 50); + }); + + + it('NotificationAPI.pollMany()', done => { + + const notificationPollManyQuery = new DeviceHive.models.query.NotificationPollManyQuery({ + deviceIds: deviceId + }); + + httpDeviceHive.notification.pollMany(notificationPollManyQuery) + .then(() => done()) + .catch(done); + + // emit notification + setTimeout(() => { + const notification = new DeviceHive.models.Notification(testDeviceNotifications[0]); + httpDeviceHive.notification.insert(deviceId, notification); + }, 50); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/DeviceTypeAPI.spec.js b/test/integration/controllers/DeviceTypeAPI.spec.js new file mode 100644 index 0000000..82cf932 --- /dev/null +++ b/test/integration/controllers/DeviceTypeAPI.spec.js @@ -0,0 +1,130 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testDeviceTypes = [ + { + name: 'name', + description: 'description' + }, { + name: 'name2', + description: 'description2' + } +]; + +describe('DeviceTypeAPI', () => { + + before(done => { + // Configaratuion DeviceHive + + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('DeviceTypeAPI.insert()', done => { + + // Configurating DeviceType model + const DeviceType = DeviceHive.models.DeviceType; + + const deviceType = new DeviceType(testDeviceTypes[0]); + const deviceType2 = new DeviceType(testDeviceTypes[1]); + + Promise.all([httpDeviceHive.deviceType.insert(deviceType), wsDeviceHive.deviceType.insert(deviceType2)]) + .then(() => done()) + .catch(done); + }); + + + it('DeviceTypeAPI.list()', done => { + + + // Configurating Device List query + const deviceTypeListQuery = new DeviceHive.models.query.DeviceTypeListQuery({ + namePattern: 'name%', + sortField: 'name', + sortOrder: 'asc', + take: 2, + skip: 0 + }); + + + Promise.all([httpDeviceHive.deviceType.list(deviceTypeListQuery), wsDeviceHive.deviceType.list(deviceTypeListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + for (const deviceTypeKey in data) { + testDeviceTypes[deviceTypeKey].id = data[deviceTypeKey].id; + testDeviceTypes[deviceTypeKey].name = data[deviceTypeKey].name; + testDeviceTypes[deviceTypeKey].description = data[deviceTypeKey].description; + assert.containsAllKeys(data[deviceTypeKey], Object.keys(testDeviceTypes[0])); + } + } + }) + .then(done) + .catch(done); + }); + + + it('DeviceTypeAPI.get()', done => { + + Promise.all([httpDeviceHive.deviceType.get(testDeviceTypes[0].id), wsDeviceHive.deviceType.get(testDeviceTypes[0].id)]) + .then(dataAll => { + const expected = testDeviceTypes[0]; + for (const data of dataAll) { + assert.isObject(data); + assert.deepInclude(data, expected); + } + }) + .then(done) + .catch(done); + }); + + + it('DeviceTypeAPI.update()', done => { + + // Configurating DeviceType model + const DeviceType = DeviceHive.models.DeviceType; + + const deviceType = new DeviceType(testDeviceTypes[0]); + const deviceType2 = new DeviceType(testDeviceTypes[1]); + + Promise.all([httpDeviceHive.deviceType.update(deviceType), wsDeviceHive.deviceType.update(deviceType2)]) + .then(() => done()) + .catch(done); + + }); + + it('DeviceTypeAPI.count()', done => { + + // Configurating DeviceType List query + const deviceTypeListQuery = new DeviceHive.models.query.DeviceTypeCountQuery({ + name: 'name', + namePattern: 'namePattern' + }); + + Promise.all([httpDeviceHive.deviceType.count(deviceTypeListQuery), wsDeviceHive.deviceType.count(deviceTypeListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + assert.property(data, 'count'); + } + }) + .then(done) + .catch(done); + }); + + it('DeviceTypeAPI.delete()', done => { + + Promise.all([httpDeviceHive.deviceType.delete(testDeviceTypes[0].id), wsDeviceHive.deviceType.delete(testDeviceTypes[1].id)]) + .then(() => done()) + .catch(done); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/NetworkAPI.spec.js b/test/integration/controllers/NetworkAPI.spec.js new file mode 100644 index 0000000..12f6a6b --- /dev/null +++ b/test/integration/controllers/NetworkAPI.spec.js @@ -0,0 +1,129 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testNetworks = [ + { + name: 'name', + description: 'description' + }, { + name: 'name2', + description: 'description2' + } +]; + +describe('NetworkAPI', () => { + + before(done => { + // Configaratuion DeviceHive + + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('NetworkAPI.insert()', done => { + + // Configurating Network model + const Network = DeviceHive.models.Network; + + const network = new Network(testNetworks[0]); + const network2 = new Network(testNetworks[1]); + + Promise.all([httpDeviceHive.network.insert(network), wsDeviceHive.network.insert(network2)]) + .then(() => done()) + .catch(done); + }); + + + it('NetworkAPI.list()', done => { + + // Configurating Device List query + const networkListQuery = new DeviceHive.models.query.NetworkListQuery({ + namePattern: 'name%', + sortField: 'name', + sortOrder: 'asc', + take: 2, + skip: 0 + }); + + + Promise.all([httpDeviceHive.network.list(networkListQuery), wsDeviceHive.network.list(networkListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + for (const networkKey in data) { + testNetworks[networkKey].id = data[networkKey].id; + testNetworks[networkKey].name = data[networkKey].name; + testNetworks[networkKey].description = data[networkKey].description; + assert.containsAllKeys(data[networkKey], Object.keys(testNetworks[0])); + } + } + }) + .then(done) + .catch(done); + }); + + + it('NetworkAPI.get()', done => { + + Promise.all([httpDeviceHive.network.get(testNetworks[0].id), wsDeviceHive.network.get(testNetworks[0].id)]) + .then(dataAll => { + const expected = testNetworks[0]; + for (const data of dataAll) { + assert.isObject(data); + assert.deepInclude(data, expected); + } + }) + .then(done) + .catch(done); + }); + + + it('NetworkAPI.update()', done => { + + // Configurating Network model + const Network = DeviceHive.models.Network; + + const network = new Network(testNetworks[0]); + const network2 = new Network(testNetworks[1]); + + Promise.all([httpDeviceHive.network.update(network), wsDeviceHive.network.update(network2)]) + .then(() => done()) + .catch(done); + + }); + + it('NetworkAPI.count()', done => { + + // Configurating Network List query + const networkListQuery = new DeviceHive.models.query.NetworkCountQuery({ + name: 'name', + namePattern: 'namePattern' + }); + + Promise.all([httpDeviceHive.network.count(networkListQuery), wsDeviceHive.network.count(networkListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + assert.property(data, 'count'); + } + }) + .then(done) + .catch(done); + }); + + it('NetworkAPI.delete()', done => { + + Promise.all([httpDeviceHive.network.delete(testNetworks[0].id), wsDeviceHive.network.delete(testNetworks[1].id)]) + .then(() => done()) + .catch(done); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/PluginAPI.spec.js b/test/integration/controllers/PluginAPI.spec.js new file mode 100644 index 0000000..fab0122 --- /dev/null +++ b/test/integration/controllers/PluginAPI.spec.js @@ -0,0 +1,132 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testPlugins = [ + { + name: `testName${new Date().getMilliseconds()}`, + description: 'description', + parameters: { + jsonString: 'string' + } + } +]; + +describe('PluginAPI', () => { + + before(done => { + // Configaratuion DeviceHive + + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('PluginAPI.register()', done => { + const plugin = new DeviceHive.models.Plugin(testPlugins[0]); + const pluginQuery = new DeviceHive.models.query.PluginRegisterQuery({ + returnCommands: 'true', + returnUpdatedCommands: 'false', + returnNotifications: 'false' + }); + + Promise.all([httpDeviceHive.plugin.register(plugin, pluginQuery)]) + .then(dataAll => { + for (const data of dataAll) { + testPlugins[0] = Object.assign({}, testPlugins[0], data); + } + }) + .then(() => done()) + .catch(done); + }); + + it('PluginAPI.list()', done => { + + // Configurating Plugin List query + const pluginListQuery = new DeviceHive.models.query.PluginListQuery({ + take: 1, + skip: 0 + }); + + Promise.all([httpDeviceHive.plugin.list(pluginListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + assert.isArray(data); + } + }) + .then(() => done()) + .catch(done); + }); + + + // TODO + // it('PluginAPI.update()', done => { + // }); + + + it('PluginAPI.count()', done => { + + // Configurating Plugin List query + const pluginCountQuery = new DeviceHive.models.query.PluginCountQuery({ + status: '1' + }); + + Promise.all([httpDeviceHive.plugin.count(pluginCountQuery)]) + .then(dataAll => { + for (const data of dataAll) { + assert.property(data, 'count'); + } + }) + .then(() => done()) + .catch(done); + }); + + it('TokenAPI.createPluginToken()', done => { + + // Configurating Token model + const token = new DeviceHive.models.PluginToken({ + actions: [0], + expiration: '2018-02-09T10:09:03.033Z', + type: 0, + topicName: testPlugins[0].topicName + }); + + Promise.all([httpDeviceHive.token.createPluginToken(token)]) + .then(dataAll => { + for (const data of dataAll) { + const expectedKeys = ['accessToken', 'refreshToken'] + assert.containsAllKeys(data, expectedKeys); + } + }) + .then(done) + .catch(done); + }); + + it('TokenAPI.authPlugin()', done => { + Promise.all([httpDeviceHive.token.authPlugin(testPlugins[0].accessToken)]) + .then(dataAll => { + for (const data of dataAll) { + const expectedKeys = ['tpc', 'a', 'e', 't']; + assert.containsAllKeys(data, expectedKeys) + } + }) + .then(done) + .catch(done); + }); + + it('PluginAPI.delete()', done => { + + Promise.all([httpDeviceHive.plugin.delete(testPlugins[0].topicName)]) + .then(() => done()) + .catch(done); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/ServerInfoAPI.spec.js b/test/integration/controllers/ServerInfoAPI.spec.js new file mode 100644 index 0000000..4b21d60 --- /dev/null +++ b/test/integration/controllers/ServerInfoAPI.spec.js @@ -0,0 +1,61 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +describe('ServerInfoAPI', () => { + + before(done => { + // Configaratuion DeviceHive + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + + it('InfoAPI.getServerInfo()', done => { + + Promise.all([httpDeviceHive.info.getServerInfo(), wsDeviceHive.info.getServerInfo()]) + .then(dataAll => { + for (const data of dataAll) { + const expectedKeys = ['apiVersion', 'serverTimestamp'] + assert.isObject(data); + assert.containsAllKeys(data, expectedKeys); + } + }) + .then(done) + .catch(done); + }); + + it('InfoAPI.getCacheInfo()', done => { + + Promise.all([httpDeviceHive.info.getCacheInfo()]) + .then(dataAll => { + for (const data of dataAll) { + const expectedKeys = ['serverTimestamp', 'cacheStats'] + assert.isObject(data); + assert.containsAllKeys(data, expectedKeys); + } + }) + .then(done) + .catch(done); + }); + + it('InfoAPI.getClusterInfo()', done => { + Promise.all([httpDeviceHive.info.getClusterInfo(), wsDeviceHive.info.getClusterInfo()]) + .then(dataAll => { + for (const data of dataAll) { + assert.isObject(data); + } + }) + .then(done) + .catch(done); + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/TokenAPI.spec.js b/test/integration/controllers/TokenAPI.spec.js new file mode 100644 index 0000000..d6e7aab --- /dev/null +++ b/test/integration/controllers/TokenAPI.spec.js @@ -0,0 +1,96 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testToken = { + login: config.server.http.login, + password: config.server.http.password +}; + +describe('TokenAPI', () => { + + before(done => { + // Configaratuion DeviceHive + + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + it('TokenAPI.createUserToken()', done => { + + // Configurating Token model + const token = new DeviceHive.models.UserToken({ + userId: 1, + actions: ['string'], + networkIds: ['string'], + deviceTypeIds: ['string'], + expiration: '2030-02-09T10:09:03.033Z' + }); + + Promise.all([httpDeviceHive.token.createUserToken(token), wsDeviceHive.token.createUserToken(token)]) + .then(dataAll => { + for (const data of dataAll) { + const expectedkeys = ['accessToken', 'refreshToken']; + assert.isObject(data); + assert.containsAllKeys(data, expectedkeys); + testToken.accessToken = data.accessToken; + testToken.refreshToken = data.refreshToken; + } + }) + .then(done) + .catch(done); + }); + + it('TokenAPI.refresh()', done => { + + // Configurating Token model + + Promise.all([httpDeviceHive.token.refresh(testToken.refreshToken)]) + .then(dataAll => { + for (const data of dataAll) { + const expectedkeys = ['accessToken']; + assert.isObject(data); + assert.containsAllKeys(data, expectedkeys); + testToken.accessToken = data.accessToken; + testToken.refreshToken = data.refreshToken; + } + }) + .then(done) + .catch(done); + + // sent data + events.once('request', data => { + assert.equal(data.method, 'POST', 'Not correct method'); + assert.equal(data.url.pathname, `/token/refresh`, 'Not correct URL'); + assert.deepEqual(data.body, expectedBody, 'Not correct body'); + + done(); + }); + }); + + + it('TokenAPI.login()', done => { + + Promise.all([httpDeviceHive.token.login(testToken.login, testToken.password), wsDeviceHive.token.login(testToken.login, testToken.password)]) + .then(dataAll => { + for (const data of dataAll) { + const expectedkeys = ['accessToken', 'refreshToken']; + assert.isObject(data); + assert.containsAllKeys(data, expectedkeys); + testToken.accessToken = data.accessToken; + testToken.refreshToken = data.refreshToken; + } + }) + .then(done) + .catch(done) + }); +}); \ No newline at end of file diff --git a/test/integration/controllers/UserAPI.spec.js b/test/integration/controllers/UserAPI.spec.js new file mode 100644 index 0000000..088c911 --- /dev/null +++ b/test/integration/controllers/UserAPI.spec.js @@ -0,0 +1,269 @@ +const chai = require(`chai`); +const assert = chai.assert; +const config = require('../config'); + +const EventEmitter = require('events'); +const events = new EventEmitter(); + +const DeviceHive = require('../../../index'); + +const httpDeviceHive = new DeviceHive(config.server.http); +const wsDeviceHive = new DeviceHive(config.server.ws) + + +const testUsers = [ + { + login: 'testLogin', + role: 1, + status: 1, + password: 'password', + lastLogin: '2018-02-09T10:09:03.033Z', + data: { + jsonString: 'jsonString' + }, + introReviewed: false, + allDeviceTypesAvailable: false + }, { + login: 'testLogin2', + role: 1, + status: 1, + password: 'password', + lastLogin: '2018-02-09T10:09:03.033Z', + data: { + jsonString: 'jsonString' + }, + introReviewed: false, + allDeviceTypesAvailable: false + } +]; + +describe('UserAPI', () => { + + before(done => { + // Configaratuion DeviceHive + + Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => done()); + }); + + it('UserAPI.insert()', done => { + + // Configurating User model + const User = DeviceHive.models.User; + + const user = new User(testUsers[0]); + const user2 = new User(testUsers[1]); + + Promise.all([httpDeviceHive.user.insert(user), wsDeviceHive.user.insert(user2)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.list()', done => { + + // Configurating User List query + const userListQuery = new DeviceHive.models.query.UserListQuery({ + loginPattern: 'testLogin%', + role: 1, + status: 1, + sortField: 'login', + sortOrder: 'asc', + take: 2, + skip: 0, + }); + + Promise.all([httpDeviceHive.user.list(userListQuery), wsDeviceHive.user.list(userListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + for (const userKey in data) { + testUsers[userKey].id = data[userKey].id; + testUsers[userKey].lastLogin = data[userKey].lastLogin; + + const expectedKeys = Object.keys(testUsers[0]); + expectedKeys.splice(expectedKeys.indexOf('password'), 1); + assert.containsAllKeys(data[userKey], expectedKeys); + } + } + }) + .then(done) + .catch(done); + }); + + it('UserAPI.get()', done => { + + Promise.all([httpDeviceHive.user.get(testUsers[0].id), wsDeviceHive.user.get(testUsers[0].id)]) + .then(dataAll => { + const expected = Object.assign({}, testUsers[0]); + delete expected.password; + for (const data of dataAll) { + assert.isObject(data); + assert.deepInclude(data, expected); + } + }) + .then(done) + .catch(done); + }); + + it('UserAPI.getCurrent()', done => { + + Promise.all([httpDeviceHive.user.getCurrent(), wsDeviceHive.user.getCurrent()]) + .then(dataAll => { + const expected = Object.assign({}, testUsers[0]); + delete expected.password; + for (const data of dataAll) { + const expectedKeys = Object.keys(testUsers[0]); + expectedKeys.splice(expectedKeys.indexOf('password'), 1); + + assert.isObject(data); + assert.containsAllKeys(data, expectedKeys); + } + }) + .then(done) + .catch(done); + }); + + + it('UserAPI.update()', done => { + + // Configurating User model + const User = DeviceHive.models.User; + + const user = new User(testUsers[0]); + const user2 = new User(testUsers[1]); + + Promise.all([httpDeviceHive.user.update(user), wsDeviceHive.user.update(user2)]) + .then(() => done()) + .catch(done); + + }); + + + it('UserAPI.updateCurrent()', done => { + + // Configurating User model + const user = new DeviceHive.models.User({ + status: 0 + }); + + Promise.all([httpDeviceHive.user.updateCurrent(user), wsDeviceHive.user.updateCurrent(user)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.count()', done => { + + // Configurating User List query + const userListQuery = new DeviceHive.models.query.UserCountQuery({ + login: 'login', + loginPattern: 'loginPattern', + role: '1', + status: '1', + }); + + Promise.all([httpDeviceHive.user.count(userListQuery), wsDeviceHive.user.count(userListQuery)]) + .then(dataAll => { + for (const data of dataAll) { + assert.property(data, 'count'); + } + }) + .then(done) + .catch(done); + }); + + + it('UserAPI.getDeviceTypes()', done => { + + Promise.all([httpDeviceHive.user.getDeviceTypes(testUsers[0].id)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.assignAllDeviceTypes()', done => { + + Promise.all([httpDeviceHive.user.assignAllDeviceTypes(testUsers[0].id)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.unassignAllDeviceTypes()', done => { + + Promise.all([httpDeviceHive.user.unassignAllDeviceTypes(testUsers[0].id)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.assignDeviceType()', done => { + + Promise.all([httpDeviceHive.user.assignDeviceType(testUsers[0].id, 1)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.getDeviceType()', done => { + + Promise.all([httpDeviceHive.user.getDeviceType(testUsers[0].id, 1)]) + .then(dataAll => { + for (const data of dataAll) { + assert.isObject(data); + assert.property(data, 'deviceType'); + } + }) + .then(done) + .catch(done); + }); + + + it('UserAPI.unassignDeviceType()', done => { + + Promise.all([httpDeviceHive.user.unassignDeviceType(testUsers[0].id, 1)]) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.assignNetwork()', done => { + + httpDeviceHive.user.assignNetwork(testUsers[0].id, 1) + .then(() => wsDeviceHive.user.assignNetwork(testUsers[1].id, 1)) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.getNetwork()', done => { + + Promise.all([httpDeviceHive.user.getNetwork(testUsers[0].id, 1), wsDeviceHive.user.getNetwork(testUsers[1].id, 1)]) + .then(dataAll => { + for (const data of dataAll) { + assert.isObject(data); + assert.property(data, 'network'); + } + }) + .then(done) + .catch(done); + }); + + + it('UserAPI.unassignNetwork()', done => { + + httpDeviceHive.user.unassignNetwork(testUsers[0].id, 1) + .then(() => wsDeviceHive.user.unassignNetwork(testUsers[1].id, 1)) + .then(() => done()) + .catch(done); + }); + + + it('UserAPI.delete()', done => { + + Promise.all([httpDeviceHive.user.delete(testUsers[0].id), wsDeviceHive.user.delete(testUsers[1].id)]) + .then(() => done()) + .catch(done); + }); + +}); \ No newline at end of file diff --git a/test/unit/controllers/ConfigurationAPI.http.spec.js b/test/unit/controllers/ConfigurationAPI.http.spec.js index 69474d2..d26a741 100644 --- a/test/unit/controllers/ConfigurationAPI.http.spec.js +++ b/test/unit/controllers/ConfigurationAPI.http.spec.js @@ -84,8 +84,7 @@ describe('ConfigurationAPI HTTP', () => { // Configurating Configaration model const expectedBody = { name: 'myTestName', - value: 'string', - entityVersion: '1' + value: 'string' }; const configuration = new DeviceHive.models.Configuration(expectedBody); diff --git a/test/unit/controllers/DeviceAPI.http.spec.js b/test/unit/controllers/DeviceAPI.http.spec.js index 4df0037..3705d0e 100644 --- a/test/unit/controllers/DeviceAPI.http.spec.js +++ b/test/unit/controllers/DeviceAPI.http.spec.js @@ -107,7 +107,7 @@ describe('DeviceAPI', () => { name: 'myTestName', networkId: 1, deviceTypeId: 1, - blocked: false + isBlocked: false }; const device = new DeviceHive.models.Device(expectedBody); diff --git a/test/unit/controllers/DeviceAPI.ws.spec.js b/test/unit/controllers/DeviceAPI.ws.spec.js index b3b8c03..3ae25d4 100644 --- a/test/unit/controllers/DeviceAPI.ws.spec.js +++ b/test/unit/controllers/DeviceAPI.ws.spec.js @@ -97,7 +97,7 @@ describe('DeviceAPI WS', () => { name: 'myTestName', networkId: 1, deviceTypeId: 1, - blocked: false + isBlocked: false }; const device = new DeviceHive.models.Device(expected); diff --git a/test/unit/controllers/NotificationAPI.http.spec.js b/test/unit/controllers/DeviceNotificationAPI.http.spec.js similarity index 100% rename from test/unit/controllers/NotificationAPI.http.spec.js rename to test/unit/controllers/DeviceNotificationAPI.http.spec.js diff --git a/test/unit/controllers/NotificationAPI.ws.spec.js b/test/unit/controllers/DeviceNotificationAPI.ws.spec.js similarity index 100% rename from test/unit/controllers/NotificationAPI.ws.spec.js rename to test/unit/controllers/DeviceNotificationAPI.ws.spec.js diff --git a/test/unit/models/Configuration.spec.js b/test/unit/models/Configuration.spec.js index 38b92b9..e9d606d 100644 --- a/test/unit/models/Configuration.spec.js +++ b/test/unit/models/Configuration.spec.js @@ -6,8 +6,7 @@ describe('Configuration', () => { const expected = { name: 'string', - value: 'string', - entityVersion: 0 + value: 'string' }; diff --git a/test/unit/models/Device.spec.js b/test/unit/models/Device.spec.js index 6921455..ab37321 100644 --- a/test/unit/models/Device.spec.js +++ b/test/unit/models/Device.spec.js @@ -11,7 +11,7 @@ describe('Device', () => { }, networkId: 0, deviceTypeId: 0, - blocked: false + isBlocked: false };