Ejemplo n.º 1
0
        public void DeserializeList_EmptyResponseWithTopLevelLinks_CanDeserialize()
        {
            // Arrange
            var content = new Document
            {
                Links = new TopLevelLinks {
                    Self = _linkValues["self"], Next = _linkValues["next"], Last = _linkValues["last"]
                },
                Data = new List <ResourceObject>()
            };
            var body = JsonConvert.SerializeObject(content);

            // Act
            var result = _deserializer.DeserializeList <TestResource>(body);

            // Assert
            Assert.Empty(result.Data);
            Assert.NotNull(result.Links);
            TopLevelLinks links = result.Links;

            Assert.Equal(_linkValues["self"], links.Self);
            Assert.Equal(_linkValues["next"], links.Next);
            Assert.Equal(_linkValues["last"], links.Last);
        }
        public async Task Fields_Query_Selects_All_Fieldset_With_HasOne()
        {
            // Arrange
            _dbContext.TodoItems.RemoveRange(_dbContext.TodoItems);
            _dbContext.SaveChanges();
            var owner    = _personFaker.Generate();
            var todoItem = new TodoItem
            {
                Description = "s",
                Ordinal     = 123,
                CreatedDate = DateTime.Now,
                Owner       = owner
            };

            _dbContext.TodoItems.Add(todoItem);
            _dbContext.SaveChanges();

            var builder = new WebHostBuilder()
                          .UseStartup <Startup>();
            var httpMethod = new HttpMethod("GET");

            using var server = new TestServer(builder);
            var client = server.CreateClient();

            var route         = "/api/v1/todoItems?include=owner&fields[owner]=firstName,the-Age";
            var request       = new HttpRequestMessage(httpMethod, route);
            var options       = _fixture.GetService <IJsonApiOptions>();
            var resourceGraph = new ResourceGraphBuilder(options).AddResource <Person>().AddResource <TodoItemClient>("todoItems").Build();
            var deserializer  = new ResponseDeserializer(resourceGraph);
            // Act
            var response = await client.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadAsStringAsync();

            var deserializedTodoItems = deserializer.DeserializeList <TodoItemClient>(body).Data;

            foreach (var item in deserializedTodoItems.Where(i => i.Owner != null))
            {
                Assert.Null(item.Owner.LastName);
                Assert.NotNull(item.Owner.FirstName);
                Assert.NotEqual(0, item.Owner.Age);
            }
        }
        public async Task Can_Include_Nested_Relationships()
        {
            // Arrange
            const string route = "/api/v1/todoItems?include=collection.owner";

            var options       = _fixture.GetService <IJsonApiOptions>();
            var resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance)
                                .AddResource <TodoItemClient>("todoItems")
                                .AddResource <TodoItemCollection, Guid>()
                                .AddResource <Person>()
                                .Build();
            var deserializer = new ResponseDeserializer(resourceGraph, new DefaultResourceFactory(_fixture.ServiceProvider));
            var todoItem     = new TodoItem
            {
                Collection = new TodoItemCollection
                {
                    Owner = new Person()
                }
            };

            var context = _fixture.GetService <AppDbContext>();

            context.TodoItems.RemoveRange(context.TodoItems);
            context.TodoItems.Add(todoItem);
            await context.SaveChangesAsync();

            // Act
            var response = await _fixture.Client.GetAsync(route);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var body = await response.Content.ReadAsStringAsync();

            var todoItems = deserializer.DeserializeList <TodoItem>(body).Data;

            var responseTodoItem = Assert.Single(todoItems);

            Assert.NotNull(responseTodoItem);
            Assert.NotNull(responseTodoItem.Collection);
            Assert.NotNull(responseTodoItem.Collection.Owner);
        }