|
| 1 | +using JSONAPI.Attributes; |
| 2 | +using JSONAPI.Json; |
| 3 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace JSONAPI.Tests.Json |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class LinkTemplateTests |
| 12 | + { |
| 13 | + private class Post |
| 14 | + { |
| 15 | + public string Id { get; set; } |
| 16 | + |
| 17 | + public string Title { get; set; } |
| 18 | + |
| 19 | + [SerializeAs(SerializeAsOptions.Link)] |
| 20 | + [LinkTemplate("/users/{0}")] |
| 21 | + public virtual User Author { get; set; } |
| 22 | + } |
| 23 | + |
| 24 | + private class User |
| 25 | + { |
| 26 | + public string Id { get; set; } |
| 27 | + |
| 28 | + public string Name { get; set; } |
| 29 | + } |
| 30 | + |
| 31 | + private Post ThePost { get; set; } |
| 32 | + |
| 33 | + [TestInitialize] |
| 34 | + public void SetupModels() |
| 35 | + { |
| 36 | + ThePost = new Post |
| 37 | + { |
| 38 | + Id = "2", |
| 39 | + Title = "How to fry an egg", |
| 40 | + Author = new User |
| 41 | + { |
| 42 | + Id = "5", |
| 43 | + Name = "Bob" |
| 44 | + } |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + [DeploymentItem(@"Data\LinkTemplateTest.json")] |
| 50 | + public void GetResourceWithLinkTemplateRelationship() |
| 51 | + { |
| 52 | + var formatter = new JsonApiFormatter |
| 53 | + { |
| 54 | + PluralizationService = new JSONAPI.Core.PluralizationService() |
| 55 | + }; |
| 56 | + var stream = new MemoryStream(); |
| 57 | + |
| 58 | + formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null); |
| 59 | + |
| 60 | + // Assert |
| 61 | + var expected = JsonHelpers.MinifyJson(File.ReadAllText("LinkTemplateTest.json")); |
| 62 | + var output = Encoding.ASCII.GetString(stream.ToArray()); |
| 63 | + Trace.WriteLine(output); |
| 64 | + Assert.AreEqual(output.Trim(), expected); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments