-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MF-950 - Runtime error in normalizer - CBOR SenML (#974)
* Add Transformer tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix readers and writers Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix README typo Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Tidy vendor Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove link field from docs Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
- Loading branch information
1 parent
5edd797
commit 54168fa
Showing
100 changed files
with
5,348 additions
and
54,391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,6 @@ var ( | |
Unit: "U", | ||
Time: 123456, | ||
UpdateTime: 1234, | ||
Link: "link", | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package senml_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/mainflux/mainflux" | ||
"github.com/mainflux/mainflux/transformers/senml" | ||
mfsenml "github.com/mainflux/senml" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTransform(t *testing.T) { | ||
// Following hex-encoded bytes correspond to the content of: | ||
// [{-2: "base-name", -3: 100.0, -4: "base-unit", -1: 10, -5: 10.0, -6: 100.0, 0: "name", 1: "unit", 6: 300.0, 7: 150.0, 2: 42.0, 5: 10.0}] | ||
// For more details for mapping SenML labels to integers, please take a look here: https://tools.ietf.org/html/rfc8428#page-19. | ||
cborBytes, err := hex.DecodeString("81ac2169626173652d6e616d6522fb40590000000000002369626173652d756e6974200a24fb402400000000000025fb405900000000000000646e616d650164756e697406fb4072c0000000000007fb4062c0000000000002fb404500000000000005fb4024000000000000") | ||
require.Nil(t, err, "Decoding CBOR expected to succeed") | ||
|
||
jsonBytes, err := hex.DecodeString("5b7b22626e223a22626173652d6e616d65222c226274223a3130302c226275223a22626173652d756e6974222c2262766572223a31302c226276223a31302c226273223a3130302c226e223a226e616d65222c2275223a22756e6974222c2274223a3330302c227574223a3135302c2276223a34322c2273223a31307d5d") | ||
require.Nil(t, err, "Decoding CBOR expected to succeed") | ||
|
||
tooManyBytes, err := hex.DecodeString("82AD2169626173652D6E616D6522F956402369626173652D756E6974200A24F9490025F9564000646E616D650164756E697406F95CB0036331323307F958B002F9514005F94900AA2169626173652D6E616D6522F956402369626173652D756E6974200A24F9490025F9564000646E616D6506F95CB007F958B005F94900") | ||
require.Nil(t, err, "Decoding CBOR expected to succeed") | ||
|
||
tr := senml.New() | ||
msg := mainflux.Message{ | ||
Channel: "channel", | ||
Subtopic: "subtopic", | ||
Publisher: "publisher", | ||
Protocol: "protocol", | ||
Payload: jsonBytes, | ||
} | ||
|
||
// 82AD2169626173652D6E616D6522F956402369626173652D756E6974200A24F9490025F9564000646E616D650164756E697406F95CB0036331323307F958B002F9514005F94900AA2169626173652D6E616D6522F956402369626173652D756E6974200A24F9490025F9564000646E616D6506F95CB007F958B005F94900 | ||
|
||
jsonPld := msg | ||
jsonPld.ContentType = senml.JSON | ||
jsonPld.Payload = jsonBytes | ||
|
||
cborPld := msg | ||
cborPld.ContentType = senml.CBOR | ||
cborPld.Payload = cborBytes | ||
|
||
tooManyMsg := msg | ||
tooManyMsg.ContentType = senml.CBOR | ||
tooManyMsg.Payload = tooManyBytes | ||
|
||
val := 52.0 | ||
sum := 110.0 | ||
msgs := []senml.Message{senml.Message{ | ||
Channel: "channel", | ||
Subtopic: "subtopic", | ||
Publisher: "publisher", | ||
Protocol: "protocol", | ||
Name: "base-namename", | ||
Unit: "unit", | ||
Time: 400, | ||
UpdateTime: 150, | ||
Value: &val, | ||
Sum: &sum, | ||
}, | ||
} | ||
|
||
cases := []struct { | ||
desc string | ||
msg mainflux.Message | ||
msgs interface{} | ||
err error | ||
}{ | ||
{ | ||
desc: "test normalize CBOR", | ||
msg: cborPld, | ||
msgs: msgs, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "test normalize JSON", | ||
msg: jsonPld, | ||
msgs: msgs, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "test normalize defaults to JSON", | ||
msg: msg, | ||
msgs: msgs, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "test invalid payload", | ||
msg: tooManyMsg, | ||
msgs: nil, | ||
err: mfsenml.ErrTooManyValues, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
msgs, err := tr.Transform(tc.msg) | ||
assert.Equal(t, tc.msgs, msgs, fmt.Sprintf("%s expected %v, got %v", tc.desc, tc.msgs, msgs)) | ||
assert.Equal(t, tc.err, err, fmt.Sprintf("%s expected %s, got %s", tc.desc, tc.err, err)) | ||
} | ||
} |
Oops, something went wrong.