1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
Mainflux.mainflux/transformers/senml/transformer_test.go
Ivan Milošević 880e193b0a
NOISSUE - Implement errors package in senml transformer, readers and writers (#1108)
* Implement errors package in senml transformer, readers and writers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Remove unused const
Return wrapped error in postgres writer

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* fix default db host in postgres writer

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* fix capital letters in errors messages

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* use svcName instead of postgres for Promethius initialization

Signed-off-by: Ivan Milošević <iva@blokovi.com>
2020-04-13 12:57:53 +02:00

109 lines
3.4 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package senml_test
import (
"encoding/hex"
"fmt"
"testing"
"github.com/mainflux/mainflux/broker"
"github.com/mainflux/mainflux/errors"
"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 := broker.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 broker.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.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s expected %s, got %s", tc.desc, tc.err, err))
}
}