From ce2e261bc92a794d9ed1f9ff0727a7b31c1088e0 Mon Sep 17 00:00:00 2001 From: mapedraza Date: Fri, 20 Oct 2023 17:32:30 +0200 Subject: [PATCH] Make assertions more verbose --- package.json | 1 + test/functional/functional-tests.js | 146 +++++++++------------------- 2 files changed, 45 insertions(+), 102 deletions(-) diff --git a/package.json b/package.json index 025cd8308..c77b9a495 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "amqplib": "~0.5.1", "async": "2.6.4", "body-parser": "1.20.0", + "chai": "^4.3.10", "dateformat": "3.0.3", "express": "4.18.1", "iotagent-node-lib": "https://github.com/telefonicaid/iotagent-node-lib.git#master", diff --git a/test/functional/functional-tests.js b/test/functional/functional-tests.js index cf799b074..3eb33417e 100755 --- a/test/functional/functional-tests.js +++ b/test/functional/functional-tests.js @@ -28,8 +28,9 @@ const iotaJson = require('../..'); const config = require('./config-test.js'); const nock = require('nock'); +const chai = require('chai'); +const expect = chai.expect; const iotAgentLib = require('iotagent-node-lib'); -const should = require('should'); const async = require('async'); const utils = require('../utils'); @@ -41,91 +42,18 @@ let contextBrokerMock; describe('FUNCTIONAL TESTS', function () { beforeEach(function (done) { + this.timeout(6000); nock.cleanAll(); - iotaJson.start(config, function () { - done(); + iotaJson.start(config, function (error) { + done(error); }); }); afterEach(function (done) { nock.cleanAll(); - async.series([iotAgentLib.clearAll, iotaJson.stop], done); }); - describe('Basic group provision without attributes', function () { - const provision = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: { - services: [ - { - resource: '/iot/json', - apikey: '123456', - entity_type: 'TheLightType', - cbHost: 'http://192.168.1.1:1026', - commands: [], - lazy: [], - attributes: [], - static_attributes: [] - } - ] - }, - headers: { - 'fiware-service': 'smartgondor', - 'fiware-servicepath': '/gardens' - } - }; - - const measure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - status: true - }, - qs: { - i: 'MQTT_2', - k: '123456' - } - }; - - const expectation = { - id: 'TheLightType:MQTT_2', - type: 'TheLightType', - status: { - value: true, - type: 'string' - } - }; - - beforeEach(function (done) { - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartgondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v2/entities?options=upsert', expectation) - .reply(204); - - request(provision, function (error, response, body) { - done(); - }); - }); - - it('should return a 200 OK with no error', function (done) { - request(measure, function (error, result, body) { - should.not.exist(error); - result.statusCode.should.equal(200); - done(); - }); - }); - - it('should send its value to the Context Broker', function (done) { - request(measure, function (error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - describe('Basic group provision with attributes', function () { const provision = { url: 'http://localhost:' + config.iota.server.port + '/iot/services', @@ -164,54 +92,68 @@ describe('FUNCTIONAL TESTS', function () { const measure = { url: 'http://localhost:' + config.http.port + '/iot/json', method: 'POST', - json: { - s: true, - t: 20 - }, qs: { i: 'MQTT_2', k: '123456' + }, + json: { + s: true, + t: 20 } }; const expectation = { id: 'TheLightType2:MQTT_2', type: 'TheLightType2', - status: { - value: true, - type: 'Boolean' - }, temperature: { - value: 20, + value: 10, type: 'Number' + }, + status: { + value: false, + type: 'Boolean' } }; beforeEach(function (done) { + request(provision, function (error, response, body) { + let err = null; + if (response.statusCode != 201) { + err = new Error('Error creating the service'); + } + done(err); + }); + }); + + afterEach(function () { + nock.cleanAll(); + }); + + it('should send its value to the Context Broker', async function () { + let receivedBody; contextBrokerMock = nock('http://192.168.1.1:1026') .matchHeader('fiware-service', 'smartgondor') .matchHeader('fiware-servicepath', '/gardens') - .post('/v2/entities?options=upsert', expectation) + .post('/v2/entities?options=upsert', function (body) { + receivedBody = body; // Save the received body for later comparison + return true; + }) .reply(204); - request(provision, function (error, response, body) { - done(); + // Send a measure to the IoT Agent and wait for the response + const response = await new Promise((resolve, reject) => { + request(measure, function (error, result, body) { + error ? reject(error) : resolve(result); + }); }); - }); - it('should return a 200 OK with no error', function (done) { - request(measure, function (error, result, body) { - should.not.exist(error); - result.statusCode.should.equal(200); - done(); - }); - }); + // Validate the response status code and the receivedBody + expect(response.statusCode, 'Measure response: Status code differs from 200').to.equal(200); + expect(response.body, 'Measure response: body not empty').to.be.empty; - it('should send its value to the Context Broker', function (done) { - request(measure, function (error, result, body) { - contextBrokerMock.done(); - done(); - }); + // Validate Context Broker Expectation + contextBrokerMock.done(); // Ensure the request was made, no matter the body content + expect(receivedBody, 'CB payload: message differs from expectation').to.deep.equal(expectation); }); }); });