Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Schema\JsonSchemaModelBuilderTests.cs

Symbol Coverage: 100.00% (45 of 45)

Branch Coverage: 100.00% (4 of 4)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 100


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.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
 1
      string first = @"{
42
 1
  ""id"":""first"",
43
 1
  ""type"":""object"",
44
 1
  ""properties"":
45
 1
  {
46
 1
    ""firstproperty"":{""type"":""string""},
47
 1
    ""secondproperty"":{""type"":""string"",""maxLength"":10},
48
 1
    ""thirdproperty"":{
49
 1
      ""type"":""object"",
50
 1
      ""properties"":
51
 1
      {
52
 1
        ""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
53
 1
      }
54
 1
    }
55
 1
  },
56
 1
  ""additionalProperties"":{}
57
 1
}";
58

  
59
 1
      string second = @"{
60
 1
  ""id"":""second"",
61
 1
  ""type"":""object"",
62
 1
  ""extends"":{""$ref"":""first""},
63
 1
  ""properties"":
64
 1
  {
65
 1
    ""secondproperty"":{""type"":""any""},
66
 1
    ""thirdproperty"":{
67
 1
      ""extends"":{
68
 1
        ""properties"":
69
 1
        {
70
 1
          ""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
71
 1
        },
72
 1
        ""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
73
 1
      },
74
 1
      ""type"":""object"",
75
 1
      ""properties"":
76
 1
      {
77
 1
        ""thirdproperty_firstproperty"":{""pattern"":""hi""}
78
 1
      },
79
 1
      ""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
80
 1
    },
81
 1
    ""fourthproperty"":{""type"":""string""}
82
 1
  },
83
 1
  ""additionalProperties"":false
84
 1
}";
85

  
86
 1
      JsonSchemaResolver resolver = new JsonSchemaResolver();
87
 1
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
88
 1
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
89

  
90
 1
      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
91

  
92
 1
      JsonSchemaModel model = modelBuilder.Build(secondSchema);
93

  
94
 1
      Assert.AreEqual(4, model.Properties.Count);
95

  
96
 1
      Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);
97

  
98
 1
      Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
99
 1
      Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
100
 1
      Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
101
 1
      Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);
102

  
103
 1
      Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
104
 1
      Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
105
 1
      Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
106
 1
      Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
107
 1
      Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);
108

  
109
 1
      Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
110
 1
      Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
111
 1
      Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
112
 1
      Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
113
 1
      Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
114
 1
      Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
115
 1
      Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
116
 1
      Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
117
 1
      Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
118
 1
    }
119

  
120
    [Test]
121
    public void CircularReference()
122
    {
123
 1
      string json = @"{
124
 1
  ""id"":""CircularReferenceArray"",
125
 1
  ""description"":""CircularReference"",
126
 1
  ""type"":[""array""],
127
 1
  ""items"":{""$ref"":""CircularReferenceArray""}
128
 1
}";
129

  
130
 1
      JsonSchema schema = JsonSchema.Parse(json);
131

  
132
 1
      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
133

  
134
 1
      JsonSchemaModel model = modelBuilder.Build(schema);
135

  
136
 1
      Assert.AreEqual(JsonSchemaType.Array, model.Type);
137

  
138
 1
      Assert.AreEqual(model, model.Items[0]);
139
 1
    }
140

  
141
    [Test]
142
    public void Optional()
143
    {
144
 1
      string schemaJson = @"{
145
 1
  ""description"":""A person"",
146
 1
  ""type"":""object"",
147
 1
  ""properties"":
148
 1
  {
149
 1
    ""name"":{""type"":""string""},
150
 1
    ""hobbies"":{""type"":""string"",optional:true},
151
 1
    ""age"":{""type"":""integer"",optional:true}
152
 1
  }
153
 1
}";
154

  
155
 1
      JsonSchema schema = JsonSchema.Parse(schemaJson);
156
 1
      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
157
 1
      JsonSchemaModel model = modelBuilder.Build(schema);
158

  
159
 1
      Assert.AreEqual(JsonSchemaType.Object, model.Type);
160
 1
      Assert.AreEqual(3, model.Properties.Count);
161
 1
      Assert.AreEqual(false, model.Properties["name"].Optional);
162
 1
      Assert.AreEqual(true, model.Properties["hobbies"].Optional);
163
 1
      Assert.AreEqual(true, model.Properties["age"].Optional);
164
 1
    }
165
  }
166
}