aboutsummaryrefslogtreecommitdiff
path: root/jsonmatch/diff_test.go
blob: a59d304172a675be490e6ea2ec5e2937eb5a4126 (plain)
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
// SPDX-FileCopyrightText: 2025 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package jsonmatch_test

import (
	"encoding/json"
	"errors"
	"fmt"
	"strings"
	"testing"

	. "github.com/majewsky/gg/internal/test"
	"github.com/majewsky/gg/jsonmatch"
	. "github.com/majewsky/gg/option"
)

// assert that types implement the expected interfaces
// (CaptureField needs dynamic casts because CaptureField() returns `any`)
var (
	_ jsonmatch.Diffable = jsonmatch.Array{}
	_ jsonmatch.Diffable = jsonmatch.Object{}
	_ jsonmatch.Diffable = jsonmatch.Null()
	_ jsonmatch.Diffable = jsonmatch.Scalar("foo")
	_ jsonmatch.Diffable = jsonmatch.Scalar(42)
	_ jsonmatch.Diffable = jsonmatch.Scalar(false)
	_                    = jsonmatch.CaptureField(Some(1).AsPointer()).(json.Marshaler)
	_                    = jsonmatch.CaptureField(Some(1).AsPointer()).(json.Unmarshaler)
)

func TestCanonicalizesActualPayload(t *testing.T) {
	testCases := [][]byte{
		// all of these are functionally identical, so they should produce an empty diff
		// against our expectations regardless of key order and whitespace
		[]byte(`{"data": {"qux":[5,null,15], "foo": 42, "bar": "hello world"}}`),
		[]byte(`{"data":{"bar":"hello world","foo":42,"qux":[5,null,15]}}`),
		[]byte(`{
			"data": {
				"bar": "hello world",
				"qux": [
					5,
					null,
					15
				],
				"foo": 42
			}
		}`),
	}

	for _, message := range testCases {
		t.Logf("message = %q", message)

		// we test with several variants of `expected` using different underlying
		// types that represent identical JSON payloads, but in different ways
		match := jsonmatch.Object{
			"data": jsonmatch.Object{
				"foo": 42,
				"bar": "hello world",
				"qux": jsonmatch.Array{5, nil, 15},
			},
		}
		AssertEqual(t, match.DiffAgainst(message), nil)

		// changing the type of `data` to map[string]any does not change anything at all;
		// using the jsonmatch.Object name on this level is mostly syntactic sugar to communicate intent
		match = jsonmatch.Object{
			"data": map[string]any{
				"foo": 42,
				"bar": "hello world",
				"qux": []any{5, nil, 15},
			},
		}
		AssertEqual(t, match.DiffAgainst(message), nil)

		// this is using subtypes that our logic cannot recurse into
		// (map[opaqueString]any instead of map[string]any and []Option[int] instead of []any);
		// comparison will be less granular and only be able to fail on the level of the opaque subtype, but it will still work
		type opaqueString string
		match = jsonmatch.Object{
			"data": map[opaqueString]any{
				"foo": 42,
				"bar": "hello world",
				"qux": []Option[int]{Some(5), None[int](), Some(15)},
			},
		}
		AssertEqual(t, match.DiffAgainst(message), nil)

		// this is using a specific struct type instead of a map[string]any, which results in a different serialization
		// (map[string]any serializes with keys sorted alphabetically, but structs serialize with keys sorted by field declaration order;
		// jsonmatch knows how to normalize this and thus correctly reports an empty diff because the serializations are identical except for field order)
		match = jsonmatch.Object{
			"data": struct {
				Foo int           `json:"foo"`
				Bar string        `json:"bar"`
				Qux []Option[int] `json:"qux"`
			}{
				Foo: 42,
				Bar: "hello world",
				Qux: []Option[int]{Some(5), None[int](), Some(15)},
			},
		}
		AssertEqual(t, match.DiffAgainst(message), nil)

		// to try and trip up the normalization shown above, this match deliberately contains an unmarshalable object;
		// jsonmatch should recognize that marshaling and unmarshaling does not work and skip the normalization
		match = jsonmatch.Object{
			"data": unmarshalableObject{},
		}
		AssertEqual(t, match.DiffAgainst(message), []jsonmatch.Diff{{
			Kind:         "type mismatch",
			Pointer:      "/data",
			ExpectedJSON: `<not marshalable to JSON, %#v is jsonmatch_test.unmarshalableObject{}>`,
			ActualJSON:   `{"bar":"hello world","foo":42,"qux":[5,null,15]}`,
		}})
	}
}

