Esempio n. 1
0
        public async Task GetByTargetIdReturnsRecordsAllPages()
        {
            var targetId           = Guid.NewGuid();
            var expected           = UpsertNotes(targetId, 9);
            var expectedFirstPage  = expected.OrderByDescending(x => x.CreatedAt).Take(5);
            var expectedSecondPage = expected.Except(expectedFirstPage).OrderByDescending(x => x.CreatedAt);

            var query = new GetNotesByTargetIdQuery()
            {
                TargetId = targetId, PageSize = 5
            };
            var response = await _classUnderTest.GetByTargetIdAsync(query).ConfigureAwait(false);

            response.Should().NotBeNull();
            response.Results.Should().BeEquivalentTo(expectedFirstPage);
            response.PaginationDetails.HasNext.Should().BeTrue();
            response.PaginationDetails.NextToken.Should().NotBeNull();

            query.PaginationToken = response.PaginationDetails.NextToken;
            response = await _classUnderTest.GetByTargetIdAsync(query).ConfigureAwait(false);

            response.Should().NotBeNull();
            response.Results.Should().BeEquivalentTo(expectedSecondPage);
            response.PaginationDetails.HasNext.Should().BeFalse();
            response.PaginationDetails.NextToken.Should().BeNull();

            _logger.VerifyExact(LogLevel.Debug, $"Querying NotesByCreated index for targetId {query.TargetId}", Times.Exactly(2));
        }
        public void QueryShouldErrorWithNullTargetId()
        {
            var query  = new GetNotesByTargetIdQuery();
            var result = _sut.TestValidate(query);

            result.ShouldHaveValidationErrorFor(x => x.TargetId);
        }
        public async Task GetByTargetIdUseCaseGatewayReturnsListReturnsResponseList(string paginationToken)
        {
            // Arrange
            var id    = Guid.NewGuid();
            var query = new GetNotesByTargetIdQuery {
                TargetId = id, PaginationToken = paginationToken
            };
            var notes         = _fixture.CreateMany <Note>(5).ToList();
            var gatewayResult = new PagedResult <Note>(notes, new PaginationDetails(paginationToken));

            _mockGateway.Setup(x => x.GetByTargetIdAsync(query)).ReturnsAsync(gatewayResult);

            // Act
            var response = await _classUnderTest.ExecuteAsync(query).ConfigureAwait(false);

            // Assert
            response.Results.Should().BeEquivalentTo(notes.ToResponse());
            if (string.IsNullOrEmpty(paginationToken))
            {
                response.PaginationDetails.NextToken.Should().BeNull();
            }
            else
            {
                response.PaginationDetails.DecodeNextToken().Should().Be(paginationToken);
            }
        }
        public void QueryShouldErrorWithInvalidPageSize(int invalidPageSize)
        {
            var query = new GetNotesByTargetIdQuery()
            {
                TargetId = Guid.NewGuid(), PageSize = invalidPageSize
            };
            var result = _sut.TestValidate(query);

            result.ShouldHaveValidationErrorFor(x => x.PageSize);
        }
        public void QueryShouldNotErrorWithNoPageSize()
        {
            var query = new GetNotesByTargetIdQuery()
            {
                TargetId = Guid.NewGuid()
            };
            var result = _sut.TestValidate(query);

            result.ShouldNotHaveValidationErrorFor(x => x.PageSize);
        }
        public async Task <IActionResult> GetByTargetIdAsync([FromQuery] GetNotesByTargetIdQuery query)
        {
            var response = await _getByTargetIdUseCase.ExecuteAsync(query).ConfigureAwait(false);

            if ((null == response) || !response.Results.Any())
            {
                return(NotFound(query.TargetId));
            }

            return(Ok(response));
        }
Esempio n. 7
0
        public async Task GetByTargetIdReturnsEmptyIfNoRecords()
        {
            var query = new GetNotesByTargetIdQuery()
            {
                TargetId = Guid.NewGuid()
            };
            var response = await _classUnderTest.GetByTargetIdAsync(query).ConfigureAwait(false);

            response.Should().NotBeNull();
            response.Results.Should().BeEmpty();
            response.PaginationDetails.HasNext.Should().BeFalse();
            response.PaginationDetails.NextToken.Should().BeNull();

            _logger.VerifyExact(LogLevel.Debug, $"Querying NotesByCreated index for targetId {query.TargetId}", Times.Once());
        }
        public async Task GetByTargetIdUseCaseGatewayReturnsEmptyReturnsEmptyList(string paginationToken)
        {
            // Arrange
            var id    = Guid.NewGuid();
            var query = new GetNotesByTargetIdQuery {
                TargetId = id, PaginationToken = paginationToken
            };
            var gatewayResult = new PagedResult <Note>(new List <Note>());

            _mockGateway.Setup(x => x.GetByTargetIdAsync(query)).ReturnsAsync(gatewayResult);

            // Act
            var response = await _classUnderTest.ExecuteAsync(query).ConfigureAwait(false);

            // Assert
            response.Results.Should().BeEmpty();
        }
Esempio n. 9
0
        public void GetPersonByIdAsyncExceptionIsThrown(string paginationToken)
        {
            // Arrange
            var id    = Guid.NewGuid();
            var query = new GetNotesByTargetIdQuery {
                TargetId = id, PaginationToken = paginationToken
            };
            var exception = new ApplicationException("Test exception");

            _mockGetByTargetIdUseCase.Setup(x => x.ExecuteAsync(query)).ThrowsAsync(exception);

            // Act
            Func <Task <IActionResult> > func = async() => await _sut.GetByTargetIdAsync(query).ConfigureAwait(false);

            // Assert
            func.Should().Throw <ApplicationException>().WithMessage(exception.Message);
        }
