public async Task HandlingUpdateWhenExpectedVersionIsNotEqualToAggregateVersionShouldRaiseConcurrencyException() { //Arrange string title = @"Lorem Ipsum is simply dummy text"; string newTitle = @"New Lorem Ipsum is simply dummy text"; string description = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"; string url = "http://www.test.com"; UpdateSpeechCommandMessage command = new UpdateSpeechCommandMessage(Guid.NewGuid(), newTitle, description, url, SpeechType.Conferences.ToString(), 2); Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>(); Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); var speech = new Domain.SpeechAggregate.Speech(Guid.NewGuid(), new Title(title), new UrlValue("http://mysite.com"), new Description(description), SpeechType.Conferences); Mock <IEventStoreRepository <Domain.SpeechAggregate.Speech> > moqEventStoreRepository = new Mock <IEventStoreRepository <Domain.SpeechAggregate.Speech> >(); moqEventStoreRepository.Setup(m => m.GetByIdAsync <Domain.SpeechAggregate.Speech>(It.IsAny <Guid>())) .Returns(Task.FromResult(speech)); IUpdateSpeechUseCase usecase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object, mockEventSourcingSubscriber.Object, moqEventStoreRepository.Object); //Act //Assert await Assert.ThrowsAsync <ConcurrencyException>(() => usecase.Handle(command)); }
public async Task HandlingUpdateWhenCommandIsNotNullShouldUpdateSpeechTitle() { //Arrange string title = @"Lorem Ipsum is simply dummy text"; string newTitle = @"New Lorem Ipsum is simply dummy text"; string description = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"; string url = "http://www.test.com"; UpdateSpeechCommandMessage command = new UpdateSpeechCommandMessage(Guid.NewGuid(), newTitle, description, url, SpeechType.Conferences.Value.ToString(), 0); Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>(); Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); moqSpeechRepository.Setup(x => x.UpdateAsync(It.IsAny <Domain.SpeechAggregate.Speech>())) .Returns(Task.CompletedTask).Verifiable(); var speech = new Domain.SpeechAggregate.Speech(Guid.NewGuid(), new Title(title), new UrlValue(url), new Description(description), SpeechType.Conferences); Mock <IEventStoreRepository <Domain.SpeechAggregate.Speech> > moqEventStoreRepository = new Mock <IEventStoreRepository <Domain.SpeechAggregate.Speech> >(); moqEventStoreRepository.Setup(m => m.GetByIdAsync <Domain.SpeechAggregate.Speech>(It.IsAny <Guid>())) .Returns(Task.FromResult(speech)); IUpdateSpeechUseCase usecase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object, mockEventSourcingSubscriber.Object, moqEventStoreRepository.Object); //Act await usecase.Handle(command); //Assert moqSpeechRepository.Verify(m => m.UpdateAsync(It.Is <Domain.SpeechAggregate.Speech>(n => n.Id.Equals(speech.Id) && n.Description.Value.Equals(command.Description, StringComparison.InvariantCultureIgnoreCase) && n.Title.Value.Equals(command.Title) && n.Url.Value.Equals(command.Url, StringComparison.InvariantCultureIgnoreCase) && n.Type.Equals(new SpeechType(command.Type)) )), Times.Once); mockEventSourcingSubscriber.Verify(m => m.Subscribe(It.IsAny <IEventSourcing>()), Times.Once); moqUnitOfWork.Verify(m => m.Commit(), Times.Once); }
public async Task RegisterSpeechUseCaseWithNullSpeechThrowsExceptionTest() { //Arrange Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); //Act IRegisterSpeechUseCase usecase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object); //Assert await Assert.ThrowsAsync <ApplicationArgumentNullException>(() => usecase.Handle(null)); }
public async Task RegisterSpeechUseCaseWithNullSpeechThrowsExceptionTest() { //Arrange Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); var moqSpeechRepository = new Mock <ISpeechRepository>(); //Act var useCase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object); //Assert await Assert.ThrowsAsync <NullReferenceException>(() => useCase.Handle(null)); }
public async Task RegisterSpeechUseCaseWithNullSpeechThrowsExceptionTest() { //Arrange Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>(); //Act IRegisterSpeechUseCase usecase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object, mockEventSourcingSubscriber.Object, It.IsAny <IEventStoreRepository <Domain.SpeechAggregate.Speech> >()); //Assert await Assert.ThrowsAsync <ArgumentNullApplicationException>(() => usecase.Handle(null)); }
public async Task RegisterSpeechUseCaseWithValidInputReturnSuccessTest() { //Arrange /* ------------ I will use UnitOfWork pattern, it will help me to treat aggregate roots * as a unit for the purpose of data changes */ Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); moqUnitOfWork.Setup(m => m.Commit()).Verifiable(); /* ------------ I will use repository pattern, aggregate roots are the only objects my * code loads from the repository.*/ Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); moqSpeechRepository.Setup(m => m.CreateAsync(It.IsAny <Domain.SpeechAggregate.Speech>())) .Returns(Task.FromResult <ISpeechRepository>(null)).Verifiable(); // ------------ I'm on the command side of CQRS pattern, so I don't need an output port // ------------ I need a command to regsiter a new speech var registerSpeechCommand = new RegisterSpeechCommandMessage( "Microservices getting started", "A Microservices from scratch online event Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took rem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "http://microservices-getting-started.logcorner.com", "2"); var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>(); //Act // ------------ RegisterSpeechUseCase is the object under test IRegisterSpeechUseCase usecase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object, mockEventSourcingSubscriber.Object); await usecase.Handle(registerSpeechCommand); //Assert /* ------------ The object returns void , so I will verify that a new Speech will be inserted into the database * when SaveChanges is called.*/ moqSpeechRepository.Verify(m => m.CreateAsync(It.IsAny <Domain.SpeechAggregate.Speech>()), Times.Once, "CreateAsync must be called only once"); // Verify that SaveChanges is called moqUnitOfWork.Verify(m => m.Commit(), Times.Once, "Commit must be called only once"); }
public async Task RegisterSpeechUseCaseSuccessTest() { //Arrange //Use repository pattern to aggregate roots that are the only objects to be loaded from the repository Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); moqSpeechRepository.Setup(m => m.CreateAsync(It.IsAny <Domain.SpeechAggregate.Speech>())).Returns(Task.FromResult <ISpeechRepository>(null)).Verifiable(); /* Using UnitOFWork pattern, helps t treat aggregate rootsa as a unit for the purpose of datachanges */ var moqUnitOfWork = new Mock <IUnitOfWork>(); moqUnitOfWork.Setup(m => m.Commit()).Verifiable(); //Command side of the CQRS pattern, no need for an output port //Register a new speech var registerSpeechCommand = new RegisterSpeechCommandMessage( "Microservices getting started", "A microservices from scratch online event", "https://github.com/Dontunee/BuildingMicroservicesThroughEventDrivenArchitecture", "2" ); //Act //RegisterSpeechUseCase is the object under test IRegisterSpeechUseCase registerSpeechUseCase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object); await registerSpeechUseCase.Handle(registerSpeechCommand); //Assert //Verify that a new speech will be inserted into the database when savechanges is called // var data = new Mock<Domain.SpeechAggregate.Speech>(); //moqSpeechRepository.Verify(m => m.CreateAsync(data.Object), Times.Once, "Create async must be called only once"); //Verify that savechanges is called moqUnitOfWork.Verify(m => m.Commit(), Times.Once, "Commit must be called only once"); }
public async Task HandlingUpdateWhenSpeechDoesNotExistShouldRaiseApplicationNotFoundException() { //Arrange UpdateSpeechCommandMessage command = new UpdateSpeechCommandMessage( Guid.Empty, null, null, null, null, 0); Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>(); Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>(); var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>(); Mock <IEventStoreRepository <Domain.SpeechAggregate.Speech> > moqEventStoreRepository = new Mock <IEventStoreRepository <Domain.SpeechAggregate.Speech> >(); moqEventStoreRepository.Setup(m => m.GetByIdAsync <Domain.SpeechAggregate.Speech>(command.SpeechId)) .Returns(Task.FromResult((Domain.SpeechAggregate.Speech)null)); IUpdateSpeechUseCase usecase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object, mockEventSourcingSubscriber.Object, moqEventStoreRepository.Object); //Act //Assert await Assert.ThrowsAsync <NotFoundApplicationException>(() => usecase.Handle(command)); }