public async Task Handle_OrderStockCommandWithoutInsuficientFounds_ThrowsAppException(string symbol, int amount) { var repository = new Mock <IStockRepository>(); var traderRepository = new Mock <ITraderRepository>(); var request = new OrderStockCommand { Symbol = symbol, Amount = amount }; repository.Setup(r => r.GetBySymbol(It.IsAny <String>())).ReturnsAsync(new Stock() { Symbol = "a", CurrentPrice = 10.0 }); traderRepository.Setup(r => r.GetById(It.IsAny <int>())).ReturnsAsync(new Trader() { Name = "joão", AccountAmmount = 0, FinancialAssets = new System.Collections.Generic.List <FinancialAsset>() }); var handler = new OrderStockCommandHandler(repository.Object, traderRepository.Object); var result = await handler.Handle(request, new CancellationToken()); Assert.True(result.Error); Assert.Equal("saldo insuficiente", result.ErrorMessage); }
public async Task Handle_OrderStockCommand_WithSuccess(string symbol, int amount) { var repository = new Mock <IStockRepository>(); var traderRepository = new Mock <ITraderRepository>(); var request = new OrderStockCommand { Symbol = symbol, Amount = amount }; var query = new OrderStockCommandHandler(repository.Object, traderRepository.Object); repository.Setup(r => r.GetBySymbol(It.IsAny <String>())).ReturnsAsync(new Stock() { Symbol = "a", CurrentPrice = 10.0 }); traderRepository.Setup(r => r.GetById(It.IsAny <int>())).ReturnsAsync(new Trader() { Name = "joão", AccountAmmount = 1000, FinancialAssets = new System.Collections.Generic.List <FinancialAsset>() }); var result = await query.Handle(request, new CancellationToken()); Assert.False(result.Error); Assert.Equal("sucesso", result.Data); }
public async Task <IActionResult> OrderStock( [FromBody] OrderStockCommand command, [FromServices] IMediator mediator) { var result = await mediator.Send(command); if (result.Error) { return(BadRequest(result.ErrorMessage)); } return(Ok(result.Data)); }
public async Task OrderStock_WithInvalidRequest_ReturnBadRequest() { var mediator = new Mock <IMediator>(); var controller = new StockController(); var command = new OrderStockCommand(); var response = await Task.FromResult(new BaseResponse <string>() { Error = true, ErrorMessage = "error" }); mediator.Setup(m => m.Send(It.IsAny <OrderStockCommand>(), It.IsAny <CancellationToken>())).ReturnsAsync(response); var result = await controller.OrderStock(command, mediator.Object); var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal(response.ErrorMessage, badRequestObjectResult.Value); }
public async Task OrderStock_WithValidRequest_ReturnOk() { var mediator = new Mock <IMediator>(); var controller = new StockController(); var command = new OrderStockCommand(); var response = await Task.FromResult(new BaseResponse <string>("sucesso")); mediator.Setup(m => m.Send(It.IsAny <OrderStockCommand>(), It.IsAny <CancellationToken>())).ReturnsAsync(response); var result = await controller.OrderStock(command, mediator.Object); var okResult = Assert.IsType <OkObjectResult>(result); Assert.Equal(response.Data, okResult.Value); }
public async Task Handle_OrderStockCommand_WithInvalidFinancialAsset(string symbol, int amount) { var repository = new Mock <IStockRepository>(); var traderRepository = new Mock <ITraderRepository>(); var request = new OrderStockCommand { Symbol = symbol, Amount = amount }; repository.Setup(r => r.GetBySymbol(It.IsAny <String>())).ReturnsAsync(() => null); traderRepository.Setup(r => r.GetById(It.IsAny <int>())).ReturnsAsync(new Trader() { Name = "joão", AccountAmmount = 0, FinancialAssets = new System.Collections.Generic.List <FinancialAsset>() }); var handler = new OrderStockCommandHandler(repository.Object, traderRepository.Object); var result = await handler.Handle(request, new CancellationToken()); Assert.True(result.Error); Assert.Equal("ativo inválido", result.ErrorMessage); }