public async Task TopUpBalance(Guid id, TopUpCustomerBalanceDto topUpDto, [FromServices] IMediator mediator) { if (id != topUpDto.CustomerID) { throw new IdMismatchException(); } await mediator.Send(new TopUpCustomerBalanceRequest { TopUpCustomerBalanceDto = topUpDto }); }
public async Task GivenIdInParamMatchIdInContent_WhenTopUpCustomerBalance_ThenBalanceToppedUp() { // Arrange var dto = new TopUpCustomerBalanceDto() { CustomerID = Guid.NewGuid(), TopUpAmount = 100 }; // Act HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, Url + dto.CustomerID + "/CurrentBalance"); message.Content = new ObjectContent <TopUpCustomerBalanceDto>(dto, new JsonMediaTypeFormatter()); var response = await _client.SendAsync(message); // Assert response.EnsureSuccessStatusCode(); _mediator.Verify(m => m.Send(It.Is <TopUpCustomerBalanceRequest>(r => r.TopUpCustomerBalanceDto.IsEquivalentTo(dto)), It.IsAny <CancellationToken>()), Times.Once); }
public async Task GivenIdInParamDoesNotMatchIdInContent_WhenTopUpBalance_ThenReturnBadRequest() { // Arrange var dto = new TopUpCustomerBalanceDto() { CustomerID = Guid.NewGuid(), TopUpAmount = 100 }; // Act HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, Url + Guid.NewGuid() + "/CurrentBalance"); message.Content = new ObjectContent <TopUpCustomerBalanceDto>(dto, new JsonMediaTypeFormatter()); var response = await _client.SendAsync(message); var returned = await response.Content.ReadAsAsync <ProductionExceptionResult>(); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); _mediator.Verify(m => m.Send(It.Is <TopUpCustomerBalanceRequest>(r => r.TopUpCustomerBalanceDto.IsEquivalentTo(dto)), It.IsAny <CancellationToken>()), Times.Never); Assert.Equal(nameof(IdMismatchException), returned.ExceptionType); }
public async Task GivenCustomerExists_WhenTopUpCustomerBalance_ThenBalanceToppedUp() { // Arrange var customer = new Customer() { ID = Guid.NewGuid(), CurrentBalance = 200 }; using (var ctx = _dbContextCreator.CreateDbContext()) { ctx.Add(customer); await ctx.SaveChangesAsync(); } var dto = new TopUpCustomerBalanceDto() { CustomerID = customer.ID, TopUpAmount = 100 }; // Act HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, Url + customer.ID.ToString() + "/CurrentBalance"); message.Content = new ObjectContent <TopUpCustomerBalanceDto>(dto, new JsonMediaTypeFormatter()); var response = await _client.SendAsync(message); // Assert response.EnsureSuccessStatusCode(); using (var ctx = _dbContextCreator.CreateDbContext()) { var customerInDb = await ctx.Customer.FirstOrDefaultAsync(); Assert.Equal(customer.CurrentBalance + dto.TopUpAmount, customerInDb.CurrentBalance); } }