-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
resource.go
478 lines (425 loc) · 15.3 KB
/
resource.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package pb
import (
"sort"
"testing"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/schema/interfaces"
"github.com/plgd-dev/device/v2/test/resource/types"
"github.com/plgd-dev/go-coap/v3/message"
"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"
"github.com/plgd-dev/hub/v2/test/oauth-server/service"
"github.com/plgd-dev/hub/v2/test/pb/baseline"
"github.com/stretchr/testify/require"
)
type sortResourcesIdsByHref []*commands.ResourceId
func (a sortResourcesIdsByHref) Len() int { return len(a) }
func (a sortResourcesIdsByHref) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortResourcesIdsByHref) Less(i, j int) bool {
return a[i].GetHref() < a[j].GetHref()
}
func sortResourceIds(s []*commands.ResourceId) []*commands.ResourceId {
v := sortResourcesIdsByHref(s)
sort.Sort(v)
return v
}
func CmpResourceIds(t *testing.T, expected, got []*commands.ResourceId) {
require.Len(t, got, len(expected))
expectedSorted := sortResourceIds(expected)
gotSorted := sortResourceIds(got)
test.CheckProtobufs(t, expectedSorted, gotSorted, test.RequireToCheckFunc(require.Equal))
}
func MakeCreateSwitchResourceResponseData(id string) map[string]interface{} {
return map[string]interface{}{
"href": test.TestResourceSwitchesInstanceHref(id),
"if": []interface{}{interfaces.OC_IF_A, interfaces.OC_IF_BASELINE},
"rt": []interface{}{types.BINARY_SWITCH},
"rep": map[string]interface{}{
"rt": []interface{}{types.BINARY_SWITCH},
"if": []interface{}{interfaces.OC_IF_A, interfaces.OC_IF_BASELINE},
"value": false,
},
"p": map[string]interface{}{
"bm": uint64(schema.Discoverable | schema.Observable),
},
}
}
func MakeResourceCreated(t *testing.T, deviceID, href string, resourceTypes []string, correlationID string, data map[string]interface{}) *events.ResourceCreated {
return &events.ResourceCreated{
ResourceId: commands.NewResourceID(deviceID, href),
Status: commands.Status_CREATED,
Content: &commands.Content{
CoapContentFormat: int32(message.AppOcfCbor),
ContentType: message.AppOcfCbor.String(),
Data: func() []byte {
if data == nil {
return nil
}
return test.EncodeToCbor(t, data)
}(),
},
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
ResourceTypes: resourceTypes,
}
}
func CleanUpResourceCreated(e *events.ResourceCreated, resetCorrelationID bool) *events.ResourceCreated {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
return e
}
func CmpResourceCreated(t *testing.T, expected, got *events.ResourceCreated) {
require.NotEmpty(t, expected)
require.NotEmpty(t, got)
if expected.GetContent().GetData() != nil && got.GetContent().GetData() != nil {
cleanData := func(data map[interface{}]interface{}) {
delete(data, "ins") // instance_id is a random value
}
gotData, ok := test.DecodeCbor(t, got.GetContent().GetData()).(map[interface{}]interface{})
require.True(t, ok)
expectedData, ok := test.DecodeCbor(t, expected.GetContent().GetData()).(map[interface{}]interface{})
require.True(t, ok)
cleanData(expectedData)
cleanData(gotData)
require.Equal(t, expectedData, gotData)
got.GetContent().Data = nil
expected.GetContent().Data = nil
}
resetCorrelationID := expected.GetAuditContext().GetCorrelationId() == ""
CleanUpResourceCreated(expected, resetCorrelationID)
CleanUpResourceCreated(got, resetCorrelationID)
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func CleanUpResourceChanged(e *events.ResourceChanged, resetCorrelationID bool) *events.ResourceChanged {
if e == nil {
return nil
}
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
e.Etag = nil
return e
}
func CmpResourceChangedData(t *testing.T, expected, got []byte) {
cleanUp := func(v interface{}) {
data := v.(map[interface{}]interface{})
delete(data, "ins")
delete(data, "eps")
delete(data, "pi")
delete(data, "piid")
}
if gotData, ok := test.DecodeCbor(t, got).([]interface{}); ok {
expectedData := test.DecodeCbor(t, expected).([]interface{})
for _, v := range gotData {
cleanUp(v)
}
require.Equal(t, expectedData, gotData)
return
}
gotData := test.DecodeCbor(t, got)
cleanUp(gotData)
expectedData := test.DecodeCbor(t, expected)
cleanUp(expectedData)
require.Equal(t, expectedData, gotData)
}
func CmpResourceChanged(t *testing.T, expected, got *events.ResourceChanged, cmpInterface string) {
require.NotEmpty(t, expected)
require.NotEmpty(t, got)
if expected.GetContent().GetData() != nil && got.GetContent().GetData() != nil {
cmpFn := CmpResourceChangedData
if cmpInterface == interfaces.OC_IF_BASELINE {
cmpFn = baseline.CmpResourceChangedData
}
cmpFn(t, expected.GetContent().GetData(), got.GetContent().GetData())
got.GetContent().Data = nil
expected.GetContent().Data = nil
}
resetCorrelationID := expected.GetAuditContext().GetCorrelationId() == ""
CleanUpResourceChanged(expected, resetCorrelationID)
CleanUpResourceChanged(got, resetCorrelationID)
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func makeCborContent(t *testing.T, data interface{}) *commands.Content {
return &commands.Content{
CoapContentFormat: int32(message.AppOcfCbor),
ContentType: message.AppOcfCbor.String(),
Data: func() []byte {
if data == nil {
return nil
}
return test.EncodeToCbor(t, data)
}(),
}
}
func MakeResourceChanged(t *testing.T, deviceID, href string, resourceTypes []string, correlationID string, data interface{}) *events.ResourceChanged {
return &events.ResourceChanged{
ResourceId: &commands.ResourceId{
DeviceId: deviceID,
Href: href,
},
Status: commands.Status_OK,
Content: makeCborContent(t, data),
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
ResourceTypes: resourceTypes,
}
}
func MakeResourceDeleted(deviceID, href string, resourceTypes []string, correlationID string) *events.ResourceDeleted {
return &events.ResourceDeleted{
ResourceId: commands.NewResourceID(deviceID, href),
Status: commands.Status_OK,
Content: &commands.Content{
CoapContentFormat: int32(-1),
},
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
ResourceTypes: resourceTypes,
}
}
func CleanResourceDeleted(e *events.ResourceDeleted, resetCorrelationID bool) *events.ResourceDeleted {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
return e
}
type resourceData map[interface{}]interface{}
func getResourceData(t *testing.T, content *commands.Content) map[string]resourceData {
resData := make(map[string]resourceData)
addData := func(v interface{}) {
res, ok := v.(map[interface{}]interface{})
require.True(t, ok)
href, _ := res["href"].(string)
delete(res, "eps")
delete(res, "ins")
delete(res, "pi")
delete(res, "piid")
resData[href] = res
}
data := test.DecodeCbor(t, content.GetData())
resources, ok := data.([]interface{})
if ok {
for _, resource := range resources {
addData(resource)
}
return resData
}
addData(data)
return resData
}
func checkContent(t *testing.T, expected, got *commands.Content) {
if expected.GetData() != nil && got.GetData() != nil {
gotData := getResourceData(t, got)
require.NotEmpty(t, gotData)
expectedData := getResourceData(t, expected)
require.NotEmpty(t, expectedData)
require.Equal(t, expectedData, gotData)
got.Data = nil
expected.Data = nil
}
}
func CmpResourceDeleted(t *testing.T, expected, got *events.ResourceDeleted) {
require.NotEmpty(t, expected)
require.NotEmpty(t, got)
checkContent(t, expected.GetContent(), got.GetContent())
resetCorrelationID := expected.GetAuditContext().GetCorrelationId() == ""
CleanResourceDeleted(expected, resetCorrelationID)
CleanResourceDeleted(got, resetCorrelationID)
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func MakeResourceRetrieved(t *testing.T, deviceID, href string, resourceTypes []string, correlationID string, data interface{}) *events.ResourceRetrieved {
return &events.ResourceRetrieved{
ResourceId: &commands.ResourceId{
DeviceId: deviceID,
Href: href,
},
Status: commands.Status_OK,
Content: makeCborContent(t, data),
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
ResourceTypes: resourceTypes,
}
}
func CleanUpResourceRetrieved(e *events.ResourceRetrieved, resetCorrelationID bool) *events.ResourceRetrieved {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
e.Etag = nil
return e
}
func CmpResourceRetrieved(t *testing.T, expected, got *events.ResourceRetrieved) {
require.NotEmpty(t, expected)
require.NotEmpty(t, got)
checkContent(t, expected.GetContent(), got.GetContent())
resetCorrelationID := expected.GetAuditContext().GetCorrelationId() == ""
CleanUpResourceRetrieved(expected, resetCorrelationID)
CleanUpResourceRetrieved(got, resetCorrelationID)
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func MakeResourceUpdated(t *testing.T, deviceID, href string, resourceTypes []string, correlationID string, data interface{}) *events.ResourceUpdated {
return &events.ResourceUpdated{
ResourceId: &commands.ResourceId{
DeviceId: deviceID,
Href: href,
},
Status: commands.Status_OK,
Content: func() *commands.Content {
if data == nil {
return &commands.Content{
CoapContentFormat: -1,
}
}
return makeCborContent(t, data)
}(),
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
ResourceTypes: resourceTypes,
}
}
func CleanUpResourceUpdated(e *events.ResourceUpdated, resetCorrelationID bool) *events.ResourceUpdated {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
return e
}
func CmpResourceUpdated(t *testing.T, expected, got *events.ResourceUpdated) {
require.NotEmpty(t, expected)
require.NotEmpty(t, got)
if expected.GetContent().GetData() != nil && got.GetContent().GetData() != nil {
gotData := test.DecodeCbor(t, got.GetContent().GetData())
got.GetContent().Data = nil
expectedData := test.DecodeCbor(t, expected.GetContent().GetData())
expected.GetContent().Data = nil
require.Equal(t, expectedData, gotData)
}
resetCorrelationID := expected.GetAuditContext().GetCorrelationId() == ""
CleanUpResourceUpdated(expected, resetCorrelationID)
CleanUpResourceUpdated(got, resetCorrelationID)
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
type sortResourcesByHref []*pb.Resource
func (a sortResourcesByHref) Len() int { return len(a) }
func (a sortResourcesByHref) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortResourcesByHref) Less(i, j int) bool {
return a[i].GetData().GetResourceId().GetHref() < a[j].GetData().GetResourceId().GetHref()
}
func sortResources(s []*pb.Resource) []*pb.Resource {
v := sortResourcesByHref(s)
sort.Sort(v)
return v
}
func CmpResourceValues(t *testing.T, expected, got []*pb.Resource) {
cleanUpData := func(d map[interface{}]interface{}) {
delete(d, "eps")
delete(d, "ins")
delete(d, "pi")
delete(d, "piid")
}
getData := func(t *testing.T, res *pb.Resource) interface{} {
if res.GetData().GetContent().GetData() == nil &&
res.GetData().GetContent().GetContentType() == "" &&
res.GetData().GetContent().GetCoapContentFormat() == -1 {
return nil
}
d := test.DecodeCbor(t, res.GetData().GetContent().GetData())
if m, ok := d.(map[interface{}]interface{}); ok {
cleanUpData(m)
return d
}
if a, ok := d.([]interface{}); ok {
for _, m := range a {
cleanUpData(m.(map[interface{}]interface{}))
}
return d
}
return d
}
require.Len(t, got, len(expected))
expectedSorted := sortResources(expected)
gotSorted := sortResources(got)
for k := range expectedSorted {
e := expectedSorted[k]
g := gotSorted[k]
dataExpected := getData(t, e)
e.Data.Content.Data = nil
dataGot := getData(t, g)
g.Data.Content.Data = nil
require.Equal(t, dataExpected, dataGot)
resetCorrelationID := e.GetData().GetAuditContext().GetCorrelationId() == ""
CleanUpResourceChanged(e.GetData(), resetCorrelationID)
CleanUpResourceChanged(g.GetData(), resetCorrelationID)
test.CheckProtobufs(t, e, g, test.RequireToCheckFunc(require.Equal))
}
}
// compare only deviceId, href and resourceTypes
func CmpResourceValuesBasic(t *testing.T, expected, got []*pb.Resource) {
require.Len(t, got, len(expected))
expectedData := make(map[string][]string)
gotData := make(map[string][]string)
for idx := range expected {
expectedData[expected[idx].GetData().GetResourceId().ToString()] = expected[idx].GetTypes()
gotData[got[idx].GetData().GetResourceId().ToString()] = got[idx].GetTypes()
}
require.Equal(t, expectedData, gotData)
}
func CmpHubConfigurationResponse(t *testing.T, expected, got *pb.HubConfigurationResponse) {
require.NotEmpty(t, got.GetCertificateAuthorities())
got.CertificateAuthorities = ""
require.NotEqual(t, int64(0), got.GetCurrentTime())
got.CurrentTime = 0
test.CheckProtobufs(t, expected, got, test.RequireToCheckFunc(require.Equal))
}
func CleanUpResourceLinksSnapshotTaken(e *events.ResourceLinksSnapshotTaken) *events.ResourceLinksSnapshotTaken {
e.EventMetadata = nil
for _, r := range e.GetResources() {
r.ValidUntil = 0
}
return e
}
func CleanUpResourceLinksPublished(e *events.ResourceLinksPublished, resetCorrelationID bool) *events.ResourceLinksPublished {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
test.CleanUpResourcesArray(e.GetResources())
return e
}
func FindResourceInResourceLinksPublishedByHref(e *events.ResourceLinksPublished, href string) *commands.Resource {
for _, r := range e.GetResources() {
if r.GetHref() == href {
return r
}
}
return nil
}
func MakeResourceLinksPublished(deviceID string, resources []*commands.Resource, correlationID string) *events.ResourceLinksPublished {
return &events.ResourceLinksPublished{
DeviceId: deviceID,
Resources: resources,
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
}
}
func CleanUpResourceLinksUnpublished(e *events.ResourceLinksUnpublished, resetCorrelationID bool) *events.ResourceLinksUnpublished {
if e.GetAuditContext() != nil && resetCorrelationID {
e.GetAuditContext().CorrelationId = ""
}
e.EventMetadata = nil
e.OpenTelemetryCarrier = nil
sort.Strings(e.GetHrefs())
return e
}
func MakeResourceLinksUnpublished(deviceID string, resources []string, correlationID string) *events.ResourceLinksUnpublished {
return &events.ResourceLinksUnpublished{
DeviceId: deviceID,
Hrefs: resources,
AuditContext: commands.NewAuditContext(service.DeviceUserID, correlationID, service.DeviceUserID),
}
}