-
Notifications
You must be signed in to change notification settings - Fork 11
/
kiota_serializer_test.go
143 lines (128 loc) · 4.79 KB
/
kiota_serializer_test.go
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
package abstractions
import (
"testing"
"github.com/microsoft/kiota-abstractions-go/internal"
"github.com/microsoft/kiota-abstractions-go/serialization"
assert "github.com/stretchr/testify/assert"
)
var jsonContentType = "application/json"
func TestItDefendsSerializationEmptyContentType(t *testing.T) {
result, err := serialization.Serialize("", nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsSerializationNilValue(t *testing.T) {
result, err := serialization.Serialize(jsonContentType, nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsCollectionSerializationEmptyContentType(t *testing.T) {
result, err := serialization.SerializeCollection("", nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsCollectionSerializationNilValue(t *testing.T) {
result, err := serialization.SerializeCollection(jsonContentType, nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItSerializesObject(t *testing.T) {
serializedValue := "{\"id\":\"123\"}"
metaFactory := func() serialization.SerializationWriterFactory {
return &internal.MockSerializerFactory{
SerializedValue: serializedValue,
}
}
RegisterDefaultSerializer(metaFactory)
person := internal.NewPerson()
id := "123"
person.SetId(&id)
result, err := serialization.Serialize(jsonContentType, person)
assert.Nil(t, err)
assert.NotNil(t, result)
assert.Equal(t, serializedValue, string(result))
serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory)
}
func TestItSerializesACollectionOfObjects(t *testing.T) {
serializedValue := "[{\"id\":\"123\"}]"
metaFactory := func() serialization.SerializationWriterFactory {
return &internal.MockSerializerFactory{
SerializedValue: serializedValue,
}
}
RegisterDefaultSerializer(metaFactory)
person := internal.NewPerson()
id := "123"
person.SetId(&id)
result, err := serialization.SerializeCollection(jsonContentType, []serialization.Parsable{person})
assert.Nil(t, err)
assert.NotNil(t, result)
assert.Equal(t, serializedValue, string(result))
serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory)
}
func TestItDefendsDeserializationEmptyContentType(t *testing.T) {
result, err := serialization.Deserialize("", nil, nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsDeserializationNilContent(t *testing.T) {
result, err := serialization.Deserialize(jsonContentType, nil, nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsDeserializationNilFactory(t *testing.T) {
result, err := serialization.Deserialize(jsonContentType, make([]byte, 0), nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsCollectionDeserializationEmptyContentType(t *testing.T) {
result, err := serialization.DeserializeCollection("", nil, nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsCollectionDeserializationNilContent(t *testing.T) {
result, err := serialization.DeserializeCollection(jsonContentType, nil, nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDefendsCollectionDeserializationNilFactory(t *testing.T) {
result, err := serialization.DeserializeCollection(jsonContentType, make([]byte, 0), nil)
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestItDeserializesAnObject(t *testing.T) {
person := internal.NewPerson()
id := "123"
person.SetId(&id)
metaFactory := func() serialization.ParseNodeFactory {
return &internal.MockParseNodeFactory{
SerializedValue: person,
}
}
RegisterDefaultDeserializer(metaFactory)
result, err := serialization.Deserialize(jsonContentType, []byte("{\"id\": \"123\"}"), internal.CreatePersonFromDiscriminatorValue)
assert.Nil(t, err)
assert.NotNil(t, result)
resultAsPerson, ok := result.(*internal.Person)
assert.True(t, ok)
assert.Equal(t, id, *resultAsPerson.GetId())
serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory)
}
func TestItDeserializesAnObjectCollection(t *testing.T) {
person := internal.NewPerson()
id := "123"
person.SetId(&id)
metaFactory := func() serialization.ParseNodeFactory {
return &internal.MockParseNodeFactory{
SerializedValue: []serialization.Parsable{person},
}
}
RegisterDefaultDeserializer(metaFactory)
result, err := serialization.DeserializeCollection(jsonContentType, []byte("[{\"id\": \"123\"}]"), internal.CreatePersonFromDiscriminatorValue)
assert.Nil(t, err)
assert.NotNil(t, result)
resultAsPerson, ok := result[0].(*internal.Person)
assert.True(t, ok)
assert.Equal(t, id, *resultAsPerson.GetId())
serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory)
}