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