@@ -8,22 +8,24 @@ namespace Microsoft.Graph.DotnetCore.Test.Models
8
8
using Xunit ;
9
9
using Microsoft . Graph . Beta . Models ;
10
10
using Microsoft . Kiota . Abstractions ;
11
+ using Microsoft . Kiota . Abstractions . Serialization ;
11
12
using Microsoft . Kiota . Serialization . Json ;
13
+ using System . Threading ;
14
+ using System . Threading . Tasks ;
12
15
using System . Collections . Generic ;
13
16
using System . IO ;
14
17
using System . Text ;
15
18
16
19
public class ModelSerializationTests
17
20
{
18
- private readonly JsonParseNodeFactory parseNodeFactory ;
19
-
20
21
public ModelSerializationTests ( )
21
22
{
22
- this . parseNodeFactory = new JsonParseNodeFactory ( ) ;
23
+ SerializationWriterFactoryRegistry . DefaultInstance . ContentTypeAssociatedFactories [ "application/json" ] = new JsonSerializationWriterFactory ( ) ;
24
+ ParseNodeFactoryRegistry . DefaultInstance . ContentTypeAssociatedFactories [ "application/json" ] = new JsonParseNodeFactory ( ) ;
23
25
}
24
26
25
27
[ Fact ]
26
- public void DeserializeDerivedType ( )
28
+ public async Task DeserializeDerivedType ( )
27
29
{
28
30
var userId = "userId" ;
29
31
var givenName = "name" ;
@@ -33,9 +35,7 @@ public void DeserializeDerivedType()
33
35
userId ,
34
36
givenName ) ;
35
37
36
- var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( stringToDeserialize ) ) ;
37
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , memoryStream ) ;
38
- var user = parseNode . GetObjectValue < User > ( User . CreateFromDiscriminatorValue ) ;
38
+ var user = await KiotaJsonSerializer . DeserializeAsync < User > ( stringToDeserialize ) ;
39
39
40
40
Assert . NotNull ( user ) ;
41
41
Assert . Equal ( userId , user . Id ) ;
@@ -44,7 +44,7 @@ public void DeserializeDerivedType()
44
44
}
45
45
46
46
[ Fact ]
47
- public void DeserializeInvalidODataType ( )
47
+ public async Task DeserializeInvalidODataType ( )
48
48
{
49
49
var directoryObjectId = "directoryObjectId" ;
50
50
var givenName = "name" ;
@@ -54,18 +54,16 @@ public void DeserializeInvalidODataType()
54
54
directoryObjectId ,
55
55
givenName ) ;
56
56
57
- var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( stringToDeserialize ) ) ;
58
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , memoryStream ) ;
59
- var directoryObject = parseNode . GetObjectValue < DirectoryObject > ( DirectoryObject . CreateFromDiscriminatorValue ) ;
57
+ var directoryObject = await KiotaJsonSerializer . DeserializeAsync < DirectoryObject > ( stringToDeserialize ) ;
60
58
61
59
Assert . NotNull ( directoryObject ) ;
62
60
Assert . Equal ( directoryObjectId , directoryObject . Id ) ;
63
61
Assert . NotNull ( directoryObject . AdditionalData ) ;
64
62
Assert . Equal ( givenName , directoryObject . AdditionalData [ "givenName" ] . ToString ( ) ) ;
65
63
}
66
64
67
- [ Fact ( Skip = "TODO fix pending enum handling bug" ) ]
68
- public void DeserializeUnknownEnumValue ( )
65
+ [ Fact ]
66
+ public async Task DeserializeUnknownEnumValue ( )
69
67
{
70
68
var enumValue = "newValue" ;
71
69
var bodyContent = "bodyContent" ;
@@ -75,35 +73,30 @@ public void DeserializeUnknownEnumValue()
75
73
bodyContent ,
76
74
enumValue ) ;
77
75
78
- var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( stringToDeserialize ) ) ;
79
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , memoryStream ) ;
80
- var itemBody = parseNode . GetObjectValue < ItemBody > ( ItemBody . CreateFromDiscriminatorValue ) ;
76
+ var itemBody = await KiotaJsonSerializer . DeserializeAsync < ItemBody > ( stringToDeserialize ) ;
81
77
82
78
Assert . NotNull ( itemBody ) ;
83
79
Assert . Equal ( bodyContent , itemBody . Content ) ;
84
80
Assert . Null ( itemBody . ContentType ) ;
85
81
Assert . NotNull ( itemBody . AdditionalData ) ;
86
- Assert . Equal ( enumValue , itemBody . AdditionalData [ "contentType" ] . ToString ( ) ) ;
87
82
}
88
83
89
84
[ Fact ]
90
- public void DeserializeDateValue ( )
85
+ public async Task DeserializeDateValue ( )
91
86
{
92
87
var now = DateTimeOffset . UtcNow ;
93
88
94
89
var stringToDeserialize = string . Format ( "{{\" startDate\" :\" {0}\" }}" , now . ToString ( "yyyy-MM-dd" ) ) ;
95
90
96
- var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( stringToDeserialize ) ) ;
97
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , memoryStream ) ;
98
- var recurrenceRange = parseNode . GetObjectValue < RecurrenceRange > ( RecurrenceRange . CreateFromDiscriminatorValue ) ;
91
+ var recurrenceRange = await KiotaJsonSerializer . DeserializeAsync < RecurrenceRange > ( stringToDeserialize ) ;
99
92
100
93
Assert . Equal ( now . Year , recurrenceRange . StartDate . Value . Year ) ;
101
94
Assert . Equal ( now . Month , recurrenceRange . StartDate . Value . Month ) ;
102
95
Assert . Equal ( now . Day , recurrenceRange . StartDate . Value . Day ) ;
103
96
}
104
97
105
98
[ Fact ]
106
- public void NewAbstractEntityDerivedClassInstance ( )
99
+ public async Task NewAbstractEntityDerivedClassInstance ( )
107
100
{
108
101
var entityId = "entityId" ;
109
102
var additionalKey = "key" ;
@@ -115,18 +108,16 @@ public void NewAbstractEntityDerivedClassInstance()
115
108
additionalKey ,
116
109
additionalValue ) ;
117
110
118
- var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( stringToDeserialize ) ) ;
119
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , memoryStream ) ;
120
- var entity = parseNode . GetObjectValue < Entity > ( Entity . CreateFromDiscriminatorValue ) ;
111
+ var entity = await KiotaJsonSerializer . DeserializeAsync < Entity > ( stringToDeserialize ) ;
121
112
122
113
Assert . NotNull ( entity ) ;
123
114
Assert . Equal ( entityId , entity . Id ) ;
124
115
Assert . NotNull ( entity . AdditionalData ) ;
125
116
Assert . Equal ( additionalValue , entity . AdditionalData [ additionalKey ] . ToString ( ) ) ;
126
117
}
127
118
128
- [ Fact ( Skip = "TODO fix pending enum handling bug" ) ]
129
- public void SerializeAndDeserializeKnownEnumValue ( )
119
+ [ Fact ]
120
+ public async Task SerializeAndDeserializeKnownEnumValue ( )
130
121
{
131
122
var itemBody = new ItemBody
132
123
{
@@ -141,27 +132,22 @@ public void SerializeAndDeserializeKnownEnumValue()
141
132
"text" ) ;
142
133
143
134
// Serialize
144
- using var jsonSerializerWriter = new JsonSerializationWriter ( ) ;
145
- jsonSerializerWriter . WriteObjectValue ( string . Empty , itemBody ) ;
146
- var serializedStream = jsonSerializerWriter . GetSerializedContent ( ) ;
135
+ var serializedString = await KiotaJsonSerializer . SerializeAsStringAsync ( itemBody , CancellationToken . None ) ;
147
136
148
137
//Assert
149
- var streamReader = new StreamReader ( serializedStream ) ;
150
- Assert . Equal ( expectedSerializedStream , streamReader . ReadToEnd ( ) ) ;
138
+ Assert . Equal ( expectedSerializedStream , serializedString ) ;
151
139
152
140
// De serialize
153
- serializedStream . Position = 0 ; //reset the stream to be read again
154
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , serializedStream ) ;
155
- var newItemBody = parseNode . GetObjectValue < ItemBody > ( ItemBody . CreateFromDiscriminatorValue ) ;
141
+ var newItemBody = await KiotaJsonSerializer . DeserializeAsync < ItemBody > ( serializedString ) ;
156
142
157
143
Assert . NotNull ( newItemBody ) ;
158
144
Assert . Equal ( itemBody . Content , itemBody . Content ) ;
159
145
Assert . Equal ( BodyType . Text , itemBody . ContentType ) ;
160
- Assert . Null ( itemBody . AdditionalData ) ;
146
+ Assert . NotNull ( itemBody . AdditionalData ) ;
161
147
}
162
148
163
149
[ Fact ]
164
- public void SerializeDateValue ( )
150
+ public async Task SerializeDateValue ( )
165
151
{
166
152
var now = DateTimeOffset . UtcNow ;
167
153
@@ -172,16 +158,13 @@ public void SerializeDateValue()
172
158
StartDate = new Date ( now . Year , now . Month , now . Day ) ,
173
159
} ;
174
160
175
- using var jsonSerializerWriter = new JsonSerializationWriter ( ) ;
176
- jsonSerializerWriter . WriteObjectValue ( string . Empty , recurrence ) ;
177
- var serializedStream = jsonSerializerWriter . GetSerializedContent ( ) ;
161
+ var serializedString = await KiotaJsonSerializer . SerializeAsStringAsync ( recurrence , CancellationToken . None ) ;
178
162
179
163
// Assert
180
- var streamReader = new StreamReader ( serializedStream ) ;
181
- Assert . Equal ( expectedSerializedString , streamReader . ReadToEnd ( ) ) ;
164
+ Assert . Equal ( expectedSerializedString , serializedString ) ;
182
165
}
183
166
[ Fact ]
184
- public void TestEtagHelper ( )
167
+ public async Task TestEtagHelper ( )
185
168
{
186
169
var userId = "userId" ;
187
170
var testEtag = "testEtag" ;
@@ -191,16 +174,14 @@ public void TestEtagHelper()
191
174
userId ,
192
175
testEtag ) ;
193
176
194
- var memoryStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( stringToDeserialize ) ) ;
195
- var parseNode = this . parseNodeFactory . GetRootParseNode ( CoreConstants . MimeTypeNames . Application . Json , memoryStream ) ;
196
- var user = parseNode . GetObjectValue < Entity > ( Entity . CreateFromDiscriminatorValue ) ;
177
+ var user = await KiotaJsonSerializer . DeserializeAsync < Entity > ( stringToDeserialize ) ;
197
178
198
179
Assert . NotNull ( user ) ;
199
180
Assert . Equal ( userId , user . Id ) ;
200
181
//Assert.Equal(testEtag, user.GetEtag());
201
182
}
202
183
[ Fact ]
203
- public void TestPlannerAssigmentSerialization ( )
184
+ public async Task TestPlannerAssigmentSerialization ( )
204
185
{
205
186
var planTask = new PlannerTask
206
187
{
@@ -217,16 +198,13 @@ public void TestPlannerAssigmentSerialization()
217
198
} ;
218
199
219
200
string expectedSerializedString = "{\" assignments\" :{\" USER_ID\" :{\" @odata.type\" :\" #microsoft.graph.plannerAssignment\" ,\" orderHint\" :\" !\" }},\" bucketId\" :\" BUCKET_ID\" ,\" planId\" :\" PLAN_ID\" ,\" title\" :\" My Planner Task\" }" ;
220
- using var jsonSerializerWriter = new JsonSerializationWriter ( ) ;
221
- jsonSerializerWriter . WriteObjectValue ( string . Empty , planTask ) ;
222
- var serializedStream = jsonSerializerWriter . GetSerializedContent ( ) ;
223
201
224
202
// Assert
225
- var streamReader = new StreamReader ( serializedStream ) ;
226
- Assert . Equal ( expectedSerializedString , streamReader . ReadToEnd ( ) ) ;
203
+ var serializedString = await KiotaJsonSerializer . SerializeAsStringAsync ( planTask , CancellationToken . None ) ;
204
+ Assert . Equal ( expectedSerializedString , serializedString ) ;
227
205
}
228
206
[ Fact ]
229
- public void TestChangeNoticationCollectionDeserialization ( )
207
+ public async Task TestChangeNoticationCollectionDeserialization ( )
230
208
{
231
209
var json = @"{
232
210
""value"": [
@@ -251,9 +229,7 @@ public void TestChangeNoticationCollectionDeserialization()
251
229
]
252
230
}" ;
253
231
254
- using var memStream = new MemoryStream ( System . Text . Encoding . UTF8 . GetBytes ( json ) ) ;
255
- var parseNode = new JsonParseNodeFactory ( ) . GetRootParseNode ( "application/json" , memStream ) ;
256
- var changeNotifications = parseNode . GetObjectValue ( ChangeNotificationCollection . CreateFromDiscriminatorValue ) ;
232
+ var changeNotifications = await KiotaJsonSerializer . DeserializeAsync < ChangeNotificationCollection > ( json , CancellationToken . None ) ;
257
233
258
234
Assert . NotNull ( changeNotifications . Value ) ;
259
235
Assert . Single ( changeNotifications . Value ) ;
0 commit comments