UNPKG

lwm2m

Version:

Library for developing servers and client of OMA Lightweight M2M

190 lines (159 loc) 5.16 kB
/* * Copyright 2017 Telefonica Investigación y Desarrollo, S.A.U * * This file is part of node-lwm2m * * node-lwm2m 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. * * node-lwm2m 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 node-lwm2m. * 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] * * Author: Alexandre Moreno <alex_moreno@tutk.com> */ 'use strict'; var should = require('should'), // jshint ignore:line senml = require('../lib/senml'), Schema = require('../').Schema, deviceSchema = new Schema(require('../lib/oma/device')); var object = { manufacturer: 'Open Mobile Alliance', modelNumber: 'Lightweight M2M Client', serialNumber: '345000123', firmwareVer: '1.0', powerSrcs: [ 1, 5 ], srcVoltage: [ 3800, 5000 ], srcCurrent: [ 125, 900 ], batteryLevel: 100, memoryFree: 15, errorCode: [ 0 ], currentTime: new Date('2013-05-02T10:40:15.000Z'), utcOffset: '+02:00', timeZone: 'U', }; var payload = '{"e":[' + '{"n":"0","sv":"Open Mobile Alliance"},' + '{"n":"1","sv":"Lightweight M2M Client"},' + '{"n":"2","sv":"345000123"},' + '{"n":"3","sv":"1.0"},' + '{"n":"6/0","v":1},' + '{"n":"6/1","v":5},' + '{"n":"7/0","v":3800},' + '{"n":"7/1","v":5000},' + '{"n":"8/0","v":125},' + '{"n":"8/1","v":900},' + '{"n":"9","v":100},' + '{"n":"10","v":15},' + '{"n":"11/0","v":0},' + '{"n":"13","v":1367491215},' + '{"n":"14","sv":"+02:00"},' + '{"n":"15","sv":"U"}]}'; describe('application/vnd.oma.lwm2m+json', function() { describe('types', function() { it('should return original integer', function() { var schema = new Schema({ foo: { id:3, type:'Integer' }, }); var value = { foo: -42 }; var buf = senml.serialize(value, schema); senml.parse(buf, schema).should.be.eql(value); }); it('should return original array', function() { var schema = new Schema({ foo: { id:3, type:['Integer'] }, }); var value = { foo: [-1, 0, 1] }; var buf = senml.serialize(value, schema); senml.parse(buf, schema).should.be.eql(value); }); it('should return original float', function() { var schema = new Schema({ foo: { id:3, type:'Float' }, }); var value = { foo: 42.42 }; var buf = senml.serialize(value, schema); senml.parse(buf, schema).should.be.eql(value); }); it('should return original boolean', function() { var schema = new Schema({ foo: { id:3, type:'Boolean' }, }); var value = { foo: true }; var buf = senml.serialize(value, schema); senml.parse(buf, schema).should.be.eql(value); }); it('should return original buffer', function() { var schema = new Schema({ foo: { id:3, type:'Opaque' }, }); var value = { foo: Buffer.from('bar') }; var buf = senml.serialize(value, schema); senml.parse(buf, schema).should.be.eql(value); }); }); describe('#serialize', function() { it('should return a valid payload', function() { var dev = senml.serialize(object, deviceSchema); dev.should.equal(payload); }); it('should skip user properties', function() { var obj = Object.assign({}, object, { foo: 'bar' }); var dev = senml.serialize(obj, deviceSchema); dev.should.equal(payload); }); }); describe('#parse', function() { it('should return an object', function() { var dev = senml.parse(payload, deviceSchema); dev.should.be.an.Object().and.not.empty(); }); it('should strictly return matching resources from schema', function() { var dev = senml.parse(payload, deviceSchema), keys = Object.keys(deviceSchema.resources); Object.keys(dev).should.matchEach(function(it) { return it.should.be.oneOf(keys); }); }); it('should return a composite object when using Object links', function() { var Bar = new Schema({ bar: { type: 'String', id: 2, }, baz: { type: 'Boolean', id: 3, }, }); var Foo = new Schema({ foo: { type: 'Objlnk', id: 1, schema: Bar, }, }); var test = '{"bn":"/","e":[' + '{"n":"65/0/1","ov":"66:0"},' + '{"n":"66/0/2","sv":"test"},' + '{"n":"66/0/3","bv":false}]}'; var result = senml.parse(test, Foo); result.should.have.properties({ foo: { bar: 'test', baz: false, }, }); }); }); });