func TestCapturesFields(t *testing.T) {
	const (
		uuid1 = "2cff2c65-f775-4ed5-8f86-be0998b19781"
		uuid2 = "ce38aa5c-62ed-4367-a2f8-cbe2d73094a8"
	)
	message := fmt.Appendf(nil, `{"objects":[{"id":"%s","tags":["foo"]},{"id":"%s","tags":["bar"]}]}`, uuid1, uuid2)

	// check that CaptureField() works as intended when contained within one of the supported container types
	type opaqueString string
	var (
		capturedUUID1 string
		capturedUUID2 string
		capturedTag1  opaqueString // check that capturing also works for custom types
	)
	match := jsonmatch.Object{
		"objects": []jsonmatch.Object{
			{
				"id":   jsonmatch.CaptureField(&capturedUUID1),
				"tags": []string{"foo"},
			},
			{
				"id":   jsonmatch.CaptureField(&capturedUUID2),
				"tags": []any{jsonmatch.CaptureField(&capturedTag1)},
			},
		},
	}

	AssertEqual(t, match.DiffAgainst(message), nil)
	AssertEqual(t, capturedUUID1, uuid1)
	AssertEqual(t, capturedUUID2, uuid2)
	AssertEqual(t, capturedTag1, "bar")

	// check that CaptureField() complains when unmarshaling JSON messages into incompatible types
	var (
		capturedUUID3 int
	)
	match = jsonmatch.Object{
		"objects": []jsonmatch.Object{
			{
				"id":   jsonmatch.CaptureField(&capturedUUID3),
				"tags": []string{"foo"},
			},
			{
				"id":   uuid2,
				"tags": []string{"bar"},
			},
		},
	}

	AssertEqual(t, match.DiffAgainst(message), []jsonmatch.Diff{{
		Kind:         "cannot unmarshal into capture slot (json: cannot unmarshal string into Go value of type int)",
		Pointer:      "/objects/0/id",
		ExpectedJSON: "<capture slot of type *int>",
		ActualJSON:   fmt.Sprintf("%q", uuid1),
	}})

	// check that CaptureField() does not work when contained within unsupported types
	//
	// This is a restriction that could be lifted in the future, but it would involve using advanced
	// reflection shenanigans that complicate the implementation. The fact that this example uses
	// somewhat contrived types to even be able to place a capture inside another structure shows that
	// this restriction ought not be too problematic in practice.
	capturedUUID1 = "unset"
	capturedUUID2 = "unset"
	capturedTag1 = "unset"
	match = jsonmatch.Object{
		"objects": []struct {
			ID   any   `json:"id"`
			Tags []any `json:"tags"`
		}{
			{
				ID:   jsonmatch.CaptureField(&capturedUUID1),
				Tags: []any{"foo"},
			},
			{
				ID:   jsonmatch.CaptureField(&capturedUUID2),
				Tags: []any{jsonmatch.CaptureField(&capturedTag1)},
			},
		},
	}

	AssertEqual(t, match.DiffAgainst(message), []jsonmatch.Diff{{
		Kind:         "value mismatch",
		Pointer:      "/objects",
		ActualJSON:   fmt.Sprintf(`[{"id":"%s","tags":["foo"]},{"id":"%s","tags":["bar"]}]`, uuid1, uuid2),
		ExpectedJSON: `[{"id":"unset","tags":["foo"]},{"id":"unset","tags":["unset"]}]`,
	}})
}

func TestFailsOnValueMismatch(t *testing.T) {
	message := []byte(`{"users": [
		{"id":23,"name":"Alice","tags":[{"name":"admin"},{"name":"senior"}]},
		{"id":42,"name":"Bob","tags":[{"name":"support"}]}
	]}`)
	match := jsonmatch.Object{
		"users": []map[string]any{ // also side-note, because we did not have it anywhere else, this covers recursion into []map[string]any
			{
				"id":     23,
				"name":   "Alicia",                                      // should be "Alice"
				"status": "fixing stuff",                                // unexpected field
				"tags":   []jsonmatch.Object{{"name": "administrator"}}, // name should be "admin"; second list entry missing
			},
			{
				// "id" field is missing
				"name": "Bob",
				"tags": []jsonmatch.Object{{"name": "support"}, {"name": "postmaster"}}, // unexpected list entry
			},
		},
	}

	AssertEqual(t, match.DiffAgainst(message), []jsonmatch.Diff{
		{
			Kind:         "value mismatch",
			Pointer:      "/users/0/name",
			ActualJSON:   `"Alice"`,
			ExpectedJSON: `"Alicia"`,
		},
		{
			Kind:         "value mismatch",
			Pointer:      "/users/0/tags/0/name",
			ActualJSON:   `"admin"`,
			ExpectedJSON: `"administrator"`,
		},
		{
			Kind:         "value mismatch",
			Pointer:      "/users/0/tags/1",
			ActualJSON:   `{"name":"senior"}`,
			ExpectedJSON: `<missing>`,
		},
		{
			Kind:         "value mismatch",
			Pointer:      "/users/0/status",
			ActualJSON:   `<missing>`,
			ExpectedJSON: `"fixing stuff"`,
		},
		{
			Kind:         "value mismatch",
			Pointer:      "/users/1/id",
			ActualJSON:   `42`,
			ExpectedJSON: `<missing>`,
		},
		{
			Kind:         "value mismatch",
			Pointer:      "/users/1/tags/1",
			ActualJSON:   `<missing>`,
			ExpectedJSON: `{"name":"postmaster"}`,
		},
	})
}

