-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
device.go
86 lines (77 loc) · 3.04 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package pb
import (
"testing"
pbGrpc "github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
"github.com/plgd-dev/hub/v2/test"
oauthService "github.com/plgd-dev/hub/v2/test/oauth-server/service"
"github.com/stretchr/testify/require"
)
func CmpDeviceValues(t *testing.T, expected, got []*pbGrpc.Device) {
require.Len(t, got, len(expected))
cleanUp := func(dev *pbGrpc.Device) {
dev.ProtocolIndependentId = ""
dev.Metadata.Connection.OnlineValidUntil = 0
dev.Metadata.Connection.Id = ""
dev.Metadata.Connection.ConnectedAt = 0
if dev.Metadata.TwinSynchronization != nil {
dev.Metadata.TwinSynchronization.SyncingAt = 0
dev.Metadata.TwinSynchronization.InSyncAt = 0
dev.Metadata.TwinSynchronization.CommandMetadata = nil
}
dev.Data = nil
}
for idx := range expected {
cleanUp(expected[idx])
cleanUp(got[idx])
}
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func MakeDeviceMetadataUpdated(deviceID string, connectionStatus commands.Connection_Status, connectionProtocol commands.Connection_Protocol, twinEnabled bool, twinSynchronizationState commands.TwinSynchronization_State, correlationID string) *events.DeviceMetadataUpdated {
return &events.DeviceMetadataUpdated{
DeviceId: deviceID,
Connection: &commands.Connection{
Status: connectionStatus,
Protocol: connectionProtocol,
},
TwinSynchronization: &commands.TwinSynchronization{
State: twinSynchronizationState,
},
TwinEnabled: twinEnabled,
AuditContext: commands.NewAuditContext(oauthService.DeviceUserID, correlationID),
}
}
func CleanUpDeviceMetadataUpdated(e *events.DeviceMetadataUpdated, resetCorrelationID bool) *events.DeviceMetadataUpdated {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
if e.GetConnection() != nil {
e.GetConnection().OnlineValidUntil = 0
e.GetConnection().Id = ""
e.GetConnection().ConnectedAt = 0
}
if e.GetTwinSynchronization() != nil {
e.GetTwinSynchronization().CommandMetadata = nil
e.GetTwinSynchronization().SyncingAt = 0
e.GetTwinSynchronization().InSyncAt = 0
}
return e
}
func CmpDeviceMetadataUpdated(t *testing.T, expected, got *events.DeviceMetadataUpdated) {
resetCorrelationID := expected.GetAuditContext().GetCorrelationId() == ""
CleanUpDeviceMetadataUpdated(expected, resetCorrelationID)
CleanUpDeviceMetadataUpdated(got, resetCorrelationID)
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func CmpDeviceMetadataUpdatedSlice(t *testing.T, expected, got []*events.DeviceMetadataUpdated) {
require.Len(t, got, len(expected))
for idx := range expected {
resetCorrelationID := expected[idx].GetAuditContext().GetCorrelationId() == ""
CleanUpDeviceMetadataUpdated(expected[idx], resetCorrelationID)
CleanUpDeviceMetadataUpdated(got[idx], resetCorrelationID)
}
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}