Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Serialization\EntitiesSerializationTests.cs

Symbol Coverage: 100.00% (66 of 66)

Branch Coverage: 100.00% (5 of 5)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 243


L V Source
1
#region License
2
// Copyright (c) 2007 James Newton-King
3
//
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
11
// conditions:
12
//
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
15
//
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
24
#endregion
25

  
26
#if !PocketPC && !SILVERLIGHT && !NET20
27
using System;
28
using System.Collections.Generic;
29
using System.Data;
30
using System.Linq;
31
using System.Text;
32
using Newtonsoft.Json.Converters;
33
using NUnit.Framework;
34

  
35
namespace Newtonsoft.Json.Tests.Serialization
36
{
37
  public class EntitiesSerializationTests : TestFixtureBase
38
  {
39
    [Test]
40
    public void SerializeEntity()
41
    {
42
 1
      Folder rootFolder = CreateEntitiesTestData();
43

  
44
 1
      string json = JsonConvert.SerializeObject(rootFolder, Formatting.Indented, new IsoDateTimeConverter());
45

  
46
 1
      string expected = @"{
47
 1
  ""$id"": ""1"",
48
 1
  ""FolderId"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e"",
49
 1
  ""Name"": ""Root folder"",
50
 1
  ""Description"": ""Description!"",
51
 1
  ""CreatedDate"": ""2000-12-10T10:50:00Z"",
52
 1
  ""Files"": [],
53
 1
  ""ChildFolders"": [
54
 1
    {
55
 1
      ""$id"": ""2"",
56
 1
      ""FolderId"": ""484936e2-7cbb-4592-93ff-b2103e5705e4"",
57
 1
      ""Name"": ""Child folder"",
58
 1
      ""Description"": ""Description!"",
59
 1
      ""CreatedDate"": ""2001-11-20T10:50:00Z"",
60
 1
      ""Files"": [
61
 1
        {
62
 1
          ""$id"": ""3"",
63
 1
          ""FileId"": ""cc76d734-49f1-4616-bb38-41514228ac6c"",
64
 1
          ""Name"": ""File 1"",
65
 1
          ""Description"": ""Description!"",
66
 1
          ""CreatedDate"": ""2002-10-30T10:50:00Z"",
67
 1
          ""Folder"": {
68
 1
            ""$ref"": ""2""
69
 1
          },
70
 1
          ""EntityKey"": {
71
 1
            ""$id"": ""4"",
72
 1
            ""EntitySetName"": ""File"",
73
 1
            ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
74
 1
            ""EntityKeyValues"": [
75
 1
              {
76
 1
                ""Key"": ""FileId"",
77
 1
                ""Type"": ""System.Guid"",
78
 1
                ""Value"": ""cc76d734-49f1-4616-bb38-41514228ac6c""
79
 1
              }
80
 1
            ]
81
 1
          }
82
 1
        }
83
 1
      ],
84
 1
      ""ChildFolders"": [],
85
 1
      ""ParentFolder"": {
86
 1
        ""$ref"": ""1""
87
 1
      },
88
 1
      ""EntityKey"": {
89
 1
        ""$id"": ""5"",
90
 1
        ""EntitySetName"": ""Folder"",
91
 1
        ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
92
 1
        ""EntityKeyValues"": [
93
 1
          {
94
 1
            ""Key"": ""FolderId"",
95
 1
            ""Type"": ""System.Guid"",
96
 1
            ""Value"": ""484936e2-7cbb-4592-93ff-b2103e5705e4""
97
 1
          }
98
 1
        ]
99
 1
      }
100
 1
    }
101
 1
  ],
102
 1
  ""ParentFolder"": null,
103
 1
  ""EntityKey"": {
104
 1
    ""$id"": ""6"",
105
 1
    ""EntitySetName"": ""Folder"",
106
 1
    ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
107
 1
    ""EntityKeyValues"": [
108
 1
      {
109
 1
        ""Key"": ""FolderId"",
110
 1
        ""Type"": ""System.Guid"",
111
 1
        ""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
112
 1
      }
113
 1
    ]
114
 1
  }
115
 1
}";
116

  
117
 1
      Assert.AreEqual(expected, json);
118
 1
    }
119

  
120
    [Test]
121
    public void DeserializeEntity()
122
    {
123
 1
      string json = @"{
124
 1
  ""$id"": ""1"",
125
 1
  ""FolderId"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e"",
126
 1
  ""Name"": ""Root folder"",
127
 1
  ""Description"": ""Description!"",
128
 1
  ""CreatedDate"": ""2000-12-10T10:50:00Z"",
129
 1
  ""Files"": [],
130
 1
  ""ChildFolders"": [
131
 1
    {
132
 1
      ""$id"": ""2"",
133
 1
      ""FolderId"": ""484936e2-7cbb-4592-93ff-b2103e5705e4"",
134
 1
      ""Name"": ""Child folder"",
135
 1
      ""Description"": ""Description!"",
136
 1
      ""CreatedDate"": ""2001-11-20T10:50:00Z"",
137
 1
      ""Files"": [
138
 1
        {
139
 1
          ""$id"": ""3"",
140
 1
          ""FileId"": ""cc76d734-49f1-4616-bb38-41514228ac6c"",
141
 1
          ""Name"": ""File 1"",
142
 1
          ""Description"": ""Description!"",
143
 1
          ""CreatedDate"": ""2002-10-30T10:50:00Z"",
144
 1
          ""Folder"": {
145
 1
            ""$ref"": ""2""
146
 1
          },
147
 1
          ""EntityKey"": {
148
 1
            ""$id"": ""4"",
149
 1
            ""EntitySetName"": ""File"",
150
 1
            ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
151
 1
            ""EntityKeyValues"": [
152
 1
              {
153
 1
                ""Key"": ""FileId"",
154
 1
                ""Type"": ""System.Guid"",
155
 1
                ""Value"": ""cc76d734-49f1-4616-bb38-41514228ac6c""
156
 1
              }
157
 1
            ]
158
 1
          }
159
 1
        }
160
 1
      ],
161
 1
      ""ChildFolders"": [],
162
 1
      ""ParentFolder"": {
163
 1
        ""$ref"": ""1""
164
 1
      },
165
 1
      ""EntityKey"": {
166
 1
        ""$id"": ""5"",
167
 1
        ""EntitySetName"": ""Folder"",
168
 1
        ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
169
 1
        ""EntityKeyValues"": [
170
 1
          {
171
 1
            ""Key"": ""FolderId"",
172
 1
            ""Type"": ""System.Guid"",
173
 1
            ""Value"": ""484936e2-7cbb-4592-93ff-b2103e5705e4""
174
 1
          }
175
 1
        ]
176
 1
      }
177
 1
    }
178
 1
  ],
179
 1
  ""ParentFolder"": null,
180
 1
  ""EntityKey"": {
181
 1
    ""$id"": ""6"",
182
 1
    ""EntitySetName"": ""Folder"",
183
 1
    ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
184
 1
    ""EntityKeyValues"": [
185
 1
      {
186
 1
        ""Key"": ""FolderId"",
187
 1
        ""Type"": ""System.Guid"",
188
 1
        ""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
189
 1
      }
190
 1
    ]
191
 1
  }
192
 1
}";
193

  
194
 1
      Folder f = JsonConvert.DeserializeObject<Folder>(json, new IsoDateTimeConverter());
195

  
196
 1
      Assert.IsNotNull(f);
197
 1
      Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), f.FolderId);
