public async Task Given_TheCallIsCancelledByUser_When_HandlerIsCalled_Then_TheHandlerShouldThrowOperationCancelledException() { using (var cancellationTokenSource = new CancellationTokenSource()) { var cancellationToken = cancellationTokenSource.Token; cancellationTokenSource.Cancel(); var setupInvoice = new Invoice { Amount = 100, Identifier = "TEST-100" }; await _applicationDbContext.Invoices.AddAsync(setupInvoice); await _applicationDbContext.SaveChangesAsync(); var setupUpdateInvoiceDto = new UpdateInvoiceDto() { Amount = 200, Identifier = "TEST-200", InvoiceId = setupInvoice.InvoiceId }; var setupUpdateInvoiceCommand = new UpdateInvoiceCommand(setupUpdateInvoiceDto); Func <Task <UpdateInvoiceCommandResponse> > func = async() => await _systemUnderTest.Handle(setupUpdateInvoiceCommand, cancellationToken); await func.Should().ThrowAsync <OperationCanceledException>(); } }
public async Task Given_ThereIsAnInvoiceUpdate_When_HandlerIsCalled_Then_TheInvoiceIsUpdatedAndOkStatusReturned() { //arrange var setupInvoice = new Invoice { Amount = 100, Identifier = "TEST-100" }; await _applicationDbContext.Invoices.AddAsync(setupInvoice); await _applicationDbContext.SaveChangesAsync(); var setupUpdateInvoiceDto = new UpdateInvoiceDto() { Amount = 200, Identifier = "TEST-200", InvoiceId = setupInvoice.InvoiceId }; var setupUpdateInvoiceCommand = new UpdateInvoiceCommand(setupUpdateInvoiceDto); //act var response = await _systemUnderTest.Handle(setupUpdateInvoiceCommand, default); //assert response.StatusCode.Should().Be(HttpStatusCode.OK); response.Error.Should().BeNull(); response.HasError.Should().BeFalse(); var databaseInvoice = await _applicationDbContext.Invoices .SingleOrDefaultAsync(invoice => invoice.InvoiceId == setupInvoice.InvoiceId); databaseInvoice.Should().NotBeNull(); databaseInvoice.Amount.Should().Be(setupUpdateInvoiceDto.Amount); databaseInvoice.Identifier.Should().Be(setupUpdateInvoiceDto.Identifier); }
public async Task Given_TheUpdateInvoiceFailsInDatabase_When_HandlerIsCalled_Then_ItShouldReturnInternalServerError() { using (var cancellationTokenSource = new CancellationTokenSource()) { //arrange var cancellationToken = cancellationTokenSource.Token; var setupException = new Exception("test update exception"); var setupInvoice = new Invoice { Amount = 100, Identifier = "TEST-100" }; await _applicationDbContext.Invoices.AddAsync(setupInvoice); await _applicationDbContext.SaveChangesAsync(); var setupUpdateInvoiceDto = new UpdateInvoiceDto() { Amount = 200, Identifier = "TEST-200", InvoiceId = setupInvoice.InvoiceId }; var setupUpdateInvoiceCommand = new UpdateInvoiceCommand(setupUpdateInvoiceDto); var mockRepositoryOutput = new Queue <Invoice>(new[] { setupInvoice, null }); var mockApplicationUnitOfWork = _mockRepository.Create <IApplicationUnitOfWork>(); var mockInvoiceRepository = _mockRepository.Create <IApplicationRepository <Invoice> >(); mockInvoiceRepository.Setup(x => x.SingleOrDefaultAsync(It.IsAny <Expression <Func <Invoice, bool> > >(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => mockRepositoryOutput.Dequeue()); mockInvoiceRepository.Setup(x => x.Update(It.IsAny <Invoice>())); mockApplicationUnitOfWork.Setup(x => x.CommitAsync(cancellationToken)).ThrowsAsync(setupException); mockApplicationUnitOfWork.Setup(x => x.Invoices).Returns(mockInvoiceRepository.Object); _systemUnderTest = new UpdateInvoiceCommandHandler(mockApplicationUnitOfWork.Object, _logger.Object, _authenticatedUserService.Object); //act var response = await _systemUnderTest.Handle(setupUpdateInvoiceCommand, cancellationToken); //assert response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); response.Error.Should().NotBeNull(); response.Error.ErrorCode.Should().Be(ApplicationConstants.ErrorCodes.UpdateInvoiceError); response.Error.ErrorMessage.Should().Be(setupException.Message); var databaseInvoice = await _applicationDbContext.Invoices .SingleOrDefaultAsync(invoice => invoice.InvoiceId == setupInvoice.InvoiceId); databaseInvoice.Should().NotBeNull(); databaseInvoice.Amount.Should().Be(setupInvoice.Amount); databaseInvoice.Identifier.Should().Be(setupInvoice.Identifier); } }
public async Task Given_ThereIsAnInvoiceUpdateAndAnotherInvoiceHasTheSameIdentifier_When_HandlerIsCalled_Then_BadRequestResponseIsReturned() { //arrange var setupInvoice1 = new Invoice { Amount = 100, Identifier = "TEST-100" }; var setupInvoice2 = new Invoice { Amount = 200, Identifier = "TEST-200" }; await _applicationDbContext.Invoices.AddAsync(setupInvoice1); await _applicationDbContext.Invoices.AddAsync(setupInvoice2); await _applicationDbContext.SaveChangesAsync(); var setupUpdateInvoiceDto = new UpdateInvoiceDto() { Amount = 200, Identifier = "TEST-200", InvoiceId = setupInvoice1.InvoiceId }; var setupUpdateInvoiceCommand = new UpdateInvoiceCommand(setupUpdateInvoiceDto); //act var response = await _systemUnderTest.Handle(setupUpdateInvoiceCommand, default); //assert response.StatusCode.Should().Be(HttpStatusCode.BadRequest); response.Error.Should().NotBeNull(); response.HasError.Should().BeTrue(); response.Error.ErrorCode.Should().Be(ApplicationConstants.ErrorCodes.BusinessValidationError); response.Error.ErrorMessage.Should().Be(string.Format(ApplicationConstants.ErrorMessages.DuplicateInvoiceIdentifier, setupUpdateInvoiceDto.Identifier)); var databaseInvoice1 = await _applicationDbContext.Invoices .SingleOrDefaultAsync(invoice => invoice.InvoiceId == setupInvoice1.InvoiceId); databaseInvoice1.Amount.Should().Be(setupInvoice1.Amount); databaseInvoice1.Identifier.Should().Be(setupInvoice1.Identifier); var databaseInvoice2 = await _applicationDbContext.Invoices .SingleOrDefaultAsync(invoice => invoice.InvoiceId == setupInvoice2.InvoiceId); databaseInvoice2.Amount.Should().Be(setupInvoice2.Amount); databaseInvoice2.Identifier.Should().Be(setupInvoice2.Identifier); }
public async Task Given_ThereIsAnInvoiceUpdateForAnInvoiceCreatedByOtherUser_When_HandlerIsCalled_Then_ForbidenResponseIsReturned() { //arrange var setupInvoice = new Invoice { Amount = 100, Identifier = "TEST-100" }; await _applicationDbContext.Invoices.AddAsync(setupInvoice); await _applicationDbContext.SaveChangesAsync(); var setupUpdateInvoiceDto = new UpdateInvoiceDto() { Amount = 200, Identifier = "TEST-200", InvoiceId = setupInvoice.InvoiceId }; var setupUpdateInvoiceCommand = new UpdateInvoiceCommand(setupUpdateInvoiceDto); _authenticatedUserService.Setup(x => x.GetUserId()).Returns(200); //act var response = await _systemUnderTest.Handle(setupUpdateInvoiceCommand, default); //assert response.StatusCode.Should().Be(HttpStatusCode.Forbidden); response.Error.Should().NotBeNull(); response.HasError.Should().BeTrue(); response.Error.ErrorCode.Should().Be(ApplicationConstants.ErrorCodes.AuthorizationError); response.Error.ErrorMessage.Should().Be(string.Format(ApplicationConstants.ErrorMessages.InvoiceCreatedByDifferentUser, setupUpdateInvoiceDto.InvoiceId)); var databaseInvoice = await _applicationDbContext.Invoices .SingleOrDefaultAsync(invoice => invoice.InvoiceId == setupInvoice.InvoiceId); databaseInvoice.Should().NotBeNull(); databaseInvoice.Amount.Should().Be(setupInvoice.Amount); databaseInvoice.Identifier.Should().Be(setupInvoice.Identifier); }
public IActionResult UpdateInvoice([FromBody] UpdateInvoiceDto body) { Response response = new Response(); response.data = new List <Invoice>(); response.code = 0; var transaction = this._context.Database.BeginTransaction(); try { var invoice = this._context.Invoice.SingleOrDefault(i => i.id == body.invoiceId); if (invoice != null) { var clientInvoice = this._context.Client.SingleOrDefault(c => c.id == invoice.client_id); if (clientInvoice != null) { if (body.clientName != null) { clientInvoice.name = body.clientName; } if (body.clientAddress != null) { clientInvoice.address = body.clientAddress; } if (body.clientPhone != null) { clientInvoice.phone_number = body.clientPhone; } invoice.client = clientInvoice; this._context.Update(clientInvoice); this._context.SaveChanges(); transaction.Commit(); response.code = 1; response.message = "Ok"; response.data.Add(invoice); return(Ok(response)); } else { response.message = "Not Found Client"; return(NotFound(response)); } } else { response.message = "Not Found Invoice"; return(NotFound(response)); } } catch (Exception e) { transaction.Rollback(); response.code = 0; response.message = e.Message; return(BadRequest(response)); } }
public async Task <ActionResult> UpdateInvoice([FromBody] UpdateInvoiceDto updateInvoiceModel, CancellationToken cancellationToken) { var response = await _mediator.Send(new UpdateInvoiceCommand(updateInvoiceModel), cancellationToken); return(ProcessResult(response)); }