1/*
2Copyright 2014 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 runtime_test
18
19import (
20	"bytes"
21	"encoding/json"
22	"reflect"
23	"testing"
24
25	"k8s.io/apimachinery/pkg/runtime"
26)
27
28func TestEmbeddedRawExtensionMarshal(t *testing.T) {
29	type test struct {
30		Ext runtime.RawExtension
31	}
32
33	extension := test{Ext: runtime.RawExtension{Raw: []byte(`{"foo":"bar"}`)}}
34	data, err := json.Marshal(extension)
35	if err != nil {
36		t.Fatalf("unexpected error: %v", err)
37	}
38	if string(data) != `{"Ext":{"foo":"bar"}}` {
39		t.Errorf("unexpected data: %s", string(data))
40	}
41}
42func TestEmbeddedRawExtensionUnmarshal(t *testing.T) {
43	type test struct {
44		Ext runtime.RawExtension
45	}
46
47	testCases := map[string]struct {
48		orig test
49	}{
50		"non-empty object": {
51			orig: test{Ext: runtime.RawExtension{Raw: []byte(`{"foo":"bar"}`)}},
52		},
53		"empty object": {
54			orig: test{Ext: runtime.RawExtension{}},
55		},
56	}
57
58	for k, tc := range testCases {
59		new := test{}
60		data, _ := json.Marshal(tc.orig)
61		if err := json.Unmarshal(data, &new); err != nil {
62			t.Errorf("%s: umarshal error: %v", k, err)
63		}
64		if !reflect.DeepEqual(tc.orig, new) {
65			t.Errorf("%s: unmarshaled struct differs from original: %v %v", k, tc.orig, new)
66		}
67	}
68}
69
70func TestEmbeddedRawExtensionRoundTrip(t *testing.T) {
71	type test struct {
72		Ext runtime.RawExtension
73	}
74
75	testCases := map[string]struct {
76		orig test
77	}{
78		"non-empty object": {
79			orig: test{Ext: runtime.RawExtension{Raw: []byte(`{"foo":"bar"}`)}},
80		},
81		"empty object": {
82			orig: test{Ext: runtime.RawExtension{}},
83		},
84	}
85
86	for k, tc := range testCases {
87		new1 := test{}
88		new2 := test{}
89		data, err := json.Marshal(tc.orig)
90		if err != nil {
91			t.Errorf("1st marshal error: %v", err)
92		}
93		if err = json.Unmarshal(data, &new1); err != nil {
94			t.Errorf("1st unmarshal error: %v", err)
95		}
96		newData, err := json.Marshal(new1)
97		if err != nil {
98			t.Errorf("2st marshal error: %v", err)
99		}
100		if err = json.Unmarshal(newData, &new2); err != nil {
101			t.Errorf("2nd unmarshal error: %v", err)
102		}
103		if !bytes.Equal(data, newData) {
104			t.Errorf("%s: re-marshaled data differs from original: %v %v", k, data, newData)
105		}
106		if !reflect.DeepEqual(tc.orig, new1) {
107			t.Errorf("%s: unmarshaled struct differs from original: %v %v", k, tc.orig, new1)
108		}
109		if !reflect.DeepEqual(new1, new2) {
110			t.Errorf("%s: re-unmarshaled struct differs from original: %v %v", k, new1, new2)
111		}
112	}
113}
114