Skip to content

Commit

Permalink
#29 & #30 add tests and code hardening
Browse files Browse the repository at this point in the history
  • Loading branch information
bachwehbi committed Feb 12, 2017
1 parent 0569f85 commit 31fa8da
Show file tree
Hide file tree
Showing 5 changed files with 663 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -13,3 +13,4 @@ results

npm-debug.log
node_modules
coverage
6 changes: 6 additions & 0 deletions .jshintrc
@@ -0,0 +1,6 @@
{
"node": true,
"indent": 2,
"expr": true,
"asi": true
}
16 changes: 15 additions & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "beebotte",
"description": "Beebotte is an open cloud platform for real time connected objects. This package provides the implementation of Beebotte API in nodejs.",
"version": "1.0.0",
"version": "1.0.1",
"author": {
"name": "Bachar Wehbi",
"email": "bwehbi@beebotte.com"
Expand All @@ -11,6 +11,13 @@
"type": "git",
"url": "https://github.com/beebotte/bbt_node.git"
},
"scripts": {
"test": "mocha --recursive --bail --reporter spec test 2>&1",
"ci": "mocha --recursive --bail --watch test",
"coverage": "rm -rf coverage; istanbul cover _mocha -- --recursive --reporter spec --bail",
"jshint-lib": "jshint lib",
"jshint-test": "jshint test"
},
"main": "./index.js",
"engines": {
"node": ">=0.10.40",
Expand All @@ -36,5 +43,12 @@
"joi": "6.10.0",
"mqtt": "2.1.3"
},
"devDependencies": {
"chai": "*",
"coveralls": "~2.11.1",
"jshint": "~2.9.1",
"istanbul": "~0.4.0",
"mocha": "*"
},
"license": "MIT"
}
129 changes: 129 additions & 0 deletions test/mqtt.js
@@ -0,0 +1,129 @@
var chai = require('chai'),
assert = chai.assert,
expect = chai.expect,
bbt = require('../index');

var token = ''

function createConnection() {
return new bbt.Connector({
apiKey: process.env.APIKEY,
secretKey: process.env.SECRETKEY
});
}

function createMqttConnection(ssl) {
return new bbt.Stream({transport: {
type: 'mqtt',
apiKey: process.env.APIKEY,
secretKey: process.env.SECRETKEY
}});
}

describe('beebotte.mqtt', function() {

var mqttclient
var msg = null

function onSub (message) {
msg = message.data
}

before(function() {
mqttclient = createMqttConnection(true)
})

it('should receive connected event upon connection', function (done){

var connected = false

setTimeout(function () {
if (!connected) {
done('Failed to receive connected event after 5 seconds from connection')
}
}, 15000)

mqttclient.on('connected', function() {
connected = true
done()
})
})

it('should subscribe to channel with success', function (done){

var subscribed = false

setTimeout(function () {
if (!subscribed) {
done('Failed to subscribe after 2 seconds from subscription request')
}
}, 5000)

mqttclient.on('subscribed', function (sub) {
expect(sub.channel).to.be.equal('test')
expect(sub.resource).to.be.equal('test')
subscribed = true
done()
})

mqttclient.subscribe('test', 'test', {read: true, write: true}, onSub)
})

it('should receive published message with success', function (done){

setTimeout(function () {
expect(msg).to.be.equal('msg1')
done()
}, 500)

mqttclient.publish('test', 'test', 'msg1')
})

it('should receive written message with success', function (done){

setTimeout(function () {
expect(msg).to.be.equal('msg2')
done()
}, 500)

mqttclient.write('test', 'test', 'msg2')
})

it('should unsubscribe from channel with success', function (done){

var unsubscribed = false

setTimeout(function () {
if (!unsubscribed) {
done('Failed to unsubscribe after 2 seconds from unsubscription request')
}
}, 5000)

mqttclient.on('unsubscribed', function (sub) {
expect(sub.channel).to.be.equal('test')
expect(sub.resource).to.be.equal('test')
unsubscribed = true
done()
})

mqttclient.unsubscribe('test', 'test')
})

it('should disconnect with success', function (done){

var disconnected = false

setTimeout(function () {
if (!disconnected) {
done('Failed to receive disconnected event after 2 seconds from disconnection')
}
}, 5000)

mqttclient.on('disconnected', function () {
disconnected = true
done()
})

mqttclient.disconnect()
})
})

0 comments on commit 31fa8da

Please sign in to comment.