From 5e43ea8719053a96d514a284882d258283605661 Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Fri, 28 Jul 2023 07:27:06 +0000 Subject: [PATCH 1/8] Fix for 690 --- CHANGES_NEXT_RELEASE | 1 + lib/bindings/HTTPBinding.js | 7 ++++++- .../ngsiv2/contextRequests/singleMeasuresRawTypes.json | 8 +------- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 8d17d739c..9564edc18 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,4 @@ +- Fix: binary data representation when sending data through HTTP & MQTT #690 - Fix: update tests according new default value for appendMode - Fix: use 'options=upsert' when update ngsiv2 CB entities and appendMode is enabled (iotagent-node-lib#956) - Fix: do not propagate group config (timestamp and explicitAttrs) to autoprovisioned devices (at database level) (iotagent-node-lib#1377) diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index cc41a69d0..a356905c4 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -127,7 +127,9 @@ function parseData(req, res, next) { next(error); } else { req.jsonPayload = data; - + if (req.body !== undefined || req.body === Object || Buffer.isBuffer(req.body)) { + data = data.toString('hex'); + } config.getLogger().debug(context, 'Parsed data: [%j]', data); next(); } @@ -290,6 +292,9 @@ function handleIncomingMeasure(req, res, next) { if (req.attr && req.jsonPayload) { config.getLogger().debug(context, 'Parsing attr [%s] with value [%s]', req.attr, req.jsonPayload); + if (req.body !== undefined || req.body === Object || Buffer.isBuffer(req.body)) { + req.jsonPayload = req.jsonPayload.toString('hex'); + } const theAttr = [{ name: req.attr, value: req.jsonPayload, type: 'None' }]; attributeArr.push(theAttr); } else { diff --git a/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes.json b/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes.json index a8a1b2036..79a951f57 100644 --- a/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes.json +++ b/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes.json @@ -3,12 +3,6 @@ "type":"AnMQTTDevice", "humidity":{ "type": "degrees", - "value": { - "type": "Buffer", - "data": [ - 51, - 50 - ] - } + "value": "3332" } } From 289662d7e2e66a99571aa9df6e938f14288d8dba Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Mon, 31 Jul 2023 09:48:07 +0000 Subject: [PATCH 2/8] MQTT test case for binary data --- .../ngsiv2/MQTT_receive_measures-test2.js | 28 +++++++++++++++++++ .../singleMeasuresRawTypes1.json | 8 ++++++ 2 files changed, 36 insertions(+) create mode 100644 test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json diff --git a/test/unit/ngsiv2/MQTT_receive_measures-test2.js b/test/unit/ngsiv2/MQTT_receive_measures-test2.js index cbb97005d..81f10aecf 100644 --- a/test/unit/ngsiv2/MQTT_receive_measures-test2.js +++ b/test/unit/ngsiv2/MQTT_receive_measures-test2.js @@ -403,4 +403,32 @@ describe('MQTT: Measure reception ', function () { }); }); }); + describe('When a POST single Raw measure arrives for the HTTP binding', function () { + beforeEach(function () { + contextBrokerMock + .matchHeader('fiware-service', 'smartgondor') + .matchHeader('fiware-servicepath', '/gardens') + .post( + '/v2/entities?options=upsert', + utils.readExampleFile('./test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json') + ) + .reply(204); + }); + it('should send its value to the Context Broker', function (done) { + mqttClient.publish('/1234/MQTT_2/attrs/humidity', '0x4D', null, function (error) { + setTimeout(function () { + contextBrokerMock.done(); + done(); + }, 100); + }); + }); + it('should return a 200 OK with no error', function (done) { + mqttClient.publish('/1234/MQTT_2/attrs/humidity', '0x4D', null, function (error) { + setTimeout(function () { + contextBrokerMock.done(); + done(); + }, 100); + }); + }); + }); }); diff --git a/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json b/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json new file mode 100644 index 000000000..88a42889f --- /dev/null +++ b/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json @@ -0,0 +1,8 @@ +{ + "id":"Second MQTT Device", + "type":"AnMQTTDevice", + "humidity":{ + "type": "degrees", + "value": "30783444" + } +} From 6521d77c4bcc884d909186afce327bfaeefffd36 Mon Sep 17 00:00:00 2001 From: Keshav-NEC <102344018+Keshav-NEC@users.noreply.github.com> Date: Wed, 9 Aug 2023 21:24:07 +0530 Subject: [PATCH 3/8] Update test/unit/ngsiv2/MQTT_receive_measures-test2.js Co-authored-by: mapedraza <40356341+mapedraza@users.noreply.github.com> --- test/unit/ngsiv2/MQTT_receive_measures-test2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/ngsiv2/MQTT_receive_measures-test2.js b/test/unit/ngsiv2/MQTT_receive_measures-test2.js index 81f10aecf..1ab14414b 100644 --- a/test/unit/ngsiv2/MQTT_receive_measures-test2.js +++ b/test/unit/ngsiv2/MQTT_receive_measures-test2.js @@ -415,7 +415,7 @@ describe('MQTT: Measure reception ', function () { .reply(204); }); it('should send its value to the Context Broker', function (done) { - mqttClient.publish('/1234/MQTT_2/attrs/humidity', '0x4D', null, function (error) { + mqttClient.publish('/1234/MQTT_2/attrs/humidity', 'A$Ci1', null, function (error) { setTimeout(function () { contextBrokerMock.done(); done(); From cd50c6dc71661669ba06667b879ec4860b9f903f Mon Sep 17 00:00:00 2001 From: Keshav-NEC <102344018+Keshav-NEC@users.noreply.github.com> Date: Wed, 9 Aug 2023 21:24:35 +0530 Subject: [PATCH 4/8] Update singleMeasuresRawTypes1.json --- test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json b/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json index 88a42889f..64f831987 100644 --- a/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json +++ b/test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json @@ -3,6 +3,6 @@ "type":"AnMQTTDevice", "humidity":{ "type": "degrees", - "value": "30783444" + "value": "4124436931" } } From bf45b6b74fce46bd3d43eecb6ea6e31208ebcf99 Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Thu, 10 Aug 2023 15:22:36 +0530 Subject: [PATCH 5/8] Latest_Changes --- lib/bindings/HTTPBinding.js | 8 ++------ test/unit/ngsiv2/MQTT_receive_measures-test2.js | 8 -------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index a356905c4..a1ab1ae67 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -127,9 +127,7 @@ function parseData(req, res, next) { next(error); } else { req.jsonPayload = data; - if (req.body !== undefined || req.body === Object || Buffer.isBuffer(req.body)) { - data = data.toString('hex'); - } + data = data.toString('hex'); config.getLogger().debug(context, 'Parsed data: [%j]', data); next(); } @@ -292,9 +290,7 @@ function handleIncomingMeasure(req, res, next) { if (req.attr && req.jsonPayload) { config.getLogger().debug(context, 'Parsing attr [%s] with value [%s]', req.attr, req.jsonPayload); - if (req.body !== undefined || req.body === Object || Buffer.isBuffer(req.body)) { - req.jsonPayload = req.jsonPayload.toString('hex'); - } + req.jsonPayload = req.jsonPayload.toString('hex'); const theAttr = [{ name: req.attr, value: req.jsonPayload, type: 'None' }]; attributeArr.push(theAttr); } else { diff --git a/test/unit/ngsiv2/MQTT_receive_measures-test2.js b/test/unit/ngsiv2/MQTT_receive_measures-test2.js index 1ab14414b..cb5509866 100644 --- a/test/unit/ngsiv2/MQTT_receive_measures-test2.js +++ b/test/unit/ngsiv2/MQTT_receive_measures-test2.js @@ -422,13 +422,5 @@ describe('MQTT: Measure reception ', function () { }, 100); }); }); - it('should return a 200 OK with no error', function (done) { - mqttClient.publish('/1234/MQTT_2/attrs/humidity', '0x4D', null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); }); }); From 008ef3496fcefd50f308e947840a80eb65e3ed26 Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Wed, 16 Aug 2023 07:17:04 +0000 Subject: [PATCH 6/8] FixForTestcaseFailing --- lib/bindings/HTTPBinding.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index a1ab1ae67..71ea243f4 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -127,7 +127,9 @@ function parseData(req, res, next) { next(error); } else { req.jsonPayload = data; - data = data.toString('hex'); + if (req.body !== undefined || req.body === Object || Buffer.isBuffer(req.body)) { + data = data.toString('hex'); + } config.getLogger().debug(context, 'Parsed data: [%j]', data); next(); } From a018bd26e101cc31247f026617454946e3830be9 Mon Sep 17 00:00:00 2001 From: Keshav-NEC <102344018+Keshav-NEC@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:44:27 +0530 Subject: [PATCH 7/8] Update HTTPBinding.js --- lib/bindings/HTTPBinding.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index 71ea243f4..26f0a0c69 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -127,7 +127,7 @@ function parseData(req, res, next) { next(error); } else { req.jsonPayload = data; - if (req.body !== undefined || req.body === Object || Buffer.isBuffer(req.body)) { + if (req.body !== undefined ) { data = data.toString('hex'); } config.getLogger().debug(context, 'Parsed data: [%j]', data); From f5f7406f41ec3d2a4260baff81ee54a9df24a32a Mon Sep 17 00:00:00 2001 From: Keshav-NEC <102344018+Keshav-NEC@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:18:51 +0530 Subject: [PATCH 8/8] Update CHANGES_NEXT_RELEASE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fermín Galán Márquez --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 98df44706..8f0686530 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ -- Fix: binary data representation when sending data through HTTP & MQTT #690 +- Fix: binary data representation when sending data through HTTP & MQTT (#690)