public void TestHandleUserNotExist() { var publishedEvents = new List <IEvent>(); var depositServiceMock = new Mock <IAggregateService <DepositAggregate> >(); var accountServiceMock = new Mock <IAggregateService <AccountAggregate> >(); accountServiceMock.Setup(m => m.Load(It.IsAny <Guid>())).Returns(() => new AccountAggregate()); var eventPublisherMock = new Mock <IEventPublisher>(); eventPublisherMock.Setup(m => m.Publish(It.IsAny <IEvent>())).Callback((IEvent e) => publishedEvents.Add(e)); var commandHandler = new CreateDepositCommandHandler(depositServiceMock.Object, accountServiceMock.Object, eventPublisherMock.Object); var accountId = Guid.NewGuid(); ICommand command = new CreateDepositCommand { AccountId = accountId }; commandHandler.Handle(command); Assert.Single(publishedEvents); var publishedEvent = publishedEvents.First(); Assert.True(publishedEvent is ErrorEvent); Assert.Equal("Account not found", ((ErrorEvent)publishedEvent).ErrorMessage); Assert.Equal(JsonSerializer.Serialize(new AccountAggregate()), ((ErrorEvent)publishedEvent).ErrorDataJson); }
public void TestDepositGuidIsTaken() { var accountId = Guid.NewGuid(); var publishedEvents = new List <IEvent>(); var depositServiceMock = new Mock <IAggregateService <DepositAggregate> >(); var counter = 0; var depositGuid = Guid.Empty; depositServiceMock.Setup(m => m.Load(It.IsAny <Guid>())).Returns((Guid guid) => { var aggregate = new DepositAggregate(); if (counter > 3) { depositGuid = guid; return(aggregate); } counter++; aggregate.Apply(new CreateDepositEvent { ItemGuid = guid }); return(aggregate); }); var accountServiceMock = new Mock <IAggregateService <AccountAggregate> >(); accountServiceMock.Setup(m => m.Load(It.IsAny <Guid>())).Returns(() => new MockAggregate(accountId)); var eventPublisherMock = new Mock <IEventPublisher>(); eventPublisherMock.Setup(m => m.Publish(It.IsAny <IEvent>())).Callback((IEvent e) => publishedEvents.Add(e)); var commandHandler = new CreateDepositCommandHandler(depositServiceMock.Object, accountServiceMock.Object, eventPublisherMock.Object); var command = new CreateDepositCommand { AccountId = accountId }; commandHandler.Handle(command); Assert.Equal(2, publishedEvents.Count); var first = publishedEvents[0]; Assert.True(first is CreateDepositEvent); var createDepositEvent = (CreateDepositEvent)first; Assert.NotEqual(Guid.Empty, depositGuid); Assert.Equal(depositGuid, createDepositEvent.ItemGuid); Assert.Equal(Guid.Empty, publishedEvents[0].EventGuid); Assert.Equal(Guid.Empty, publishedEvents[1].EventGuid); }
public async Task Test_Transaction_Created() { //Arrange var options = new DbContextOptionsBuilder <BankAppDbContext>() .UseInMemoryDatabase(databaseName: "Test_Withdraw_exceed") .Options; int actual = 0; //Act using (var context = new BankAppDbContext(options)) { var senderAccount = new Account { Balance = 100, }; var recieverAccount = new Account { Balance = 0 }; context.AddRange(senderAccount, recieverAccount); await context.SaveChangesAsync(); var withdrawCommand = new CreateWithdrawCommandHandler(context); await withdrawCommand.Handle(new CreateWithdrawCommand { Amount = 10, AccountId = senderAccount.AccountId }, new CancellationToken()); var depositCommand = new CreateDepositCommandHandler(context); await depositCommand.Handle(new CreateDepositCommand { Amount = 10, AccountId = senderAccount.AccountId }, new CancellationToken()); var transferCommand = new CreateTransferCommandHandler(context); await transferCommand.Handle(new CreateTransferCommand { Amount = 10, SenderAccountId = senderAccount.AccountId, RecieverAccountId = recieverAccount.AccountId }, new CancellationToken()); actual = await context.Transactions.CountAsync(); } //Assert that method creates 4 transfers int expected = 4; Assert.AreEqual(expected, actual); }
public void TestHandle() { var accountId = Guid.NewGuid(); var publishedEvents = new List <IEvent>(); var depositServiceMock = new Mock <IAggregateService <DepositAggregate> >(); depositServiceMock.Setup(m => m.Load(It.IsAny <Guid>())).Returns((Guid guid) => new DepositAggregate()); var accountServiceMock = new Mock <IAggregateService <AccountAggregate> >(); accountServiceMock.Setup(m => m.Load(It.IsAny <Guid>())).Returns(() => new MockAggregate(accountId)); var eventPublisherMock = new Mock <IEventPublisher>(); eventPublisherMock.Setup(m => m.Publish(It.IsAny <IEvent>())).Callback((IEvent e) => publishedEvents.Add(e)); var commandHandler = new CreateDepositCommandHandler(depositServiceMock.Object, accountServiceMock.Object, eventPublisherMock.Object); var command = new CreateDepositCommand { AccountId = accountId }; commandHandler.Handle(command); Assert.Equal(2, publishedEvents.Count); var first = publishedEvents[0]; Assert.True(first is CreateDepositEvent); var createDepositEvent = (CreateDepositEvent)first; var second = publishedEvents[1]; Assert.True(second is AddDepositToAccountEvent); var addDepositToAccountEvent = (AddDepositToAccountEvent)second; Assert.Equal(createDepositEvent.ItemGuid, addDepositToAccountEvent.DepositId); Assert.Equal(createDepositEvent.AccountGuid, addDepositToAccountEvent.ItemGuid); }
public async Task Test_Negative_Deposit() { //Arrange var options = new DbContextOptionsBuilder <BankAppDbContext>() .UseInMemoryDatabase(databaseName: "Test_Negative_Deposit") .Options; //Act using (var context = new BankAppDbContext(options)) { var account = new Account { Balance = 10, }; context.Accounts.Add(account); await context.SaveChangesAsync(); var x = new CreateDepositCommandHandler(context); await x.Handle(new CreateDepositCommand { Amount = -20, AccountId = account.AccountId }, new CancellationToken()); } //Assert that method throws NegativeAmountException }
public void TestHandleEmptyGuid() { var publishedEvents = new List <IEvent>(); var depositServiceMock = new Mock <IAggregateService <DepositAggregate> >(); var accountServiceMock = new Mock <IAggregateService <AccountAggregate> >(); var eventPublisherMock = new Mock <IEventPublisher>(); eventPublisherMock.Setup(m => m.Publish(It.IsAny <IEvent>())).Callback((IEvent e) => publishedEvents.Add(e)); var commandHandler = new CreateDepositCommandHandler(depositServiceMock.Object, accountServiceMock.Object, eventPublisherMock.Object); var accountId = Guid.Empty; ICommand command = new CreateDepositCommand { AccountId = accountId }; commandHandler.Handle(command); Assert.Single(publishedEvents); var publishedEvent = publishedEvents.First(); Assert.True(publishedEvent is ErrorEvent); Assert.Equal("Account Guid empty", ((ErrorEvent)publishedEvent).ErrorMessage); Assert.Equal("{\"AccountId\":\"" + Guid.Empty + "\"}", ((ErrorEvent)publishedEvent).ErrorDataJson); }