-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver_test.go
More file actions
337 lines (294 loc) · 9.28 KB
/
observer_test.go
File metadata and controls
337 lines (294 loc) · 9.28 KB
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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrpslog
import (
"context"
"encoding/base64"
"log/slog"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/wrp-go/v5"
)
// Test handlers are defined in handlers_test.go
func TestObserver_NilLogger(t *testing.T) {
ob := Observer{
Logger: nil,
Level: slog.LevelInfo,
Message: "test message",
Fields: []FieldOpt{Source(), Destination()},
}
// Should not panic
ob.ObserveWRP(context.Background(), wrp.Message{})
}
func TestObserver_DisabledLevel(t *testing.T) {
handler := newRecordHandler(slog.LevelWarn)
logger := slog.New(handler)
ob := Observer{
Logger: logger,
Level: slog.LevelInfo, // Below handler's level
Message: "test message",
Fields: []FieldOpt{Source()},
}
ob.ObserveWRP(context.Background(), wrp.Message{Source: "test"})
require.Empty(t, handler.records)
}
func TestObserver_FieldOptions(t *testing.T) {
status := int64(200)
rdr := int64(1)
payload := []byte("test payload")
fullMsg := wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Source: "dns:test.example.com/service",
Destination: "mac:112233445566/config",
TransactionUUID: "uuid-1234",
ContentType: "application/json",
Accept: "application/msgpack",
Status: &status,
RequestDeliveryResponse: &rdr,
Headers: []string{"X-Header: value"},
Metadata: map[string]string{"key": "value"},
Path: "/api/v1/test",
Payload: payload,
ServiceName: "test-service",
URL: "http://example.com",
PartnerIDs: []string{"partner1"},
SessionID: "session-abc",
QualityOfService: wrp.QOSMediumValue,
}
defaultFields := []FieldOpt{
MessageType(),
Source(),
Destination(),
TransactionUUID(),
ContentType(),
Accept(),
Status(),
RequestDeliveryResponse(),
Headers(),
Metadata(),
Path(),
PayloadAsBase64(),
PayloadSize(),
ServiceName(),
URL(),
PartnerIDs(),
SessionID(),
QualityOfService(),
}
alwaysFields := []FieldOpt{
MessageType(),
SourceAlways(),
DestinationAlways(),
TransactionUUIDAlways(),
ContentTypeAlways(),
AcceptAlways(),
StatusAlways(),
RequestDeliveryResponseAlways(),
HeadersAlways(),
MetadataAlways(),
PathAlways(),
PayloadAsBase64Always(),
PayloadSizeAlways(),
ServiceNameAlways(),
URLAlways(),
PartnerIDsAlways(),
SessionIDAlways(),
QualityOfServiceAlways(),
}
fullExpected := map[string]any{
fMsgType: int64(wrp.SimpleRequestResponseMessageType),
fSource: "dns:test.example.com/service",
fDestination: "mac:112233445566/config",
fTransactionUUID: "uuid-1234",
fContentType: "application/json",
fAccept: "application/msgpack",
fStatus: int64(200),
fRequestDeliveryResponse: int64(1),
fHeaders: []string{"X-Header: value"},
fMetadata: map[string]string{"key": "value"},
fPath: "/api/v1/test",
fPayload: base64.StdEncoding.EncodeToString(payload),
fPayloadSize: int64(len(payload)),
fServiceName: "test-service",
fURL: "http://example.com",
fPartnerIDs: []string{"partner1"},
fSessionID: "session-abc",
fQualityOfService: int64(wrp.QOSMediumValue),
}
emptyExpected := map[string]any{
fMsgType: int64(0),
fSource: "",
fDestination: "",
fTransactionUUID: "",
fContentType: "",
fAccept: "",
fStatus: int64(0),
fRequestDeliveryResponse: int64(0),
fHeaders: []string(nil),
fMetadata: map[string]string(nil),
fPath: "",
fPayload: "",
fPayloadSize: int64(0),
fServiceName: "",
fURL: "",
fPartnerIDs: []string(nil),
fSessionID: "",
fQualityOfService: int64(0),
}
tests := []struct {
name string
fields []FieldOpt
msg wrp.Message
expected map[string]any
}{
{
name: "all_present_default",
fields: defaultFields,
msg: fullMsg,
expected: fullExpected,
},
{
name: "all_present_always",
fields: alwaysFields,
msg: fullMsg,
expected: fullExpected,
},
{
name: "all_empty_default",
fields: defaultFields,
msg: wrp.Message{},
expected: map[string]any{fMsgType: int64(0), fQualityOfService: int64(0)}, // Only MessageType and QualityOfService logged
},
{
name: "all_empty_always",
fields: alwaysFields,
msg: wrp.Message{},
expected: emptyExpected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := newRecordHandler(slog.LevelInfo)
ob := Observer{
Logger: slog.New(handler),
Level: slog.LevelInfo,
Message: "wrp message",
Fields: tt.fields,
}
ob.ObserveWRP(context.Background(), tt.msg)
require.Len(t, handler.records, 1)
attrs := handler.getAttrs(0)
require.Len(t, attrs, len(tt.expected))
for _, attr := range attrs {
assert.Equal(t, tt.expected[attr.Key], attr.Value.Any(), "field %s", attr.Key)
}
})
}
}
func TestObserver_MessageTypeAsString(t *testing.T) {
handler := newRecordHandler(slog.LevelInfo)
logger := slog.New(handler)
ob := Observer{
Logger: logger,
Level: slog.LevelInfo,
Message: "wrp message",
Fields: []FieldOpt{MessageTypeAsString()},
}
ob.ObserveWRP(context.Background(), wrp.Message{Type: wrp.SimpleRequestResponseMessageType})
require.Len(t, handler.records, 1)
attrs := handler.getAttrs(0)
require.Len(t, attrs, 1)
assert.Equal(t, fMsgType, attrs[0].Key)
assert.Equal(t, wrp.SimpleRequestResponseMessageType.String(), attrs[0].Value.Any())
}
func TestObserver_DuplicateFieldsLastWins(t *testing.T) {
handler := newRecordHandler(slog.LevelInfo)
logger := slog.New(handler)
ob := Observer{
Logger: logger,
Level: slog.LevelInfo,
Message: "wrp message",
Fields: []FieldOpt{
MessageType(), // First: numeric
MessageTypeAsString(), // Second: string (should win)
},
}
ob.ObserveWRP(context.Background(), wrp.Message{Type: wrp.SimpleRequestResponseMessageType})
require.Len(t, handler.records, 1)
attrs := handler.getAttrs(0)
require.Len(t, attrs, 1)
assert.Equal(t, fMsgType, attrs[0].Key)
// String format should win since it was specified last
assert.Equal(t, wrp.SimpleRequestResponseMessageType.String(), attrs[0].Value.Any())
}
var fieldMap = map[string]string{
"Type": fMsgType,
"Source": fSource,
"Destination": fDestination,
"TransactionUUID": fTransactionUUID,
"ContentType": fContentType,
"Accept": fAccept,
"Status": fStatus,
"RequestDeliveryResponse": fRequestDeliveryResponse,
"Headers": fHeaders,
"Metadata": fMetadata,
"Path": fPath,
"Payload": fPayload,
"ServiceName": fServiceName,
"URL": fURL,
"PartnerIDs": fPartnerIDs,
"SessionID": fSessionID,
"QualityOfService": fQualityOfService,
}
func TestFieldOpt_JSONTags(t *testing.T) {
// Spans and IncludeSpans are deprecated fields in wrp-go and intentionally
// not supported by this package.
ignored := map[string]struct{}{
"Spans": {},
"IncludeSpans": {},
}
msgType := reflect.TypeOf(wrp.Message{})
for fieldName, expectedTag := range fieldMap {
t.Run(fieldName, func(t *testing.T) {
field, found := msgType.FieldByName(fieldName)
require.True(t, found, "Field '%s' not found in wrp.Message", fieldName)
jsonTag := field.Tag.Get("json")
list := strings.SplitN(jsonTag, ",", 2)
require.NotEmpty(t, list[0], "Field '%s' does not have a JSON tag", fieldName)
assert.Equal(t, list[0], expectedTag, "Constant for field '%s' does not match the JSON tag", fieldName)
})
}
// Ensure all fields in wrp.Message are represented in the fieldMap
for i := 0; i < msgType.NumField(); i++ {
field := msgType.Field(i)
if _, found := ignored[field.Name]; found {
continue
}
assert.Contains(t, fieldMap, field.Name, "Field '%s' is not represented in the fieldMap", field.Name)
}
}
func TestEnsureNoMissingFields(t *testing.T) {
// Ensure that for every field in wrp.Message, there is a corresponding FieldOpt function
msgType := reflect.TypeOf(wrp.Message{})
for i := 0; i < msgType.NumField(); i++ {
field := msgType.Field(i)
if !field.IsExported() {
continue
}
tag, found := fieldMap[field.Name]
require.True(t, found, "Field '%s' is not represented in the fieldMap", field.Name)
jsonTag := field.Tag.Get("json")
list := strings.Split(jsonTag, ",")
found = false
for _, item := range list {
if strings.TrimSpace(item) == tag {
found = true
break
}
}
assert.True(t, found, "Constant for field '%s' (%s) does not match the JSON tag: %s", field.Name, tag, jsonTag)
}
}