public async Task GetMessagesWithRecipient_ShouldReturnBadRequestResult_WhenModelValidationFails()
    {
        // Arrange
        const int recipientId = 1;

        GetMessagesWithRecipientQueryParams queryParams = new GetMessagesWithRecipientQueryParams();

        RecipientController controller = new RecipientController(_mediatorMock.Object);

        controller.ModelState.AddModelError("", "");

        // Act
        ActionResult <IEnumerable <ChatMessageResource> > response = await controller.GetMessagesWithRecipient(recipientId, queryParams);

        // Assert
        Assert.IsType <BadRequestObjectResult>(response.Result);
    }
    public async Task GetMessagesWithRecipient_ShouldReturnRecipients_WhenRecipientExist()
    {
        // Arrange
        const int recipientId = 1;

        GetMessagesWithRecipientQueryParams queryParams = new GetMessagesWithRecipientQueryParams();

        _mediatorMock
        .Setup(m => m.Send(It.IsAny <RecipientExistsQuery>(), It.IsAny <CancellationToken>()))
        .ReturnsAsync(true);

        _mediatorMock
        .Setup(m => m.Send(It.IsAny <GetMessagesWithRecipientQuery>(), It.IsAny <CancellationToken>()))
        .ReturnsAsync(new List <ChatMessageResource>());

        RecipientController controller = new RecipientController(_mediatorMock.Object);

        // Act
        ActionResult <IEnumerable <ChatMessageResource> > response = await controller.GetMessagesWithRecipient(recipientId, queryParams);

        // Assert
        Assert.IsType <OkObjectResult>(response.Result);
    }
    public async Task GetMessagesWithRecipient_ShouldReturnNotFoundResult_WhenRecipientDoesNotExist()
    {
        // Arrange
        const int recipientId = 424;

        GetMessagesWithRecipientQueryParams queryParams = new GetMessagesWithRecipientQueryParams();

        _mediatorMock
        .Setup(m => m.Send(It.IsAny <RecipientExistsQuery>(), It.IsAny <CancellationToken>()))
        .ReturnsAsync(false);

        RecipientController controller = new RecipientController(_mediatorMock.Object);

        // Act
        ActionResult <IEnumerable <ChatMessageResource> > response = await controller.GetMessagesWithRecipient(recipientId, queryParams);

        // Assert
        NotFoundObjectResult result = Assert.IsType <NotFoundObjectResult>(response.Result);

        ErrorResource error = Assert.IsType <ErrorResource>(result.Value);

        Assert.NotNull(error);
        Assert.Equal(StatusCodes.Status404NotFound, error.StatusCode);
    }
예제 #4
0
        public async Task <ActionResult <IEnumerable <ChatMessageResource> > > GetMessagesWithRecipient([FromRoute] int recipientId, [FromQuery] GetMessagesWithRecipientQueryParams boundaries, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Check if the given recipient exists
            RecipientExistsQuery existsQuery = new RecipientExistsQuery {
                RecipientId = recipientId
            };

            bool exists = await _mediator.Send(existsQuery, cancellationToken);

            if (!exists)
            {
                return(NotFound(new ErrorResource
                {
                    StatusCode = StatusCodes.Status404NotFound,
                    Message = $"Recipient with ID '{recipientId}' does not exist"
                }));
            }

            // Get messages with given recipient
            GetMessagesWithRecipientQuery fetchQuery = new GetMessagesWithRecipientQuery
            {
                RecipientId = recipientId,
                Limit       = boundaries.Limit,
                Before      = boundaries.Before,
                After       = boundaries.After,
            };

            IEnumerable <ChatMessageResource> messages = await _mediator.Send(fetchQuery, cancellationToken);

            return(Ok(messages));
        }