public void PublicConstructor(string resourceProviderID) { if (resourceProviderID is null) { Assert.Throws <ArgumentNullException>(() => { ResourceIdentifier myResource = ResourceIdentifier.Create(resourceProviderID); }); } else { ResourceIdentifier myResource = ResourceIdentifier.Create(resourceProviderID); Assert.AreEqual(myResource.ToString(), resourceProviderID); } }
public void When_id_null_should_serialize_as_string() { var root = DocumentRoot.Create(new { Id = (int?)null, Type = "articles", Title = "My title", Author = ResourceIdentifier.Create(new { Id = 7357, //int relationship Type = "person", Name = "Scribble McPen" }) }); var json = JsonConvert.SerializeObject(root, new JsonApiSerializerSettings() { NullValueHandling = NullValueHandling.Include, Formatting = Formatting.Indented }); var expectedjson = @"{ ""data"": { ""id"": null, ""type"": ""articles"", ""attributes"": { ""title"": ""My title"" }, ""relationships"": { ""author"": { ""data"": { ""id"": ""7357"", ""type"": ""person"" } } } }, ""included"": [ { ""id"": ""7357"", ""type"": ""person"", ""attributes"": { ""name"": ""Scribble McPen"" } } ] }"; Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance); }
public void When_id_int_in_explicit_identifier_should_serialize_as_string() { var root = DocumentRoot.Create(new { Id = 7357, //int id Type = "articles", Title = "My title", Author = ResourceIdentifier.Create(new { Id = 7357, //int relationship Type = "person", Name = "Scribble McPen" }) }); var json = JsonConvert.SerializeObject(root, settings); var expectedjson = @"{ ""data"": { ""id"": ""7357"", ""type"": ""articles"", ""attributes"": { ""title"": ""My title"" }, ""relationships"": { ""author"": { ""data"": { ""id"": ""7357"", ""type"": ""person"" } } } }, ""included"": [ { ""id"": ""7357"", ""type"": ""person"", ""attributes"": { ""name"": ""Scribble McPen"" } } ] }"; Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance); }
public void When_resource_identifier_should_serialize() { var root = new DocumentRoot <ArticleWithResourceIdentifier> { Data = new ArticleWithResourceIdentifier { Id = "1234", Title = "My Article", Author = new ResourceIdentifier <Person>() { Meta = new Meta { { "primary-author", true } }, Value = new Person { Id = "333", FirstName = "John", LastName = "Smith", Twitter = "jsmi", Meta = new Meta() { { "best-sellers", 4 } } } }, Comments = new List <ResourceIdentifier <Comment> > { ResourceIdentifier.Create(new Comment() { Id = "57", Body = "I give it a 5 out of 7" }) } } }; var json = JsonConvert.SerializeObject(root, settings); var expectedjson = @"{ ""data"": { ""id"": ""1234"", ""type"": ""articles"", ""attributes"": { ""title"": ""My Article"" }, ""relationships"": { ""author"": { ""data"": { ""id"":""333"", ""type"":""people"", ""meta"":{ ""primary-author"": true} } }, ""comments"": { ""data"": [{ ""id"":""57"", ""type"":""comments"", }] } } }, ""included"" : [ { ""id"": ""333"", ""type"": ""people"", ""attributes"":{ ""first-name"": ""John"", ""last-name"": ""Smith"", ""twitter"": ""jsmi"" }, ""meta"":{ ""best-sellers"": 4} }, { ""id"": ""57"", ""type"": ""comments"", ""attributes"":{ ""body"": ""I give it a 5 out of 7"" } } ] }"; Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance); }
public void InvalidRPIds(string invalidID) { Assert.Throws <ArgumentOutOfRangeException>(() => { ResourceIdentifier subject = invalidID; }); Assert.Throws <ArgumentOutOfRangeException>(() => { ResourceIdentifier subject = ResourceIdentifier.Create(invalidID); }); }