public void DeserializesBelongsToRelationships()
        {
            var target = new ResourceDeserializer(_singleJson, typeof(Person));
            var result = target.Deserialize() as Person;
            var job = result?.Job;

            Assert.Equal(_person.Job.Id, job?.Id);
            Assert.Null(job?.Name);
            Assert.Equal(0, job?.NumberOfEmployees);
        }
        public void DeserializesAttributes()
        {
            var target = new ResourceDeserializer(_singleJson, typeof(Person));
            var result = target.Deserialize() as Person;

            Assert.Equal(_person.Identifier, result?.Identifier);
            Assert.Equal(_person.FirstName, result?.FirstName);
            Assert.Equal(_person.LastName, result?.LastName);
            Assert.Equal(_person.Age, result?.Age);
        }
        public void DeserializesBelongsToRelationshipsWithNullData()
        {
            var person = JToken.Parse("{'data': {'relationships': {'job': {data: null}}}}");
            var target = new ResourceDeserializer(person, typeof(Person));
            var result = target.Deserialize() as Person;

            var job = result?.Job;

            Assert.Null(job);
        }
        public void DeserializesWithoutId()
        {
            (_singleJson["data"] as JObject)?.Property("id").Remove();
            var target = new ResourceDeserializer(_singleJson, typeof(Person));
            var result = target.Deserialize() as Person;

            Assert.Equal(null, result?.Identifier);
            Assert.Equal(_person.FirstName, result?.FirstName);
            Assert.Equal(_person.LastName, result?.LastName);
            Assert.Equal(_person.Age, result?.Age);
        }
        public void DeserializesHasManyRelationship()
        {
            var target = new ResourceDeserializer(_singleJson, typeof(Person));
            var result = target.Deserialize() as Person;

            var expected = _person.Friends.Single();
            var actual = result?.Friends.Single();

            Assert.Equal(expected.Identifier, actual?.Identifier);
            Assert.Null(actual?.FirstName);
            Assert.Null(actual?.LastName);
            Assert.Equal(default(int?), actual?.Age);
            Assert.Null(actual?.Job);
            Assert.Null(actual?.Friends);
        }
        public void DeserializesEnumerables()
        {
            var target = new ResourceDeserializer(_collectionJson, typeof(Person[]));
            var result = target.Deserialize() as Person[];

            Assert.Equal(_people.Length, result?.Length);
            for (var i = 0; i < _people.Length; i++)
            {
                Assert.Equal(_people[i].Identifier, result?[i].Identifier);
            }
        }
        public void DeserializesHasManyRelationship()
        {
            var target = new ResourceDeserializer(_singleJson, typeof(Person));
            var result = target.Deserialize() as Person;

            var expectedFriend = _person.Friends.Single();
            var actualFriend = result?.Friends.Single();

            Assert.Equal(expectedFriend.Identifier, actualFriend?.Identifier);
            Assert.Null(actualFriend?.FirstName);
            Assert.Null(actualFriend?.LastName);
            Assert.Equal(default(int?), actualFriend?.Age);
            Assert.Null(actualFriend?.Job);
            Assert.Null(actualFriend?.Friends);

            var expectedFamilyMembers = _person.FamilyMembers.ToArray();
            var actualFamilyMembers = result?.FamilyMembers?.ToArray();

            Assert.NotNull(actualFamilyMembers);
            Assert.Equal(expectedFamilyMembers.Length, actualFamilyMembers.Length);
            expectedFamilyMembers.Zip(actualFamilyMembers, (expected, actual) =>
            {
                Assert.Equal(expected.Identifier, actual.Identifier);
                return true;
            });
        }
        public void ValidatesRootIsObject()
        {
            var content = FileAsJson("no-object-root.json");
            var target = new ResourceDeserializer(content, typeof(Person));

            Assert.Throws<JsonApiException>(() => target.Deserialize());
        }
        public void ValidatesTopLevelMemberExclusivity()
        {
            var content = FileAsJson("invalid-top-level.json");
            var target = new ResourceDeserializer(content, typeof(Person));

            Assert.Throws<JsonApiException>(() => target.Deserialize());
        }
        public void ValidatesNoIncludedWithoutData()
        {
            var content = FileAsJson("included-no-data.json");
            var target = new ResourceDeserializer(content, typeof(Person));

            Assert.Throws<JsonApiException>(() => target.Deserialize());
        }
        public void ValidatesMutuallyExclusiveTopLevelMembers()
        {
            var content = FileAsJson("errors-and-data.json");
            var target = new ResourceDeserializer(content, typeof(Person));

            Assert.Throws<JsonApiException>(() => target.Deserialize());
        }
        public void ValidatesRequiredTopLevelMembers()
        {
            var content = FileAsJson("no-data-errors-meta.json");
            var target = new ResourceDeserializer(content, typeof(Person));

            Assert.Throws<JsonApiException>(() => target.Deserialize());
        }