|
| 1 | +using FluentAssertions; |
| 2 | +using JsonApiSerializer; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Pubg.Net.Infrastructure.JsonConverters; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Pubg.Net.UnitTests.JsonConverters |
| 10 | +{ |
| 11 | + public class RelationshipIdConverterTests |
| 12 | + { |
| 13 | + public class TestArticle |
| 14 | + { |
| 15 | + [JsonProperty] |
| 16 | + public int Id { get; set; } |
| 17 | + |
| 18 | + [JsonProperty] |
| 19 | + public string Title { get; set; } |
| 20 | + |
| 21 | + [JsonProperty] |
| 22 | + public string Body { get; set; } |
| 23 | + |
| 24 | + [JsonProperty] |
| 25 | + public DateTime Created { get; set; } |
| 26 | + |
| 27 | + [JsonProperty] |
| 28 | + public DateTime Updated { get; set; } |
| 29 | + |
| 30 | + [JsonProperty("author")] |
| 31 | + [JsonConverter(typeof(RelationshipIdConverter))] |
| 32 | + public IEnumerable<string> AuthorIds { get; set; } |
| 33 | + } |
| 34 | + |
| 35 | + string jsonApiLiteral = |
| 36 | + @"{ |
| 37 | + ""data"": |
| 38 | + { |
| 39 | + ""type"": ""TestArticle"", |
| 40 | + ""id"": ""1"", |
| 41 | + ""attributes"": |
| 42 | + { |
| 43 | + ""title"": ""JSON API paints my bikeshed!"", |
| 44 | + ""body"": ""The shortest article. Ever."", |
| 45 | + ""created"": ""2015-05-22T14:56:29.000Z"", |
| 46 | + ""updated"": ""2015-05-22T14:56:28.000Z"" |
| 47 | + }, |
| 48 | + ""relationships"": |
| 49 | + { |
| 50 | + ""author"": |
| 51 | + { |
| 52 | + ""data"": |
| 53 | + [ |
| 54 | + {""id"": 42, ""type"": ""people""}, |
| 55 | + {""id"": 43, ""type"": ""people""}, |
| 56 | + {""id"": 44, ""type"": ""people""}, |
| 57 | + {""id"": 45, ""type"": ""people""} |
| 58 | + ] |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }"; |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void RelationshipIdConverter_Converts_FromJsonApiFormat() |
| 66 | + { |
| 67 | + TestArticle testArticle = JsonConvert.DeserializeObject<TestArticle>(jsonApiLiteral, new JsonApiSerializerSettings()); |
| 68 | + |
| 69 | + testArticle.Id.Should().Equals(1); |
| 70 | + testArticle.Title.Should().Equals("JSON API paints my bikeshed!"); |
| 71 | + testArticle.Body.Should().Equals("The shortest article. Ever."); |
| 72 | + testArticle.Created.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:29.000Z")); |
| 73 | + testArticle.Updated.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:28.000Z")); |
| 74 | + testArticle.AuthorIds.Should().NotBeNullOrEmpty(); |
| 75 | + testArticle.AuthorIds.Should().Contain(new [] { "42", "43", "44", "45" }); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void RelationshipIdConverter_Converts_FromJsonApiFormat_MultipleTimes() |
| 80 | + { |
| 81 | + TestArticle testArticle = JsonConvert.DeserializeObject<TestArticle>(jsonApiLiteral, new JsonApiSerializerSettings()); |
| 82 | + |
| 83 | + var reserializedArticle = JsonConvert.SerializeObject(testArticle); |
| 84 | + var deserializedArticle = JsonConvert.DeserializeObject<TestArticle>(reserializedArticle); |
| 85 | + |
| 86 | + deserializedArticle.Id.Should().Equals(1); |
| 87 | + deserializedArticle.Title.Should().Equals("JSON API paints my bikeshed!"); |
| 88 | + deserializedArticle.Body.Should().Equals("The shortest article. Ever."); |
| 89 | + deserializedArticle.Created.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:29.000Z")); |
| 90 | + deserializedArticle.Updated.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:28.000Z")); |
| 91 | + deserializedArticle.AuthorIds.Should().NotBeNullOrEmpty(); |
| 92 | + deserializedArticle.AuthorIds.Should().Contain(new[] { "42", "43", "44", "45" }); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void RelationshipIdConverter_Converts_FromApplicationJson() |
| 97 | + { |
| 98 | + var baseArticle = new TestArticle |
| 99 | + { |
| 100 | + Id = 1, |
| 101 | + Title = "adsd", |
| 102 | + Body = "asda", |
| 103 | + Created = Convert.ToDateTime("2015-05-22T14:56:29.000Z"), |
| 104 | + Updated = Convert.ToDateTime("2015-05-22T14:56:29.000Z"), |
| 105 | + AuthorIds = new[] { "11", "12" } |
| 106 | + }; |
| 107 | + |
| 108 | + var serializedArticle = JsonConvert.SerializeObject(baseArticle); |
| 109 | + |
| 110 | + var deSerialzedArticle = JsonConvert.DeserializeObject<TestArticle>(serializedArticle); |
| 111 | + |
| 112 | + deSerialzedArticle.Id.Should().Equals(baseArticle.Id); |
| 113 | + deSerialzedArticle.Title.Should().Equals(baseArticle.Title); |
| 114 | + deSerialzedArticle.Body.Should().Equals(baseArticle.Body); |
| 115 | + deSerialzedArticle.Created.Should().Equals(baseArticle.Created); |
| 116 | + deSerialzedArticle.Updated.Should().Equals(baseArticle.Updated); |
| 117 | + deSerialzedArticle.AuthorIds.Should().NotBeNullOrEmpty(); |
| 118 | + deSerialzedArticle.AuthorIds.Should().Contain(baseArticle.AuthorIds); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments