Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Schema\JsonSchemaBuilderTests.cs

Symbol Coverage: 99.37% (157 of 158)

Branch Coverage: 95.83% (23 of 24)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 255


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
using System;
27
using System.Collections.Generic;
28
using System.Linq;
29
using System.Text;
30
using NUnit.Framework;
31
using Newtonsoft.Json.Schema;
32
using System.IO;
33
using Newtonsoft.Json.Linq;
34

  
35
namespace Newtonsoft.Json.Tests.Schema
36
{
37
  public class JsonSchemaBuilderTests : TestFixtureBase
38
  {
39
    [Test]
40
    public void Simple()
41
    {
42
 1
      string json = @"
43
 1
{
44
 1
  ""description"": ""A person"",
45
 1
  ""type"": ""object"",
46
 1
  ""properties"":
47
 1
  {
48
 1
    ""name"": {""type"":""string""},
49
 1
    ""hobbies"": {
50
 1
      ""type"": ""array"",
51
 1
      ""items"": {""type"":""string""}
52
 1
    }
53
 1
  }
54
 1
}
55
 1
";
56

  
57
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
58
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
59

  
60
 1
      Assert.AreEqual("A person", schema.Description);
61
 1
      Assert.AreEqual(JsonSchemaType.Object, schema.Type);
62

  
63
 1
      Assert.AreEqual(2, schema.Properties.Count);
64

  
65
 1
      Assert.AreEqual(JsonSchemaType.String, schema.Properties["name"].Type);
66
 1
      Assert.AreEqual(JsonSchemaType.Array, schema.Properties["hobbies"].Type);
67
 1
      Assert.AreEqual(JsonSchemaType.String, schema.Properties["hobbies"].Items[0].Type);
68
 1
    }
69

  
70
    [Test]
71
    public void MultipleTypes()
72
    {
73
 1
      string json = @"{
74
 1
  ""description"":""Age"",
75
 1
  ""type"":[""string"", ""integer""]
76
 1
}";
77

  
78
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
79
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
80

  
81
 1
      Assert.AreEqual("Age", schema.Description);
82
 1
      Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Integer, schema.Type);
83
 1
    }
84

  
85
    [Test]
86
    public void MultipleItems()
87
    {
88
 1
      string json = @"{
89
 1
  ""description"":""MultipleItems"",
90
 1
  ""type"":""array"",
91
 1
  ""items"": [{""type"":""string""},{""type"":""array""}]
92
 1
}";
93

  
94
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
95
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
96

  
97
 1
      Assert.AreEqual("MultipleItems", schema.Description);
98
 1
      Assert.AreEqual(JsonSchemaType.String, schema.Items[0].Type);
99
 1
      Assert.AreEqual(JsonSchemaType.Array, schema.Items[1].Type);
100
 1
    }
101

  
102
    [Test]
103
    public void AdditionalProperties()
104
    {
105
 1
      string json = @"{
106
 1
  ""description"":""AdditionalProperties"",
107
 1
  ""type"":[""string"", ""integer""],
108
 1
  ""additionalProperties"":{""type"":[""object"", ""boolean""]}
109
 1
}";
110

  
111
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
112
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
113

  
114
 1
      Assert.AreEqual("AdditionalProperties", schema.Description);
115
 1
      Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Boolean, schema.AdditionalProperties.Type);
116
 1
    }
117

  
118
    [Test]
119
    public void Optional()
120
    {
121
 1
      string json = @"{
122
 1
  ""description"":""Optional"",
123
 1
  ""optional"":true
124
 1
}";
125

  
126
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
127
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
128

  
129
 1
      Assert.AreEqual("Optional", schema.Description);
130
 1
      Assert.AreEqual(true, schema.Optional);
131
 1
    }
132

  
133
    [Test]
134
    public void ReadOnly()
135
    {
136
 1
      string json = @"{
137
 1
  ""description"":""ReadOnly"",
138
 1
  ""readonly"":true
139
 1
}";
140

  
141
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
142
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
143

  
144
 1
      Assert.AreEqual("ReadOnly", schema.Description);
145
 1
      Assert.AreEqual(true, schema.ReadOnly);
146
 1
    }
147

  
148
    [Test]
149
    public void Hidden()
150
    {
151
 1
      string json = @"{
152
 1
  ""description"":""Hidden"",
153
 1
  ""hidden"":true
154
 1
}";
155

  
156
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
157
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
158

  
159
 1
      Assert.AreEqual("Hidden", schema.Description);
160
 1
      Assert.AreEqual(true, schema.Hidden);
161
 1
    }
162

  
163
    [Test]
164
    public void Id()
