public void WorksOnExceptions()
        {
            var target = new JsonApiSerializer<PersonResource>();
            var result = target.Serialize(new FileNotFoundException(), DefaultUrl);

            _output.WriteLine(result.ToString());

            Assert.Null(result["data"]);
            Assert.NotNull(result["errors"]);
        }
        /// <summary>
        /// Produces the Json Api response that represents the given @object.
        /// </summary>
        /// <param name="object">The object to serialize.</param>
        /// <param name="requestUri">The request uri that prompted the response.</param>
        /// <returns>A <see cref="JToken"/> representing the object.</returns>
        public JToken Serialize(object @object, Uri requestUri)
        {
            var request      = new HttpRequestMessage(HttpMethod.Get, requestUri);
            var queryContext = GetQueryContext(request.GetQueryNameValuePairs());

            _serializer.QueryContext = queryContext;

            var preprocessResult = _serializer.PreprocessContent(@object, new T(), requestUri);

            return(JsonApiSerializer.Serialize(preprocessResult));
        }
        /// <summary>
        /// Produces the Json Api response that represents the given @object.
        /// </summary>
        /// <param name="object">The object to serialize.</param>
        /// <param name="requestUri">The request uri that prompted the response.</param>
        /// <param name="config">The configuration to be used for serialization.</param>
        /// <returns>A <see cref="JToken"/> representing the object.</returns>
        public JToken Serialize(object @object, Uri requestUri, JsonApiConfiguration config = null)
        {
            if (config == null)
            {
                config = new JsonApiConfiguration();
            }

            var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
            var queryContext = GetQueryContext(request.GetQueryNameValuePairs());

            _serializer.QueryContext = queryContext;

            var preprocessResult = _serializer.PreprocessContent(@object, new T(), requestUri, config);
            return JsonApiSerializer.Serialize(preprocessResult);
        }
        public void AppliesFilters()
        {
            var target = new JsonApiSerializer<CompanyResource>
            {
                AllowQuery = true
            };

            var companies = Get.Companies(100).ToList().AsQueryable();
            var result = target.Serialize(companies, new Uri(DefaultUrl, "?filter[location]=1"));
            _output.WriteLine(result.ToString());

            var filtered = ((JArray)result["data"]).ToList();

            var expected = companies.Where(x => x.Location == LocationType.National).ToList();

            Assert.Equal(expected.Count, filtered.Count);
        }
 public void HasAContract()
 {
     var target = new JsonApiSerializer<PersonResource>();
     Assert.Throws<ArgumentNullException>(() => target.Serialize(Get.Person(), null));
 }
        public void QueryOrderCorrect()
        {
            var target = new JsonApiSerializer<PersonResource>
            {
                AllowQuery = true,
                Paginate = true,
                ItemsPerPage = 10
            };

            var people = Get.People(100).AsQueryable();
            var result = target.Serialize(people, new Uri(DefaultUrl, "?sort=-id"));
            _output.WriteLine(result.ToString());

            var ids = ((JArray)result["data"]).Select(t => t["id"].Value<string>());
            var expected = Enumerable.Range(0, 100)
                .OrderByDescending(i => i)
                .Take(10)
                .Select(i => i.ToString());

            Assert.Equal(expected, ids);
        }
        public void GivesUsefulErrorForEnumerable()
        {
            var target = new JsonApiSerializer<PersonResource>
            {
                AllowQuery = true
            };

            var people = Get.People(100);
            var result = target.Serialize(people, new Uri(DefaultUrl, "?sort=fail-me"));
            _output.WriteLine(result.ToString());

            var error = result["errors"][0];

            Assert.Equal("Attribute 'fail-me' not found.", error["title"].Value<string>());
        }
        public void UsesUrlBuilder()
        {
            var target = new JsonApiSerializer<PersonResource>
            {
                UrlPathBuilder = new CanonicalUrlPathBuilder()
            };

            var result = target.Serialize(Get.Person(), DefaultUrl);
            _output.WriteLine(result.ToString());

            var related = result["data"]["relationships"]["job"]["links"]["related"].Value<Uri>()
                .AbsolutePath;

            Assert.Equal("/corporations/456/", related);
        }
        public void UsesConverters()
        {
            var target = new JsonApiSerializer<CompanyResource>();
            var company = Get.Company();
            target.JsonConverters.Add(new StringEnumConverter());
            var result = target.Serialize(company, DefaultUrl);

            _output.WriteLine(result.ToString());

            Assert.Equal(company.Location.ToString(), result["data"]["attributes"].Value<string>("location"));
        }
        public void AppliesSorting()
        {
            var target = new JsonApiSerializer<PersonResource>
            {
                AllowQuery = true
            };

            // people needs to be > 80 so we always get doubles and we can 
            // verify the -id properly
            var people = Get.People(100).AsQueryable();
            var result = target.Serialize(people, new Uri(DefaultUrl, "?sort=+age,-id"));
            _output.WriteLine(result.ToString());

            var props = ((JArray)result["data"]).Select(t => new
            {
                Age = t["attributes"]["age"].Value<int>(),
                Id = t["id"].Value<string>()
            }).ToList();

            var expected = props.OrderBy(p => p.Age).ThenByDescending(p => p.Id);

            Assert.Equal(expected, props);
        }
        public void PropertyInteraction()
        {
            var target = new JsonApiSerializer<PersonResource>
            {
                ItemsPerPage = 2,
                Paginate = false
            };
            var people = Get.People(5);
            var result = target.Serialize(people, DefaultUrl);

            _output.WriteLine(result.ToString());

            Assert.Equal(5, result["data"].Count());
            Assert.Null(result["links"]["next"]);
            Assert.Null(result["links"]["first"]);
            Assert.NotNull(result["links"]["self"]);
        }
        public void AppliesPagination()
        {
            var target = new JsonApiSerializer<PersonResource>
            {
                ItemsPerPage = 5,
                Paginate = true
            };
            var people = Get.People(20).AsQueryable();
            var result = target.Serialize(people, DefaultUrl);

            _output.WriteLine(result.ToString());

            Assert.Equal(5, result["data"].Count());
            Assert.NotNull(result["links"]["next"]);
            Assert.NotNull(result["links"]["first"]);
            Assert.Null(result["links"]["prev"]);
        }