public void PropertyWasPresentTest()
        {
            using (var inputStream = File.OpenRead("MetadataManagerPropertyWasPresentRequest.json"))
            {
                // Arrange
                var modelManager = new ModelManager(new PluralizationService());
                modelManager.RegisterResourceType(typeof(Post));
                modelManager.RegisterResourceType(typeof(Author));
                JsonApiFormatter formatter = new JsonApiFormatter(modelManager);

                var p = (Post) formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;

                // Act
                bool idWasSet = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Id"));
                bool titleWasSet = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Title"));
                bool authorWasSet = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Author"));
                bool commentsWasSet = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Comments"));

                // Assert
                Assert.IsTrue(idWasSet, "Id was not reported as set, but was.");
                Assert.IsFalse(titleWasSet, "Title was reported as set, but was not.");
                Assert.IsTrue(authorWasSet, "Author was not reported as set, but was.");
                Assert.IsFalse(commentsWasSet, "Comments was reported as set, but was not.");
            }
        }
示例#2
0
        public async Task UnderpostingTest()
        {
            // Arrange
            JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
            MemoryStream     stream    = new MemoryStream();

            EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context);

            string underpost = @"{""posts"":{""id"":""" + p.Id.ToString() + @""",""title"":""Not at all linkbait!""}}";

            stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(underpost));

            int previousCommentsCount = p.Comments.Count;

            // Act
            Post pUpdated;

            pUpdated = (Post)await formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent) null, (System.Net.Http.Formatting.IFormatterLogger) null);

            pUpdated = await materializer.MaterializeUpdateAsync <Post>(pUpdated);

            // Assert
            Assert.AreEqual(previousCommentsCount, pUpdated.Comments.Count, "Comments were wiped out!");
            Assert.AreEqual("Not at all linkbait!", pUpdated.Title, "Title was not updated.");
        }
        public void PropertyWasPresentTest()
        {
            // Arrange

            JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
            MemoryStream     stream    = new MemoryStream();

            stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""posts"":{""id"":42,""links"":{""author"":""18""}}}"));

            Post p;

            p = (Post)formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

            // Act
            bool idWasSet       = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Id"));
            bool titleWasSet    = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Title"));
            bool authorWasSet   = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Author"));
            bool commentsWasSet = MetadataManager.Instance.PropertyWasPresent(p, p.GetType().GetProperty("Comments"));

            // Assert
            Assert.IsTrue(idWasSet, "Id was not reported as set, but was.");
            Assert.IsFalse(titleWasSet, "Title was reported as set, but was not.");
            Assert.IsTrue(authorWasSet, "Author was not reported as set, but was.");
            Assert.IsFalse(commentsWasSet, "Comments was reported as set, but was not.");
        }
示例#4
0
        public async Task DeserializePostIntegrationTest()
        {
            // Arrange
            JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
            MemoryStream     stream    = new MemoryStream();

            EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context);

            // Serialize a post and change the JSON... Not "unit" at all, I know, but a good integration test I think...
            formatter.WriteToStreamAsync(typeof(Post), p, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
            string serializedPost = System.Text.Encoding.ASCII.GetString(stream.ToArray());

            // Change the post title (a scalar value)
            serializedPost = serializedPost.Replace("Linkbait!", "Not at all linkbait!");
            // Remove a comment (Note that order is undefined/not deterministic!)
            serializedPost = Regex.Replace(serializedPost, String.Format(@"(""comments""\s*:\s*\[[^]]*)(,""{0}""|""{0}"",)", c3.Id), @"$1");

            // Reread the serialized JSON...
            stream.Dispose();
            stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(serializedPost));

            // Act
            Post pUpdated;

            pUpdated = (Post)await formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent) null, (System.Net.Http.Formatting.IFormatterLogger) null);

            pUpdated = await materializer.MaterializeUpdateAsync <Post>(pUpdated);

            // Assert
            Assert.AreEqual(a, pUpdated.Author);
            Assert.AreEqual("Not at all linkbait!", pUpdated.Title);
            Assert.AreEqual(2, pUpdated.Comments.Count());
            Assert.IsFalse(pUpdated.Comments.Contains(c3));
            //Debug.WriteLine(sw.ToString());
        }
示例#5
0
        public void DeserializeNonStandardIdTest()
        {
            var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
            var stream    = new FileStream("NonStandardIdTest.json", FileMode.Open);

            // Act
            IList <NonStandardIdThing> things;

            things = (IList <NonStandardIdThing>)formatter.ReadFromStreamAsync(typeof(NonStandardIdThing), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;
            stream.Close();

            // Assert
            things.Count.Should().Be(1);
            things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
        }
示例#6
0
        public void DeserializeExtraRelationshipTest()
        {
            JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
            MemoryStream     stream    = new MemoryStream();

            stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""authors"":{""id"":13,""name"":""Jason Hater"",""links"":{""posts"":[],""bogus"":[""PANIC!""]}}}"));

            // Act
            Author a;

            a = (Author)formatter.ReadFromStreamAsync(typeof(Author), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

            // Assert
            Assert.AreEqual("Jason Hater", a.Name); // Completed without exceptions and didn't timeout!
        }
示例#7
0
        public void DeserializeNonStandardIdWithoutId()
        {
            var    formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
            string json      = File.ReadAllText("NonStandardIdTest.json");

            json = Regex.Replace(json, @"""id"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,", ""); // remove the uuid attribute
            var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));

            // Act
            IList <NonStandardIdThing> things;

            things = (IList <NonStandardIdThing>)formatter.ReadFromStreamAsync(typeof(NonStandardIdThing), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

            // Assert
            json.Should().NotContain("\"id\"", "The \"id\" attribute was supposed to be removed, test methodology problem!");
            things.Count.Should().Be(1);
            things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
        }