198
 1
      Assert.AreEqual("Folder", f.EntityKey.EntitySetName);
199
 1
      Assert.AreEqual("DataServicesTestDatabaseEntities", f.EntityKey.EntityContainerName);
200
 1
      Assert.AreEqual("Folder", f.EntityKey.EntitySetName);
201
 1
      Assert.AreEqual(false, f.EntityKey.IsTemporary);
202
 1
      Assert.AreEqual(1, f.EntityKey.EntityKeyValues.Length);
203
 1
      Assert.AreEqual("FolderId", f.EntityKey.EntityKeyValues[0].Key);
204
 1
      Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), f.EntityKey.EntityKeyValues[0].Value);
205
 1
      Assert.AreEqual("Root folder", f.Name);
206
 1
      Assert.AreEqual(new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc), f.CreatedDate);
207
 1
      Assert.AreEqual(null, f.ParentFolder);
208
 1
      Assert.AreEqual(1, f.ChildFolders.Count);
209

  
210
 1
      Folder childFolder = f.ChildFolders.ElementAt(0);
211

  
212
 1
      Assert.AreEqual("Child folder", childFolder.Name);
213
 1
      Assert.AreEqual("Description!", childFolder.Description);
214
 1
      Assert.AreEqual(f, childFolder.ParentFolder);
215
 1
      Assert.AreEqual(f, childFolder.ParentFolderReference.Value);
216
      // is this a problem?
217
 1
      Assert.AreEqual(null, childFolder.ParentFolderReference.EntityKey);
218
 1
    }
219

  
220
    [Test]
221
    public void SerializeMultiValueEntityKey()