Esempio n. 10
0
        public async Task GetPersonByIdAsyncNotFoundReturnsNotFound(string paginationToken)
        {
            // Arrange
            var id    = Guid.NewGuid();
            var query = new GetNotesByTargetIdQuery {
                TargetId = id, PaginationToken = paginationToken
            };

            _mockGetByTargetIdUseCase.Setup(x => x.ExecuteAsync(query)).ReturnsAsync((PagedResult <NoteResponseObject>)null);

            // Act
            var response = await _sut.GetByTargetIdAsync(query).ConfigureAwait(false);

            // Assert
            response.Should().BeOfType(typeof(NotFoundObjectResult));
            (response as NotFoundObjectResult).Value.Should().Be(id);
        }
        public void GetByTargetIdExceptionIsThrown(string paginationToken)
        {
            // Arrange
            var id    = Guid.NewGuid();
            var query = new GetNotesByTargetIdQuery {
                TargetId = id, PaginationToken = paginationToken
            };
            var exception = new ApplicationException("Test exception");

            _mockGateway.Setup(x => x.GetByTargetIdAsync(query)).ThrowsAsync(exception);

            // Act
            Func <Task <PagedResult <NoteResponseObject> > > func = async() =>
                                                                    await _classUnderTest.ExecuteAsync(query).ConfigureAwait(false);

            // Assert
            func.Should().Throw <ApplicationException>().WithMessage(exception.Message);
        }
Esempio n. 12
0
        public async Task GetByTargetIdReturnsNoPaginationTokenIfPageSizeEqualsRecordCount()
        {
            var targetId = Guid.NewGuid();
            var expected = UpsertNotes(targetId, 10);

            var query = new GetNotesByTargetIdQuery()
            {
                TargetId = targetId, PageSize = 10
            };
            var response = await _classUnderTest.GetByTargetIdAsync(query).ConfigureAwait(false);

            response.Should().NotBeNull();
            response.Results.Should().BeEquivalentTo(expected);
            response.PaginationDetails.HasNext.Should().BeFalse();
            response.PaginationDetails.NextToken.Should().BeNull();

            _logger.VerifyExact(LogLevel.Debug, $"Querying NotesByCreated index for targetId {query.TargetId}", Times.Once());
        }
Esempio n. 13
0
        public async Task <PagedResult <Note> > GetByTargetIdAsync(GetNotesByTargetIdQuery query)
        {
            int pageSize = query.PageSize.HasValue ? query.PageSize.Value : MAX_RESULTS;
            var dbNotes  = new List <NoteDb>();
            var table    = _dynamoDbContext.GetTargetTable <NoteDb>();

            var queryConfig = new QueryOperationConfig
            {
                IndexName       = GETNOTESBYTARGETIDINDEX,
                BackwardSearch  = true,
                ConsistentRead  = true,
                Limit           = pageSize,
                PaginationToken = PaginationDetails.DecodeToken(query.PaginationToken),
                Filter          = new QueryFilter(TARGETID, QueryOperator.Equal, query.TargetId)
            };
            var search = table.Query(queryConfig);

            _logger.LogDebug($"Querying {queryConfig.IndexName} index for targetId {query.TargetId}");
            var resultsSet = await search.GetNextSetAsync().ConfigureAwait(false);

            var paginationToken = search.PaginationToken;

            if (resultsSet.Any())
            {
                dbNotes.AddRange(_dynamoDbContext.FromDocuments <NoteDb>(resultsSet));

                // Look ahead for any more, but only if we have a token
                if (!string.IsNullOrEmpty(PaginationDetails.EncodeToken(paginationToken)))
                {
                    queryConfig.PaginationToken = paginationToken;
                    queryConfig.Limit           = 1;
                    search     = table.Query(queryConfig);
                    resultsSet = await search.GetNextSetAsync().ConfigureAwait(false);

                    if (!resultsSet.Any())
                    {
                        paginationToken = null;
                    }
                }
            }

            return(new PagedResult <Note>(dbNotes.Select(x => x.ToDomain()), new PaginationDetails(paginationToken)));
        }
Esempio n. 14
0
        public async Task GetPersonByIdAsyncFoundReturnsResponse(string paginationToken)
        {
            // Arrange
            var id    = Guid.NewGuid();
            var query = new GetNotesByTargetIdQuery {
                TargetId = id, PaginationToken = paginationToken
            };
            var notes       = _fixture.CreateMany <NoteResponseObject>(5).ToList();
            var pagedResult = new PagedResult <NoteResponseObject>(notes, new PaginationDetails(paginationToken));

            _mockGetByTargetIdUseCase.Setup(x => x.ExecuteAsync(query)).ReturnsAsync(pagedResult);

            // Act
            var response = await _sut.GetByTargetIdAsync(query).ConfigureAwait(false);

            // Assert
            response.Should().BeOfType(typeof(OkObjectResult));
            (response as OkObjectResult).Value.Should().BeEquivalentTo(pagedResult);
        }
Esempio n. 15
0
        public async Task <PagedResult <NoteResponseObject> > ExecuteAsync(GetNotesByTargetIdQuery query)
        {
            var gatewayResult = await _gateway.GetByTargetIdAsync(query).ConfigureAwait(false);

            return(new PagedResult <NoteResponseObject>(gatewayResult.Results.ToResponse(), gatewayResult.PaginationDetails));
        }