165
    {
166
 1
      string json = @"{
167
 1
  ""description"":""Id"",
168
 1
  ""id"":""testid""
169
 1
}";
170

  
171
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
172
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
173

  
174
 1
      Assert.AreEqual("Id", schema.Description);
175
 1
      Assert.AreEqual("testid", schema.Id);
176
 1
    }
177

  
178
    [Test]
179
    public void Title()
180
    {
181
 1
      string json = @"{
182
 1
  ""description"":""Title"",
183
 1
  ""title"":""testtitle""
184
 1
}";
185

  
186
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
187
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
188

  
189
 1
      Assert.AreEqual("Title", schema.Description);
190
 1
      Assert.AreEqual("testtitle", schema.Title);
191
 1
    }
192

  
193
    [Test]
194
    public void Pattern()
195
    {
196
 1
      string json = @"{
197
 1
  ""description"":""Pattern"",
198
 1
  ""pattern"":""testpattern""
199
 1
}";
200

  
201
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
202
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
203

  
204
 1
      Assert.AreEqual("Pattern", schema.Description);
205
 1
      Assert.AreEqual("testpattern", schema.Pattern);
206
 1
    }
207

  
208
    [Test]
209
    public void Format()
210
    {
211
 1
      string json = @"{
212
 1
  ""description"":""Format"",
213
 1
  ""format"":""testformat""
214
 1
}";
215

  
216
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
217
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
218

  
219
 1
      Assert.AreEqual("Format", schema.Description);
220
 1
      Assert.AreEqual("testformat", schema.Format);
221
 1
    }
222

  
223
    [Test]
224
    public void Requires()
225
    {
226
 1
      string json = @"{
227
 1
  ""description"":""Requires"",
228
 1
  ""requires"":""PurpleMonkeyDishwasher""
229
 1
}";
230

  
231
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
232
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
233

  
234
 1
      Assert.AreEqual("Requires", schema.Description);
235
 1
      Assert.AreEqual("PurpleMonkeyDishwasher", schema.Requires);
236
 1
    }
237

  
238
    [Test]
239
    public void IdentitySingle()
240
    {
241
 1
      string json = @"{
242
 1
  ""description"":""Identity"",
243
 1
  ""identity"":""PurpleMonkeyDishwasher""
244
 1
}";
245

  
246
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
247
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
248

  
249
 1
      Assert.AreEqual("Identity", schema.Description);
250
 1
      Assert.AreEqual(1, schema.Identity.Count);
251
 1
      Assert.AreEqual("PurpleMonkeyDishwasher", schema.Identity[0]);
252
 1
    }
253

  
254
    [Test]
255
    public void IdentityMultiple()
256
    {
257
 1
      string json = @"{
258
 1
  ""description"":""Identity"",
259
 1
  ""identity"":[""PurpleMonkeyDishwasher"",""Antelope""]
260
 1
}";
261

  
262
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
263
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
264

  
265
 1
      Assert.AreEqual("Identity", schema.Description);
266
 1
      Assert.AreEqual(2, schema.Identity.Count);
267
 1
      Assert.AreEqual("PurpleMonkeyDishwasher", schema.Identity[0]);
268
 1
      Assert.AreEqual("Antelope", schema.Identity[1]);
269
 1
    }
270

  
271
    [Test]
272
    public void MinimumMaximum()
273
    {
274
 1
      string json = @"{
275
 1
  ""description"":""MinimumMaximum"",
276
 1
  ""minimum"":1.1,
277
 1
  ""maximum"":1.2,
278
 1
  ""minItems"":1,
279
 1
  ""maxItems"":2,
280
 1
  ""minLength"":5,
281
 1
  ""maxLength"":50,
282
 1
  ""maxDecimal"":3,
283
 1
}";
284

  
285
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
286
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
287

  
288
 1
      Assert.AreEqual("MinimumMaximum", schema.Description);
289
 1
      Assert.AreEqual(1.1, schema.Minimum);
290
 1
      Assert.AreEqual(1.2, schema.Maximum);
291
 1
      Assert.AreEqual(1, schema.MinimumItems);
292
 1
      Assert.AreEqual(2, schema.MaximumItems);
293
 1
      Assert.AreEqual(5, schema.MinimumLength);
294
 1
      Assert.AreEqual(50, schema.MaximumLength);
295
 1
      Assert.AreEqual(3, schema.MaximumDecimals);
296
 1
    }
297

  
298
    [Test]
299
    public void DisallowSingleType()
300
    {
301
 1
      string json = @"{
302
 1
  ""description"":""DisallowSingleType"",
303
 1
  ""disallow"":""string""
304
 1
}";
305

  
306
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
307
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
308

  
309
 1
      Assert.AreEqual("DisallowSingleType", schema.Description);
