diff --git a/Dockerfile b/Dockerfile index 87e7dd2..86dd5d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,18 @@ -FROM node:8.7.0-alpine +FROM node:9.4.0-alpine ENV WORK_DIR=/usr/src/app/ RUN mkdir -p ${WORK_DIR} \ && cd ${WORK_DIR} WORKDIR ${WORK_DIR} -RUN apk add --no-cache --virtual .gyp \ - python \ - make \ - g++ - COPY . ${WORK_DIR} -RUN npm install \ - && apk del .gyp - -RUN npm install pm2 -g +RUN apk add --no-cache --virtual .gyp \ + python make g++ \ + && npm install \ + && apk del .gyp \ + && npm install pm2 -g \ + && npm cache clean --force EXPOSE 1883 -CMD ["pm2-docker", "src/broker.js"] \ No newline at end of file +CMD ["pm2-docker", "src/broker.js"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..88713af --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,34 @@ +properties([ + buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '7', numToKeepStr: '7')) +]) + +def publish_branches = ["development", "master"] + +stage('Build and publish Docker image in CI repository') { + node('docker') { + checkout scm + echo 'Building image ...' + def mqtt = docker.build('devicehiveci/devicehive-mqtt:${BRANCH_NAME}', '--pull -f Dockerfile .') + + echo 'Pushing image to CI repository ...' + docker.withRegistry('https://registry.hub.docker.com', 'devicehiveci_dockerhub'){ + mqtt.push() + } + } +} + +if (publish_branches.contains(env.BRANCH_NAME)) { + stage('Publish image in main repository') { + node('docker') { + // Builds from 'master' branch will have 'latest' tag + def IMAGE_TAG = (env.BRANCH_NAME == 'master') ? 'latest' : env.BRANCH_NAME + + docker.withRegistry('https://registry.hub.docker.com', 'devicehiveci_dockerhub'){ + sh """ + docker tag devicehiveci/devicehive-mqtt:${BRANCH_NAME} registry.hub.docker.com/devicehive/devicehive-mqtt:${IMAGE_TAG} + docker push registry.hub.docker.com/devicehive/devicehive-mqtt:${IMAGE_TAG} + """ + } + } + } +} diff --git a/README.md b/README.md index 462f6e1..512ccfd 100644 --- a/README.md +++ b/README.md @@ -36,19 +36,39 @@ Start Up === The **devicehive-mqtt** broker can be launched directly via node, docker container or docker-compose. With last choice is pretty easy to scale the broker horizontally. -Also you might to specify a set of environmental variables that are described in the next paragraph. +Also you might to specify a set of configurations/environmental variables that are described in the next paragraph. -Environmental variables ---- - `NODE_ENV` - "prod" for production, "dev" for development - `BROKER_PORT` - port on wich broker will start (default: 1883) - `WS_SERVER_URL` - path to Web Socket server (default: ws://playground.devicehive.com/api/websocket) - `REDIS_SERVER_HOST` - Redis storage host (default: localhost) - `REDIS_SERVER_PORT` - Redis storage port (default: 6379) - `DEBUG` - [debug] modules logger (modules: subscriptionmanager, websocketfactory, websocketmanager) - `APP_LOG_LEVEL` - application logger level (levels: debug, info, warn, error) - `ENABLE_PM` - enable process monitoring with [PM2] module - +# Configuration +## Broker + [path-to-broker-project]/src/config.json + + - **_BROKER_PORT_** - port on wich broker will start (default: 1883) + - **_WS_SERVER_URL_** - path to Web Socket server (default: ws://localhost:8080/dh/websocket) + - **_REDIS_SERVER_HOST_** - Redis storage host (default: localhost) + - **_REDIS_SERVER_PORT_** - Redis storage port (default: 6379) + - **_APP_LOG_LEVEL_** - application logger level (levels: debug, info, warn, error) + - **_ENABLE_PM_** - enable process monitoring with [PM2] module + +Each configuration field can be overridden with corresponding environmental variable with "BROKER" prefix, for example: + + BROKER.BROKER_PORT=6000 + +Prefix separator can be overridden by **_ENVSEPARATOR_** environmental variable. Example: + + ENVSEPARATOR=_ + BROKER_BROKER_PORT=6000 + +## Broker modules logging +Through the "DEBUG" ([debug]) environment variable you are able to specify next modules loggers: + +- **_subscriptionmanager_** - SubscriptionManager module logging; +- **_websocketfactory_** - WebSocketFactory module logging; +- **_websocketmanager_** - WebSocketManager module logging; + +Example: + + DEBUG=subscriptionmanager,websocketfactory,websocketmanager + Run with Node --- In the folder of cloned **devicehive-mqtt** repo run next commands: @@ -59,11 +79,7 @@ Install all dependencies: Start broker: - node ./src/broker.js - -Where: - -_environmental-variables-list_ - list of environmental variables, e.g. _NODE_ENV=prod,BROKER_PORT=1883,..._ + node ./src/broker.js Also, it's pretty useful to enable process monitoring with [PM2] module (ENABLE_PM environmental variables) and start the broker via PM2. @@ -125,6 +141,7 @@ DeviceHive messaging structure projection on MQTT topic structure === [DeviceHive] has next structure entities: - network +- device type - device - message type * notification @@ -146,14 +163,17 @@ To receive responses of request the MQTT client should subscribe to the response Where _requestAction_ ia a request action (**user/get**, **device/delete**, **token/refresh etc.**) Response topic should be always private (e.g. with client ID mentioned) -The MQTT client is able to subscribe to the notification/command topic to receive notification/command push messages +The MQTT client is able to subscribe to the notification/command/command_update topic to receive notification/command/command_update push messages - dh/notification///[@] + dh/notification////[@] - dh/command///[@] + dh/command////[@] + + dh/command_update////[@] Where: - networkID - id of the network +- deviceTypeID - id of the device type - deviceID - id of the device - notificationName - notification name - commandName - command name @@ -234,15 +254,15 @@ _**Connection with username and password, subscription for notification and comm client.on('connect', () => { /* Subscribe for notification push messages with name = notificationName of device with id = deviceId on network with id = networkId */ - client.subscribe('dh/notification///'); + client.subscribe('dh/notification////'); /* Subscribe for notification push messages with name = notificationName of any device on network with id = networkId */ - client.subscribe('dh/notification//+/'); + client.subscribe('dh/notification///+/'); /* Subscribe for command push messages with name = commandName of device with id = deviceId on network with id = networkId */ - client.subscribe('dh/command///'); + client.subscribe('dh/command////'); /* Subscribe for command push messages on network with id = networkId for any device with any command name */ diff --git a/config/index.js b/config/index.js new file mode 100644 index 0000000..ebab19d --- /dev/null +++ b/config/index.js @@ -0,0 +1,7 @@ +const path = require(`path`); +const configurator = require(`json-evn-configurator`); + + +module.exports = { + broker: configurator(path.join(__dirname, `../src/config.json`), `BROKER`) +}; \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d3adc9b..a39eb6a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,7 +19,7 @@ services: - cote - redis environment: - - WS_SERVER_URL=ws://playground.dev.devicehive.com/api/websocket + - WS_SERVER_URL=ws://playground-dev.devicehive.com/api/websocket - REDIS_SERVER_HOST=redis - REDIS_SERVER_PORT=6379 - ENABLE_PM=false @@ -30,7 +30,7 @@ services: - cote - redis environment: - - WS_SERVER_URL=ws://playground.dev.devicehive.com/api/websocket + - WS_SERVER_URL=ws://playground-dev.devicehive.com/api/websocket - REDIS_SERVER_HOST=redis - REDIS_SERVER_PORT=6379 - ENABLE_PM=false @@ -41,7 +41,7 @@ services: - cote - redis environment: - - WS_SERVER_URL=ws://playground.dev.devicehive.com/api/websocket + - WS_SERVER_URL=ws://playground-dev.devicehive.com/api/websocket - REDIS_SERVER_HOST=redis - REDIS_SERVER_PORT=6379 - ENABLE_PM=false @@ -52,7 +52,7 @@ services: - cote - redis environment: - - WS_SERVER_URL=ws://playground.dev.devicehive.com/api/websocket + - WS_SERVER_URL=ws://playground-dev.devicehive.com/api/websocket - REDIS_SERVER_HOST=redis - REDIS_SERVER_PORT=6379 - ENABLE_PM=false diff --git a/lib/TopicStructure.js b/lib/TopicStructure.js index f9b2598..794fd97 100644 --- a/lib/TopicStructure.js +++ b/lib/TopicStructure.js @@ -23,6 +23,7 @@ class TopicStructure { me.domain = ``; me.action = ``; me.network = ``; + me.deviceType = ``; me.device = ``; me.name = ``; me.owner = ``; @@ -40,14 +41,16 @@ class TopicStructure { const shift = me.response || me.request ? 1 : 0; const network = partedTopicBody[2 + shift]; - const device = partedTopicBody[3 + shift]; - let name = partedTopicBody[4 + shift]; + const deviceType = partedTopicBody[3 + shift]; + const device = partedTopicBody[4 + shift]; + let name = partedTopicBody[5 + shift]; name = (me.hasOwner() && name) ? name.split(CONST.CLIENT_ID_TOPIC_SPLITTER)[0] : name; me.domain = partedTopicBody[0]; me.action = partedTopicBody[1 + shift]; me.network = (!network || CONST.MQTT.WILDCARDS.includes(network)) ? `` : network; + me.deviceType = (!deviceType || CONST.MQTT.WILDCARDS.includes(deviceType)) ? `` : deviceType; me.device = (!device || CONST.MQTT.WILDCARDS.includes(device)) ? `` : device; me.name = (!name || CONST.MQTT.WILDCARDS.includes(name)) ? `` : name; @@ -129,10 +132,20 @@ class TopicStructure { * Get topic network * @returns {*} */ - getNetwork () { + getNetworkIds () { const me = this; - return me.network; + return !me.device && me.network ? [ me.network ] : undefined; + } + + /** + * Get topic device type + * @returns {*} + */ + getDeviceTypeIds () { + const me = this; + + return !me.device && me.deviceType ? [ me.deviceType ] : undefined; } /** @@ -142,17 +155,17 @@ class TopicStructure { getDevice () { const me = this; - return me.device; + return me.device || undefined; } /** * Get topic Name * @returns {*} */ - getName () { + getNames () { const me = this; - return me.name; + return me.name ? [ me.name ] : undefined; } /** @@ -215,15 +228,16 @@ class TopicStructure { CONST.TOPICS.PARTS.NOTIFICATION : CONST.TOPICS.PARTS.COMMAND; const network = dataObject[propertyKey].networkId; + const deviceType = dataObject[propertyKey].deviceTypeId; const device = dataObject[propertyKey].deviceId; const name = dataObject[propertyKey][propertyKey]; - topicParts = [CONST.TOPICS.PARTS.DH, action, network, device, name]; + topicParts = [CONST.TOPICS.PARTS.DH, action, network, deviceType, device, name]; } else { topicParts = [CONST.TOPICS.PARTS.DH, CONST.TOPICS.PARTS.RESPONSE, dataObject.action]; } - return !!owner ? topicParts.join("/") + CONST.CLIENT_ID_TOPIC_SPLITTER + owner : topicParts.join("/"); + return owner ? `${topicParts.join("/")}${CONST.CLIENT_ID_TOPIC_SPLITTER}${owner}` : topicParts.join("/"); } } diff --git a/package-lock.json b/package-lock.json index 3b20309..c8733e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -379,12 +379,12 @@ "dev": true }, "async-listener": { - "version": "0.6.8", - "resolved": "https://registry.npmjs.org/async-listener/-/async-listener-0.6.8.tgz", - "integrity": "sha512-1Sy1jDhjlgxcSd9/ICHqiAHT8VSJ9R1lzEyWwP/4Hm9p8nVTNtU0SxG/Z15XHD/aZvQraSw9BpDU3EBcFnOVrw==", + "version": "0.6.9", + "resolved": "https://registry.npmjs.org/async-listener/-/async-listener-0.6.9.tgz", + "integrity": "sha512-E7Z2/QMs0EPt/o9wpYO/J3hmMCDdr1aVDS3ttlur5D5JlZtxhfuOwi4e7S8zbYIxA5qOOYdxfqGj97XAfdNvkQ==", "requires": { "semver": "5.4.1", - "shimmer": "1.1.0" + "shimmer": "1.2.0" } }, "asynckit": { @@ -450,9 +450,9 @@ } }, "binary-extensions": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.10.0.tgz", - "integrity": "sha1-muuabF6IY4qtFx4Wf1kAq+JINdA=" + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.11.0.tgz", + "integrity": "sha1-RqoXUftqL5PuXmibsQh9SxTGwgU=" }, "bindings": { "version": "1.2.1", @@ -868,12 +868,12 @@ "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=" }, "continuation-local-storage": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/continuation-local-storage/-/continuation-local-storage-3.2.0.tgz", - "integrity": "sha1-4Z/Da1lwkKXU5KOy6j68XilpSiQ=", + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/continuation-local-storage/-/continuation-local-storage-3.2.1.tgz", + "integrity": "sha512-jx44cconVqkCEEyLSKWwkvUXwO561jXMa3LPjTPsm5QR22PA0/mhe33FT4Xb5y74JDvt/Cq+5lm8S8rskLv9ZA==", "requires": { - "async-listener": "0.6.8", - "emitter-listener": "1.0.1" + "async-listener": "0.6.9", + "emitter-listener": "1.1.1" } }, "cookie": { @@ -932,7 +932,7 @@ "resolved": "https://registry.npmjs.org/cron/-/cron-1.3.0.tgz", "integrity": "sha512-K/SF7JlgMmNjcThWxkKvsHhey2EDB4CeOEWJ9aXWj3fbQJppsvTPIeyLdHfNq5IbbsMUUjRW1nr5dSO95f2E4w==", "requires": { - "moment-timezone": "0.5.13" + "moment-timezone": "0.5.14" } }, "cryptiles": { @@ -1098,18 +1098,11 @@ } }, "emitter-listener": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/emitter-listener/-/emitter-listener-1.0.1.tgz", - "integrity": "sha1-skmepuWCMKUsJo1d8mHuzZ8Q/pc=", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/emitter-listener/-/emitter-listener-1.1.1.tgz", + "integrity": "sha1-6Lu+gkS8jg0LTvcc0UKUx/JBx+w=", "requires": { - "shimmer": "1.0.0" - }, - "dependencies": { - "shimmer": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.0.0.tgz", - "integrity": "sha1-ScLXHGeDYLgCvhiyeDgtHLuAXDk=" - } + "shimmer": "1.2.0" } }, "end-of-stream": { @@ -2726,9 +2719,9 @@ "optional": true }, "interpret": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.0.4.tgz", - "integrity": "sha1-ggzdWIuGj/sZGoCVBtbJyPISsbA=" + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.1.0.tgz", + "integrity": "sha1-ftGxQQxqDg94z5XTuEQMY/eLhhQ=" }, "ioredis": { "version": "1.15.1", @@ -2785,7 +2778,7 @@ "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-1.0.1.tgz", "integrity": "sha1-dfFmQrSA8YenEcgUFh/TpKdlWJg=", "requires": { - "binary-extensions": "1.10.0" + "binary-extensions": "1.11.0" } }, "is-buffer": { @@ -2928,6 +2921,11 @@ "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-2.0.11.tgz", "integrity": "sha1-PkQf2jCYvo0eMXGtWRvGKjPi1V8=" }, + "json-evn-configurator": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/json-evn-configurator/-/json-evn-configurator-1.0.4.tgz", + "integrity": "sha512-uO5A+bnthuWuXjzm0sk4HKyj7gywmIhvEWxx7GBj1ybAbuzk4m3pzTHEXWc38g3/sQZzFP5aZb5YBPMTJGZfLw==" + }, "json-schema": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", @@ -3286,9 +3284,9 @@ "integrity": "sha1-0uPuv/DZ05rVD1y9G1KnvOa7YRs=" }, "lolex": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/lolex/-/lolex-2.1.3.tgz", - "integrity": "sha512-BdHq78SeI+6PAUtl4atDuCt7L6E4fab3mSRtqxm4ywaXe4uP7jZ0TTcFNuU20syUjxZc2l7jFqKVMJ+AX0LnpQ==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/lolex/-/lolex-2.3.1.tgz", + "integrity": "sha512-mQuW55GhduF3ppo+ZRUTz1PRjEh1hS5BbqU7d8D0ez2OKxHDod7StPPeAVKisZR5aLkHZjdGWSL42LSONUJsZw==", "dev": true }, "long": { @@ -3422,9 +3420,9 @@ } }, "mocha": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-4.0.1.tgz", - "integrity": "sha512-evDmhkoA+cBNiQQQdSKZa2b9+W2mpLoj50367lhy+Klnx9OV8XlCIhigUnn1gaTFLQCa0kdNhEGDr0hCXOQFDw==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-4.1.0.tgz", + "integrity": "sha512-0RVnjg1HJsXY2YFDoTNzcc1NKhYuXKRrBAG2gDygmJJA136Cs2QlRliZG1mA0ap7cuaT30mw16luAeln+4RiNA==", "dev": true, "requires": { "browser-stdout": "1.3.0", @@ -3445,15 +3443,6 @@ "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==", "dev": true }, - "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", - "dev": true, - "requires": { - "ms": "2.0.0" - } - }, "supports-color": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz", @@ -3466,16 +3455,16 @@ } }, "moment": { - "version": "2.19.1", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.19.1.tgz", - "integrity": "sha1-VtoaLRy/AdOLfhr8McELz6GSkWc=" + "version": "2.20.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.20.1.tgz", + "integrity": "sha512-Yh9y73JRljxW5QxN08Fner68eFLxM5ynNOAw2LbIB1YAGeQzZT8QFSUvkAz609Zf+IHhhaUxqZK8dG3W/+HEvg==" }, "moment-timezone": { - "version": "0.5.13", - "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.13.tgz", - "integrity": "sha1-mc5cfYJyYusPH3AgRBd/YHRde5A=", + "version": "0.5.14", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.14.tgz", + "integrity": "sha1-TrOP+VOLgBCLpGekWPPtQmjM/LE=", "requires": { - "moment": "2.19.1" + "moment": "2.20.1" } }, "mongodb": { @@ -3605,9 +3594,9 @@ "integrity": "sha1-mxnDdpeOIblF7Xd2eO2VTUt7VHU=" }, "mqtt": { - "version": "2.13.1", - "resolved": "https://registry.npmjs.org/mqtt/-/mqtt-2.13.1.tgz", - "integrity": "sha512-cLDt7BDE+Y05yAzLvehF+YEJ5WqoRTar2iwa4tL6sKBcejR/gycD1k8DTxzOmnt0pPJ9PhEmtdHfmAdVN8ROcg==", + "version": "2.15.1", + "resolved": "https://registry.npmjs.org/mqtt/-/mqtt-2.15.1.tgz", + "integrity": "sha512-wcU1Ec/PqdgmWZ8InKd9298UlHbsL4ujnUdkkN1JIee0HI1Qe+JvZhO66qCYQKEH+U2XsJMcr9GncQPKUEfmRw==", "dev": true, "requires": { "commist": "1.0.0", @@ -3617,11 +3606,11 @@ "inherits": "2.0.3", "minimist": "1.2.0", "mqtt-packet": "5.4.0", - "pump": "1.0.2", + "pump": "2.0.0", "readable-stream": "2.3.3", "reinterval": "1.1.0", "split2": "2.2.0", - "websocket-stream": "5.0.1", + "websocket-stream": "5.1.1", "xtend": "4.0.1" }, "dependencies": { @@ -3658,6 +3647,16 @@ "safe-buffer": "5.1.1" } }, + "pump": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-2.0.0.tgz", + "integrity": "sha512-6MYypjOvtiXhBSTOD0Zs5eNjCGfnqi5mPsCsW+dgKTxrZzQMZQNpBo3XRkLx7id753f3EeyHLBqzqqUymIolgw==", + "dev": true, + "requires": { + "end-of-stream": "1.4.0", + "once": "1.4.0" + } + }, "readable-stream": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.3.tgz", @@ -3683,34 +3682,34 @@ } }, "ultron": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.0.tgz", - "integrity": "sha1-sHoualQagV/Go0zNRTO67DB8qGQ=", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", + "integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==", "dev": true }, "websocket-stream": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/websocket-stream/-/websocket-stream-5.0.1.tgz", - "integrity": "sha512-N3X0rs/ucnccDdyn5GkwD7HVjtvcE3scSmYzdndzma/3ovQpjUxDRP71nTliph9EpXYt9fXbdCVL+e6JuguC+A==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/websocket-stream/-/websocket-stream-5.1.1.tgz", + "integrity": "sha512-ypQ50zVCnikSvJcRFWaZh7xeCufSje5+mbJRq3mdvdNx+06TD98C+bQsSKc7FkI6y1PVuNbzkenGywxlFiQeUQ==", "dev": true, "requires": { "duplexify": "3.5.1", "inherits": "2.0.3", "readable-stream": "2.3.3", "safe-buffer": "5.1.1", - "ws": "3.2.0", + "ws": "3.3.3", "xtend": "4.0.1" } }, "ws": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-3.2.0.tgz", - "integrity": "sha512-hTS3mkXm/j85jTQOIcwVz3yK3up9xHgPtgEhDBOH3G18LDOZmSAG1omJeXejLKJakx+okv8vS1sopgs7rw0kVw==", + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-3.3.3.tgz", + "integrity": "sha512-nnWLa/NwZSt4KQJu51MYlCcSQ5g7INpOrOMt4XV8j4dqTXdmlUmSHQ8/oLC069ckre0fRsgfvsKwbTdtKLCDkA==", "dev": true, "requires": { "async-limiter": "1.0.0", "safe-buffer": "5.1.1", - "ultron": "1.1.0" + "ultron": "1.1.1" } } } @@ -4281,60 +4280,52 @@ } }, "pm2": { - "version": "2.7.2", - "resolved": "https://registry.npmjs.org/pm2/-/pm2-2.7.2.tgz", - "integrity": "sha512-dnzaW8F+5c+bcctPvXiFxEi2OEfL5gpV0QTA9duLJ5Jo/27aFYrsFjXLRUd6Lb26wjgWCIp3fNX+v2ErDgZuQQ==", + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/pm2/-/pm2-2.9.1.tgz", + "integrity": "sha512-y6Wwo/jdBxIWPzZQFmuwxsrCG4nhaq+yEArULMDD8N68q/FErvMgn25P5CmzoYz/ZeT7lIyU1gHTdx3uNFF1VA==", "requires": { - "async": "2.5.0", + "async": "2.6.0", "blessed": "0.1.81", "chalk": "1.1.3", "chokidar": "1.7.0", "cli-table-redemption": "1.0.1", - "commander": "2.11.0", + "commander": "2.12.2", "cron": "1.3.0", "debug": "3.1.0", "eventemitter2": "1.0.5", "fclone": "1.0.11", "gkt": "https://tgz.pm2.io/gkt-1.0.0.tgz", "mkdirp": "0.5.1", - "moment": "2.19.1", + "moment": "2.20.1", "needle": "1.6.0", "nssocket": "0.6.0", "pidusage": "1.2.0", "pm2-axon": "3.1.0", "pm2-axon-rpc": "0.5.0", - "pm2-deploy": "0.3.8", + "pm2-deploy": "0.3.9", "pm2-multimeter": "0.1.2", - "pmx": "1.5.4", + "pmx": "1.5.5", "promptly": "2.2.0", "semver": "5.4.1", "shelljs": "0.7.8", - "source-map-support": "0.4.18", + "source-map-support": "0.5.1", "sprintf-js": "1.1.1", "vizion": "0.2.13", "yamljs": "0.3.0" }, "dependencies": { "async": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/async/-/async-2.5.0.tgz", - "integrity": "sha512-e+lJAJeNWuPCNyxZKOBdaJGyLGHugXVQtrAwtuAe2vhxTYxFTKE73p8JuTmdH0qdQZtDvI4dhJwjZc5zsfIsYw==", + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz", + "integrity": "sha512-xAfGg1/NTLBBKlHFmnd7PlmUW9KhVQIUuSrYem9xzFUZy13ScvtyGGejaae9iAVRiRq9+Cx7DPFaAAhCpyxyPw==", "requires": { "lodash": "4.17.4" } }, "commander": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", - "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==" - }, - "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", - "requires": { - "ms": "2.0.0" - } + "version": "2.12.2", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.12.2.tgz", + "integrity": "sha512-BFnaq5ZOGcDN7FlrtBT4xxkgIToalIIxwjxLWVJ8bGTpe1LroqMiqQXdA7ygc7CRvaYS+9zfPGFnJqFSayx+AA==" }, "eventemitter2": { "version": "1.0.5", @@ -4352,16 +4343,6 @@ "amp-message": "0.1.2", "debug": "3.1.0", "escape-regexp": "0.0.1" - }, - "dependencies": { - "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", - "requires": { - "ms": "2.0.0" - } - } } }, "pm2-axon-rpc": { @@ -4371,22 +4352,12 @@ "requires": { "debug": "3.1.0", "fclone": "1.0.11" - }, - "dependencies": { - "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", - "requires": { - "ms": "2.0.0" - } - } } }, "pm2-deploy": { - "version": "0.3.8", - "resolved": "https://registry.npmjs.org/pm2-deploy/-/pm2-deploy-0.3.8.tgz", - "integrity": "sha512-oalYjMYR4rcB5S+EZHOolSSElBbYmqnOhZZVdzGg3ccttpyDt/4b++xjc+Psys7JX6wn1pMs53xDErFzOmRWig==", + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/pm2-deploy/-/pm2-deploy-0.3.9.tgz", + "integrity": "sha512-IYF45fPwfLE27BivrtodK7zzN56BNDErK7brcldIHjVIHLlk+cdhijq3kwTkPPP3Tpc3H2C942QGRgjg0hHajA==", "requires": { "async": "1.5.2", "tv4": "1.3.0" @@ -4401,9 +4372,9 @@ } }, "pmx": { - "version": "1.5.4", - "resolved": "https://registry.npmjs.org/pmx/-/pmx-1.5.4.tgz", - "integrity": "sha512-Rgfvvgc53jxowBG4ngFiBzCoICRvqNBjLMtegqpIKEth8U+4PwOG0PdJw0/OsfsYH+eHu4VHsxA13RLK1Mq2yA==", + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/pmx/-/pmx-1.5.5.tgz", + "integrity": "sha1-tuC4V27c9Y1/QGlntE2z2nfjV/A=", "requires": { "debug": "3.1.0", "json-stringify-safe": "5.0.1", @@ -4965,14 +4936,14 @@ "integrity": "sha1-3svPh0sNHl+3LhSxZKloMEjprLM=", "requires": { "glob": "7.1.2", - "interpret": "1.0.4", + "interpret": "1.1.0", "rechoir": "0.6.2" } }, "shimmer": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.1.0.tgz", - "integrity": "sha1-l9c3cTf/u6tCVSLkKf4KqJpIizU=" + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.2.0.tgz", + "integrity": "sha512-xTCx2vohXC2EWWDqY/zb4+5Mu28D+HYNSOuFzsyRDRvI/e1ICb69afwaUwfjr+25ZXldbOLyp+iDUZHq8UnTag==" }, "signal-exit": { "version": "3.0.2", @@ -4998,28 +4969,34 @@ "optional": true }, "sinon": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/sinon/-/sinon-4.0.2.tgz", - "integrity": "sha512-4mUsjHfjrHyPFGDTtNJl0q8cv4VOJGvQykI1r3fnn05ys0sQL9M1Y+DyyGNWLD2PMcoyqjJ/nFDm4K54V1eQOg==", + "version": "4.1.6", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-4.1.6.tgz", + "integrity": "sha1-nLNGvdsYDWioBEKf/hSXjX+v1ik=", "dev": true, "requires": { "diff": "3.3.1", "formatio": "1.2.0", "lodash.get": "4.4.2", - "lolex": "2.1.3", + "lolex": "2.3.1", "nise": "1.2.0", - "supports-color": "4.5.0", - "type-detect": "4.0.3" + "supports-color": "5.1.0", + "type-detect": "4.0.7" }, "dependencies": { "supports-color": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.5.0.tgz", - "integrity": "sha1-vnoN5ITexcXN34s9WRJQRJEvY1s=", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.1.0.tgz", + "integrity": "sha512-Ry0AwkoKjDpVKK4sV4h6o3UJmNRbjYm2uXhwfj3J56lMVdvnUNqzQVRztOOMGQ++w1K/TjNDFvpJk0F/LoeBCQ==", "dev": true, "requires": { "has-flag": "2.0.0" } + }, + "type-detect": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.7.tgz", + "integrity": "sha512-4Rh17pAMVdMWzktddFhISRnUnFIStObtUMNGzDwlA6w/77bmGv3aBbRdCmQR6IjzfkTo9otnW+2K/cDRhKSxDA==", + "dev": true } } }, @@ -5136,17 +5113,17 @@ } }, "source-map-support": { - "version": "0.4.18", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.4.18.tgz", - "integrity": "sha512-try0/JqxPLF9nOjvSta7tVondkP5dwgyLDjVoyMDlmjugT2lRZ1OfsrYTkCd2hkDnJTKRbO/Rl3orm8vlsUzbA==", + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.1.tgz", + "integrity": "sha512-EZNecLNrsdRk9fcdOcjjy+Z/id7cr68sdmsYtR1gA45oQ81Ccea0UvM7DdSRblO0Ie5zWX31bvJTC7Y3QZVujg==", "requires": { - "source-map": "0.5.7" + "source-map": "0.6.1" }, "dependencies": { "source-map": { - "version": "0.5.7", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", - "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=" + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" } } }, @@ -5679,7 +5656,7 @@ "resolved": "https://registry.npmjs.org/vxx/-/vxx-1.2.2.tgz", "integrity": "sha1-dB+1HG8R0zg9pvm5IBil17qAdhE=", "requires": { - "continuation-local-storage": "3.2.0", + "continuation-local-storage": "3.2.1", "debug": "2.6.9", "extend": "3.0.1", "is": "3.2.1", @@ -5688,8 +5665,8 @@ "lodash.merge": "4.6.0", "methods": "1.1.2", "semver": "5.4.1", - "shimmer": "1.1.0", - "uuid": "3.1.0" + "shimmer": "1.2.0", + "uuid": "3.2.1" }, "dependencies": { "debug": { @@ -5701,9 +5678,9 @@ } }, "uuid": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.1.0.tgz", - "integrity": "sha512-DIWtzUkw04M4k3bf1IcpS2tngXEL26YUD2M0tMDUpnUrz2hgzUBlD55a4FjdLGPvfHxS6uluGWvaVEqgBcVa+g==" + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.2.1.tgz", + "integrity": "sha512-jZnMwlb9Iku/O3smGWvZhauCf6cvvpKi4BKRiliS3cxnI+Gz9j5MEpTz2UFuXiKPJocb7gnsLHwiS05ige5BEA==" } } }, diff --git a/package.json b/package.json index a7b5151..9d787ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "devicehive-mqtt", - "version": "1.0.0", + "version": "1.1.0", "description": "MQTT plugin for DeviceHive server", "main": "src/broker.js", "scripts": { @@ -21,18 +21,19 @@ "dependencies": { "cote": "^0.15.1", "debug": "^3.1.0", + "json-evn-configurator": "^1.0.4", "lru-cache": "^4.1.1", "mosca": "^2.7.0", - "pm2": "^2.7.2", - "pmx": "^1.5.4", + "pm2": "^2.9.1", + "pmx": "^1.5.5", "randomstring": "^1.1.5", "winston": "^2.4.0" }, "devDependencies": { "chai": "^4.1.2", "co-mocha": "^1.2.1", - "mocha": "^4.0.1", - "mqtt": "^2.13.1", - "sinon": "^4.0.2" + "mocha": "^4.1.0", + "mqtt": "^2.15.1", + "sinon": "^4.1.6" } } diff --git a/src/ApplicationLogger.js b/src/ApplicationLogger.js index 71f3ef4..467dc0d 100644 --- a/src/ApplicationLogger.js +++ b/src/ApplicationLogger.js @@ -1,7 +1,6 @@ const winston = require(`winston`); const config = winston.config; -const APP_LOG_LEVEL = process.env.APP_LOG_LEVEL || `info`; /** * Application logger facade class. @@ -11,7 +10,7 @@ class ApplicationLogger { /** * Create new ApplicationLogger */ - constructor () { + constructor (loggingLevel) { const me = this; const loggerConfig = { levels: { @@ -29,7 +28,7 @@ class ApplicationLogger { transports: [ new (winston.transports.Console)({ colorize: true, - level: APP_LOG_LEVEL, + level: loggingLevel, timestamp: () => (new Date()).toISOString(), formatter: (options) => { const pid = process.pid; diff --git a/src/broker.js b/src/broker.js index 1727f1b..ed16bbc 100644 --- a/src/broker.js +++ b/src/broker.js @@ -1,3 +1,4 @@ +const BrokerConfig = require(`../config`).broker; const CONST = require('../util/constants.json'); const mosca = require('mosca'); const ApplicationLogger = require(`./ApplicationLogger.js`); @@ -8,36 +9,19 @@ const TopicStructure = require('../lib/TopicStructure.js'); const SubscriptionManager = require('../lib/SubscriptionManager.js'); const DeviceHiveUtils = require('../util/DeviceHiveUtils.js'); -/** - * Environmental variables - * NODE_ENV - "dev" for development - * BROKER_PORT - port on wich broker will start - * WS_SERVER_URL - path to Web Socket server - * REDIS_SERVER_HOST - Redis storage host - * REDIS_SERVER_PORT - Redis storage port - * DEBUG - to enable modules logging - * APP_LOG_LEVEL - application logger level - * ENABLE_PM - enable process monitoring - */ - -const IS_DEV = process.env.NODE_ENV === CONST.DEV; -const WS_SERVER_URL = process.env.WS_SERVER_URL || CONST.WS.DEV_HOST; -const BROKER_PORT = process.env.BROKER_PORT || CONST.MQTT.DEFAULT_PORT; -const REDIS_SERVER_HOST = process.env.REDIS_SERVER_HOST || CONST.PERSISTENCE.REDIS_DEV_HOST; -const REDIS_SERVER_PORT = process.env.REDIS_SERVER_PORT || CONST.PERSISTENCE.REDIS_DEV_PORT; -const appLogger = new ApplicationLogger(); +const appLogger = new ApplicationLogger(BrokerConfig.APP_LOG_LEVEL); const crossBrokerCommunicator = new CrossBrokerCommunicator(); const brokerProcessMonitoring = new BrokerProcessMonitoring(); const subscriptionManager = new SubscriptionManager(); -const wsManager = new WebSocketManager(WS_SERVER_URL); +const wsManager = new WebSocketManager(BrokerConfig.WS_SERVER_URL); const server = new mosca.Server({ - port: Number(BROKER_PORT), + port: Number(BrokerConfig.BROKER_PORT), stats: true, persistence: { factory: mosca.persistence.Redis, - host: REDIS_SERVER_HOST, - port: REDIS_SERVER_PORT, + host: BrokerConfig.REDIS_SERVER_HOST, + port: BrokerConfig.REDIS_SERVER_PORT, ttl: { subscriptions: CONST.PERSISTENCE.MAX_NUMBER_OF_SUBSCRIPTIONS, packets: CONST.PERSISTENCE.MAX_NUMBER_OF_PACKETS @@ -134,7 +118,7 @@ server.authorizeSubscribe = function (client, topic, callback) { }; server.on(`ready`, () => { - appLogger.info(`broker has been started on port: ${BROKER_PORT}`); + appLogger.info(`broker has been started on port: ${BrokerConfig.BROKER_PORT}`); }); server.on(`clientConnected`, (client) => { diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..6c4bfef --- /dev/null +++ b/src/config.json @@ -0,0 +1,8 @@ +{ + "BROKER_PORT": 1883, + "WS_SERVER_URL": "ws://localhost:8080/dh/websocket", + "REDIS_SERVER_HOST": "localhost", + "REDIS_SERVER_PORT": "6379", + "APP_LOG_LEVEL": "info", + "ENABLE_PM": true +} \ No newline at end of file diff --git a/test/config/index.js b/test/config/index.js new file mode 100644 index 0000000..e3f54a5 --- /dev/null +++ b/test/config/index.js @@ -0,0 +1,9 @@ +const path = require(`path`); +const configurator = require(`json-evn-configurator`); + + +module.exports = { + test: { + integration: configurator(path.join(__dirname, `../integration/config.json`), `INTEGRATION_TEST`) + } +}; \ No newline at end of file diff --git a/test/integration/api/authentication.js b/test/integration/api/authentication.js index 9065b1c..391a696 100644 --- a/test/integration/api/authentication.js +++ b/test/integration/api/authentication.js @@ -1,41 +1,33 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `authenticate`; const AUTHENTICATE_OPERATION = SUBJECT; -const AUTHENTICATE_ACTION = AUTHENTICATE_OPERATION; -const AUTHENTICATE_TOPIC = `${DH_RESPONSE_TOPIC}/${AUTHENTICATE_ACTION}`; const TOKEN_OPERATION = `token`; +const AUTHENTICATE_ACTION = AUTHENTICATE_OPERATION; +const AUTHENTICATE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${AUTHENTICATE_ACTION}`; const TOKEN_ACTION = TOKEN_OPERATION; -const TOKEN_TOPIC = `${DH_RESPONSE_TOPIC}/${TOKEN_ACTION}`; +const TOKEN_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${TOKEN_ACTION}`; let mqttClient; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[2].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -73,32 +65,28 @@ it(`should authenticate by access token received from server`, () => { const requestId2 = randomString.generate(); return new Promise((resolve) => { - ee.once(AUTHENTICATE_OPERATION, (message) => { - if (message.requestId === requestId2) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId1, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - ee.once(TOKEN_OPERATION, (message) => { - if (message.requestId === requestId1) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message).to.include.all.keys(`accessToken`, `refreshToken`); + ee.once(requestId2, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message).to.include.all.keys(`accessToken`, `refreshToken`); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ - action: AUTHENTICATE_ACTION, - requestId: requestId2, - token: message.accessToken - })); - } + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: AUTHENTICATE_ACTION, + requestId: requestId2, + token: message.accessToken + })); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: TOKEN_ACTION, requestId: requestId1, - login: TEST_LOGIN, - password: TEST_PASSWORD + login: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD })); }); }); diff --git a/test/integration/api/cluster.js b/test/integration/api/cluster.js index 6de9d61..4dc9034 100644 --- a/test/integration/api/cluster.js +++ b/test/integration/api/cluster.js @@ -1,38 +1,30 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `cluster`; const INFO_OPERATION = `info`; const INFO_ACTION = `${SUBJECT}/${INFO_OPERATION}`; -const INFO_TOPIC = `${DH_RESPONSE_TOPIC}/${INFO_ACTION}`; +const INFO_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INFO_ACTION}`; let mqttClient; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -57,16 +49,14 @@ it(`should get cluster information`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(INFO_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message).to.include.all.keys(`clusterInfo`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message).to.include.all.keys(`clusterInfo`); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: INFO_ACTION, requestId: requestId })); diff --git a/test/integration/api/command.js b/test/integration/api/command.js index 7bdb99a..117be7e 100644 --- a/test/integration/api/command.js +++ b/test/integration/api/command.js @@ -1,23 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `command`; const GET_OPERATION = `get`; const LIST_OPERATION = `list`; @@ -27,11 +17,11 @@ const GET_ACTION = `${SUBJECT}/${GET_OPERATION}`; const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; const INSERT_ACTION = `${SUBJECT}/${INSERT_OPERATION}`; const UPDATE_ACTION = `${SUBJECT}/${UPDATE_OPERATION}`; -const GET_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_ACTION}`; -const LIST_TOPIC = `${DH_RESPONSE_TOPIC}/${LIST_ACTION}`; -const INSERT_TOPIC = `${DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; -const UPDATE_TOPIC = `${DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; -const TEST_COMMAND_NAME = `mqtt-broker-integration-tests-command-name`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const INSERT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; +const UPDATE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; +const TEST_COMMAND_NAME = randomString.generate(); const START_TEST_COMMAND_PARAMETERS = { parameter: `startParameter` }; const UPDATED_TEST_COMMAND_PARAMETERS = { parameter: `updatedParameter` }; const COMMAND_LIFETIME = 20; @@ -39,13 +29,15 @@ let mqttClient, testCommandId; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -102,25 +94,23 @@ it(`should subscribe for "${UPDATE_TOPIC}" topic`, () => { }); }); -it(`should create new command with name: "${TEST_COMMAND_NAME}" for device with id: "${DEVICE_ID}"`, () => { +it(`should create new command with name: "${TEST_COMMAND_NAME}" for device with id: "${Config.DEVICE_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(INSERT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.command).to.be.an(`object`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.command).to.be.an(`object`); - testCommandId = message.command.id; + testCommandId = message.command.id; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: INSERT_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, command: { command: TEST_COMMAND_NAME, parameters: START_TEST_COMMAND_PARAMETERS, @@ -134,49 +124,45 @@ it(`should query the command with name: "${TEST_COMMAND_NAME}" and parameters: " const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.command).to.be.an(`object`); - expect(message.command.id).to.equal(testCommandId); - expect(message.command.command).to.equal(TEST_COMMAND_NAME); - expect(message.command.deviceId).to.equal(DEVICE_ID); - expect(message.command.parameters).to.deep.equal(START_TEST_COMMAND_PARAMETERS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.command).to.be.an(`object`); + expect(message.command.id).to.equal(testCommandId); + expect(message.command.command).to.equal(TEST_COMMAND_NAME); + expect(message.command.deviceId).to.equal(Config.DEVICE_ID); + expect(message.command.parameters).to.deep.equal(START_TEST_COMMAND_PARAMETERS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, commandId: testCommandId })); }); }); -it(`should query the list of command for device with id: "${DEVICE_ID}" with existing command with name: "${TEST_COMMAND_NAME}"`, () => { +it(`should query the list of command for device with id: "${Config.DEVICE_ID}" with existing command with name: "${TEST_COMMAND_NAME}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.commands).to.be.an(`array`); - expect(message.commands.map((commandObject) => commandObject.id)) - .to.include.members([testCommandId]); - expect(message.commands.map((commandObject) => commandObject.command)) - .to.include.members([TEST_COMMAND_NAME]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.commands).to.be.an(`array`); + expect(message.commands.map((commandObject) => commandObject.id)) + .to.include.members([testCommandId]); + expect(message.commands.map((commandObject) => commandObject.command)) + .to.include.members([TEST_COMMAND_NAME]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, take: 1000 })); }); @@ -186,18 +172,16 @@ it(`should update the command parameters: "${JSON.stringify(START_TEST_COMMAND_P const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(UPDATE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: UPDATE_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, commandId: testCommandId, command: { parameters: UPDATED_TEST_COMMAND_PARAMETERS @@ -210,23 +194,21 @@ it(`should query the updated command where updated parameters are: "${JSON.strin const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.command).to.be.an(`object`); - expect(message.command.id).to.equal(testCommandId); - expect(message.command.command).to.equal(TEST_COMMAND_NAME); - expect(message.command.deviceId).to.equal(DEVICE_ID); - expect(message.command.parameters).to.deep.equal(UPDATED_TEST_COMMAND_PARAMETERS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.command).to.be.an(`object`); + expect(message.command.id).to.equal(testCommandId); + expect(message.command.command).to.equal(TEST_COMMAND_NAME); + expect(message.command.deviceId).to.equal(Config.DEVICE_ID); + expect(message.command.parameters).to.deep.equal(UPDATED_TEST_COMMAND_PARAMETERS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, commandId: testCommandId })); }); diff --git a/test/integration/api/configuration.js b/test/integration/api/configuration.js index b4a2c06..db5fffe 100644 --- a/test/integration/api/configuration.js +++ b/test/integration/api/configuration.js @@ -1,23 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `configuration`; const GET_OPERATION = `get`; const PUT_OPERATION = `put`; @@ -25,23 +15,25 @@ const DELETE_OPERATION = `delete`; const GET_ACTION = `${SUBJECT}/${GET_OPERATION}`; const PUT_ACTION = `${SUBJECT}/${PUT_OPERATION}`; const DELETE_ACTION = `${SUBJECT}/${DELETE_OPERATION}`; -const GET_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_ACTION}`; -const PUT_TOPIC = `${DH_RESPONSE_TOPIC}/${PUT_ACTION}`; -const DELETE_TOPIC = `${DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; -const TEST_CONFIGURATION_NAME = `mqttTestsConfigurationName`; -const START_TEST_CONFIGURATION_VALUE = `mqttTestsConfigurationValue`; -const UPDATED_TEST_CONFIGURATION_VALUE = `mqttTestsConfigurationValueUpdated`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const PUT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${PUT_ACTION}`; +const DELETE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; +const TEST_CONFIGURATION_NAME = randomString.generate(); +const START_TEST_CONFIGURATION_VALUE = randomString.generate(); +const UPDATED_TEST_CONFIGURATION_VALUE = randomString.generate(); let mqttClient; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -90,18 +82,16 @@ it(`should put new configuration with name: "${TEST_CONFIGURATION_NAME}" and val const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(PUT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.configuration).to.be.an(`object`); - expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); - expect(message.configuration.value).to.equal(START_TEST_CONFIGURATION_VALUE); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.configuration).to.be.an(`object`); + expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); + expect(message.configuration.value).to.equal(START_TEST_CONFIGURATION_VALUE); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: PUT_ACTION, requestId: requestId, name: TEST_CONFIGURATION_NAME, @@ -114,18 +104,16 @@ it(`should query configuration with name: "${TEST_CONFIGURATION_NAME}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.configuration).to.be.an(`object`); - expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); - expect(message.configuration.value).to.equal(START_TEST_CONFIGURATION_VALUE); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.configuration).to.be.an(`object`); + expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); + expect(message.configuration.value).to.equal(START_TEST_CONFIGURATION_VALUE); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, name: TEST_CONFIGURATION_NAME @@ -137,18 +125,16 @@ it(`should update configuration with name: "${TEST_CONFIGURATION_NAME}" by new v const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(PUT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.configuration).to.be.an(`object`); - expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); - expect(message.configuration.value).to.equal(UPDATED_TEST_CONFIGURATION_VALUE); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.configuration).to.be.an(`object`); + expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); + expect(message.configuration.value).to.equal(UPDATED_TEST_CONFIGURATION_VALUE); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: PUT_ACTION, requestId: requestId, name: TEST_CONFIGURATION_NAME, @@ -161,18 +147,16 @@ it(`should query updated configuration with name: "${TEST_CONFIGURATION_NAME}" a const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.configuration).to.be.an(`object`); - expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); - expect(message.configuration.value).to.equal(UPDATED_TEST_CONFIGURATION_VALUE); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.configuration).to.be.an(`object`); + expect(message.configuration.name).to.equal(TEST_CONFIGURATION_NAME); + expect(message.configuration.value).to.equal(UPDATED_TEST_CONFIGURATION_VALUE); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, name: TEST_CONFIGURATION_NAME @@ -184,15 +168,13 @@ it(`should delete configuration with name: "${TEST_CONFIGURATION_NAME}"`, () => const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(DELETE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: DELETE_ACTION, requestId: requestId, name: TEST_CONFIGURATION_NAME @@ -204,15 +186,13 @@ it(`should check that configuration with name: "${TEST_CONFIGURATION_NAME}" has const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(ERROR_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.ERROR_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId })); diff --git a/test/integration/api/device.js b/test/integration/api/device.js index 02dc252..2dbf9a4 100644 --- a/test/integration/api/device.js +++ b/test/integration/api/device.js @@ -1,22 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; + const SUBJECT = `device`; const GET_OPERATION = `get`; const LIST_OPERATION = `list`; @@ -26,26 +17,28 @@ const GET_ACTION = `${SUBJECT}/${GET_OPERATION}`; const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; const SAVE_ACTION = `${SUBJECT}/${SAVE_OPERATION}`; const DELETE_ACTION = `${SUBJECT}/${DELETE_OPERATION}`; -const GET_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_ACTION}`; -const LIST_TOPIC = `${DH_RESPONSE_TOPIC}/${LIST_ACTION}`; -const SAVE_TOPIC = `${DH_RESPONSE_TOPIC}/${SAVE_ACTION}`; -const DELETE_TOPIC = `${DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; -const DEVICE_1_ID = `mqtt-broker-integration-tests-device-1-id`; -const DEVICE_2_ID = `mqtt-broker-integration-tests-device-2-id`; -const DEVICE_1_NAME = `mqtt-broker-integration-tests-device-1-name`; -const DEVICE_2_NAME = `mqtt-broker-integration-tests-device-2-name`; -const DEVICE_1_NAME_UPDATED = `mqtt-broker-integration-tests-device-2-name-updated`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const SAVE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${SAVE_ACTION}`; +const DELETE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; +const DEVICE_1_ID = randomString.generate(); +const DEVICE_2_ID = randomString.generate(); +const DEVICE_1_NAME = randomString.generate(); +const DEVICE_2_NAME = randomString.generate(); +const DEVICE_1_NAME_UPDATED = randomString.generate(); let mqttClient; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -106,21 +99,19 @@ it(`should create new device (1) with ID: "${DEVICE_1_ID}" and name: "${DEVICE_1 const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(SAVE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - resolve(); - } + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: SAVE_ACTION, requestId: requestId, deviceId: DEVICE_1_ID, device: { name: DEVICE_1_NAME, data: {}, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID } })); }); @@ -130,21 +121,19 @@ it(`should create new device (2) with ID: "${DEVICE_2_ID}" and name: "${DEVICE_2 const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(SAVE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - resolve(); - } + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: SAVE_ACTION, requestId: requestId, deviceId: DEVICE_2_ID, device: { name: DEVICE_2_NAME, data: {}, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID } })); }); @@ -154,18 +143,16 @@ it(`should query the device (1) with ID: "${DEVICE_1_ID}" and name: "${DEVICE_1_ const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.device).to.be.an(`object`); - expect(message.device.id).to.equal(DEVICE_1_ID); - expect(message.device.name).to.equal(DEVICE_1_NAME); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.device).to.be.an(`object`); + expect(message.device.id).to.equal(DEVICE_1_ID); + expect(message.device.name).to.equal(DEVICE_1_NAME); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, deviceId: DEVICE_1_ID @@ -177,18 +164,16 @@ it(`should query the device (2) with ID: "${DEVICE_2_ID}" and name: "${DEVICE_2_ const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.device).to.be.an(`object`); - expect(message.device.id).to.equal(DEVICE_2_ID); - expect(message.device.name).to.equal(DEVICE_2_NAME); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.device).to.be.an(`object`); + expect(message.device.id).to.equal(DEVICE_2_ID); + expect(message.device.name).to.equal(DEVICE_2_NAME); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, deviceId: DEVICE_2_ID @@ -200,22 +185,20 @@ it(`should query the list of devices with existing device (1) and device (2)`, ( const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.devices.map((deviceObject) => deviceObject.id)) - .to.include.members([DEVICE_1_ID, DEVICE_2_ID]); - expect(message.devices.map((deviceObject) => deviceObject.name)) - .to.include.members([DEVICE_1_NAME, DEVICE_2_NAME]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.devices.map((deviceObject) => deviceObject.id)) + .to.include.members([DEVICE_1_ID, DEVICE_2_ID]); + expect(message.devices.map((deviceObject) => deviceObject.name)) + .to.include.members([DEVICE_1_NAME, DEVICE_2_NAME]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); @@ -224,21 +207,19 @@ it(`should update the device (1) name: "${DEVICE_1_NAME}" to "${DEVICE_1_NAME_UP const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(SAVE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - resolve(); - } + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: SAVE_ACTION, requestId: requestId, deviceId: DEVICE_1_ID, device: { name: DEVICE_1_NAME_UPDATED, data: {}, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID } })); }); @@ -248,18 +229,16 @@ it(`should query the updated device (1) with ID: "${DEVICE_2_ID}" and updated na const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.device).to.be.an(`object`); - expect(message.device.id).to.equal(DEVICE_1_ID); - expect(message.device.name).to.equal(DEVICE_1_NAME_UPDATED); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.device).to.be.an(`object`); + expect(message.device.id).to.equal(DEVICE_1_ID); + expect(message.device.name).to.equal(DEVICE_1_NAME_UPDATED); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, deviceId: DEVICE_1_ID @@ -271,14 +250,12 @@ it(`should delete device (1) with ID: "${DEVICE_1_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(DELETE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - resolve(); - } + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: DELETE_ACTION, requestId: requestId, deviceId: DEVICE_1_ID @@ -290,14 +267,12 @@ it(`should delete device (2) with ID: "${DEVICE_2_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(DELETE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - resolve(); - } + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: DELETE_ACTION, requestId: requestId, deviceId: DEVICE_2_ID @@ -309,20 +284,18 @@ it(`should query the list of devices without device (1) and device (2)`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.devices.map((deviceObject) => deviceObject.id)) - .to.not.include.members([DEVICE_1_ID, DEVICE_2_ID]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.devices.map((deviceObject) => deviceObject.id)) + .to.not.include.members([DEVICE_1_ID, DEVICE_2_ID]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); diff --git a/test/integration/api/devicetype.js b/test/integration/api/devicetype.js new file mode 100644 index 0000000..3a2f276 --- /dev/null +++ b/test/integration/api/devicetype.js @@ -0,0 +1,242 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; +const mqtt = require(`mqtt`); +const EventEmitter = require('events'); +const randomString = require(`randomstring`); +const chai = require(`chai`); +const expect = chai.expect; + +const ee = new EventEmitter(); + +const SUBJECT = `devicetype`; +const LIST_OPERATION = `list`; +const COUNT_OPERATION = `count`; +const GET_OPERATION = `get`; +const INSERT_OPERATION = `insert`; +const UPDATE_OPERATION = `update`; +const DELETE_OPERATION = `delete`; +const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; +const COUNT_ACTION = `${SUBJECT}/${COUNT_OPERATION}`; +const GET_ACTION = `${SUBJECT}/${GET_OPERATION}`; +const INSERT_ACTION = `${SUBJECT}/${INSERT_OPERATION}`; +const UPDATE_ACTION = `${SUBJECT}/${UPDATE_OPERATION}`; +const DELETE_ACTION = `${SUBJECT}/${DELETE_OPERATION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const COUNT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${COUNT_ACTION}`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const INSERT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; +const UPDATE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; +const DELETE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; +const TEST_DEVICE_TYPE_NAME = randomString.generate(); +const TEST_DEVICE_TYPE_DESCRIPTION = randomString.generate(); +const TEST_DEVICE_TYPE_NEW_DESCRIPTION = randomString.generate(); +let deviceTypeCount = 0; +let mqttClient, customDeviceTypeId; + +it(`should connect to MQTT broker`, () => { + return new Promise((resolve) => { + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD + }); + + mqttClient.on(`message`, (topic, message) => { + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); + }); + + mqttClient.on('connect', () => { + resolve(); + }); + }); +}); + +it(`should subscribe for "${LIST_TOPIC}" topic`, () => { + return new Promise((resolve, reject) => { + return mqttClient.subscribe(`${LIST_TOPIC}@${mqttClient.options.clientId}`, (err) => { + return err ? reject() : resolve(); + }) + }); +}); + +it(`should subscribe for "${COUNT_TOPIC}" topic`, () => { + return new Promise((resolve, reject) => { + return mqttClient.subscribe(`${COUNT_TOPIC}@${mqttClient.options.clientId}`, (err) => { + return err ? reject() : resolve(); + }) + }); +}); + +it(`should subscribe for "${GET_TOPIC}" topic`, () => { + return new Promise((resolve, reject) => { + return mqttClient.subscribe(`${GET_TOPIC}@${mqttClient.options.clientId}`, (err) => { + return err ? reject() : resolve(); + }) + }); +}); + +it(`should subscribe for "${INSERT_TOPIC}" topic`, () => { + return new Promise((resolve, reject) => { + return mqttClient.subscribe(`${INSERT_TOPIC}@${mqttClient.options.clientId}`, (err) => { + return err ? reject() : resolve(); + }) + }); +}); + +it(`should subscribe for "${UPDATE_TOPIC}" topic`, () => { + return new Promise((resolve, reject) => { + return mqttClient.subscribe(`${UPDATE_TOPIC}@${mqttClient.options.clientId}`, (err) => { + return err ? reject() : resolve(); + }) + }); +}); + +it(`should subscribe for "${DELETE_TOPIC}" topic`, () => { + return new Promise((resolve, reject) => { + return mqttClient.subscribe(`${DELETE_TOPIC}@${mqttClient.options.clientId}`, (err) => { + return err ? reject() : resolve(); + }) + }); +}); + +it(`should get count of existing device types`, () => { + const requestId = randomString.generate(); + + return new Promise((resolve) => { + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.count).to.be.a(`number`); + deviceTypeCount = message.count; + resolve(); + }); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: COUNT_ACTION, + requestId: requestId + })); + }); +}); + +it(`should create new device type with name: "${TEST_DEVICE_TYPE_NAME}" and description: "${TEST_DEVICE_TYPE_DESCRIPTION}"`, () => { + const requestId = randomString.generate(); + + return new Promise((resolve) => { + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.deviceType.id).to.be.a(`number`); + customDeviceTypeId = message.deviceType.id; + resolve(); + }); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: INSERT_ACTION, + requestId: requestId, + deviceType: { + name: TEST_DEVICE_TYPE_NAME, + description: TEST_DEVICE_TYPE_DESCRIPTION + } + })); + }); +}); + +it(`should get new count of existing device types increased by 1`, () => { + const requestId = randomString.generate(); + + return new Promise((resolve) => { + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.count).to.be.a(`number`); + expect(message.count).to.equal(deviceTypeCount + 1); + deviceTypeCount = message.count; + resolve(); + }); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: COUNT_ACTION, + requestId: requestId + })); + }); +}); + +it(`should query the device type with name: "${TEST_DEVICE_TYPE_NAME}" and description: "${TEST_DEVICE_TYPE_DESCRIPTION}"`, () => { + const requestId = randomString.generate(); + + return new Promise((resolve) => { + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.deviceTypes).to.be.an(`array`); + expect(message.deviceTypes[0].id).to.equal(customDeviceTypeId); + expect(message.deviceTypes[0].name).to.equal(TEST_DEVICE_TYPE_NAME); + expect(message.deviceTypes[0].description).to.equal(TEST_DEVICE_TYPE_DESCRIPTION); + resolve(); + }); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: LIST_ACTION, + requestId: requestId, + name: TEST_DEVICE_TYPE_NAME + })); + }); +}); + +it(`should update device type with name: "${TEST_DEVICE_TYPE_NAME}" to new description: "${TEST_DEVICE_TYPE_NEW_DESCRIPTION}"`, () => { + const requestId1 = randomString.generate(); + const requestId2 = randomString.generate(); + + return new Promise((resolve) => { + ee.once(requestId1, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: GET_ACTION, + requestId: requestId2, + deviceTypeId: customDeviceTypeId + })); + }); + + ee.once(requestId2, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.deviceType).to.be.an(`object`); + expect(message.deviceType.id).to.equal(customDeviceTypeId); + expect(message.deviceType.name).to.equal(TEST_DEVICE_TYPE_NAME); + expect(message.deviceType.description).to.equal(TEST_DEVICE_TYPE_NEW_DESCRIPTION); + resolve(); + }); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: UPDATE_ACTION, + requestId: requestId1, + deviceTypeId: customDeviceTypeId, + deviceType: { + name: TEST_DEVICE_TYPE_NAME, + description: TEST_DEVICE_TYPE_NEW_DESCRIPTION + } + })); + }); +}); + +it(`should delete the device type with name: "${TEST_DEVICE_TYPE_NAME}" and description: "${TEST_DEVICE_TYPE_NEW_DESCRIPTION}"`, () => { + const requestId = randomString.generate(); + + return new Promise((resolve) => { + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + resolve(); + }); + + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ + action: GET_ACTION, + requestId: requestId, + deviceTypeId: customDeviceTypeId + })); + }); +}); + +it(`should disconnect from MQTT broker`, () => { + return new Promise((resolve) => { + mqttClient.end(() => { + resolve(); + }); + }); +}); \ No newline at end of file diff --git a/test/integration/api/network.js b/test/integration/api/network.js index ee09a67..0d53dc2 100644 --- a/test/integration/api/network.js +++ b/test/integration/api/network.js @@ -1,22 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; + const SUBJECT = `network`; const GET_OPERATION = `get`; const LIST_OPERATION = `list`; @@ -28,25 +19,27 @@ const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; const INSERT_ACTION = `${SUBJECT}/${INSERT_OPERATION}`; const UPDATE_ACTION = `${SUBJECT}/${UPDATE_OPERATION}`; const DELETE_ACTION = `${SUBJECT}/${DELETE_OPERATION}`; -const GET_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_ACTION}`; -const LIST_TOPIC = `${DH_RESPONSE_TOPIC}/${LIST_ACTION}`; -const INSERT_TOPIC = `${DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; -const UPDATE_TOPIC = `${DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; -const DELETE_TOPIC = `${DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; -const TEST_NETWORK_NAME = `mqtt-broker-integration-tests-network-name`; -const TEST_NETWORK_DESCRIPTION = `mqtt-broker-integration-tests-network-description`; -const UPDATED_TEST_NETWORK_DESCRIPTION = `mqtt-broker-integration-tests-network-description-updated`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const INSERT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; +const UPDATE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; +const DELETE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; +const TEST_NETWORK_NAME = randomString.generate(); +const TEST_NETWORK_DESCRIPTION = randomString.generate(); +const UPDATED_TEST_NETWORK_DESCRIPTION = randomString.generate(); let mqttClient, testNetworkId; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -119,18 +112,16 @@ it(`should create new network with name: "${TEST_NETWORK_NAME}" and description: const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(INSERT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.network).to.be.an(`object`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.network).to.be.an(`object`); - testNetworkId = message.network.id; + testNetworkId = message.network.id; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: INSERT_ACTION, requestId: requestId, network: { @@ -145,19 +136,17 @@ it(`should query the network name: "${TEST_NETWORK_NAME} and description: "${TES const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.network).to.be.an(`object`); - expect(message.network.id).to.equal(testNetworkId); - expect(message.network.name).to.equal(TEST_NETWORK_NAME); - expect(message.network.description).to.equal(TEST_NETWORK_DESCRIPTION); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.network).to.be.an(`object`); + expect(message.network.id).to.equal(testNetworkId); + expect(message.network.name).to.equal(TEST_NETWORK_NAME); + expect(message.network.description).to.equal(TEST_NETWORK_DESCRIPTION); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, networkId: testNetworkId @@ -169,22 +158,20 @@ it(`should query the list of networks with existing network name: "${TEST_NETWOR const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.networks).to.be.an(`array`); - expect(message.networks.map((networkObject) => networkObject.id)) - .to.include.members([testNetworkId]); - expect(message.networks.map((networkObject) => networkObject.name)) - .to.include.members([TEST_NETWORK_NAME]); - expect(message.networks.map((networkObject) => networkObject.description)) - .to.include.members([TEST_NETWORK_DESCRIPTION]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.networks).to.be.an(`array`); + expect(message.networks.map((networkObject) => networkObject.id)) + .to.include.members([testNetworkId]); + expect(message.networks.map((networkObject) => networkObject.name)) + .to.include.members([TEST_NETWORK_NAME]); + expect(message.networks.map((networkObject) => networkObject.description)) + .to.include.members([TEST_NETWORK_DESCRIPTION]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, take: -1 @@ -196,15 +183,13 @@ it(`should update the network description: "${TEST_NETWORK_DESCRIPTION}" to "${U const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(UPDATE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: UPDATE_ACTION, requestId: requestId, networkId: testNetworkId, @@ -219,19 +204,17 @@ it(`should query the updated network where updated description is: "${UPDATED_TE const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.network).to.be.an(`object`); - expect(message.network.id).to.equal(testNetworkId); - expect(message.network.name).to.equal(TEST_NETWORK_NAME); - expect(message.network.description).to.equal(UPDATED_TEST_NETWORK_DESCRIPTION); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.network).to.be.an(`object`); + expect(message.network.id).to.equal(testNetworkId); + expect(message.network.name).to.equal(TEST_NETWORK_NAME); + expect(message.network.description).to.equal(UPDATED_TEST_NETWORK_DESCRIPTION); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, networkId: testNetworkId @@ -243,15 +226,13 @@ it(`should delete the network"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(DELETE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: DELETE_ACTION, requestId: requestId, networkId: testNetworkId @@ -263,21 +244,19 @@ it(`should query the list of the networks without deleted network`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.networks).to.be.an(`array`); - expect(message.networks.map((networkObject) => networkObject.id)) - .to.not.include.members([testNetworkId]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.networks).to.be.an(`array`); + expect(message.networks.map((networkObject) => networkObject.id)) + .to.not.include.members([testNetworkId]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); diff --git a/test/integration/api/notification.js b/test/integration/api/notification.js index 5ccea79..b913c27 100644 --- a/test/integration/api/notification.js +++ b/test/integration/api/notification.js @@ -1,23 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `notification`; const GET_OPERATION = `get`; const LIST_OPERATION = `list`; @@ -25,22 +15,24 @@ const INSERT_OPERATION = `insert`; const GET_ACTION = `${SUBJECT}/${GET_OPERATION}`; const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; const INSERT_ACTION = `${SUBJECT}/${INSERT_OPERATION}`; -const GET_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_ACTION}`; -const LIST_TOPIC = `${DH_RESPONSE_TOPIC}/${LIST_ACTION}`; -const INSERT_TOPIC = `${DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; -const TEST_NOTIFICATION_NAME = `mqtt-broker-integration-tests-notification-name`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const INSERT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; +const TEST_NOTIFICATION_NAME = randomString.generate(); const TEST_NOTIFICATION_PARAMETERS = { parameter: `startParameter` }; let mqttClient, testNotificationId; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -85,25 +77,23 @@ it(`should subscribe for "${INSERT_TOPIC}" topic`, () => { }); }); -it(`should create new notification with name: "${TEST_NOTIFICATION_NAME}" for device with id: "${DEVICE_ID}"`, () => { +it(`should create new notification with name: "${TEST_NOTIFICATION_NAME}" for device with id: "${Config.DEVICE_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(INSERT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.notification).to.be.an(`object`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.notification).to.be.an(`object`); - testNotificationId = message.notification.id; + testNotificationId = message.notification.id; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: INSERT_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, notification: { notification: TEST_NOTIFICATION_NAME, parameters: TEST_NOTIFICATION_PARAMETERS @@ -116,49 +106,45 @@ it(`should query the notification with name: "${TEST_NOTIFICATION_NAME}"`, () => const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.notification).to.be.an(`object`); - expect(message.notification.id).to.equal(testNotificationId); - expect(message.notification.notification).to.equal(TEST_NOTIFICATION_NAME); - expect(message.notification.deviceId).to.equal(DEVICE_ID); - expect(message.notification.parameters).to.deep.equal(TEST_NOTIFICATION_PARAMETERS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.notification).to.be.an(`object`); + expect(message.notification.id).to.equal(testNotificationId); + expect(message.notification.notification).to.equal(TEST_NOTIFICATION_NAME); + expect(message.notification.deviceId).to.equal(Config.DEVICE_ID); + expect(message.notification.parameters).to.deep.equal(TEST_NOTIFICATION_PARAMETERS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, notificationId: testNotificationId })); }); }); -it(`should query the list of notifications for device with id: "${DEVICE_ID}" with existing notification with name: "${TEST_NOTIFICATION_NAME}"`, () => { +it(`should query the list of notifications for device with id: "${Config.DEVICE_ID}" with existing notification with name: "${TEST_NOTIFICATION_NAME}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.notifications).to.be.an(`array`); - expect(message.notifications.map((notificationObject) => notificationObject.id)) - .to.include.members([testNotificationId]); - expect(message.notifications.map((notificationObject) => notificationObject.notification)) - .to.include.members([TEST_NOTIFICATION_NAME]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.notifications).to.be.an(`array`); + expect(message.notifications.map((notificationObject) => notificationObject.id)) + .to.include.members([testNotificationId]); + expect(message.notifications.map((notificationObject) => notificationObject.notification)) + .to.include.members([TEST_NOTIFICATION_NAME]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, take: 1000 })); }); diff --git a/test/integration/api/server.js b/test/integration/api/server.js index 1780f7c..e67434b 100644 --- a/test/integration/api/server.js +++ b/test/integration/api/server.js @@ -1,38 +1,30 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `server`; const INFO_OPERATION = `info`; const INFO_ACTION = `${SUBJECT}/${INFO_OPERATION}`; -const INFO_TOPIC = `${DH_RESPONSE_TOPIC}/${INFO_ACTION}`; +const INFO_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INFO_ACTION}`; let mqttClient; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -57,16 +49,14 @@ it(`should get server information`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(INFO_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message).to.include.all.keys(`info`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message).to.include.all.keys(`info`); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: INFO_ACTION, requestId: requestId })); diff --git a/test/integration/api/subscription.js b/test/integration/api/subscription.js index 0adb7b7..48be958 100644 --- a/test/integration/api/subscription.js +++ b/test/integration/api/subscription.js @@ -1,45 +1,37 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `subscription`; const LIST_OPERATION = `list`; -const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; -const LIST_TOPIC = `${DH_RESPONSE_TOPIC}/${LIST_ACTION}`; const TEST_NAME = `testName`; const SUBSCRIPTION_COMMAND_TYPE = `command`; const SUBSCRIPTION_NOTIFICATION_TYPE = `notification`; -const SUBSCRIPTION_COMMAND_TOPIC_1 = `dh/${SUBSCRIPTION_COMMAND_TYPE}/${NETWORK_ID}/${DEVICE_ID}/${TEST_NAME}1`; -const SUBSCRIPTION_COMMAND_TOPIC_2 = `dh/${SUBSCRIPTION_COMMAND_TYPE}/${NETWORK_ID}/${DEVICE_ID}/${TEST_NAME}2`; -const SUBSCRIPTION_NOTIFICATION_TOPIC_1 = `dh/${SUBSCRIPTION_NOTIFICATION_TYPE}/${NETWORK_ID}/${DEVICE_ID}/${TEST_NAME}1`; -const SUBSCRIPTION_NOTIFICATION_TOPIC_2 = `dh/${SUBSCRIPTION_NOTIFICATION_TYPE}/${NETWORK_ID}/${DEVICE_ID}/${TEST_NAME}2`; +const LIST_ACTION = `${SUBJECT}/${LIST_OPERATION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const SUBSCRIPTION_COMMAND_TOPIC_1 = `dh/${SUBSCRIPTION_COMMAND_TYPE}/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${TEST_NAME}1`; +const SUBSCRIPTION_COMMAND_TOPIC_2 = `dh/${SUBSCRIPTION_COMMAND_TYPE}/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${TEST_NAME}2`; +const SUBSCRIPTION_NOTIFICATION_TOPIC_1 = `dh/${SUBSCRIPTION_NOTIFICATION_TYPE}/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${TEST_NAME}1`; +const SUBSCRIPTION_NOTIFICATION_TOPIC_2 = `dh/${SUBSCRIPTION_NOTIFICATION_TYPE}/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${TEST_NAME}2`; let mqttClient; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -108,26 +100,24 @@ it(`should subscribe for "${SUBSCRIPTION_NOTIFICATION_TOPIC_2}" topic`, () => { }); }); -it(`should query the list of command subscriptions for the user login: "${TEST_LOGIN}"`, () => { +xit(`should query the list of command subscriptions for the user login: "${Config.TEST_LOGIN}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.subscriptions).to.be.an(`object`); - expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { - return subscriptionObject.deviceIds[0]; - })).to.include.members([DEVICE_ID]); - expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { - return subscriptionObject.names[0]; - })).to.include.members([`${TEST_NAME}1`, `${TEST_NAME}1`]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.subscriptions).to.be.an(`object`); + expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { + return subscriptionObject.deviceIds[0]; + })).to.include.members([Config.DEVICE_ID]); + expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { + return subscriptionObject.names[0]; + })).to.include.members([`${TEST_NAME}1`, `${TEST_NAME}1`]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, type: SUBSCRIPTION_COMMAND_TYPE @@ -135,26 +125,24 @@ it(`should query the list of command subscriptions for the user login: "${TEST_L }); }); -it(`should query the list of notification subscriptions for the user login: "${TEST_LOGIN}"`, () => { +xit(`should query the list of notification subscriptions for the user login: "${Config.TEST_LOGIN}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.subscriptions).to.be.an(`object`); - expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { - return subscriptionObject.deviceIds[0]; - })).to.include.members([DEVICE_ID]); - expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { - return subscriptionObject.names[0]; - })).to.include.members([`${TEST_NAME}1`, `${TEST_NAME}1`]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.subscriptions).to.be.an(`object`); + expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { + return subscriptionObject.deviceIds[0]; + })).to.include.members([Config.DEVICE_ID]); + expect(Object.entries(message.subscriptions).map(([subscriptionId, subscriptionObject]) => { + return subscriptionObject.names[0]; + })).to.include.members([`${TEST_NAME}1`, `${TEST_NAME}1`]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, type: SUBSCRIPTION_NOTIFICATION_TYPE diff --git a/test/integration/api/token.js b/test/integration/api/token.js index 68b4d73..0dd5bc1 100644 --- a/test/integration/api/token.js +++ b/test/integration/api/token.js @@ -1,23 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; const SUBJECT = `token`; const TOKEN_OPERATION = SUBJECT; const CREATE_OPERATION = `create`; @@ -25,26 +15,27 @@ const REFRESH_OPERATION = `refresh`; const TOKEN_ACTION = TOKEN_OPERATION; const CREATE_ACTION = `${SUBJECT}/${CREATE_OPERATION}`; const REFRESH_ACTION = `${SUBJECT}/${REFRESH_OPERATION}`; -const TOKEN_TOPIC = `${DH_RESPONSE_TOPIC}/${TOKEN_ACTION}`; -const CREATE_TOPIC = `${DH_RESPONSE_TOPIC}/${CREATE_ACTION}`; -const REFRESH_TOPIC = `${DH_RESPONSE_TOPIC}/${REFRESH_ACTION}`; +const TOKEN_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${TOKEN_ACTION}`; +const CREATE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${CREATE_ACTION}`; +const REFRESH_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${REFRESH_ACTION}`; const TEST_PAYLOAD = { - userId: TEST_USER_ID, - networkIds: [NETWORK_ID], - deviceIds: [DEVICE_ID] + userId: Config.TEST_USER_ID, + networkIds: [Config.NETWORK_ID], + deviceTypeIds: [Config.DEVICE_TYPE_ID] }; let mqttClient, accessToken, refreshToken; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - const splittedTopic = topic.split(`/`); - ee.emit(topic.split(`/`)[splittedTopic[3] ? 3 : 2].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -89,27 +80,25 @@ it(`should subscribe for "${REFRESH_TOPIC}" topic`, () => { }); }); -it(`should create access and refresh tokens by login: "${TEST_LOGIN}" and password" "${TEST_PASSWORD}"`, () => { +it(`should create access and refresh tokens by login: "${Config.TEST_LOGIN}" and password" "${Config.TEST_PASSWORD}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(TOKEN_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message).to.include.all.keys(`accessToken`, `refreshToken`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message).to.include.all.keys(`accessToken`, `refreshToken`); - accessToken = message.accessToken; - refreshToken = message.refreshToken; + accessToken = message.accessToken; + refreshToken = message.refreshToken; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: TOKEN_ACTION, requestId: requestId, - login: TEST_LOGIN, - password: TEST_PASSWORD + login: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD })); }); }); @@ -118,19 +107,17 @@ it(`should create access and refresh tokens by payload`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(CREATE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message).to.include.all.keys(`accessToken`, `refreshToken`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message).to.include.all.keys(`accessToken`, `refreshToken`); - accessToken = message.accessToken; - refreshToken = message.refreshToken; + accessToken = message.accessToken; + refreshToken = message.refreshToken; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: CREATE_ACTION, requestId: requestId, payload: TEST_PAYLOAD @@ -142,18 +129,16 @@ it(`should refresh access token by refresh token.`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(REFRESH_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message).to.include.all.keys(`accessToken`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message).to.include.all.keys(`accessToken`); - accessToken = message.accessToken; + accessToken = message.accessToken; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: REFRESH_ACTION, requestId: requestId, refreshToken: refreshToken diff --git a/test/integration/api/user.js b/test/integration/api/user.js index a25e3de..cef049d 100644 --- a/test/integration/api/user.js +++ b/test/integration/api/user.js @@ -1,22 +1,13 @@ +const CONST = require(`../constants.json`); +const Config = require(`../../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); -const sinon = require(`sinon`); const chai = require(`chai`); const expect = chai.expect; -const assert = chai.assert; const ee = new EventEmitter(); -const DH_RESPONSE_TOPIC = `dh/response`; -const DH_REQUEST_TOPIC = `dh/request`; -const MQTT_BROKER_URL = `mqtt://localhost:1883`; -const TEST_LOGIN = `mqtt_proxy_test_login`; -const TEST_PASSWORD = `qwertyui`; -const TEST_USER_ID = 14347; -const SUCCESS_STATUS = `success`; -const ERROR_STATUS = `error`; -const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; -const NETWORK_ID = 12276; + const SUBJECT = `user`; const GET_OPERATION = `get`; const LIST_OPERATION = `list`; @@ -38,17 +29,17 @@ const UPDATE_CURRENT_ACTION = `${SUBJECT}/${UPDATE_CURRENT_OPERATION}`; const GET_NETWORK_ACTION = `${SUBJECT}/${GET_NETWORK_OPERATION}`; const ASSIGN_NETWORK_ACTION = `${SUBJECT}/${ASSIGN_NETWORK_OPERATION}`; const UNASSIGN_NETWORK_ACTION = `${SUBJECT}/${UNASSIGN_NETWORK_OPERATION}`; -const GET_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_ACTION}`; -const LIST_TOPIC = `${DH_RESPONSE_TOPIC}/${LIST_ACTION}`; -const INSERT_TOPIC = `${DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; -const UPDATE_TOPIC = `${DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; -const DELETE_TOPIC = `${DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; -const GET_CURRENT_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_CURRENT_ACTION}`; -const UPDATE_CURRENT_TOPIC = `${DH_RESPONSE_TOPIC}/${UPDATE_CURRENT_ACTION}`; -const GET_NETWORK_TOPIC = `${DH_RESPONSE_TOPIC}/${GET_NETWORK_ACTION}`; -const ASSIGN_NETWORK_TOPIC = `${DH_RESPONSE_TOPIC}/${ASSIGN_NETWORK_ACTION}`; -const UNASSIGN_NETWORK_TOPIC = `${DH_RESPONSE_TOPIC}/${UNASSIGN_NETWORK_ACTION}`; -const TEST_USER_LOGIN = `mqtt-broker-integration-tests-user-login`; +const GET_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_ACTION}`; +const LIST_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${LIST_ACTION}`; +const INSERT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${INSERT_ACTION}`; +const UPDATE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${UPDATE_ACTION}`; +const DELETE_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${DELETE_ACTION}`; +const GET_CURRENT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_CURRENT_ACTION}`; +const UPDATE_CURRENT_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${UPDATE_CURRENT_ACTION}`; +const GET_NETWORK_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${GET_NETWORK_ACTION}`; +const ASSIGN_NETWORK_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${ASSIGN_NETWORK_ACTION}`; +const UNASSIGN_NETWORK_TOPIC = `${CONST.DH_RESPONSE_TOPIC}/${UNASSIGN_NETWORK_ACTION}`; +const TEST_USER_LOGIN = randomString.generate(); const TEST_USER_PASSWORD = `qwertyui`; const START_USER_DATA = { data: `startData` }; const UPDATED_USER_DATA = { data: `updatedData` }; @@ -56,13 +47,15 @@ let mqttClient, testUserId; it(`should connect to MQTT broker`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); mqttClient.on(`message`, (topic, message) => { - ee.emit(topic.split(`/`)[3].split(`@`)[0], JSON.parse(message.toString())) + const messageObject = JSON.parse(message.toString()); + + ee.emit(messageObject.requestId, messageObject); }); mqttClient.on('connect', () => { @@ -195,18 +188,16 @@ it(`should create new user with login: "${TEST_USER_LOGIN}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(INSERT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.user.id).to.be.a(`number`); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.user.id).to.be.a(`number`); - testUserId = message.user.id; + testUserId = message.user.id; - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: INSERT_ACTION, requestId: requestId, user: { @@ -214,7 +205,6 @@ it(`should create new user with login: "${TEST_USER_LOGIN}"`, () => { role: 1, status: 0, password: TEST_USER_PASSWORD, - oldPassword: TEST_USER_PASSWORD, data: START_USER_DATA, introReviewed: true } @@ -226,17 +216,15 @@ it(`should query the list of users with existing user with login: "${TEST_USER_L const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.users.map((userObject) => userObject.login)) - .to.include.members([TEST_USER_LOGIN]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.users.map((userObject) => userObject.login)) + .to.include.members([TEST_USER_LOGIN]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, take: -1 @@ -248,18 +236,16 @@ it(`should query the users with login: "${TEST_USER_LOGIN}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.user.id).to.equal(testUserId); - expect(message.user.login).to.equal(TEST_USER_LOGIN); - expect(message.user.data).to.deep.equal(START_USER_DATA); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.user.id).to.equal(testUserId); + expect(message.user.login).to.equal(TEST_USER_LOGIN); + expect(message.user.data).to.deep.equal(START_USER_DATA); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, userId: testUserId @@ -271,15 +257,13 @@ it(`should update the users data with login: "${TEST_USER_LOGIN}" from old data: const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(UPDATE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: UPDATE_ACTION, requestId: requestId, userId: testUserId, @@ -294,18 +278,16 @@ it(`should query the users with login: "${TEST_USER_LOGIN}" with updated data: " const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.user.id).to.equal(testUserId); - expect(message.user.login).to.equal(TEST_USER_LOGIN); - expect(message.user.data).to.deep.equal(UPDATED_USER_DATA); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.user.id).to.equal(testUserId); + expect(message.user.login).to.equal(TEST_USER_LOGIN); + expect(message.user.data).to.deep.equal(UPDATED_USER_DATA); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_ACTION, requestId: requestId, userId: testUserId @@ -313,87 +295,79 @@ it(`should query the users with login: "${TEST_USER_LOGIN}" with updated data: " }); }); -it(`should assign the user with login: "${TEST_USER_LOGIN}" to network with id "${NETWORK_ID}"`, () => { +it(`should assign the user with login: "${TEST_USER_LOGIN}" to network with id "${Config.NETWORK_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(ASSIGN_NETWORK_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: ASSIGN_NETWORK_ACTION, requestId: requestId, userId: testUserId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); -it(`should query the network of the user with login: "${TEST_USER_LOGIN}" where the network id is: "${NETWORK_ID}"`, () => { +it(`should query the network of the user with login: "${TEST_USER_LOGIN}" where the network id is: "${Config.NETWORK_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_NETWORK_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.network.network.id).to.equal(NETWORK_ID); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.network.network.id).to.equal(Config.NETWORK_ID); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_NETWORK_ACTION, requestId: requestId, userId: testUserId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); -it(`should unassign the user with login: "${TEST_USER_LOGIN}" from network with id "${NETWORK_ID}"`, () => { +it(`should unassign the user with login: "${TEST_USER_LOGIN}" from network with id "${Config.NETWORK_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(UNASSIGN_NETWORK_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: UNASSIGN_NETWORK_ACTION, requestId: requestId, userId: testUserId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); -it(`should check that the user with login: "${TEST_USER_LOGIN}" is unassigned from the network with id: "${NETWORK_ID}"`, () => { +it(`should check that the user with login: "${TEST_USER_LOGIN}" is unassigned from the network with id: "${Config.NETWORK_ID}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_NETWORK_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(ERROR_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.ERROR_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_NETWORK_ACTION, requestId: requestId, userId: testUserId, - networkId: NETWORK_ID + networkId: Config.NETWORK_ID })); }); }); @@ -402,15 +376,13 @@ it(`should delete user with login: "${TEST_USER_LOGIN}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(DELETE_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: DELETE_ACTION, requestId: requestId, userId: testUserId @@ -422,17 +394,15 @@ it(`should query the list of users without user with login: "${TEST_USER_LOGIN}" const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(LIST_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.users.map((userObject) => userObject.login)) - .to.not.include.members([TEST_USER_LOGIN]); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.users.map((userObject) => userObject.login)) + .to.not.include.members([TEST_USER_LOGIN]); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: LIST_ACTION, requestId: requestId, take: -1 @@ -440,20 +410,18 @@ it(`should query the list of users without user with login: "${TEST_USER_LOGIN}" }); }); -it(`should query the current user with login: "${TEST_LOGIN}"`, () => { +it(`should query the current user with login: "${Config.TEST_LOGIN}"`, () => { const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_CURRENT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.current.login).to.equal(TEST_LOGIN); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.current.login).to.equal(Config.TEST_LOGIN); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_CURRENT_ACTION, requestId: requestId })); @@ -464,15 +432,13 @@ it(`should update the current user data to: "${JSON.stringify(UPDATED_USER_DATA) const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(UPDATE_CURRENT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: UPDATE_CURRENT_ACTION, requestId: requestId, user: { @@ -486,17 +452,15 @@ it(`should query the updated current user with updated data: "${JSON.stringify(U const requestId = randomString.generate(); return new Promise((resolve) => { - ee.once(GET_CURRENT_OPERATION, (message) => { - if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); - expect(message.current.login).to.equal(TEST_LOGIN); - expect(message.current.data).to.deep.equal(UPDATED_USER_DATA); + ee.once(requestId, (message) => { + expect(message.status).to.equal(CONST.SUCCESS_STATUS); + expect(message.current.login).to.equal(Config.TEST_LOGIN); + expect(message.current.data).to.deep.equal(UPDATED_USER_DATA); - resolve(); - } + resolve(); }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: GET_CURRENT_ACTION, requestId: requestId })); diff --git a/test/integration/broker.spec.js b/test/integration/broker.spec.js index 18c463b..a3d3c9a 100644 --- a/test/integration/broker.spec.js +++ b/test/integration/broker.spec.js @@ -1,3 +1,5 @@ +const CONST = require(`./constants.json`); +const Config = require(`../config`).test.integration; const mqtt = require(`mqtt`); const EventEmitter = require('events'); const randomString = require(`randomstring`); @@ -6,29 +8,31 @@ const chai = require(`chai`); const expect = chai.expect; const assert = chai.assert; -describe(`MQTT broker (should be run on localhost:1883)`, () => { - const DH_RESPONSE_TOPIC = `dh/response`; - const DH_REQUEST_TOPIC = `dh/request`; - const MQTT_BROKER_URL = `mqtt://localhost:1883`; - const TEST_LOGIN = `mqtt_proxy_test_login`; - const TEST_PASSWORD = `qwertyui`; - const TEST_USER_ID = 14347; - const SUCCESS_STATUS = `success`; - const ERROR_STATUS = `error`; - const DEVICE_ID = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; - const NETWORK_ID = 12276; +describe(`MQTT broker (should be run on localhost:1883)`, () => { describe(`User API`, () => require(`./api/user.js`)); + describe(`Configuration API`, () => require(`./api/configuration.js`)); + describe(`Device API`, () => require(`./api/device.js`)); + + describe(`Device Type API`, () => require(`./api/devicetype.js`)); + describe(`Network API`, () => require(`./api/network.js`)); + describe(`Command API`, () => require(`./api/command.js`)); + describe(`Notification API`, () => require(`./api/notification.js`)); + describe(`Subscription API`, () => require(`./api/subscription.js`)); + describe(`Token API`, () => require(`./api/token.js`)); + describe(`Authentication API`, () => require(`./api/authentication.js`)); + describe(`Server API`, () => require(`./api/server.js`)); + describe(`Cluster API`, () => require(`./api/cluster.js`)); describe(`Connection without credentials. Postponed authentication`, () => { @@ -37,7 +41,7 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { it(`should connect to MQTT broker without user credentials`, () => { return new Promise((resolve) => { - mqttClient = mqtt.connect(MQTT_BROKER_URL); + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL); mqttClient.on(`message`, (topic, message) => { ee.emit(topic.split(`/`)[2].split(`@`)[0], JSON.parse(message.toString())) @@ -91,7 +95,7 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { return new Promise((resolve) => { ee.once(`token`, (message) => { if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + expect(message.status).to.equal(CONST.SUCCESS_STATUS); expect(message).to.include.all.keys(`accessToken`, `refreshToken`); accessToken = message.accessToken; @@ -100,11 +104,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { } }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: `token`, requestId: requestId, - login: TEST_LOGIN, - password: TEST_PASSWORD + login: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD })); }); }); @@ -115,13 +119,13 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { return new Promise((resolve) => { ee.once(`authenticate`, (message) => { if (message.requestId === requestId) { - expect(message.status).to.equal(SUCCESS_STATUS); + expect(message.status).to.equal(CONST.SUCCESS_STATUS); resolve(); } }); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: `authenticate`, requestId: requestId, token: accessToken @@ -151,12 +155,14 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { }); describe(`Basic functionality`, () => { - let mqttClient; + let mqttClientId, mqttClient; beforeEach(() => { - mqttClient = mqtt.connect(MQTT_BROKER_URL, { - username: TEST_LOGIN, - password: TEST_PASSWORD + mqttClientId = randomString.generate(); + mqttClient = mqtt.connect(Config.MQTT_BROKER_URL, { + clientId: mqttClientId, + username: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD }); }); @@ -164,7 +170,7 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { mqttClient.end(); }); - it(`should connect to MQTT broker on "${MQTT_BROKER_URL}" as user: "${TEST_LOGIN}"`, (done) => { + it(`should connect to MQTT broker on "${Config.MQTT_BROKER_URL}" as user: "${Config.TEST_LOGIN}"`, (done) => { mqttClient.on(`connect`, () => { done(); }); @@ -175,16 +181,16 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { }); it(`should receive access and refresh tokens`, (done) => { - const responseTopic = `dh/response/token@${mqttClient.options.clientId}`; - const requestId = `integrationTestRequestId`; + const responseTopic = `dh/response/token@${mqttClientId}`; + const requestId = randomString.generate(); mqttClient.on(`connect`, () => { mqttClient.subscribe(responseTopic, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: `token`, requestId: requestId, - login: TEST_LOGIN, - password: TEST_PASSWORD + login: Config.TEST_LOGIN, + password: Config.TEST_PASSWORD })); }); }); @@ -193,7 +199,7 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { const messageObject = JSON.parse(message.toString()); expect(topic).to.equal(responseTopic); - expect(messageObject.status).to.equal(SUCCESS_STATUS); + expect(messageObject.status).to.equal(CONST.SUCCESS_STATUS); expect(messageObject.requestId).to.equal(requestId); expect(messageObject).to.include.all.keys(`accessToken`, `refreshToken`); @@ -207,15 +213,16 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { it(`should subscribe for notification/insert event and publish new notification`, (done) => { const randomNotificationName = randomString.generate(); - const subscriptionTopic = `dh/notification/${NETWORK_ID}/${DEVICE_ID}/${randomNotificationName}`; + const subscriptionTopic = `dh/notification/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${randomNotificationName}`; const action = `notification/insert`; mqttClient.on(`connect`, () => { mqttClient.subscribe(subscriptionTopic, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: action, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, notification: { notification: randomNotificationName, timestamp: new Date(), @@ -244,15 +251,16 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { it(`should subscribe for command/insert event and publish new command`, (done) => { const randomCommandName = randomString.generate(); - const subscriptionTopic = `dh/command/${NETWORK_ID}/${DEVICE_ID}/${randomCommandName}`; + const subscriptionTopic = `dh/command/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${randomCommandName}`; const action = `command/insert`; mqttClient.on(`connect`, () => { mqttClient.subscribe(subscriptionTopic, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: action, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, command: { command: randomCommandName, timestamp: new Date(), @@ -284,8 +292,8 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { it(`should subscribe for command/update event, create and update command`, (done) => { const randomCommandName = randomString.generate(); - const subscriptionTopic1 = `dh/command_update/${NETWORK_ID}/${DEVICE_ID}/${randomCommandName}`; - const subscriptionTopic2 = `dh/command/${NETWORK_ID}/${DEVICE_ID}/${randomCommandName}`; + const subscriptionTopic1 = `dh/command_update/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${randomCommandName}`; + const subscriptionTopic2 = `dh/command/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/${randomCommandName}`; const actionInsert = `command/insert`; const actionUpdate = `command/update`; const startValue = 1; @@ -294,10 +302,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { mqttClient.on(`connect`, () => { mqttClient.subscribe(subscriptionTopic1); mqttClient.subscribe(subscriptionTopic2, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: actionInsert, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, command: { command: randomCommandName, timestamp: new Date(), @@ -316,9 +325,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { if (topic === subscriptionTopic2) { expect(messageObject.command.parameters.value).to.equal(startValue); - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: actionUpdate, - deviceId: DEVICE_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, commandId: messageObject.command.id, command: { command: randomCommandName, @@ -342,8 +353,8 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { }); it(`should be able to unsubscribe from previous subscriptions`, (done) => { - const subscriptionTopic1 = `dh/notification/${NETWORK_ID}/${DEVICE_ID}/light`; - const subscriptionTopic2 = `dh/command/${NETWORK_ID}/${DEVICE_ID}/switchOff`; + const subscriptionTopic1 = `dh/notification/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/light`; + const subscriptionTopic2 = `dh/command/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/switchOff`; const actionNotification = `notification/insert`; const actionCommand = `command/insert`; const subscription1Spy = sinon.spy(); @@ -351,10 +362,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { mqttClient.on(`connect`, () => { mqttClient.subscribe(subscriptionTopic1, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: actionNotification, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, notification: { notification: `light`, timestamp: new Date(), @@ -366,10 +378,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { }); mqttClient.subscribe(subscriptionTopic2, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: actionCommand, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, command: { command: 'switchOff', timestamp: new Date(), @@ -387,10 +400,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { subscription1Spy(); mqttClient.unsubscribe(subscriptionTopic1, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: actionNotification, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, notification: { notification: `light`, timestamp: new Date(), @@ -404,10 +418,11 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { subscription2Spy(); mqttClient.unsubscribe(subscriptionTopic2, () => { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: actionCommand, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, command: { command: 'switchOn', timestamp: new Date(), @@ -436,19 +451,20 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { }).timeout(5000); it(`should send notification of same subscription for each subscriber only once`, (done) => { - const SUBSCRIPTION_TOPIC = `dh/notification/${NETWORK_ID}/${DEVICE_ID}/sharedNotification`; + const SUBSCRIPTION_TOPIC = `dh/notification/${Config.NETWORK_ID}/${Config.DEVICE_TYPE_ID}/${Config.DEVICE_ID}/sharedNotification`; const ACTION_NOTIFICATION = `notification/insert`; - const mqttClient2 = mqtt.connect(MQTT_BROKER_URL, { username: TEST_LOGIN, password: TEST_PASSWORD }); - const mqttClient3 = mqtt.connect(MQTT_BROKER_URL, { username: TEST_LOGIN, password: TEST_PASSWORD }); + const mqttClient2 = mqtt.connect(Config.MQTT_BROKER_URL, { username: Config.TEST_LOGIN, password: Config.TEST_PASSWORD }); + const mqttClient3 = mqtt.connect(Config.MQTT_BROKER_URL, { username: Config.TEST_LOGIN, password: Config.TEST_PASSWORD }); let subscriptionCounter = 0; const sharedPublisher = () => { subscriptionCounter++; if (subscriptionCounter > 2) { - mqttClient.publish(DH_REQUEST_TOPIC, JSON.stringify({ + mqttClient.publish(CONST.DH_REQUEST_TOPIC, JSON.stringify({ action: ACTION_NOTIFICATION, - deviceId: DEVICE_ID, - networkId: NETWORK_ID, + deviceId: Config.DEVICE_ID, + networkId: Config.NETWORK_ID, + deviceTypeId: Config.DEVICE_TYPE_ID, notification: { notification: `sharedNotification`, timestamp: new Date(), @@ -509,4 +525,5 @@ describe(`MQTT broker (should be run on localhost:1883)`, () => { }, 4500); }).timeout(5000); }); + }); \ No newline at end of file diff --git a/test/integration/config.json b/test/integration/config.json new file mode 100644 index 0000000..fe5d26d --- /dev/null +++ b/test/integration/config.json @@ -0,0 +1,9 @@ +{ + "MQTT_BROKER_URL": "mqtt://localhost:1883", + "TEST_LOGIN": "dhadmin", + "TEST_PASSWORD": "dhadmin_#911", + "TEST_USER_ID": 1, + "DEVICE_ID": "e50d6085-2aba-48e9-b1c3-73c673e414be", + "DEVICE_TYPE_ID": 1, + "NETWORK_ID": 1 +} \ No newline at end of file diff --git a/test/integration/constants.json b/test/integration/constants.json new file mode 100644 index 0000000..41b37e4 --- /dev/null +++ b/test/integration/constants.json @@ -0,0 +1,6 @@ +{ + "DH_RESPONSE_TOPIC": "dh/response", + "DH_REQUEST_TOPIC": "dh/request", + "SUCCESS_STATUS": "success", + "ERROR_STATUS": "error" +} \ No newline at end of file diff --git a/test/unit/lib/TopicStructure.spec.js b/test/unit/lib/TopicStructure.spec.js index 751a8d8..4ad6dee 100644 --- a/test/unit/lib/TopicStructure.spec.js +++ b/test/unit/lib/TopicStructure.spec.js @@ -11,16 +11,17 @@ describe(TopicStructure.name, () => { const requestStr = `request`; const responseStr = `response`; const networkId = `12276`; + const deviceTypeId = `1`; const deviceId = `VQjfBdTl0LvMVBt9RTJMOmwdqr6hWLjln1wZ`; const clientId = `clientId`; const name = `temperature`; const listOfMethods = [`isDH`, `getDomain`, `hasOwner`, `getOwner`, `isSubscription`, - `isResponse`, `isRequest`, `getAction`, `getNetwork`, `getDevice`, `getName`, + `isResponse`, `isRequest`, `getAction`, `getNetworkIds`, `getDeviceTypeIds`, `getDevice`, `getNames`, `isNotification`, `isCommandInsert`, `isCommandUpdate`]; const notDhTopic = `not/dh/topic`; - const dhNotificationTopic = `${dhStr}/${notificationStr}/${networkId}/${deviceId}/${name}`; - const dhCommandTopic = `${dhStr}/${commandStr}/${networkId}/${deviceId}/${name}`; - const dhCommandUpdateTopic = `${dhStr}/${commandUpdateStr}/${networkId}/${deviceId}/${name}`; + const dhNotificationTopic = `${dhStr}/${notificationStr}/${networkId}/${deviceTypeId}/${deviceId}/${name}`; + const dhCommandTopic = `${dhStr}/${commandStr}/${networkId}/${deviceTypeId}/${deviceId}/${name}`; + const dhCommandUpdateTopic = `${dhStr}/${commandUpdateStr}/${networkId}/${deviceTypeId}/${deviceId}/${name}`; const dhRequestTopic = `${dhStr}/${requestStr}`; const dhNotificationResponseTopic = `${dhStr}/${responseStr}/${notificationStr}@${clientId}`; @@ -62,16 +63,12 @@ describe(TopicStructure.name, () => { expect(topicStructure.isNotification()).to.equal(true); }); - it(`should has network id: ${networkId}`, () => { - expect(topicStructure.getNetwork()).to.equal(networkId); - }); - it(`should has device id: ${deviceId}`, () => { expect(topicStructure.getDevice()).to.equal(deviceId); }); it(`should has notification name: ${name}`, () => { - expect(topicStructure.getName()).to.equal(name); + expect(topicStructure.getNames()).to.deep.equal([ name ]); }); }); @@ -90,16 +87,12 @@ describe(TopicStructure.name, () => { expect(topicStructure.isCommandInsert()).to.equal(true); }); - it(`should has network id: ${networkId}`, () => { - expect(topicStructure.getNetwork()).to.equal(networkId); - }); - it(`should has device id: ${deviceId}`, () => { expect(topicStructure.getDevice()).to.equal(deviceId); }); it(`should has notification name: ${name}`, () => { - expect(topicStructure.getName()).to.equal(name); + expect(topicStructure.getNames()).to.deep.equal([ name ]); }); }); @@ -118,16 +111,12 @@ describe(TopicStructure.name, () => { expect(topicStructure.isCommandUpdate()).to.equal(true); }); - it(`should has network id: ${networkId}`, () => { - expect(topicStructure.getNetwork()).to.equal(networkId); - }); - it(`should has device id: ${deviceId}`, () => { expect(topicStructure.getDevice()).to.equal(deviceId); }); it(`should has notification name: ${name}`, () => { - expect(topicStructure.getName()).to.equal(name); + expect(topicStructure.getNames()).to.deep.equal([ name ]); }); }); diff --git a/test/unit/util/DeviceHiveUtils.spec.js b/test/unit/util/DeviceHiveUtils.spec.js index 764ff6a..c46192a 100644 --- a/test/unit/util/DeviceHiveUtils.spec.js +++ b/test/unit/util/DeviceHiveUtils.spec.js @@ -26,38 +26,35 @@ describe(DeviceHiveUtils.name, () => { describe(`Static method: ${staticMethodsNames.createSubscriptionDataObject}`, () => { const expectationObject1 = { - topic: `dh/notification/12276/deviceId/name`, + topic: `dh/notification/12276/1/deviceId/name`, expectation: { action: `notification/subscribe`, - networkIds: [], - deviceIds: [`deviceId`], + deviceId: `deviceId`, names: [`name`] } }; const expectationObject2 = { - topic: `dh/notification/+/deviceId/#`, + topic: `dh/notification/+/1/deviceId/#`, expectation: { action: `notification/subscribe`, - networkIds: [], - deviceIds: [`deviceId`], - names: [] + deviceId: `deviceId` } }; const expectationObject3 = { - topic: `dh/command/12276/+/temperature`, + topic: `dh/command/12276/1/+/temperature`, expectation: { action: `command/subscribe`, networkIds: [`12276`], - deviceIds: [], + deviceTypeIds: [`1`], names: [`temperature`] } }; const expectationObject4 = { - topic: `dh/command_update/12276/+/temperature`, + topic: `dh/command_update/12276/1/+/temperature`, expectation: { action: `command/subscribe`, networkIds: [`12276`], - deviceIds: [], + deviceTypeIds: [`1`], names: [`temperature`], returnUpdatedCommands: true } @@ -87,8 +84,8 @@ describe(DeviceHiveUtils.name, () => { describe(`Static method: ${staticMethodsNames.isSameTopicRoot}`, () => { const topic1 = `dh/request`; const topic2 = `dh/response`; - const topic3 = `dh/notification/1227/deviceId/#`; - const topic4 = `dh/notification/+/deviceId/#`; + const topic3 = `dh/notification/1227/1/deviceId/#`; + const topic4 = `dh/notification/+/1/deviceId/#`; const topic5 = `dh/#`; it(`"${topic1}" and "${topic2}" has different topic root`, () => { @@ -109,10 +106,10 @@ describe(DeviceHiveUtils.name, () => { }); describe(`Static method: ${staticMethodsNames.isLessGlobalTopic}`, () => { - const topic1 = `dh/notification/1227/deviceId/#`; - const topic2 = `dh/notification/+/deviceId/#`; + const topic1 = `dh/notification/1227/1/deviceId/#`; + const topic2 = `dh/notification/+/1/deviceId/#`; const topic3 = `dh/notification/#`; - const topic4 = `dh/command_update/+/deviceId/#`; + const topic4 = `dh/command_update/+/1/deviceId/#`; const topic5 = `dh/#`; const topic6 = `dh/notification/+`; @@ -138,10 +135,10 @@ describe(DeviceHiveUtils.name, () => { }); describe(`Static method: ${staticMethodsNames.isMoreGlobalTopic}`, () => { - const topic1 = `dh/notification/1227/deviceId/#`; - const topic2 = `dh/notification/+/deviceId/#`; + const topic1 = `dh/notification/1227/1/deviceId/#`; + const topic2 = `dh/notification/+/1/deviceId/#`; const topic3 = `dh/notification/#`; - const topic4 = `dh/command_update/+/deviceId/#`; + const topic4 = `dh/command_update/+/1/deviceId/#`; const topic5 = `dh/#`; const topic6 = `dh/notification/+`; @@ -167,9 +164,9 @@ describe(DeviceHiveUtils.name, () => { }); describe(`Static method: ${staticMethodsNames.getTopicSubscribeRequestAction}`, () => { - const expectation1 = [`dh/notification/1227/deviceId/name`, `notification/subscribe`]; - const expectation2 = [`dh/command/1227/deviceId/name`, `command/subscribe`]; - const expectation3 = [`dh/command_update/1227/deviceId/name`, `command/subscribe`]; + const expectation1 = [`dh/notification/1227/1/deviceId/name`, `notification/subscribe`]; + const expectation2 = [`dh/command/1227/1/deviceId/name`, `command/subscribe`]; + const expectation3 = [`dh/command_update/1227/1/deviceId/name`, `command/subscribe`]; const expectation4 = [`dh/response/notification`, ``]; it(`Action for "${expectation1[0]}" should be "${expectation1[1]}"`, () => { @@ -190,9 +187,9 @@ describe(DeviceHiveUtils.name, () => { }); describe(`Static method: ${staticMethodsNames.getTopicUnsubscribeRequestAction}`, () => { - const expectation1 = [`dh/notification/1227/deviceId/name`, `notification/unsubscribe`]; - const expectation2 = [`dh/command/1227/deviceId/name`, `command/unsubscribe`]; - const expectation3 = [`dh/command_update/1227/deviceId/name`, `command/unsubscribe`]; + const expectation1 = [`dh/notification/1227/1/deviceId/name`, `notification/unsubscribe`]; + const expectation2 = [`dh/command/1227/1/deviceId/name`, `command/unsubscribe`]; + const expectation3 = [`dh/command_update/1227/1/deviceId/name`, `command/unsubscribe`]; const expectation4 = [`dh/response/notification`, ``]; it(`Action for "${expectation1[0]}" should be "${expectation1[1]}"`, () => { @@ -213,9 +210,9 @@ describe(DeviceHiveUtils.name, () => { }); describe(`Static method: ${staticMethodsNames.getTopicSubscriptionResponseAction}`, () => { - const expectation1 = [`dh/notification/1227/deviceId/name`, `notification/insert`]; - const expectation2 = [`dh/command/1227/deviceId/name`, `command/insert`]; - const expectation3 = [`dh/command_update/1227/deviceId/name`, `command/update`]; + const expectation1 = [`dh/notification/1227/1/deviceId/name`, `notification/insert`]; + const expectation2 = [`dh/command/1227/1/deviceId/name`, `command/insert`]; + const expectation3 = [`dh/command_update/1227/1/deviceId/name`, `command/update`]; const expectation4 = [`dh/request/notification`, ``]; it(`Action for "${expectation1[0]}" should be "${expectation1[1]}"`, () => { diff --git a/util/DeviceHiveUtils.js b/util/DeviceHiveUtils.js index 1606853..d8e3c0e 100644 --- a/util/DeviceHiveUtils.js +++ b/util/DeviceHiveUtils.js @@ -14,16 +14,19 @@ class DeviceHiveUtils { */ static createSubscriptionDataObject (topic) { const topicStructure = new TopicStructure(topic); - const result = { - action: DeviceHiveUtils.getTopicSubscribeRequestAction(topic), - networkIds: topicStructure.getDevice().length > 0 ? [] : [topicStructure.getNetwork()], - deviceIds: topicStructure.getDevice() ? [topicStructure.getDevice()] : [], - names: topicStructure.getName() ? [topicStructure.getName()] : [] - }; - - if (topicStructure.isCommandUpdate()) { - result.returnUpdatedCommands = true - } + const result = {}; + const action = DeviceHiveUtils.getTopicSubscribeRequestAction(topic); + const networkIds = topicStructure.getNetworkIds(); + const deviceTypeIds = topicStructure.getDeviceTypeIds(); + const deviceId = topicStructure.getDevice(); + const names = topicStructure.getNames(); + + if (action) { result.action = action; } + if (networkIds) { result.networkIds = networkIds; } + if (deviceTypeIds) { result.deviceTypeIds = deviceTypeIds; } + if (deviceId) { result.deviceId = deviceId; } + if (names) { result.names = names; } + if (topicStructure.isCommandUpdate()) { result.returnUpdatedCommands = true; } return result; } diff --git a/util/constants.json b/util/constants.json index ab8d69e..4832d9f 100644 --- a/util/constants.json +++ b/util/constants.json @@ -28,7 +28,6 @@ "MAX_NUMBER_OF_PACKETS": 600000 }, "WS": { - "DEV_HOST": "ws://playground.devicehive.com/api/websocket", "SUCCESS_STATUS": "success", "ACTIONS": { "AUTHENTICATE": "authenticate",