public void UsesSpecifiedPropertyId()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = DefaultObject
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "abc").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(DefaultObject.Identifier, id);
        }
        public void UsesDefaultPropertyId()
        {
            var data = new PersonWithNoJob();
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new PersonWithDefaultIdResource(),
                Result = data
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(data.Id, id);
        }
        public void HandlesRecursiveProperties()
        {
            var firstModel = new Recursion.FirstModel();
            var secondModel = new Recursion.SecondModel();
            firstModel.Model = secondModel;
            secondModel.Model = firstModel;

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new Recursion.Resource(),
                Result = firstModel
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"].Value<string>("id");

            Assert.Equal(firstModel.Id, id);
        }
        public void HandlesNullValues()
        {
            var person = new Person(id: "45");

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = person
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "45").ToString()));
            _output.WriteLine(result.ToString());

            var relationships = result["data"]["relationships"];
            var attributes = result["data"]["attributes"];

            Assert.NotNull(attributes["first-name"]);
            Assert.NotNull(attributes["last-name"]);
            Assert.NotNull(attributes["age"]);

            Assert.Null(relationships["job"]["data"]);
            Assert.Null(relationships["friends"]["data"]);
        }
        public void SupportsGuidIds()
        {
            var guid = new GuidAsId();

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new PersonWithDefaultIdResource(),
                Result = guid
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            Assert.NotNull(result["data"]["id"]);
            Assert.Equal(guid.Id, Guid.Parse(result["data"]["id"].ToString()));
        }
        public void SerializeOnlyWhatYouHave()
        {
            var company = new GuidAsId();

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new CompanyResource(),
                Result = company
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            Assert.Null(result["data"]["attributes"]["name"]);
            Assert.Null(result["data"]["attributes"]["location"]);
            Assert.Null(result["data"]["attributes"]["number-of-employees"]);
        }
        public void DocumentMustNotContainIncludedForEmptySet()
        {
            var people = new Person[0];

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = people
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            Assert.Null(result["included"]);
        }
        public void HandlesNullResources()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = null
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            Assert.Equal(JTokenType.Null, result["data"].Type);
        }
        public void SerializesEnumerables()
        {
            var people = Get.People(5);

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = people
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            var included = result["included"] as JArray;
            var jobLinks = (result["data"] as JArray)?[0]["relationships"]["job"]["links"];

            Assert.Equal(1, included?.Count);
            Assert.Equal("/api/people/0/employer/", new Uri(jobLinks?["related"]?.ToString()).AbsolutePath);
        }
        public void DocumentMustContainAtLeastOneDataOrErrorOrMeta()
        {
            var people = new Person[] { };

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = people
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri().ToString()));
            _output.WriteLine(result.ToString());

            Assert.NotNull(result["data"]);
        }
        public void AttributesComplete()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = DefaultObject
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var attributes = result["data"]["attributes"];
            Assert.Equal(DefaultObject.FirstName, attributes.Value<string>("first-name"));
            Assert.Equal(DefaultObject.LastName, attributes.Value<string>("last-name"));
            Assert.Equal(DefaultObject.Age, attributes.Value<int>("age"));
        }
        public void UsesCustomIdInRelationships()
        {
            var person = new PersonWithDifferentId(id: "abc", prefill: true);
            var resource = new PersonWithDifferentIdResource();

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = resource,
                Result = person
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "abc").ToString()));
            _output.WriteLine(result.ToString());

            var id = result["data"]["relationships"]["job"]["data"].Value<string>("id");

            Assert.Equal(person.Job.CompanyId, id);
        }
        public void IncludesRelationshipData()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = DefaultObject
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var included = result["included"] as JArray;
            var job = included?[0];
            Assert.Equal(1, included?.Count);
            Assert.Equal("http://example.com/api/corporations/456/", included?[0]?["links"]?["self"]?.ToString());

            Assert.Equal(DefaultObject.Job.Id, job?["id"]);
            Assert.NotNull(job?["attributes"]);
        }
        public void SerializesRelationshipData()
        {
            var person = new PersonWithNoJob();
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new PersonWithDefaultIdResource(),
                Result = person
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var relationships = result["data"]["relationships"];
            var job = relationships["job"];
            var friends = relationships["friends"];

            Assert.Null(job["data"]);
            Assert.NotNull(friends["data"]);
        }
        public void ThrowsRightException()
        {
            var person = new PersonWithNoId();
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new CompanyResource(),
                Result = person
            };

            Assert.Throws<JsonApiException>(() =>
            {
                target.Serialize(resourceResult, GetUri(id: "123").ToString());
            });
        }
        public void UsesTitle()
        {
            var company = Get.Company();
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = new CompanyResource(),
                Result = company
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri("/corporations", "456").ToString()));
            _output.WriteLine(result.ToString());

            Assert.Equal("corporation", result["data"]["type"]);
        }
        public void AttributesSufficient()
        {
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = DefaultObject
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "123").ToString()));
            _output.WriteLine(result.ToString());

            var attributes = result["data"]["attributes"];
            Assert.True(attributes["numberOfLegs"] == null);
            Assert.Equal(3, attributes.Count());
        }
        public void UsesCustomIdInUrls()
        {
            var person = Get.Person(id: "abc");
            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = person
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "abc").ToString()));
            _output.WriteLine(result.ToString());

            var job = result["data"]["relationships"]["job"]["links"];
            var friends = result["data"]["relationships"]["friends"]["links"];

            var self = new Uri(result["links"]["self"].ToString()).AbsolutePath;
            var jobSelf = new Uri(job["self"].ToString()).AbsolutePath;
            var jobRelated = new Uri(job["related"].ToString()).AbsolutePath;
            var friendsSelf = new Uri(friends["self"].ToString()).AbsolutePath;
            var friendsRelated = new Uri(friends["related"].ToString()).AbsolutePath;
            var included = new Uri(result["included"][0]["links"]["self"].ToString()).AbsolutePath;

            Assert.Equal("/api/people/abc", self);
            Assert.Equal("/api/people/abc/relationships/employer/", jobSelf);
            Assert.Equal("/api/people/abc/employer/", jobRelated);
            Assert.Equal("/api/people/abc/relationships/friends/", friendsSelf);
            Assert.Equal("/api/people/abc/friends/", friendsRelated);
            Assert.Equal("/api/corporations/456/", included);
        }
        public void UsesCustomIdInCollections()
        {
            var person = new Person(id: "abc", prefill: true)
            {
                Friends = new List<Person>
                {
                    new Person(id: "def", prefill: true),
                    new Person(id: "ghi", prefill: true),
                    new Person(id: "jkl", prefill: true),
                }
            };

            var target = new ResourceSerializer(urlBuilder: new DefaultUrlPathBuilder("/api"));

            var resourceResult = new ResourceResult
            {
                Resource = DefaultResource,
                Result = person
            };

            var result = JToken.Parse(target.Serialize(resourceResult, GetUri(id: "abc").ToString()));
            _output.WriteLine(result.ToString());

            var ids = result["data"]["relationships"]["friends"]["data"].Select(t => t.Value<string>("id"));
            var expected = person.Friends.Select(p => p.Identifier);

            Assert.Equal(expected, ids);
        }