Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Schema\JsonSchemaNodeTests.cs

Symbol Coverage: 100.00% (19 of 19)

Branch Coverage: 100.00% (3 of 3)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 62


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 Newtonsoft.Json.Schema;
31
using NUnit.Framework;
32

  
33
namespace Newtonsoft.Json.Tests.Schema
34
{
35
  public class JsonSchemaNodeTests : TestFixtureBase
36
  {
37
    [Test]
38
    public void AddSchema()
39
    {
40
 1
      string first = @"{
41
 1
  ""id"":""first"",
42
 1
  ""type"":""object"",
43
 1
  ""properties"":
44
 1
  {
45
 1
    ""firstproperty"":{""type"":""string"",""maxLength"":10},
46
 1
    ""secondproperty"":{
47
 1
      ""type"":""object"",
48
 1
      ""properties"":
49
 1
      {
50
 1
        ""secondproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
51
 1
      }
52
 1
    }
53
 1
  },
54
 1
  ""additionalProperties"":{}
55
 1
}";
56

  
57
 1
      string second = @"{
58
 1
  ""id"":""second"",
59
 1
  ""type"":""object"",
60
 1
  ""extends"":{""$ref"":""first""},
61
 1
  ""properties"":
62
 1
  {
63
 1
    ""firstproperty"":{""type"":""string""},
64
 1
    ""secondproperty"":{
65
 1
      ""extends"":{
66
 1
        ""properties"":
67
 1
        {
68
 1
          ""secondproperty_firstproperty"":{""maxLength"":9,""minLength"":6}
69
 1
        }
70
 1
      },
71
 1
      ""type"":""object"",
72
 1
      ""properties"":
73
 1
      {
74
 1
        ""secondproperty_firstproperty"":{}
75
 1
      }
76
 1
    },
77
 1
    ""thirdproperty"":{""type"":""string""}
78
 1
  },
79
 1
  ""additionalProperties"":false
80
 1
}";
81

  
82
 1
      JsonSchemaResolver resolver = new JsonSchemaResolver();
83
 1
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
84
 1
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
85

  
86
 1
      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
87

  
88
 1
      JsonSchemaNode node = modelBuilder.AddSchema(null, secondSchema);
89

  
90
 1
      Assert.AreEqual(2, node.Schemas.Count);
91
 1
      Assert.AreEqual(2, node.Properties["firstproperty"].Schemas.Count);
92
 1
      Assert.AreEqual(3, node.Properties["secondproperty"].Schemas.Count);
93
 1
      Assert.AreEqual(3, node.Properties["secondproperty"].Properties["secondproperty_firstproperty"].Schemas.Count);
94
 1
    }
95

  
96
    [Test]
97
    public void CircularReference()
98
    {
99
 1
      string json = @"{
100
 1
  ""id"":""CircularReferenceArray"",
101
 1
  ""description"":""CircularReference"",
102
 1
  ""type"":[""array""],
103
 1
  ""items"":{""$ref"":""CircularReferenceArray""}
104
 1
}";
105

  
106
 1
      JsonSchema schema = JsonSchema.Parse(json);
107

  
108
 1
      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
109

  
110
 1
      JsonSchemaNode node = modelBuilder.AddSchema(null, schema);
111

  
112
 1
      Assert.AreEqual(1, node.Schemas.Count);
113

  
114
 1
      Assert.AreEqual(node, node.Items[0]);
115
 1
    }
116

  
117
  }
118
}