func TestFailsOnTypeMismatch(t *testing.T) {
	// several JSON values with incompatible JSON-level types, paired with their code-level representation
	testCases := []struct {
		JSON   string
		Data   any
		Scalar Option[jsonmatch.Diffable] // for testing calls to jsonmatch.Scalar().DiffAgainst() (see below)
	}{
		{
			JSON:   `null`,
			Data:   nil,
			Scalar: Some(jsonmatch.Null()),
		},
		{
			JSON:   `true`,
			Data:   true,
			Scalar: Some(jsonmatch.Scalar(true)),
		},
		{
			JSON:   `42`,
			Data:   42,
			Scalar: Some(jsonmatch.Scalar(42)),
		},
		{
			JSON:   `"foo"`,
			Data:   "foo",
			Scalar: Some(jsonmatch.Scalar("foo")),
		},
		{
			JSON:   `{"value":42}`,
			Data:   map[string]any{"value": 42},
			Scalar: None[jsonmatch.Diffable](),
		},
		{
			JSON:   `[42]`,
			Data:   []any{42},
			Scalar: None[jsonmatch.Diffable](),
		}}

	for idx1, tc1 := range testCases {
		objectMessage := fmt.Appendf(nil, `{"payload":%s}`, tc1.JSON)
		arrayMessage := fmt.Appendf(nil, `[1,%s]`, tc1.JSON)
		plainMessage := []byte(tc1.JSON)

		for idx2, tc2 := range testCases {
			// type mismatch inside of an object
			objectMatch := jsonmatch.Object{"payload": tc2.Data}
			if idx1 == idx2 {
				// if we chose matching JSON and data types, then everything works as intended
				AssertEqual(t, objectMatch.DiffAgainst(objectMessage), nil)
			} else {
				// otherwise we expect a "type mismatch" error
				AssertEqual(t, objectMatch.DiffAgainst(objectMessage), []jsonmatch.Diff{{
					Kind:         "type mismatch",
					Pointer:      "/payload",
					ActualJSON:   tc1.JSON,
					ExpectedJSON: tc2.JSON,
				}})
			}

			// type mismatch inside of an array
			arrayMatch := jsonmatch.Array{1, tc2.Data}
			if idx1 == idx2 {
				AssertEqual(t, arrayMatch.DiffAgainst(arrayMessage), nil)
			} else {
				AssertEqual(t, arrayMatch.DiffAgainst(arrayMessage), []jsonmatch.Diff{{
					Kind:         "type mismatch",
					Pointer:      "/1",
					ActualJSON:   tc1.JSON,
					ExpectedJSON: tc2.JSON,
				}})
			}

			// type mismatch for plain scalar
			if scalarMatch, ok := tc2.Scalar.Unpack(); ok {
				if idx1 == idx2 {
					AssertEqual(t, scalarMatch.DiffAgainst(plainMessage), nil)
				} else {
					AssertEqual(t, scalarMatch.DiffAgainst(plainMessage), []jsonmatch.Diff{{
						Kind:         "type mismatch",
						Pointer:      "",
						ActualJSON:   tc1.JSON,
						ExpectedJSON: tc2.JSON,
					}})
				}
			}
		}
	}
}

func TestFailsOnUnmarshalError(t *testing.T) {
	// all of these things are definitely not valid JSON messages
	testCases := [][]byte{
		// empty string
		[]byte(""),
		// looks like text/plain
		[]byte("Not found\n"),
		// looks like text/yaml
		[]byte("data:\n- 23\n- 42\n"),
		// incomplete JSON
		[]byte(`{"data":[23,`),
		// this one is not even a valid UTF-8 string
		[]byte("a\xffb\xC0\xAFc\xff"),
	}
	match := jsonmatch.Object{
		"data": jsonmatch.Array{23, 42},
	}

	for _, message := range testCases {
		diffs := match.DiffAgainst(message)
		if AssertEqual(t, len(diffs), 1) {
			diff := diffs[0]
			AssertEqual(t, strings.HasPrefix(diff.Kind, "unmarshal error ("), true)
			AssertEqual(t, strings.HasSuffix(diff.Kind, ")"), true)
			AssertEqual(t, diff.Pointer, "")
			AssertEqual(t, diff.ExpectedJSON, `{"data":[23,42]}`)
			AssertEqual(t, strings.ReplaceAll(diff.ActualJSON, "\uFFFD", ""), strings.ToValidUTF8(string(message), ""))
		}
	}
}

type unmarshalableObject struct{}

func (unmarshalableObject) MarshalJSON() ([]byte, error) {
	return nil, errors.New("this object is unmarshalable")
}

func TestArrayOnArrayAction(t *testing.T) {
	message := []byte(`[[1,3]]`) // but we assert against [[1,2]]
	expected := []jsonmatch.Diff{{
		Kind:         "value mismatch",
		Pointer:      "/0/1",
		ExpectedJSON: "2",
		ActualJSON:   "3",
	}}

	match := jsonmatch.Array{[]any{1, 2}}
	AssertEqual(t, match.DiffAgainst(message), expected)

	// this used to fail before the fix in the commit that added this test
	match = jsonmatch.Array{jsonmatch.Array{1, 2}}
	AssertEqual(t, match.DiffAgainst(message), expected)
}