Skip to content

Commit

Permalink
feat: update Add Event route to include SourceName
Browse files Browse the repository at this point in the history
- update Add Event REST path and event client
- add SourceName to Event model and related DTOs, factory method

Signed-off-by: Chris Hung <chris@iotechsys.com>
  • Loading branch information
Chris Hung committed Feb 26, 2021
1 parent 82d3046 commit 778f72f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion v2/clients/http/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewEventClient(baseUrl string) interfaces.EventClient {

func (ec *eventClient) Add(ctx context.Context, req requests.AddEventRequest) (
common.BaseWithIdResponse, errors.EdgeX) {
path := path.Join(v2.ApiEventRoute, url.QueryEscape(req.Event.ProfileName), url.QueryEscape(req.Event.DeviceName))
path := path.Join(v2.ApiEventRoute, url.QueryEscape(req.Event.ProfileName), url.QueryEscape(req.Event.DeviceName), url.QueryEscape(req.Event.SourceName))
var br common.BaseWithIdResponse
err := utils.PostRequest(ctx, &br, ec.baseUrl+path, req)
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions v2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const (
ApiVersion = "v2"
ApiBase = "/api/v2"

ApiEventRoute = ApiBase + "/event"
ApiEventProfileNameDeviceNameRoute = ApiEventRoute + "/{" + ProfileName + "}" + "/{" + DeviceName + "}"
ApiAllEventRoute = ApiEventRoute + "/" + All
ApiEventIdRoute = ApiEventRoute + "/" + Id + "/{" + Id + "}"
ApiEventCountRoute = ApiEventRoute + "/" + Count
ApiEventCountByDeviceNameRoute = ApiEventCountRoute + "/" + Device + "/" + Name + "/{" + Name + "}"
ApiEventByDeviceNameRoute = ApiEventRoute + "/" + Device + "/" + Name + "/{" + Name + "}"
ApiEventByTimeRangeRoute = ApiEventRoute + "/" + Start + "/{" + Start + "}/" + End + "/{" + End + "}"
ApiEventByAgeRoute = ApiEventRoute + "/" + Age + "/{" + Age + "}"
ApiEventRoute = ApiBase + "/event"
ApiEventProfileNameDeviceNameSourceNameRoute = ApiEventRoute + "/{" + ProfileName + "}" + "/{" + DeviceName + "}" + "/{" + SourceName + "}"
ApiAllEventRoute = ApiEventRoute + "/" + All
ApiEventIdRoute = ApiEventRoute + "/" + Id + "/{" + Id + "}"
ApiEventCountRoute = ApiEventRoute + "/" + Count
ApiEventCountByDeviceNameRoute = ApiEventCountRoute + "/" + Device + "/" + Name + "/{" + Name + "}"
ApiEventByDeviceNameRoute = ApiEventRoute + "/" + Device + "/" + Name + "/{" + Name + "}"
ApiEventByTimeRangeRoute = ApiEventRoute + "/" + Start + "/{" + Start + "}/" + End + "/{" + End + "}"
ApiEventByAgeRoute = ApiEventRoute + "/" + Age + "/{" + Age + "}"

ApiReadingRoute = ApiBase + "/reading"
ApiAllReadingRoute = ApiReadingRoute + "/" + All
Expand Down Expand Up @@ -118,6 +118,7 @@ const (
Service = "service"
Command = "command"
ProfileName = "profileName"
SourceName = "sourceName"
ServiceName = "serviceName"
ResourceName = "resourceName"
Start = "start"
Expand Down
4 changes: 3 additions & 1 deletion v2/dtos/const_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -18,6 +18,8 @@ const (
TestDescription = "TestDescription"
TestModel = "TestModel"

TestSourceName = "TestSourceName"

TestDeviceResourceName = "TestDeviceResourceName"
TestTag = "TestTag"

Expand Down
5 changes: 4 additions & 1 deletion v2/dtos/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ type Event struct {
Id string `json:"id" validate:"required,uuid"`
DeviceName string `json:"deviceName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
ProfileName string `json:"profileName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
SourceName string `json:"sourceName" validate:"required,edgex-dto-rfc3986-unreserved-chars"`
Created int64 `json:"created,omitempty"`
Origin int64 `json:"origin" validate:"required"`
Readings []BaseReading `json:"readings" validate:"gt=0,dive,required"`
Tags map[string]string `json:"tags,omitempty" xml:"-"` // Have to ignore since map not supported for XML
}

// NewEvent creates and returns an initialized Event with no Readings
func NewEvent(profileName string, deviceName string) Event {
func NewEvent(profileName, deviceName, sourceName string) Event {
return Event{
Versionable: common.NewVersionable(),
Id: uuid.NewString(),
DeviceName: deviceName,
ProfileName: profileName,
SourceName: sourceName,
Origin: time.Now().UnixNano(),
}
}
Expand All @@ -58,6 +60,7 @@ func FromEventModelToDTO(event models.Event) Event {
Id: event.Id,
DeviceName: event.DeviceName,
ProfileName: event.ProfileName,
SourceName: event.SourceName,
Created: event.Created,
Origin: event.Origin,
Readings: readings,
Expand Down
18 changes: 12 additions & 6 deletions v2/dtos/event_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -11,7 +11,7 @@ import (

"github.com/stretchr/testify/require"

v2 "github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

Expand All @@ -22,6 +22,7 @@ var valid = models.Event{
Id: TestUUID,
DeviceName: TestDeviceName,
ProfileName: TestDeviceProfileName,
SourceName: TestSourceName,
Created: TestTimestamp,
Origin: TestTimestamp,
Tags: map[string]string{
Expand All @@ -36,6 +37,7 @@ var expectedDTO = Event{
Id: TestUUID,
DeviceName: TestDeviceName,
ProfileName: TestDeviceProfileName,
SourceName: TestSourceName,
Created: TestTimestamp,
Origin: TestTimestamp,
Tags: map[string]string{
Expand Down Expand Up @@ -63,7 +65,7 @@ func TestFromEventModelToDTO(t *testing.T) {
func TestEvent_ToXML(t *testing.T) {
// Since the order in map is random we have to verify the individual items exists without depending on order
contains := []string{
"<Event><ApiVersion>v2</ApiVersion><Id>7a1707f0-166f-4c4b-bc9d-1d54c74e0137</Id><DeviceName>TestDevice</DeviceName><ProfileName>TestDeviceProfileName</ProfileName><Created>1594963842</Created><Origin>1594963842</Origin><Tags>",
"<Event><ApiVersion>v2</ApiVersion><Id>7a1707f0-166f-4c4b-bc9d-1d54c74e0137</Id><DeviceName>TestDevice</DeviceName><ProfileName>TestDeviceProfileName</ProfileName><SourceName>TestSourceName</SourceName><Created>1594963842</Created><Origin>1594963842</Origin><Tags>",
"<GatewayID>Houston-0001</GatewayID>",
"<Latitude>29.630771</Latitude>",
"<Longitude>-95.377603</Longitude>",
Expand All @@ -79,11 +81,13 @@ func TestNewEvent(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedSourceName := TestSourceName

actual := NewEvent(expectedProfileName, expectedDeviceName)
actual := NewEvent(expectedProfileName, expectedDeviceName, expectedSourceName)

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedSourceName, actual.SourceName)
assert.Equal(t, expectedProfileName, actual.ProfileName)
assert.Equal(t, expectedDeviceName, actual.DeviceName)
assert.Zero(t, len(actual.Readings))
Expand All @@ -95,6 +99,7 @@ func TestEvent_AddSimpleReading(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedSourceName := TestSourceName
expectedReadingDetails := []struct {
inputValue interface{}
resourceName string
Expand All @@ -107,7 +112,7 @@ func TestEvent_AddSimpleReading(t *testing.T) {
}
expectedReadingsCount := len(expectedReadingDetails)

target := NewEvent(expectedProfileName, expectedDeviceName)
target := NewEvent(expectedProfileName, expectedDeviceName, expectedSourceName)
for _, expected := range expectedReadingDetails {
err := target.AddSimpleReading(expected.resourceName, expected.valueType, expected.inputValue)
require.NoError(t, err)
Expand All @@ -132,13 +137,14 @@ func TestEvent_AddBinaryReading(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedDeviceName := TestDeviceName
expectedProfileName := TestDeviceProfileName
expectedSourceName := TestSourceName
expectedResourceName := TestDeviceResourceName
expectedValueType := v2.ValueTypeBinary
expectedValue := []byte("Hello World")
expectedMediaType := "application/text"
expectedReadingsCount := 1

target := NewEvent(expectedProfileName, expectedDeviceName)
target := NewEvent(expectedProfileName, expectedDeviceName, expectedSourceName)
target.AddBinaryReading(expectedResourceName, expectedValue, expectedMediaType)

require.Equal(t, expectedReadingsCount, len(target.Readings))
Expand Down
4 changes: 3 additions & 1 deletion v2/dtos/requests/const_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -18,6 +18,8 @@ const (
TestDescription = "TestDescription"
TestModel = "TestModel"

TestSourceName = "TestSourceName"

TestDeviceResourceName = "TestDeviceResourceName"
TestTag = "TestTag"

Expand Down
1 change: 1 addition & 0 deletions v2/dtos/requests/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func AddEventReqToEventModel(addEventReq AddEventRequest) (event models.Event) {
Id: addEventReq.Event.Id,
DeviceName: addEventReq.Event.DeviceName,
ProfileName: addEventReq.Event.ProfileName,
SourceName: addEventReq.Event.SourceName,
Origin: addEventReq.Event.Origin,
Readings: readings,
Tags: tags,
Expand Down
14 changes: 13 additions & 1 deletion v2/dtos/requests/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func eventData() dtos.Event {
event := dtos.NewEvent(TestDeviceProfileName, TestDeviceName)
event := dtos.NewEvent(TestDeviceProfileName, TestDeviceName, TestSourceName)
event.Id = ExampleUUID
event.Origin = TestOriginTime
event.Tags = map[string]string{
Expand Down Expand Up @@ -53,6 +53,8 @@ func TestAddEventRequest_Validate(t *testing.T) {
noDeviceName.Event.DeviceName = ""
noProfileName := eventRequestData()
noProfileName.Event.ProfileName = ""
noSourceName := eventRequestData()
noSourceName.Event.SourceName = ""
noOrigin := eventRequestData()
noOrigin.Event.Origin = 0

Expand Down Expand Up @@ -98,6 +100,7 @@ func TestAddEventRequest_Validate(t *testing.T) {
{"invalid AddEventRequest, Event Id is not an uuid", invalidEventId, true},
{"invalid AddEventRequest, no DeviceName", noDeviceName, true},
{"invalid AddEventRequest, no ProfileName", noProfileName, true},
{"invalid AddEventRequest, no SourceName", noSourceName, true},
{"invalid AddEventRequest, no Origin", noOrigin, true},
{"invalid AddEventRequest, no Reading", noReading, true},
{"invalid AddEventRequest, no Reading DeviceName", invalidReadingNoDevice, true},
Expand Down Expand Up @@ -131,6 +134,8 @@ func TestAddEventRequest_Validate(t *testing.T) {
deviceNameWithUnreservedChar.Event.DeviceName = nameWithUnreservedChars
profileNameWithUnreservedChar := eventRequestData()
profileNameWithUnreservedChar.Event.ProfileName = nameWithUnreservedChars
sourceNameWithUnreservedChar := eventRequestData()
sourceNameWithUnreservedChar.Event.SourceName = nameWithUnreservedChars
readingDeviceNameWithUnreservedChar := eventRequestData()
readingDeviceNameWithUnreservedChar.Event.Readings[0].DeviceName = nameWithUnreservedChars
readingResourceNameWithUnreservedChar := eventRequestData()
Expand All @@ -142,6 +147,7 @@ func TestAddEventRequest_Validate(t *testing.T) {
testsForNameFields := []testForNameField{
{"Valid AddEventRequest with device name containing unreserved chars", deviceNameWithUnreservedChar, false},
{"Valid AddEventRequest with profile name containing unreserved chars", profileNameWithUnreservedChar, false},
{"Valid AddEventRequest with source name containing unreserved chars", sourceNameWithUnreservedChar, false},
{"Valid AddEventRequest with reading device name containing unreserved chars", readingDeviceNameWithUnreservedChar, false},
{"Valid AddEventRequest with reading resource name containing unreserved chars", readingResourceNameWithUnreservedChar, false},
{"Valid AddEventRequest with reading profile name containing unreserved chars", readingProfileNameWithUnreservedChar, false},
Expand All @@ -153,6 +159,8 @@ func TestAddEventRequest_Validate(t *testing.T) {
deviceNameWithReservedChar.Event.DeviceName = n
profileNameWithReservedChar := eventRequestData()
profileNameWithReservedChar.Event.ProfileName = n
sourceNameWithReservedChar := eventRequestData()
sourceNameWithReservedChar.Event.SourceName = n
readingDeviceNameWithReservedChar := eventRequestData()
readingDeviceNameWithReservedChar.Event.Readings[0].DeviceName = n
readingResourceNameWithReservedChar := eventRequestData()
Expand All @@ -163,6 +171,7 @@ func TestAddEventRequest_Validate(t *testing.T) {
testsForNameFields = append(testsForNameFields,
testForNameField{"Invalid AddEventRequest with device name containing reserved char", deviceNameWithReservedChar, true},
testForNameField{"Invalid AddEventRequest with profile name containing reserved char", profileNameWithReservedChar, true},
testForNameField{"Invalid AddEventRequest with source name containing reserved char", sourceNameWithReservedChar, true},
testForNameField{"Invalid AddEventRequest with reading device name containing reserved char", readingDeviceNameWithReservedChar, true},
testForNameField{"Invalid AddEventRequest with reading resource name containing reserved char", readingResourceNameWithReservedChar, true},
testForNameField{"Invalid AddEventRequest with reading profile name containing reserved char", readingProfileNameWithReservedChar, true},
Expand Down Expand Up @@ -241,6 +250,7 @@ func Test_AddEventReqToEventModels(t *testing.T) {
Id: ExampleUUID,
DeviceName: TestDeviceName,
ProfileName: TestDeviceProfileName,
SourceName: TestSourceName,
Origin: TestOriginTime,
Readings: []models.Reading{s},
Tags: map[string]string{
Expand All @@ -265,6 +275,7 @@ func Test_AddEventReqToEventModels(t *testing.T) {
func TestNewAddEventRequest(t *testing.T) {
expectedProfileName := TestDeviceProfileName
expectedDeviceName := TestDeviceName
expectedSourceName := TestSourceName
expectedApiVersion := v2.ApiVersion

actual := NewAddEventRequest(eventData())
Expand All @@ -275,6 +286,7 @@ func TestNewAddEventRequest(t *testing.T) {
assert.NotEmpty(t, actual.Event.Id)
assert.Equal(t, expectedProfileName, actual.Event.ProfileName)
assert.Equal(t, expectedDeviceName, actual.Event.DeviceName)
assert.Equal(t, expectedSourceName, actual.Event.SourceName)
assert.NotZero(t, len(actual.Event.Readings))
assert.Zero(t, actual.Event.Created)
assert.NotZero(t, actual.Event.Origin)
Expand Down
3 changes: 2 additions & 1 deletion v2/models/event.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -12,6 +12,7 @@ type Event struct {
Id string
DeviceName string
ProfileName string
SourceName string
Created int64
Origin int64
Readings []Reading
Expand Down

0 comments on commit 778f72f

Please sign in to comment.