222
    {
223
 1
      EntityKey e = new EntityKey("DataServicesTestDatabaseEntities.Folder",
224
 1
                                  new List<EntityKeyMember>
225
 1
                                    {
226
 1
                                      new EntityKeyMember("GuidId", new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E")),
227
 1
                                      new EntityKeyMember("IntId", int.MaxValue),
228
 1
                                      new EntityKeyMember("LongId", long.MaxValue),
229
 1
                                      new EntityKeyMember("StringId", "String!"),
230
 1
                                      new EntityKeyMember("DateTimeId", new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc))
231
 1
                                    });
232

  
233
 1
      string json = JsonConvert.SerializeObject(e, Formatting.Indented);
234

  
235
 1
      Assert.AreEqual(@"{
236
 1
  ""$id"": ""1"",
237
 1
  ""EntitySetName"": ""Folder"",
238
 1
  ""EntityContainerName"": ""DataServicesTestDatabaseEntities"",
239
 1
  ""EntityKeyValues"": [
240
 1
    {
241
 1
      ""Key"": ""GuidId"",
242
 1
      ""Type"": ""System.Guid"",
243
 1
      ""Value"": ""a4e8ba80-eb24-4591-bb1c-62d3ad83701e""
244
 1
    },
245
 1
    {
246
 1
      ""Key"": ""IntId"",
247
 1
      ""Type"": ""System.Int32"",
248
 1
      ""Value"": ""2147483647""
249
 1
    },
250
 1
    {
251
 1
      ""Key"": ""LongId"",
252
 1
      ""Type"": ""System.Int64"",
253
 1
      ""Value"": ""9223372036854775807""
254
 1
    },
255
 1
    {
256
 1
      ""Key"": ""StringId"",
257
 1
      ""Type"": ""System.String"",
258
 1
      ""Value"": ""String!""
259
 1
    },
260
 1
    {
261
 1
      ""Key"": ""DateTimeId"",
262
 1
      ""Type"": ""System.DateTime"",
263
 1
      ""Value"": ""12/10/2000 10:50:00""
264
 1
    }
265
 1
  ]
266
 1
}", json);
267

  
268
 1
      EntityKey newKey = JsonConvert.DeserializeObject<EntityKey>(json);
269
 1
      Assert.IsFalse(ReferenceEquals(e, newKey));
270

  
271
 1
      Assert.AreEqual(5, newKey.EntityKeyValues.Length);
272
 1
      Assert.AreEqual("GuidId", newKey.EntityKeyValues[0].Key);
273
 1
      Assert.AreEqual(new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E"), newKey.EntityKeyValues[0].Value);
274
 1
      Assert.AreEqual("IntId", newKey.EntityKeyValues[1].Key);
275
 1
      Assert.AreEqual(int.MaxValue, newKey.EntityKeyValues[1].Value);
276
 1
      Assert.AreEqual("LongId", newKey.EntityKeyValues[2].Key);
277
 1
      Assert.AreEqual(long.MaxValue, newKey.EntityKeyValues[2].Value);
278
 1
      Assert.AreEqual("StringId", newKey.EntityKeyValues[3].Key);
279
 1
      Assert.AreEqual("String!", newKey.EntityKeyValues[3].Value);
280
 1
      Assert.AreEqual("DateTimeId", newKey.EntityKeyValues[4].Key);
281
 1
      Assert.AreEqual(new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc), newKey.EntityKeyValues[4].Value);
282
 1
    }
283

  
284
    private Folder CreateEntitiesTestData()
285
    {
286
 1
      Folder folder = new Folder();
287
 1
      folder.FolderId = new Guid("A4E8BA80-EB24-4591-BB1C-62D3AD83701E");
288
 1
      folder.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.Folder", "FolderId", folder.FolderId);
289
 1
      folder.Name = "Root folder";
290
 1
      folder.Description = "Description!";
291
 1
      folder.CreatedDate = new DateTime(2000, 12, 10, 10, 50, 0, DateTimeKind.Utc);
292
      
293
 1
      Folder childFolder = new Folder();
294
 1
      childFolder.FolderId = new Guid("484936E2-7CBB-4592-93FF-B2103E5705E4");
295
 1
      childFolder.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.Folder", "FolderId", childFolder.FolderId);
296
 1
      childFolder.Name = "Child folder";
297
 1
      childFolder.Description = "Description!";
298
 1
      childFolder.CreatedDate = new DateTime(2001, 11, 20, 10, 50, 0, DateTimeKind.Utc);
299

  
300
 1
      folder.ChildFolders.Add(childFolder);
301

  
302
 1
      File file1 = new File();
303
 1
      file1.FileId = new Guid("CC76D734-49F1-4616-BB38-41514228AC6C");
304
 1
      file1.EntityKey = new EntityKey("DataServicesTestDatabaseEntities.File", "FileId", file1.FileId);
305
 1
      file1.Name = "File 1";
306
 1
      file1.Description = "Description!";
307
 1
      file1.CreatedDate = new DateTime(2002, 10, 30, 10, 50, 0, DateTimeKind.Utc);
308

  
309
 1
      childFolder.Files.Add(file1);
310
 1
      return folder;
311
 1
    }
312
  }
313
}
314
#endif