All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Schema / JsonSchemaModelBuilderTests.cs
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.IO;
29 using System.Linq;
30 using System.Text;
31 using Newtonsoft.Json.Schema;
32 using NUnit.Framework;
33
34 namespace Newtonsoft.Json.Tests.Schema
35 {
36   public class JsonSchemaModelBuilderTests : TestFixtureBase
37   {
38     [Test]
39     public void ExtendedComplex()
40     {
41       string first = @"{
42   ""id"":""first"",
43   ""type"":""object"",
44   ""properties"":
45   {
46     ""firstproperty"":{""type"":""string""},
47     ""secondproperty"":{""type"":""string"",""maxLength"":10},
48     ""thirdproperty"":{
49       ""type"":""object"",
50       ""properties"":
51       {
52         ""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
53       }
54     }
55   },
56   ""additionalProperties"":{}
57 }";
58
59       string second = @"{
60   ""id"":""second"",
61   ""type"":""object"",
62   ""extends"":{""$ref"":""first""},
63   ""properties"":
64   {
65     ""secondproperty"":{""type"":""any""},
66     ""thirdproperty"":{
67       ""extends"":{
68         ""properties"":
69         {
70           ""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
71         },
72         ""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
73       },
74       ""type"":""object"",
75       ""properties"":
76       {
77         ""thirdproperty_firstproperty"":{""pattern"":""hi""}
78       },
79       ""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
80     },
81     ""fourthproperty"":{""type"":""string""}
82   },
83   ""additionalProperties"":false
84 }";
85
86       JsonSchemaResolver resolver = new JsonSchemaResolver();
87       JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
88       JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
89
90       JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
91
92       JsonSchemaModel model = modelBuilder.Build(secondSchema);
93
94       Assert.AreEqual(4, model.Properties.Count);
95
96       Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);
97
98       Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
99       Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
100       Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
101       Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);
102
103       Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
104       Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
105       Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
106       Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
107       Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);
108
109       Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
110       Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
111       Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
112       Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
113       Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
114       Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
115       Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
116       Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
117       Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
118     }
119
120     [Test]
121     public void CircularReference()
122     {
123       string json = @"{
124   ""id"":""CircularReferenceArray"",
125   ""description"":""CircularReference"",
126   ""type"":[""array""],
127   ""items"":{""$ref"":""CircularReferenceArray""}
128 }";
129
130       JsonSchema schema = JsonSchema.Parse(json);
131
132       JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
133
134       JsonSchemaModel model = modelBuilder.Build(schema);
135
136       Assert.AreEqual(JsonSchemaType.Array, model.Type);
137
138       Assert.AreEqual(model, model.Items[0]);
139     }
140
141     [Test]
142     public void Required()
143     {
144       string schemaJson = @"{
145   ""description"":""A person"",
146   ""type"":""object"",
147   ""properties"":
148   {
149     ""name"":{""type"":""string""},
150     ""hobbies"":{""type"":""string"",required:true},
151     ""age"":{""type"":""integer"",required:true}
152   }
153 }";
154
155       JsonSchema schema = JsonSchema.Parse(schemaJson);
156       JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
157       JsonSchemaModel model = modelBuilder.Build(schema);
158
159       Assert.AreEqual(JsonSchemaType.Object, model.Type);
160       Assert.AreEqual(3, model.Properties.Count);
161       Assert.AreEqual(false, model.Properties["name"].Required);
162       Assert.AreEqual(true, model.Properties["hobbies"].Required);
163       Assert.AreEqual(true, model.Properties["age"].Required);
164     }
165   }
166 }