forked from Hochfrequenz/BO4E-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestJsonSchemaGeneration.cs
60 lines (52 loc) · 1.83 KB
/
TestJsonSchemaGeneration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using BO4E.BO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Schema;
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace TestBO4E
{
[TestClass]
public class TestJsonSchemaGeneration
{
[TestMethod]
public void BasicTest()
{
Messlokation melo = new Messlokation();
var result = melo.GetJsonScheme().ToString();
Energiemenge em = new Energiemenge();
result = em.GetJsonScheme().ToString();
string result2 = BusinessObject.GetJsonSchema(typeof(Energiemenge)).ToString();
Assert.AreEqual(result, result2);
}
[TestMethod]
public void NegativeTest()
{
Assert.ThrowsException<ArgumentException>(() => BusinessObject.GetJsonSchema(typeof(string)), "Illegal types must result in a ArgumentException.");
}
[TestMethod]
public void TestJSchemaFileGenerationBo()
{
try
{
foreach (var type in typeof(BusinessObject).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(BusinessObject))).Reverse())
{
var schema = BusinessObject.GetJsonSchema(type);
Assert.IsNotNull(schema);
string path = $"../../../../json-schema-files/{type}.json"; // not elegant but ok ;)
if (!File.Exists(path))
{
var stream = File.Create(path);
stream.Close();
}
File.WriteAllText(path, schema.ToString(SchemaVersion.Draft7), Encoding.UTF8);
}
}
catch (JSchemaException)
{
// thats life. pay for it if you'd like to :P
}
}
}
}