diff --git a/.gjslintrc b/.gjslintrc deleted file mode 100644 index 0492419..0000000 --- a/.gjslintrc +++ /dev/null @@ -1,8 +0,0 @@ ---jsdoc ---summary ---beep ---check_html ---nomultiprocess ---debug_indentation ---time ---max_line_length=120 \ No newline at end of file diff --git a/.jshintrc b/.jshintrc index a7bb8f3..64a5587 100644 --- a/.jshintrc +++ b/.jshintrc @@ -25,7 +25,6 @@ "after": true, "beforeEach": true, "afterEach": true, - "mock": true, - "sinon": true + "mock": true } } \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 70c9108..1eb6558 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,3 @@ services: install: - npm install - -before_install: - - npm install -g grunt-cli diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 87c8ce5..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of iotagent-lwm2m-lib - * - * iotagent-lwm2m-lib is free software: you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the License, - * or (at your option) any later version. - * - * iotagent-lwm2m-lib is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with iotagent-lwm2m-lib. - * If not, seehttp://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[contacto@tid.es] - */ -'use strict'; - -/** - * Grunt tasks definitions - * - * @param {Object} grunt - */ -module.exports = function(grunt) { - - // Project configuration. - grunt.initConfig({ - pkgFile: 'package.json', - pkg: grunt.file.readJSON('package.json'), - - clean: { - reportTest: ['report/test'], - reportLint: ['report/lint'], - reportCoverage: ['report/coverage'], - siteCoverage: ['site/coverage'], - siteDoc: ['site/doc'], - siteReport: ['site/report'] - }, - - mkdir: { - reportTest: { - options: { - create: ['<%= clean.reportTest[0] %>'] - } - }, - reportLint: { - options: { - create: ['<%= clean.reportLint[0] %>'] - } - }, - reportCoverage: { - options: { - create: ['<%= clean.reportCoverage[0] %>'] - } - }, - siteCoverage: { - options: { - create: ['<%= clean.siteCoverage[0] %>'] - } - }, - siteDoc: { - options: { - create: ['<%= clean.siteDoc[0] %>'] - } - }, - siteReport: { - options: { - create: ['<%= clean.siteReport[0] %>'] - } - } - }, - - jshint: { - gruntfile: { - src: 'Gruntfile.js', - options: { - jshintrc: '.jshintrc' - } - }, - lib: { - src: ['lib/**/*.js'], - options: { - jshintrc: '.jshintrc' - } - }, - test: { - src: ['test/**/*.js'], - options: { - jshintrc: 'test/.jshintrc' - } - }, - reportGruntfile: { - src: 'Gruntfile.js', - options: { - reporter: 'checkstyle', - reporterOutput: '<%= clean.reportLint[0] %>/jshint-gruntfile.xml', - jshintrc: '.jshintrc' - } - }, - reportLib: { - src: 'lib/**/*.js', - options: { - reporter: 'checkstyle', - reporterOutput: '<%= clean.reportLint[0] %>/jshint-lib.xml', - jshintrc: 'lib/.jshintrc' - } - }, - reportTest: { - src: 'test/**/*.js', - options: { - reporter: 'checkstyle', - reporterOutput: '<%= clean.reportLint[0] %>/jshint-test.xml', - jshintrc: 'test/.jshintrc' - } - } - }, - - watch: { - gruntfile: { - files: '<%= jshint.gruntfile.src %>', - tasks: ['jshint:gruntfile'] - }, - lib: { - files: '<%= jshint.lib.src %>', - tasks: ['jshint:lib', 'test'] - }, - test: { - files: '<%= jshint.test.src %>', - tasks: ['jshint:test', 'test'] - } - }, - - mochaTest: { - unit: { - options: { - ui: 'bdd', - reporter: 'spec', - timeout: 2000 - }, - src: [ - 'tools/mocha-globals.js', - '<%= jshint.test.src %>' - ] - }, - unitReport: { - options: { - ui: 'bdd', - reporter: 'tap', - quiet: true, - captureFile: '<%= clean.reportTest[0] %>/unit_tests.tap' - }, - src: [ - 'tools/mocha-globals.js', - '<%= jshint.test.src %>' - ] - } - }, - - githubPages: { - target: { - options: { - commitMessage: 'UPDATE Doc' - }, - src: 'site' - } - }, - - dox: { - options: { - title: 'iotagent-lwm2m-lib documentation' - }, - files: { - src: ['<%= jshint.lib.src %>'], - dest: '<%= clean.siteDoc[0] %>' - } - }, - - exec: { - istanbul: { - cmd: - 'bash -c "./node_modules/.bin/istanbul cover --root lib/ --dir <%= clean.siteCoverage[0] %> -- ' + - '\\"`npm root -g`/grunt-cli/bin/grunt\\" test && ' + - './node_modules/.bin/istanbul report --dir <%= clean.siteCoverage[0] %> text-summary"' - }, - istanbulCobertura: { - cmd: - 'bash -c "./node_modules/.bin/istanbul report --dir <%= clean.siteCoverage[0] %> cobertura && ' + - 'mv <%= clean.siteCoverage[0] %>/cobertura-coverage.xml <%= clean.reportCoverage[0] %>"' - }, - githubPagesInit: { - cmd: 'bash tools/github-pages.sh' - } - }, - - plato: { - options: { - jshint: grunt.file.readJSON('.jshintrc') - }, - lib: { - files: { - '<%= clean.siteReport[0] %>': '<%= jshint.lib.src %>' - } - } - }, - - githooks: { - all: { - 'pre-commit': 'lint test' - } - }, - - gjslint: { - options: { - reporter: { - name: 'console' - }, - flags: [ - '--flagfile .gjslintrc' //use flag file' - ], - force: false - }, - gruntfile: { - src: '<%= jshint.gruntfile.src %>' - }, - lib: { - src: '<%= jshint.lib.src %>' - }, - test: { - src: '<%= jshint.test.src %>' - }, - report: { - options: { - reporter: { - name: 'gjslint_xml', - dest: '<%= clean.reportLint[0] %>/gjslint.xml' - }, - flags: [ - '--flagfile .gjslintrc' - ], - force: false - }, - src: ['<%= jshint.gruntfile.src %>', '<%= jshint.lib.src %>', '<%= jshint.test.src %>'] - } - } - }); - - // These plugins provide necessary tasks. - grunt.loadNpmTasks('grunt-contrib-clean'); - grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.loadNpmTasks('grunt-contrib-watch'); - grunt.loadNpmTasks('grunt-mocha-test'); - grunt.loadNpmTasks('grunt-github-pages'); - grunt.loadNpmTasks('grunt-exec'); - grunt.loadNpmTasks('grunt-plato'); - grunt.loadNpmTasks('grunt-gjslint'); - grunt.loadNpmTasks('grunt-dox'); - grunt.loadNpmTasks('grunt-mkdir'); - grunt.loadNpmTasks('grunt-githooks'); - - grunt.loadTasks('tools/tasks'); - - grunt.registerTask('init-pages', ['exec:githubPagesInit']); - - grunt.registerTask('test', 'Run tests', ['mochaTest:unit']); - - grunt.registerTask('test-report', 'Generate tests reports', - ['clean:reportTest', 'mkdir:reportTest', 'mochaTest:unitReport']); - - grunt.registerTask('coverage', 'Print coverage report', - ['clean:siteCoverage', 'exec:istanbul']); - - grunt.registerTask('coverage-report', 'Generate Cobertura report', - ['clean:reportCoverage', 'mkdir:reportCoverage', 'coverage', 'exec:istanbulCobertura']); - - grunt.registerTask('complexity', 'Generate code complexity reports', ['plato']); - - grunt.registerTask('doc', 'Generate source code JSDoc', ['dox']); - - grunt.registerTask('lint-jshint', 'Check source code style with JsHint', - ['jshint:gruntfile', 'jshint:lib', 'jshint:test']); - - grunt.registerTask('lint-gjslint', 'Check source code style with Google Closure Linter', - ['gjslint:gruntfile', 'gjslint:lib', 'gjslint:test']); - - grunt.registerTask('lint', 'Check source code style', ['lint-jshint', 'lint-gjslint']); - - grunt.registerTask('lint-report', 'Generate checkstyle reports', - ['clean:reportLint', 'mkdir:reportLint', 'jshint:reportGruntfile', 'jshint:reportLib', - 'jshint:reportTest', 'gjslint:report']); - - grunt.registerTask('site', ['doc', 'coverage', 'complexity', 'githubPages']); - - grunt.registerTask('init-dev-env', ['githooks']); - - // Default task. - grunt.registerTask('default', ['lint-jshint', 'test']); - -}; diff --git a/README.md b/README.md index 67de76b..61d2a92 100644 --- a/README.md +++ b/README.md @@ -390,146 +390,72 @@ Some of the more common variables can be configured using environment variables. All contributions to this project are welcome. Developers planning to contribute should follow the [Contribution Guidelines](./docs/contribution.md) ### Project build -The project is managed using Grunt Task Runner. + +The project is managed using npm. For a list of available task, type + ```bash -grunt --help +npm run ``` The following sections show the available options in detail. - ### Testing -[Mocha](http://mochajs.org/) Test Runner + [Chai](http://chaijs.com/) Assertion Library + [Sinon](http://sinonjs.org/) Spies, stubs. +[Mocha](http://visionmedia.github.io/mocha/) Test Runner + [Should.js](https://shouldjs.github.io/) Assertion Library. -The test environment is preconfigured to run [BDD](http://chaijs.com/api/bdd/) testing style with -`chai.expect` and `chai.should()` available globally while executing tests, as well as the [Sinon-Chai](http://chaijs.com/plugins/sinon-chai) plugin. +The test environment is preconfigured to run BDD testing style. Module mocking during testing can be done with [proxyquire](https://github.com/thlorenz/proxyquire) To run tests, type -```bash -grunt test -``` -Tests reports can be used together with Jenkins to monitor project quality metrics by means of TAP or XUnit plugins. -To generate TAP report in `report/test/unit_tests.tap`, type ```bash -grunt test-report +npm test ``` - ### Coding guidelines -jshint, gjslint +jshint -Uses provided .jshintrc and .gjslintrc flag files. The latter requires Python and its use can be disabled -while creating the project skeleton with grunt-init. +Uses provided .jshintrc flag file. To check source code style, type -```bash -grunt lint -``` -Checkstyle reports can be used together with Jenkins to monitor project quality metrics by means of Checkstyle -and Violations plugins. -To generate Checkstyle and JSLint reports under `report/lint/`, type ```bash -grunt lint-report +npm run lint ``` - ### Continuous testing Support for continuous testing by modifying a src file or a test. For continuous testing, type + ```bash -grunt watch +npm run test:watch ``` +If you want to continuously check also source code style, use instead: -### Source Code documentation -dox-foundation - -Generates HTML documentation under `site/doc/`. It can be used together with jenkins by means of DocLinks plugin. -For compiling source code documentation, type ```bash -grunt doc +npm run watch ``` - ### Code Coverage Istanbul Analizes the code coverage of your tests. To generate an HTML coverage report under `site/coverage/` and to print out a summary, type -```bash -# Use git-bash on Windows -grunt coverage -``` -To generate a Cobertura report in `report/coverage/cobertura-coverage.xml` that can be used together with Jenkins to -monitor project quality metrics by means of Cobertura plugin, type ```bash # Use git-bash on Windows -grunt coverage-report -``` - - -### Code complexity -Plato - -Analizes code complexity using Plato and stores the report under `site/report/`. It can be used together with jenkins -by means of DocLinks plugin. -For complexity report, type -```bash -grunt complexity +npm run test:coverage ``` -### PLC - -Update the contributors for the project -```bash -grunt contributors -``` - - -### Development environment - -Initialize your environment with git hooks. -```bash -grunt init-dev-env -``` - -We strongly suggest you to make an automatic execution of this task for every developer simply by adding the following -lines to your `package.json` -``` -{ - "scripts": { - "postinstall": "grunt init-dev-env" - } -} -``` +### Clean - -### Site generation - -There is a grunt task to generate the GitHub pages of the project, publishing also coverage, complexity and JSDocs pages. -In order to initialize the GitHub pages, use: - -```bash -grunt init-pages -``` - -This will also create a site folder under the root of your repository. This site folder is detached from your repository's -history, and associated to the gh-pages branch, created for publishing. This initialization action should be done only -once in the project history. Once the site has been initialized, publish with the following command: +Removes `node_modules` and `coverage` folders, and `package-lock-json` file so that a fresh copy of the project is restored. ```bash -grunt site +# Use git-bash on Windows +npm run clean ``` - -This command will only work after the developer has executed init-dev-env (that's the goal that will create the detached site). - -This command will also launch the coverage, doc and complexity task (see in the above sections). - diff --git a/lib/services/model/Device.js b/lib/services/model/Device.js index 970e024..00340d9 100644 --- a/lib/services/model/Device.js +++ b/lib/services/model/Device.js @@ -24,7 +24,7 @@ var mongoose = require('mongoose'), Schema = mongoose.Schema, - autoIncrement = require('mongoose-auto-increment'); + autoIncrement = require('mongoose-plugin-autoinc'); var Device = new Schema({ id: Number, @@ -38,7 +38,6 @@ var Device = new Schema({ }); function load(db) { - autoIncrement.initialize(db); module.exports.model = db.model('Device', Device); Device.plugin(autoIncrement.plugin, { model: 'Device', field: 'id' }); module.exports.internalSchema = Device; diff --git a/package.json b/package.json index 26e5523..e79328f 100644 --- a/package.json +++ b/package.json @@ -19,44 +19,35 @@ "node": ">=0.10.8" }, "scripts": { - "test": "grunt" + "clean": "rm -rf package-lock.json && rm -rf node_modules && rm -rf coverage", + "test": "mocha --recursive 'test/**/*.js' --reporter spec --timeout 3000 --ui bdd --exit", + "test:watch": "npm run test -- -w ./lib", + "lint": "jshint lib/**/*.js test/**/*.js --config .jshintrc", + "test:coverage": "istanbul cover _mocha -- --recursive 'test/**/*.js' --reporter spec --exit", + "watch": "watch 'npm test && npm run lint' ./lib ./test" }, "bin": { "iotagent-lwm2m-client": "bin/iotagent-lwm2m-client.js", "iotagent-lwm2m-server": "bin/iotagent-lwm2m-server.js" }, "dependencies": { - "async": "1.5.2", - "coap": "0.21.0", - "coap-cli": "0.4.0", + "async": "2.6.1", + "coap": "0.22.0", + "coap-cli": "0.5.1", "command-node": "0.1.1", - "mongodb": "2.2.35", - "mongoose": "4.4.3", - "mongoose-auto-increment": "5.0.1", - "logops": "0.5.0", - "underscore": "1.7.0" + "logops": "1.0.0", + "mongodb": "3.1.8", + "mongoose": "5.3.9", + "mongoose-plugin-autoinc": "1.1.9", + "underscore": "1.9.1" }, "devDependencies": { - "grunt": "0.4.5", - "closure-linter-wrapper": "1.0.1", - "grunt-contrib-clean": "0.7.0", - "grunt-contrib-jshint": "0.12.0", - "grunt-contrib-watch": "0.6.1", - "grunt-dox": "0.5.0", - "grunt-exec": "0.4.6", - "grunt-githooks": "0.5.0", - "grunt-github-pages": "0.0.4", - "grunt-gjslint": "0.2.1", - "grunt-mkdir": "0.1.2", - "grunt-mocha-test": "0.12.7", - "grunt-plato": "~1.3.0", - "mocha": "2.4.5", - "should": "8.2.2", - "chai": "~1.8.0", - "sinon": "~1.7.3", - "istanbul": "~0.1.34", - "sinon-chai": "2.4.0", - "proxyquire": "0.5.1" + "istanbul": "~0.4.5", + "jshint": "^2.9.6", + "mocha": "5.2.0", + "proxyquire": "2.1.0", + "should": "13.2.3", + "watch": "^1.0.2" }, "keywords": [] } diff --git a/test/.jshintrc b/test/.jshintrc index 7f39c99..f9389d9 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -26,8 +26,7 @@ "after": true, "beforeEach": true, "afterEach": true, - "mock": true, - "sinon": true + "mock": true }, "predef": [ diff --git a/test/unit/server/device-registry-mongo-test.js b/test/unit/server/device-registry-mongo-test.js index 634f8cd..6f24622 100644 --- a/test/unit/server/device-registry-mongo-test.js +++ b/test/unit/server/device-registry-mongo-test.js @@ -73,7 +73,7 @@ describe('MongoDB Device registry', function() { delete config.server.deviceRegistry; delete config.server.mongodb; - iotAgentDb.collection('devices').remove(function(error) { + iotAgentDb.db().collection('devices').remove(function(error) { iotAgentDb.close(function(error) { libLwm2m2.stop(testInfo.serverInfo, done); }); @@ -83,7 +83,7 @@ describe('MongoDB Device registry', function() { describe('When a registration request arrives to the server', function() { it('should insert a new device in the "devices" collection', function(done) { utils.registerClient('ROOM001', function (error) { - iotAgentDb.collection('devices').find({}).toArray(function(err, docs) { + iotAgentDb.db().collection('devices').find({}).toArray(function(err, docs) { should.not.exist(err); should.exist(docs); should.exist(docs.length); @@ -92,14 +92,13 @@ describe('MongoDB Device registry', function() { should.exist(docs[0].path); should.exist(docs[0].lifetime); should.exist(docs[0].type); - should.exist(docs[0].id); done(); }); }); }); it('should store the device with all its attributes', function(done) { utils.registerClient('ROOM001', function (error) { - iotAgentDb.collection('devices').find({}).toArray(function(err, docs) { + iotAgentDb.db().collection('devices').find({}).toArray(function(err, docs) { should.exist(docs[0]); should.exist(docs[0].path); should.exist(docs[0].lifetime); diff --git a/tools/README.md b/tools/README.md deleted file mode 100644 index cbf3818..0000000 --- a/tools/README.md +++ /dev/null @@ -1,4 +0,0 @@ -TOOLS for project -================== - -Include in this directory all build related tools and scripts needed for the project building \ No newline at end of file diff --git a/tools/github-pages.sh b/tools/github-pages.sh deleted file mode 100644 index bab41b4..0000000 --- a/tools/github-pages.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -git checkout --orphan gh-pages -git rm -rf . -git checkout master -- ghpages -mv ghpages/* . -rmdir ghpages/ -echo "node_modules" > .gitignore -git add . -git commit -am "CREATE GitHub pages for the project" -git push origin gh-pages -git checkout master -git clone git://github.com/dmj/iotagent-lwm2m-lib.git site -cd site -git checkout gh-pages - diff --git a/tools/init-site-env.sh b/tools/init-site-env.sh deleted file mode 100644 index ebf944e..0000000 --- a/tools/init-site-env.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -git clone git://github.com/dmj/iotagent-lwm2m-lib.git site -cd site -git checkout gh-pages \ No newline at end of file diff --git a/tools/mocha-globals.js b/tools/mocha-globals.js deleted file mode 100644 index d26fa19..0000000 --- a/tools/mocha-globals.js +++ /dev/null @@ -1,16 +0,0 @@ -var sinon = require ('sinon'), - chai = require ('chai'), - sinonChai = require('sinon-chai'); - -chai.use(sinonChai); - -global.expect = chai.expect; -global.should = chai.should(); - -beforeEach(function(){ - global.sinon = sinon.sandbox.create(); -}); - -afterEach(function(){ - global.sinon.restore(); -}); diff --git a/tools/tasks/README.md b/tools/tasks/README.md deleted file mode 100644 index 4358a87..0000000 --- a/tools/tasks/README.md +++ /dev/null @@ -1,8 +0,0 @@ -Grunt Tasks -=========== - -Include here all the custom grunt tasks related to the project. - -Try to isolate common tasks in separate repositories, and share with your colleagues (*) - -(* pending to solve npm full compliance within pdihub) diff --git a/tools/tasks/contributors.js b/tools/tasks/contributors.js deleted file mode 100644 index a8f9bc4..0000000 --- a/tools/tasks/contributors.js +++ /dev/null @@ -1,35 +0,0 @@ -module.exports = function(grunt) { - - var exec = require('child_process').exec; - - grunt.registerTask('contributors', 'Update contributors in package.json', function() { - var done = this.async(); - - exec('git log --pretty=short | git shortlog -nse', function(err, stdout) { - var pkgFile = grunt.config('pkgFile'); - var pkg = grunt.file.readJSON(pkgFile); - - pkg.contributors = stdout.toString().split('\n').slice(1, -1).map(function(line) { - return line.replace(/^[\W\d]+/, ''); - }); - - grunt.file.write(pkgFile, JSON.stringify(pkg, null, ' ') + '\n'); - - exec('git status -s ' + pkgFile, function(err, stdout) { - if (!stdout) { - grunt.log.ok('The contributors list is already up to date.'); - return done(); - } - - exec('git commit ' + pkgFile + ' -m "UPDATE contributors"', function(err) { - if (err) { - grunt.log.error('Cannot commit contributors changes.'); - } else { - grunt.log.ok('The contributors list has been updated.'); - } - done(); - }); - }); - }); - }); -};