forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpatch_atomic_test.go
More file actions
224 lines (187 loc) · 7.14 KB
/
Copy pathjsonpatch_atomic_test.go
File metadata and controls
224 lines (187 loc) · 7.14 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
package jsonpatch
import (
"testing"
)
func TestAtomicField_DifferentValues_SingleReplace(t *testing.T) {
a := `{"PolicyDocument": {"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": "s3:GetObject", "Resource": "*"}]}}`
b := `{"PolicyDocument": {"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": "s3:PutObject", "Resource": "*"}]}}`
collections := Collections{
EntitySets: EntitySets{},
Arrays: []Path{},
Atomics: []Path{"$.PolicyDocument"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
if patch[0].Operation != "replace" {
t.Errorf("expected replace operation, got %s", patch[0].Operation)
}
if patch[0].Path != "/PolicyDocument" {
t.Errorf("expected path /PolicyDocument, got %s", patch[0].Path)
}
}
func TestAtomicField_EqualValues_NoPatch(t *testing.T) {
a := `{"PolicyDocument": {"Version": "2012-10-17", "Statement": [{"Effect": "Allow"}]}}`
b := `{"PolicyDocument": {"Version": "2012-10-17", "Statement": [{"Effect": "Allow"}]}}`
collections := Collections{
Atomics: []Path{"$.PolicyDocument"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 0 {
t.Fatalf("expected 0 patch operations, got %d: %v", len(patch), patch)
}
}
func TestAtomicField_NestedArrayDiffers_SingleReplace(t *testing.T) {
// Even though the nested array has different elements, atomic should produce a single replace
a := `{"Config": {"Items": [1, 2, 3]}}`
b := `{"Config": {"Items": [4, 5, 6]}}`
collections := Collections{
Atomics: []Path{"$.Config"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
if patch[0].Operation != "replace" {
t.Errorf("expected replace, got %s", patch[0].Operation)
}
if patch[0].Path != "/Config" {
t.Errorf("expected path /Config, got %s", patch[0].Path)
}
}
func TestAtomicField_NullToValue_Replace(t *testing.T) {
// null→map type change produces replace (handled by diff before atomic check)
a := `{"PolicyDocument": null}`
b := `{"PolicyDocument": {"Version": "2012-10-17"}}`
collections := Collections{
Atomics: []Path{"$.PolicyDocument"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
// Type change from null to map is a single replace — correct atomic behavior
if patch[0].Operation != "replace" {
t.Errorf("expected replace operation, got %s", patch[0].Operation)
}
if patch[0].Path != "/PolicyDocument" {
t.Errorf("expected path /PolicyDocument, got %s", patch[0].Path)
}
}
func TestAtomicField_NonAtomicUnchanged(t *testing.T) {
// Non-atomic fields should still recurse as before
a := `{"PolicyDocument": {"Version": "old"}, "Name": "test"}`
b := `{"PolicyDocument": {"Version": "new"}, "Name": "test"}`
collections := Collections{} // no atomics
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
// Should recurse into PolicyDocument and produce a replace on /PolicyDocument/Version
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
if patch[0].Path != "/PolicyDocument/Version" {
t.Errorf("expected recursive path /PolicyDocument/Version, got %s", patch[0].Path)
}
}
func TestAtomicField_ExtraKeysInActual_SingleReplace(t *testing.T) {
// Atomic field where actual has extra keys not in desired — should be a single replace with desired value
a := `{"Policy": {"Version": "2012-10-17", "Id": "extra", "Statement": []}}`
b := `{"Policy": {"Version": "2012-10-17", "Statement": [{"Effect": "Allow"}]}}`
collections := Collections{
Atomics: []Path{"$.Policy"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
if patch[0].Operation != "replace" {
t.Errorf("expected replace, got %s", patch[0].Operation)
}
if patch[0].Path != "/Policy" {
t.Errorf("expected path /Policy, got %s", patch[0].Path)
}
}
func TestAtomicField_Array_SingleReplace(t *testing.T) {
// An array field marked Atomic must produce a single whole-array replace,
// not per-element remove+add. AWS Cloud Control does not reliably apply a
// remove+add pair against a mutually-exclusive list (e.g. NetworkFirewall
// FirewallPolicy.StatefulDefaultActions), leaving both old and new values.
a := `{"L": ["aws:drop_strict"]}`
b := `{"L": ["aws:drop_established"]}`
collections := Collections{
Atomics: []Path{"$.L"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
if patch[0].Operation != "replace" {
t.Errorf("expected replace operation, got %s", patch[0].Operation)
}
if patch[0].Path != "/L" {
t.Errorf("expected path /L, got %s", patch[0].Path)
}
}
func TestAtomicField_NestedArray_SingleReplace(t *testing.T) {
// The PLA-37 shape: an array nested inside an object, addressed by a dotted
// hint key (FirewallPolicy.StatefulDefaultActions -> $.FirewallPolicy.StatefulDefaultActions).
a := `{"FirewallPolicy": {"StatefulDefaultActions": ["aws:drop_strict"]}}`
b := `{"FirewallPolicy": {"StatefulDefaultActions": ["aws:drop_established"]}}`
collections := Collections{
Atomics: []Path{"$.FirewallPolicy.StatefulDefaultActions"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 1 {
t.Fatalf("expected 1 patch operation, got %d: %v", len(patch), patch)
}
if patch[0].Operation != "replace" {
t.Errorf("expected replace operation, got %s", patch[0].Operation)
}
if patch[0].Path != "/FirewallPolicy/StatefulDefaultActions" {
t.Errorf("expected path /FirewallPolicy/StatefulDefaultActions, got %s", patch[0].Path)
}
if v, ok := patch[0].Value.([]any); !ok || len(v) != 1 || v[0] != "aws:drop_established" {
t.Errorf("expected value [aws:drop_established], got %v", patch[0].Value)
}
}
func TestAtomicField_Array_EqualContent_NoPatch(t *testing.T) {
// Atomic array equal in content (set semantics) must produce no patch — even
// when element order differs — to avoid a perpetual drift loop when the
// provider returns the same values in a different order.
a := `{"L": ["a", "b"]}`
b := `{"L": ["b", "a"]}`
collections := Collections{
Atomics: []Path{"$.L"},
}
patch, err := CreatePatch([]byte(a), []byte(b), collections, nil, PatchStrategyExactMatch)
if err != nil {
t.Fatal(err)
}
if len(patch) != 0 {
t.Fatalf("expected 0 patch operations, got %d: %v", len(patch), patch)
}
}