1/*
2Copyright 2019 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package defaulting
18
19import (
20	"bytes"
21	"reflect"
22	"testing"
23
24	structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
25	"k8s.io/apimachinery/pkg/util/json"
26)
27
28func TestDefault(t *testing.T) {
29	tests := []struct {
30		name     string
31		json     string
32		schema   *structuralschema.Structural
33		expected string
34	}{
35		{"empty", "null", nil, "null"},
36		{"scalar", "4", &structuralschema.Structural{
37			Generic: structuralschema.Generic{
38				Default: structuralschema.JSON{"foo"},
39			},
40		}, "4"},
41		{"scalar array", "[1,2]", &structuralschema.Structural{
42			Items: &structuralschema.Structural{
43				Generic: structuralschema.Generic{
44					Default: structuralschema.JSON{"foo"},
45				},
46			},
47		}, "[1,2]"},
48		{"object array", `[{"a":1},{"b":1},{"c":1}]`, &structuralschema.Structural{
49			Items: &structuralschema.Structural{
50				Properties: map[string]structuralschema.Structural{
51					"a": {
52						Generic: structuralschema.Generic{
53							Default: structuralschema.JSON{"A"},
54						},
55					},
56					"b": {
57						Generic: structuralschema.Generic{
58							Default: structuralschema.JSON{"B"},
59						},
60					},
61					"c": {
62						Generic: structuralschema.Generic{
63							Default: structuralschema.JSON{"C"},
64						},
65					},
66				},
67			},
68		}, `[{"a":1,"b":"B","c":"C"},{"a":"A","b":1,"c":"C"},{"a":"A","b":"B","c":1}]`},
69		{"object array object", `{"array":[{"a":1},{"b":2}],"object":{"a":1},"additionalProperties":{"x":{"a":1},"y":{"b":2}}}`, &structuralschema.Structural{
70			Properties: map[string]structuralschema.Structural{
71				"array": {
72					Items: &structuralschema.Structural{
73						Properties: map[string]structuralschema.Structural{
74							"a": {
75								Generic: structuralschema.Generic{
76									Default: structuralschema.JSON{"A"},
77								},
78							},
79							"b": {
80								Generic: structuralschema.Generic{
81									Default: structuralschema.JSON{"B"},
82								},
83							},
84						},
85					},
86				},
87				"object": {
88					Properties: map[string]structuralschema.Structural{
89						"a": {
90							Generic: structuralschema.Generic{
91								Default: structuralschema.JSON{"N"},
92							},
93						},
94						"b": {
95							Generic: structuralschema.Generic{
96								Default: structuralschema.JSON{"O"},
97							},
98						},
99					},
100				},
101				"additionalProperties": {
102					Generic: structuralschema.Generic{
103						AdditionalProperties: &structuralschema.StructuralOrBool{
104							Structural: &structuralschema.Structural{
105								Properties: map[string]structuralschema.Structural{
106									"a": {
107										Generic: structuralschema.Generic{
108											Default: structuralschema.JSON{"alpha"},
109										},
110									},
111									"b": {
112										Generic: structuralschema.Generic{
113											Default: structuralschema.JSON{"beta"},
114										},
115									},
116								},
117							},
118						},
119					},
120				},
121				"foo": {
122					Generic: structuralschema.Generic{
123						Default: structuralschema.JSON{"bar"},
124					},
125				},
126			},
127		}, `{"array":[{"a":1,"b":"B"},{"a":"A","b":2}],"object":{"a":1,"b":"O"},"additionalProperties":{"x":{"a":1,"b":"beta"},"y":{"a":"alpha","b":2}},"foo":"bar"}`},
128		{"empty and null", `[{},{"a":1},{"a":0},{"a":0.0},{"a":""},{"a":null},{"a":[]},{"a":{}}]`, &structuralschema.Structural{
129			Items: &structuralschema.Structural{
130				Properties: map[string]structuralschema.Structural{
131					"a": {
132						Generic: structuralschema.Generic{
133							Default: structuralschema.JSON{"A"},
134						},
135					},
136				},
137			},
138		}, `[{"a":"A"},{"a":1},{"a":0},{"a":0.0},{"a":""},{"a":null},{"a":[]},{"a":{}}]`},
139	}
140	for _, tt := range tests {
141		t.Run(tt.name, func(t *testing.T) {
142			var in interface{}
143			if err := json.Unmarshal([]byte(tt.json), &in); err != nil {
144				t.Fatal(err)
145			}
146
147			var expected interface{}
148			if err := json.Unmarshal([]byte(tt.expected), &expected); err != nil {
149				t.Fatal(err)
150			}
151
152			Default(in, tt.schema)
153			if !reflect.DeepEqual(in, expected) {
154				var buf bytes.Buffer
155				enc := json.NewEncoder(&buf)
156				enc.SetIndent("", "  ")
157				err := enc.Encode(in)
158				if err != nil {
159					t.Fatalf("unexpected result mashalling error: %v", err)
160				}
161				t.Errorf("expected: %s\ngot: %s", tt.expected, buf.String())
162			}
163		})
164	}
165}
166