|
| 1 | +/* |
| 2 | + * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U |
| 3 | + * |
| 4 | + * This file is part of fiware-iotagent-lib |
| 5 | + * |
| 6 | + * fiware-iotagent-lib is free software: you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the License, |
| 9 | + * or (at your option) any later version. |
| 10 | + * |
| 11 | + * fiware-iotagent-lib is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * See the GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public |
| 17 | + * License along with fiware-iotagent-lib. |
| 18 | + * If not, see http://www.gnu.org/licenses/. |
| 19 | + * |
| 20 | + * For those usages not covered by the GNU Affero General Public License |
| 21 | + * please contact with::[email protected] |
| 22 | + * |
| 23 | + * Modified by: Daniel Calvo - ATOS Research & Innovation |
| 24 | + */ |
| 25 | + |
| 26 | +const iotAgentLib = require('../../../../lib/fiware-iotagent-lib'); |
| 27 | +const should = require('should'); |
| 28 | +const logger = require('logops'); |
| 29 | +const nock = require('nock'); |
| 30 | +let contextBrokerMock; |
| 31 | +const iotAgentConfig = { |
| 32 | + contextBroker: { |
| 33 | + host: '192.168.1.1', |
| 34 | + port: '1026', |
| 35 | + ngsiVersion: 'ld', |
| 36 | + jsonLdContext: 'http://context.json-ld' |
| 37 | + }, |
| 38 | + server: { |
| 39 | + port: 4041 |
| 40 | + }, |
| 41 | + types: { |
| 42 | + Light: { |
| 43 | + commands: [], |
| 44 | + type: 'Light', |
| 45 | + lazy: [ |
| 46 | + { |
| 47 | + name: 'temperature', |
| 48 | + type: 'centigrades' |
| 49 | + } |
| 50 | + ], |
| 51 | + active: [ |
| 52 | + { |
| 53 | + name: 'pressure', |
| 54 | + type: 'Hgmm' |
| 55 | + } |
| 56 | + ] |
| 57 | + } |
| 58 | + }, |
| 59 | + service: 'smartgondor', |
| 60 | + subservice: 'gardens', |
| 61 | + providerUrl: 'http://smartgondor.com' |
| 62 | +}; |
| 63 | + |
| 64 | +describe('NGSI-LD - Custom plugin', function () { |
| 65 | + let updateInvoked = false; |
| 66 | + let queryInvoked = false; |
| 67 | + |
| 68 | + function updatePlugin(entity, typeInformation, callback) { |
| 69 | + updateInvoked = true; |
| 70 | + return callback(null, entity, typeInformation); |
| 71 | + } |
| 72 | + function queryPlugin(entity, typeInformation, callback) { |
| 73 | + queryInvoked = true; |
| 74 | + return callback(null, entity, typeInformation); |
| 75 | + } |
| 76 | + beforeEach(function (done) { |
| 77 | + logger.setLevel('FATAL'); |
| 78 | + |
| 79 | + iotAgentLib.activate(iotAgentConfig, function () { |
| 80 | + iotAgentLib.clearAll(function () { |
| 81 | + iotAgentLib.addUpdateMiddleware(updatePlugin); |
| 82 | + iotAgentLib.addQueryMiddleware(queryPlugin); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(function (done) { |
| 89 | + iotAgentLib.clearAll(function () { |
| 90 | + iotAgentLib.deactivate(done); |
| 91 | + updateInvoked = false; |
| 92 | + queryInvoked = false; |
| 93 | + }); |
| 94 | + }); |
| 95 | + describe('When an update occurs', function () { |
| 96 | + const values = [ |
| 97 | + { |
| 98 | + name: 'state', |
| 99 | + type: 'Boolean', |
| 100 | + value: 'true' |
| 101 | + }, |
| 102 | + { |
| 103 | + name: 'dimming', |
| 104 | + type: 'Number', |
| 105 | + value: 23 |
| 106 | + } |
| 107 | + ]; |
| 108 | + |
| 109 | + beforeEach(function () { |
| 110 | + nock.cleanAll(); |
| 111 | + |
| 112 | + contextBrokerMock = nock('http://192.168.1.1:1026') |
| 113 | + .matchHeader('fiware-service', 'smartgondor') |
| 114 | + .matchHeader('fiware-servicepath', 'gardens') |
| 115 | + .post('/ngsi-ld/v1/entityOperations/upsert/') |
| 116 | + .query({ options: 'update' }) |
| 117 | + .reply(204); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should invoke the plugin', function (done) { |
| 121 | + iotAgentLib.update('light1', 'Light', '', values, function (error) { |
| 122 | + should.not.exist(error); |
| 123 | + contextBrokerMock.done(); |
| 124 | + updateInvoked.should.equal(true); |
| 125 | + done(); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + describe('When an query occurs', function () { |
| 130 | + beforeEach(function () { |
| 131 | + nock.cleanAll(); |
| 132 | + |
| 133 | + contextBrokerMock = nock('http://192.168.1.1:1026') |
| 134 | + .matchHeader('fiware-service', 'smartgondor') |
| 135 | + .matchHeader('fiware-servicepath', 'gardens') |
| 136 | + .get('/ngsi-ld/v1/entities/urn:ngsi-ld:Light:light1') |
| 137 | + .query({ attrs: 'state,dimming' }) |
| 138 | + .reply(200, { state: 'good', dimming: '23' }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should invoke the plugin', function (done) { |
| 142 | + const attributes = ['state', 'dimming']; |
| 143 | + iotAgentLib.query('light1', 'Light', '', attributes, function (error) { |
| 144 | + should.not.exist(error); |
| 145 | + contextBrokerMock.done(); |
| 146 | + should.not.exist(error); |
| 147 | + queryInvoked.should.equal(true); |
| 148 | + done(); |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments