Skip to content

Commit

Permalink
Merge branch 'master' into issue-528
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudxxx8 authored Mar 2, 2021
2 parents 034c2e8 + f9dfc94 commit 47da0a4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
16 changes: 14 additions & 2 deletions v2/dtos/requests/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
"github.com/fxamacker/cbor/v2"
)

// AddEventRequest defines the Request Content for POST event DTO.
Expand Down Expand Up @@ -49,13 +50,24 @@ func (a AddEventRequest) Validate() error {
return nil
}

type unmarshal func([]byte, interface{}) error

func (a *AddEventRequest) UnmarshalJSON(b []byte) error {
return a.Unmarshal(b, json.Unmarshal)
}

func (a *AddEventRequest) UnmarshalCBOR(b []byte) error {
return a.Unmarshal(b, cbor.Unmarshal)
}

func (a *AddEventRequest) Unmarshal(b []byte, f unmarshal) error {
// To avoid recursively invoke unmarshaler interface, intentionally create a struct to represent AddEventRequest DTO
var addEvent struct {
common.BaseRequest
Event dtos.Event
}
if err := json.Unmarshal(b, &addEvent); err != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
if err := f(b, &addEvent); err != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal the byte array.", err)
}

*a = AddEventRequest(addEvent)
Expand Down
45 changes: 45 additions & 0 deletions v2/dtos/requests/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
"github.com/fxamacker/cbor/v2"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -234,6 +235,50 @@ func TestAddEvent_UnmarshalJSON(t *testing.T) {
}
}

func TestAddEvent_UnmarshalCBOR(t *testing.T) {
expected := eventRequestData()
expected.RequestId = ExampleUUID
validData, err := cbor.Marshal(expected)
require.NoError(t, err)

validValueTypeLowerCase := eventRequestData()
validValueTypeLowerCase.RequestId = ExampleUUID
validValueTypeLowerCase.Event.Readings[0].ValueType = "uint8"
validValueTypeLowerCaseData, err := cbor.Marshal(validValueTypeLowerCase)
require.NoError(t, err)

validValueTypeUpperCase := eventRequestData()
validValueTypeUpperCase.RequestId = ExampleUUID
validValueTypeUpperCase.Event.Readings[0].ValueType = "UINT8"
validValueTypeUpperCaseData, err := cbor.Marshal(validValueTypeUpperCase)
require.NoError(t, err)

tests := []struct {
name string
data []byte
wantErr bool
}{
{"unmarshal AddEventRequest with success", validData, false},
{"unmarshal AddEventRequest with success, valid value type uint8", validValueTypeLowerCaseData, false},
{"unmarshal AddEventRequest with success, valid value type UINT8", validValueTypeUpperCaseData, false},
{"unmarshal invalid AddEventRequest, empty data", []byte{}, true},
{"unmarshal invalid AddEventRequest, string data", []byte("Invalid AddEventRequest"), true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var addEvent AddEventRequest
err := addEvent.UnmarshalCBOR(tt.data)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, expected, addEvent, "Unmarshal did not result in expected AddEventRequest.")
}
})
}
}

func Test_AddEventReqToEventModels(t *testing.T) {
valid := eventRequestData()
s := models.SimpleReading{
Expand Down

0 comments on commit 47da0a4

Please sign in to comment.