示例#8
0
        public void DeserializeCollectionIntegrationTest()
        {
            // Arrange
            JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
            MemoryStream     stream    = new MemoryStream();

            formatter.WriteToStreamAsync(typeof(Post), new List <Post> {
                p, p2
            }, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
            stream.Seek(0, SeekOrigin.Begin);

            // Act
            IList <Post> posts;

            posts = (IList <Post>)formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

            // Assert
            Assert.AreEqual(2, posts.Count);
            Assert.AreEqual(p.Id, posts[0].Id); // Order matters, right?
        }
        public void DeserializeNonStandardId()
        {
            var modelManager = new ModelManager(new PluralizationService());
            modelManager.RegisterResourceType(typeof(NonStandardIdThing));
            var formatter = new JsonApiFormatter(modelManager);
            string json = File.ReadAllText("NonStandardIdTest.json");
            json = Regex.Replace(json, @"""uuid"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,",""); // remove the uuid attribute
            var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));

            // Act
            IList<NonStandardIdThing> things;
            things = (IList<NonStandardIdThing>)formatter.ReadFromStreamAsync(typeof(NonStandardIdThing), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

            // Assert
            json.Should().NotContain("uuid", "The \"uuid\" attribute was supposed to be removed, test methodology problem!");
            things.Count.Should().Be(1);
            things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
        }
        public void DeserializeNonStandardIdTest()
        {
            var modelManager = new ModelManager(new PluralizationService());
            modelManager.RegisterResourceType(typeof(NonStandardIdThing));
            var formatter = new JsonApiFormatter(modelManager);
            var stream = new FileStream("NonStandardIdTest.json",FileMode.Open);

            // Act
            IList<NonStandardIdThing> things;
            things = (IList<NonStandardIdThing>)formatter.ReadFromStreamAsync(typeof(NonStandardIdThing), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;
            stream.Close();

            // Assert
            things.Count.Should().Be(1);
            things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
        }
        public void DeserializeExtraRelationshipTest()
        {
            // Arrange
            var modelManager = new ModelManager(new PluralizationService());
            modelManager.RegisterResourceType(typeof(Author));
            var formatter = new JsonApiFormatter(modelManager);
            MemoryStream stream = new MemoryStream();

            stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""data"":{""id"":13,""type"":""authors"",""attributes"":{""name"":""Jason Hater""},""relationships"":{""posts"":{""data"":[]},""bogus"":{""data"":[]}}}}"));

            // Act
            Author a;
            a = (Author)formatter.ReadFromStreamAsync(typeof(Author), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

            // Assert
            Assert.AreEqual("Jason Hater", a.Name); // Completed without exceptions and didn't timeout!
        }
        public async Task DeserializeRawJsonTest()
        {
            using (var inputStream = File.OpenRead("DeserializeRawJsonTest.json"))
            {
                // Arrange
                var modelManager = new ModelManager(new PluralizationService());
                modelManager.RegisterResourceType(typeof(Comment));
                var formatter = new JsonApiFormatter(modelManager);

                // Act
                var comments = ((IEnumerable<Comment>)await formatter.ReadFromStreamAsync(typeof (Comment), inputStream, null, null)).ToArray();

                // Assert
                Assert.AreEqual(2, comments.Count());
                Assert.AreEqual(null, comments[0].CustomData);
                Assert.AreEqual("{\"foo\":\"bar\"}", comments[1].CustomData);
            }
        }
        public async Task Deserializes_attributes_properly()
        {
            using (var inputStream = File.OpenRead("DeserializeAttributeRequest.json"))
            {
                // Arrange
                var modelManager = new ModelManager(new PluralizationService());
                modelManager.RegisterResourceType(typeof(Sample));
                var formatter = new JsonApiFormatter(modelManager);

                // Act
                var deserialized = (IList<Sample>)await formatter.ReadFromStreamAsync(typeof(Sample), inputStream, null, null);

                // Assert
                deserialized.Count.Should().Be(2);
                deserialized[0].ShouldBeEquivalentTo(s1);
                deserialized[1].ShouldBeEquivalentTo(s2);
            }
        }
        public void Deserializes_collections_properly()
        {
            using (var inputStream = File.OpenRead("DeserializeCollectionRequest.json"))
            {
                // Arrange
                var modelManager = new ModelManager(new PluralizationService());
                modelManager.RegisterResourceType(typeof(Post));
                modelManager.RegisterResourceType(typeof(Author));
                modelManager.RegisterResourceType(typeof(Comment));
                var formatter = new JsonApiFormatter(modelManager);

                // Act
                var posts = (IList<Post>)formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;

                // Assert
                posts.Count.Should().Be(2);
                posts[0].Id.Should().Be(p.Id);
                posts[0].Title.Should().Be(p.Title);
                posts[0].Author.Id.Should().Be(a.Id);
                posts[0].Comments.Count.Should().Be(2);
                posts[0].Comments[0].Id.Should().Be(400);
                posts[0].Comments[1].Id.Should().Be(401);
                posts[1].Id.Should().Be(p2.Id);
                posts[1].Title.Should().Be(p2.Title);
                posts[1].Author.Id.Should().Be(a.Id);
            }
        }