Skip to content

Commit 4bb8964

Browse files
authored
Merge pull request #1 from FINTmodels/suggestions-jarle
Suggestions for improvement
2 parents ab38df8 + a0106db commit 4bb8964

8 files changed

Lines changed: 89 additions & 51 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Xunit;
2+
using Newtonsoft.Json;
3+
using System.Diagnostics;
4+
5+
namespace FINT.Model.Resource.Test
6+
{
7+
public class AbstractCollectionResourcesTest
8+
{
9+
PersonResources personResources;
10+
public AbstractCollectionResourcesTest()
11+
{
12+
personResources = new PersonResources();
13+
personResources.AddResource(new PersonResource { Name = "test1" });
14+
personResources.AddResource(new PersonResource { Name = "test2" });
15+
}
16+
17+
[Fact]
18+
public void Get_Total_items()
19+
{
20+
Assert.Equal(2, personResources.TotalItems);
21+
}
22+
23+
[Fact]
24+
public void Serialize_Deserialize_PersonResources_getContent()
25+
{
26+
var json = JsonConvert.SerializeObject(personResources, Formatting.Indented);
27+
var deserialized = JsonConvert.DeserializeObject<PersonResources>(json);
28+
var deserializedContent = deserialized.GetContent();
29+
var names = deserializedContent.ConvertAll(p => p.Name);
30+
31+
Assert.Equal(2, deserializedContent.Count);
32+
Assert.True(names.Contains("test1"));
33+
Assert.True(names.Contains("test2"));
34+
}
35+
36+
}
37+
}

FINT.Model.Resource.Test/FINT.Model.Resource.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
99
<PackageReference Include="xunit" Version="2.2.0" />
1010
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
11+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1112
</ItemGroup>
1213
<ItemGroup>
1314
<ProjectReference Include="..\FINT.Model.Resource\FINT.Model.Resource.csproj">

FINT.Model.Resource.Test/LinkTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ public void Create_Link_with_PersonResource_placeholder()
99
{
1010
var link = Link.with(typeof(PersonResource), "/id");
1111

12-
Assert.Equal(link.href, "${test.person}/id");
12+
Assert.Equal("${test.person}/id", link.href);
1313
}
1414

1515
[Fact]
1616
public void Create_Link_with_Person_placeholder()
1717
{
1818
var link = Link.with(typeof(Person), "/id");
1919

20-
Assert.Equal(link.href, "${test.person}/id");
20+
Assert.Equal("${test.person}/id", link.href);
2121
}
2222
}
2323
}

FINT.Model.Resource.Test/Person.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
{
33
public class Person
44
{
5-
65
}
76
}

FINT.Model.Resource.Test/PersonResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public class PersonResource
44
{
5-
5+
public string Name { get; set; }
66
}
77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FINT.Model.Resource.Test
2+
{
3+
public class PersonResources : AbstractCollectionResources<PersonResource>
4+
{
5+
6+
}
7+
}

FINT.Model.Resource/AbstractCollectionResources.cs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,47 @@
44

55
namespace FINT.Model.Resource
66
{
7-
public abstract class AbstractCollectionResources<T>
7+
public abstract class AbstractCollectionResources<T>
8+
{
9+
[JsonProperty(PropertyName = "_links")]
10+
protected readonly Dictionary<string, List<Link>> Links = new Dictionary<string, List<Link>>();
11+
12+
[JsonProperty(PropertyName = "_embedded")]
13+
protected EmbeddedResources<T> Embedded = new EmbeddedResources<T>();
14+
15+
[JsonProperty(PropertyName = "total_items")]
16+
public int TotalItems
17+
{
18+
get
19+
{
20+
return Embedded.Entries.Count;
21+
}
22+
}
23+
24+
public void AddResource(T resource)
25+
{
26+
Embedded.Entries.Add(resource);
27+
}
28+
29+
public List<Link> GetSelfLinks()
830
{
9-
[JsonProperty(PropertyName = "_links")]
10-
protected readonly Dictionary<string, List<Link>> Links = new Dictionary<string, List<Link>>();
11-
12-
[JsonProperty(PropertyName = "_embedded")]
13-
protected EmbeddedResources<T> Embedded = new EmbeddedResources<T>();
14-
15-
[JsonProperty(PropertyName = "total_items")]
16-
public int TotalItems { get; set; }
17-
18-
19-
[OnSerialized]
20-
internal void OnSerializedMethod(StreamingContext context)
21-
{
22-
TotalItems = Embedded.Entries.Count;
23-
}
24-
25-
public void AddResource(T resource)
26-
{
27-
Embedded.Entries.Add(resource);
28-
}
29-
30-
public List<Link> GetSelfLinks()
31-
{
32-
return Links["self"];
33-
}
34-
35-
public List<T> GetContent()
36-
{
37-
return Embedded.Entries;
38-
}
39-
40-
public class EmbeddedResources<T>
41-
{
42-
public EmbeddedResources()
43-
{
44-
Entries = new List<T>();
45-
}
46-
47-
[JsonProperty(PropertyName = "_entries")]
48-
public List<T> Entries { get; set; }
49-
}
31+
return Links["self"];
32+
}
33+
34+
public List<T> GetContent()
35+
{
36+
return Embedded.Entries;
37+
}
38+
39+
public class EmbeddedResources<T>
40+
{
41+
public EmbeddedResources()
42+
{
43+
Entries = new List<T>();
44+
}
45+
46+
[JsonProperty(PropertyName = "_entries")]
47+
public List<T> Entries { get; set; }
5048
}
49+
}
5150
}

FINT.Model.Resource/FINT.Model.Resource.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
<Authors>FINT</Authors>
66
<PackageTags>fint</PackageTags>
77
</PropertyGroup>
8-
<ItemGroup>
9-
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
10-
<HintPath>..\..\..\..\.nuget\packages\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
11-
</Reference>
12-
</ItemGroup>
138
<ItemGroup>
149
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1510
</ItemGroup>

0 commit comments

Comments
 (0)