-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a84da9c
commit 3fccd01
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Sockets; | ||
using System.Text.Json; | ||
using NUnit.Framework; | ||
|
||
namespace Json.Schema.Tests | ||
{ | ||
[TestFixture] | ||
public class SelfValidationTest | ||
{ | ||
public static IEnumerable<TestCaseData> TestData => | ||
new[] | ||
{ | ||
new TestCaseData(nameof(MetaSchemas.Draft6), MetaSchemas.Draft6), | ||
new TestCaseData(nameof(MetaSchemas.Draft7), MetaSchemas.Draft7), | ||
new TestCaseData(nameof(MetaSchemas.Draft201909), MetaSchemas.Draft201909) | ||
}; | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public void Hardcoded(string testName, JsonSchema schema) | ||
{ | ||
var json = JsonSerializer.Serialize(schema); | ||
var validation = schema.Validate(JsonDocument.Parse(json).RootElement); | ||
|
||
validation.AssertValid(); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
[Ignore("Schema equality not a feature yet")] | ||
public void Online(string testName, JsonSchema schema) | ||
{ | ||
try | ||
{ | ||
var localSchemaJson = JsonSerializer.Serialize(schema); | ||
|
||
var onlineSchemaJson = new HttpClient().GetStringAsync(schema.Keywords.OfType<IdKeyword>().Single().Id).Result; | ||
var onlineSchema = JsonSerializer.Deserialize<JsonSchema>(onlineSchemaJson); | ||
|
||
var localValidation = schema.Validate(JsonDocument.Parse(onlineSchemaJson).RootElement); | ||
var onlineValidation = onlineSchema.Validate(JsonDocument.Parse(localSchemaJson).RootElement); | ||
|
||
try | ||
{ | ||
Console.WriteLine("Asserting schema equality"); | ||
Assert.AreEqual(onlineSchema, schema); | ||
|
||
Console.WriteLine("Validating local against online"); | ||
onlineValidation.AssertValid(); | ||
Console.WriteLine("Validating online against local"); | ||
localValidation.AssertValid(); | ||
|
||
Console.WriteLine("Asserting json equality"); | ||
Assert.AreEqual(onlineSchemaJson, localSchemaJson); | ||
} | ||
catch (Exception) | ||
{ | ||
Console.WriteLine("Online {0}", onlineSchemaJson); | ||
Console.WriteLine("Local {0}", localSchemaJson); | ||
throw; | ||
} | ||
} | ||
catch (WebException) | ||
{ | ||
Assert.Inconclusive(); | ||
} | ||
catch (HttpRequestException) | ||
{ | ||
Assert.Inconclusive(); | ||
} | ||
catch (SocketException) | ||
{ | ||
Assert.Inconclusive(); | ||
} | ||
catch (AggregateException e) | ||
{ | ||
if (e.InnerExceptions.OfType<WebException>().Any() || e.InnerExceptions.OfType<HttpRequestException>().Any()) | ||
Assert.Inconclusive(); | ||
throw; | ||
} | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
[Ignore("Schema equality not a feature yet")] | ||
public void RoundTrip(string testName, JsonSchema schema) | ||
{ | ||
var json = JsonSerializer.Serialize(schema, new JsonSerializerOptions{WriteIndented = true}); | ||
Console.WriteLine(json); | ||
var returnTrip = JsonSerializer.Deserialize<JsonSchema>(json); | ||
|
||
Assert.AreEqual(schema, returnTrip); | ||
} | ||
} | ||
} |