Skip to content

Commit 8d253e6

Browse files
author
Asgeir Nilsen
committed
Added Fact for testing JSON deserialization.
1 parent 892c96f commit 8d253e6

12 files changed

Lines changed: 688 additions & 480 deletions

.gitignore

Lines changed: 288 additions & 288 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.0</TargetFramework>
4-
<IsPackable>false</IsPackable>
5-
</PropertyGroup>
6-
<ItemGroup>
7-
<PackageReference Include="FINT.Model.Administrasjon" Version="2.7.0" />
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
9-
<PackageReference Include="xunit" Version="2.2.0" />
10-
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
11-
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
12-
</ItemGroup>
13-
<ItemGroup>
14-
<ProjectReference Include="..\FINT.Model.Resource\FINT.Model.Resource.csproj">
15-
<Project>{4224DD92-4BC5-460D-93B7-735838EEABDE}</Project>
16-
<Name>FINT.Model.Resource</Name>
17-
</ProjectReference>
18-
</ItemGroup>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="FINT.Model.Administrasjon" Version="2.7.0" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
9+
<PackageReference Include="xunit" Version="2.2.0" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
11+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<ProjectReference Include="..\FINT.Model.Resource\FINT.Model.Resource.csproj">
15+
<Project>{4224DD92-4BC5-460D-93B7-735838EEABDE}</Project>
16+
<Name>FINT.Model.Resource</Name>
17+
</ProjectReference>
18+
</ItemGroup>
1919
</Project>
Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,57 @@
1-
using Xunit;
2-
3-
namespace FINT.Model.Resource.Test
4-
{
5-
public class LinkTest
6-
{
7-
[Fact]
8-
public void Create_Link_with_PersonResource_placeholder()
9-
{
10-
var link = Link.with(typeof(PersonResource), "/id");
11-
12-
Assert.Equal("${test.person}/id", link.href);
13-
}
14-
15-
[Fact]
16-
public void Create_Link_with_Person_placeholder()
17-
{
18-
var link = Link.with(typeof(Person), "/id");
19-
20-
Assert.Equal("${test.person}/id", link.href);
21-
}
22-
}
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Diagnostics;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace FINT.Model.Resource.Test
8+
{
9+
public class LinkTest
10+
{
11+
private readonly ITestOutputHelper output;
12+
13+
public LinkTest(ITestOutputHelper output)
14+
{
15+
this.output = output;
16+
}
17+
18+
[Fact]
19+
public void Create_Link_with_PersonResource_placeholder()
20+
{
21+
var link = Link.with(typeof(PersonResource), "/id");
22+
23+
Assert.Equal("${test.person}/id", link.href);
24+
}
25+
26+
[Fact]
27+
public void Create_Link_with_Person_placeholder()
28+
{
29+
var link = Link.with(typeof(Person), "/id");
30+
31+
Assert.Equal("${test.person}/id", link.href);
32+
}
33+
34+
[Fact]
35+
public void Serialise_Link_with_Person_placeholder()
36+
{
37+
var link = Link.with(typeof(Person), "/id");
38+
39+
Assert.Equal("${test.person}/id", link.href);
40+
41+
output.WriteLine(ObjectDumper.Dump(link));
42+
var settings = new JsonSerializerSettings
43+
{
44+
ContractResolver = new LowercaseContractResolver()
45+
};
46+
var json = JsonConvert.SerializeObject(link, settings);
47+
48+
output.WriteLine(json);
49+
50+
var deserializeObject = JsonConvert.DeserializeObject<Link>(json);
51+
output.WriteLine(ObjectDumper.Dump(deserializeObject));
52+
Assert.NotNull(deserializeObject.href);
53+
Assert.Equal(link.href, deserializeObject.href);
54+
55+
}
56+
}
2357
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json.Serialization;
2+
3+
namespace FINT.Model.Resource.Test
4+
{
5+
internal class LowercaseContractResolver : DefaultContractResolver
6+
{
7+
protected override string ResolvePropertyName(string propertyName)
8+
{
9+
return propertyName.ToLower();
10+
}
11+
}
12+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Reflection;
5+
using System.Text;
6+
7+
namespace FINT.Model.Resource.Test
8+
{
9+
public class ObjectDumper
10+
{
11+
private int _level;
12+
private readonly int _indentSize;
13+
private readonly StringBuilder _stringBuilder;
14+
private readonly List<int> _hashListOfFoundElements;
15+
16+
private ObjectDumper(int indentSize)
17+
{
18+
_indentSize = indentSize;
19+
_stringBuilder = new StringBuilder();
20+
_hashListOfFoundElements = new List<int>();
21+
}
22+
23+
public static string Dump(object element)
24+
{
25+
return Dump(element, 2);
26+
}
27+
28+
public static string Dump(object element, int indentSize)
29+
{
30+
var instance = new ObjectDumper(indentSize);
31+
return instance.DumpElement(element);
32+
}
33+
34+
private string DumpElement(object element)
35+
{
36+
if (element == null || element is ValueType || element is string)
37+
{
38+
Write(FormatValue(element));
39+
}
40+
else
41+
{
42+
var objectType = element.GetType();
43+
if (!typeof(IEnumerable).IsAssignableFrom(objectType))
44+
{
45+
Write("{{{0}}}", objectType.FullName);
46+
_hashListOfFoundElements.Add(element.GetHashCode());
47+
_level++;
48+
}
49+
50+
var enumerableElement = element as IEnumerable;
51+
if (enumerableElement != null)
52+
{
53+
foreach (object item in enumerableElement)
54+
{
55+
if (item is IEnumerable && !(item is string))
56+
{
57+
_level++;
58+
DumpElement(item);
59+
_level--;
60+
}
61+
else
62+
{
63+
if (!AlreadyTouched(item))
64+
DumpElement(item);
65+
else
66+
Write("{{{0}}} <-- bidirectional reference found", item.GetType().FullName);
67+
}
68+
}
69+
}
70+
else
71+
{
72+
MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
73+
foreach (var memberInfo in members)
74+
{
75+
var fieldInfo = memberInfo as FieldInfo;
76+
var propertyInfo = memberInfo as PropertyInfo;
77+
78+
if (fieldInfo == null && propertyInfo == null)
79+
continue;
80+
81+
var type = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType;
82+
object value = fieldInfo != null
83+
? fieldInfo.GetValue(element)
84+
: propertyInfo.GetValue(element, null);
85+
86+
if (type.IsValueType || type == typeof(string))
87+
{
88+
Write("{0}: {1}", memberInfo.Name, FormatValue(value));
89+
}
90+
else
91+
{
92+
var isEnumerable = typeof(IEnumerable).IsAssignableFrom(type);
93+
Write("{0}: {1}", memberInfo.Name, isEnumerable ? "..." : "{ }");
94+
95+
var alreadyTouched = !isEnumerable && AlreadyTouched(value);
96+
_level++;
97+
if (!alreadyTouched)
98+
DumpElement(value);
99+
else
100+
Write("{{{0}}} <-- bidirectional reference found", value.GetType().FullName);
101+
_level--;
102+
}
103+
}
104+
}
105+
106+
if (!typeof(IEnumerable).IsAssignableFrom(objectType))
107+
{
108+
_level--;
109+
}
110+
}
111+
112+
return _stringBuilder.ToString();
113+
}
114+
115+
private bool AlreadyTouched(object value)
116+
{
117+
if (value == null)
118+
return false;
119+
120+
var hash = value.GetHashCode();
121+
for (var i = 0; i < _hashListOfFoundElements.Count; i++)
122+
{
123+
if (_hashListOfFoundElements[i] == hash)
124+
return true;
125+
}
126+
return false;
127+
}
128+
129+
private void Write(string value, params object[] args)
130+
{
131+
var space = new string(' ', _level * _indentSize);
132+
133+
if (args != null)
134+
value = string.Format(value, args);
135+
136+
_stringBuilder.AppendLine(space + value);
137+
}
138+
139+
private string FormatValue(object o)
140+
{
141+
if (o == null)
142+
return ("null");
143+
144+
if (o is DateTime)
145+
return (((DateTime)o).ToShortDateString());
146+
147+
if (o is string)
148+
return string.Format("\"{0}\"", o);
149+
150+
if (o is char && (char)o == '\0')
151+
return string.Empty;
152+
153+
if (o is ValueType)
154+
return (o.ToString());
155+
156+
if (o is IEnumerable)
157+
return ("...");
158+
159+
return ("{ }");
160+
}
161+
}
162+
}

FINT.Model.Resource.Test/Person.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace FINT.Model.Resource.Test
2-
{
3-
public class Person
4-
{
5-
}
1+
namespace FINT.Model.Resource.Test
2+
{
3+
public class Person
4+
{
5+
}
66
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
namespace FINT.Model.Resource.Test
2-
{
3-
public class PersonResource
4-
{
5-
public string Name { get; set; }
6-
}
1+
namespace FINT.Model.Resource.Test
2+
{
3+
public class PersonResource
4+
{
5+
public string Name { get; set; }
6+
}
77
}

FINT.Model.Resource.sln

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FINT.Model.Resource", "FINT.Model.Resource\FINT.Model.Resource.csproj", "{4224DD92-4BC5-460D-93B7-735838EEABDE}"
4-
EndProject
5-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FINT.Model.Resource.Test", "FINT.Model.Resource.Test\FINT.Model.Resource.Test.csproj", "{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}"
6-
EndProject
7-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B495DBFE-E971-490B-941F-ED1C79CB9FCA}"
8-
ProjectSection(SolutionItems) = preProject
9-
README.md = README.md
10-
.gitignore = .gitignore
11-
Jenkinsfile = Jenkinsfile
12-
version.txt = version.txt
13-
NuGet.Config = NuGet.Config
14-
EndProjectSection
15-
EndProject
16-
Global
17-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18-
Debug|Any CPU = Debug|Any CPU
19-
Release|Any CPU = Release|Any CPU
20-
EndGlobalSection
21-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22-
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23-
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
24-
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
25-
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Release|Any CPU.Build.0 = Release|Any CPU
26-
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27-
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
28-
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
29-
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Release|Any CPU.Build.0 = Release|Any CPU
30-
EndGlobalSection
31-
EndGlobal
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FINT.Model.Resource", "FINT.Model.Resource\FINT.Model.Resource.csproj", "{4224DD92-4BC5-460D-93B7-735838EEABDE}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FINT.Model.Resource.Test", "FINT.Model.Resource.Test\FINT.Model.Resource.Test.csproj", "{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}"
6+
EndProject
7+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B495DBFE-E971-490B-941F-ED1C79CB9FCA}"
8+
ProjectSection(SolutionItems) = preProject
9+
README.md = README.md
10+
.gitignore = .gitignore
11+
Jenkinsfile = Jenkinsfile
12+
version.txt = version.txt
13+
NuGet.Config = NuGet.Config
14+
EndProjectSection
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
Release|Any CPU = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{4224DD92-4BC5-460D-93B7-735838EEABDE}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{E60BCD77-6CFE-4338-B11E-A4A76A64A2DE}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)