forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkTemplateTests.cs
More file actions
68 lines (58 loc) · 1.88 KB
/
Copy pathLinkTemplateTests.cs
File metadata and controls
68 lines (58 loc) · 1.88 KB
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
61
62
63
64
65
66
67
68
using JSONAPI.Attributes;
using JSONAPI.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.IO;
using System.Text;
using JSONAPI.Core;
namespace JSONAPI.Tests.Json
{
[TestClass]
public class LinkTemplateTests
{
private class Post
{
public string Id { get; set; }
public string Title { get; set; }
[SerializeAs(SerializeAsOptions.RelatedLink)]
[LinkTemplate("/users/{0}")]
public virtual User Author { get; set; }
}
private class User
{
public string Id { get; set; }
public string Name { get; set; }
}
private Post ThePost { get; set; }
[TestInitialize]
public void SetupModels()
{
ThePost = new Post
{
Id = "2",
Title = "How to fry an egg",
Author = new User
{
Id = "5",
Name = "Bob"
}
};
}
[TestMethod]
[DeploymentItem(@"Data\LinkTemplateTest.json")]
public void GetResourceWithLinkTemplateRelationship()
{
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Post));
modelManager.RegisterResourceType(typeof(User));
var formatter = new JsonApiFormatter(modelManager);
var stream = new MemoryStream();
formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);
// Assert
var expected = JsonHelpers.MinifyJson(File.ReadAllText("LinkTemplateTest.json"));
var output = Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
Assert.AreEqual(output.Trim(), expected);
}
}
}