310
 1
      Assert.AreEqual(JsonSchemaType.String, schema.Disallow);
311
 1
    }
312

  
313
    [Test]
314
    public void DisallowMultipleTypes()
315
    {
316
 1
      string json = @"{
317
 1
  ""description"":""DisallowMultipleTypes"",
318
 1
  ""disallow"":[""string"",""number""]
319
 1
}";
320

  
321
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
322
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
323

  
324
 1
      Assert.AreEqual("DisallowMultipleTypes", schema.Description);
325
 1
      Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Float, schema.Disallow);
326
 1
    }
327

  
328
    [Test]
329
    public void DefaultPrimitiveType()
330
    {
331
 1
      string json = @"{
332
 1
  ""description"":""DefaultPrimitiveType"",
333
 1
  ""default"":1.1
334
 1
}";
335

  
336
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
337
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
338

  
339
 1
      Assert.AreEqual("DefaultPrimitiveType", schema.Description);
340
 1
      Assert.AreEqual(1.1, (double)schema.Default);
341
 1
    }
342

  
343
    [Test]
344
    public void DefaultComplexType()
345
    {
346
 1
      string json = @"{
347
 1
  ""description"":""DefaultComplexType"",
348
 1
  ""default"":{""pie"":true}
349
 1
}";
350

  
351
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
352
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
353

  
354
 1
      Assert.AreEqual("DefaultComplexType", schema.Description);
355
 1
      Assert.IsTrue(JToken.DeepEquals(JObject.Parse(@"{""pie"":true}"), schema.Default));
356
 1
    }
357

  
358
    [Test]
359
    public void Options()
360
    {
361
 1
      string json = @"{
362
 1
  ""description"":""NZ Island"",
363
 1
  ""type"":""string"",
364
 1
  ""options"":
365
 1
  [
366
 1
    {""value"":""NI"",""label"":""North Island""},
367
 1
    {""value"":""SI"",""label"":""South Island""}
368
 1
  ]
369
 1
}";
370

  
371
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
372
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
373

  
374
 1
      Assert.AreEqual("NZ Island", schema.Description);
375
 1
      Assert.AreEqual(JsonSchemaType.String, schema.Type);
376

  
377
 1
      Assert.AreEqual(2, schema.Options.Count);
378
 1
      Assert.AreEqual("North Island", schema.Options[new JValue("NI")]);
379
 1
      Assert.AreEqual("South Island", schema.Options[new JValue("SI")]);
380
 1
    }
381

  
382
    [Test]
383
    public void Enum()
384
    {
385
 1
      string json = @"{
386
 1
  ""description"":""Type"",
387
 1
  ""type"":[""string"",""array""],
388
 1
  ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
389
 1
}";
390

  
391
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
392
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
393

  
394
 1
      Assert.AreEqual("Type", schema.Description);
395
 1
      Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Array, schema.Type);
396

  
397
 1
      Assert.AreEqual(8, schema.Enum.Count);
398
 1
      Assert.AreEqual("string", (string)schema.Enum[0]);
399
 1
      Assert.AreEqual("any", (string)schema.Enum[schema.Enum.Count - 1]);
400
 1
    }
401

  
402
    [Test]
403
    public void CircularReference()
404
    {
405
 1
      string json = @"{
406
 1
  ""id"":""CircularReferenceArray"",
407
 1
  ""description"":""CircularReference"",
408
 1
  ""type"":[""array""],
409
 1
  ""items"":{""$ref"":""CircularReferenceArray""}
410
 1
}";
411

  
412
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
413
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
414

  
415
 1
      Assert.AreEqual("CircularReference", schema.Description);
416
 1
      Assert.AreEqual("CircularReferenceArray", schema.Id);
417
 1
      Assert.AreEqual(JsonSchemaType.Array, schema.Type);
418

  
419
 1
      Assert.AreEqual(schema, schema.Items[0]);
420
 1
    }
421

  
422
    [Test]
423
    [ExpectedException(typeof(Exception), ExpectedMessage = @"Could not resolve schema reference for Id 'MyUnresolvedReference'.")]
424
    public void UnresolvedReference()
425
    {
426
 1
      string json = @"{
427
 1
  ""id"":""CircularReferenceArray"",
428
 1
  ""description"":""CircularReference"",
429
 1
  ""type"":[""array""],
430
 1
  ""items"":{""$ref"":""MyUnresolvedReference""}
431
 1
}";
432

  
433
 1
      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
434
 1
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
435
 0
    }
436
  }
437
}