Esempio n. 1
0
        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.IntValue, 2);

            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> moqEventStoreRepository =
                new Mock <IEventStoreRepository>();

            moqEventStoreRepository.Setup(m =>
                                          m.GetByIdAsync <Domain.SpeechAggregate.Speech>(It.IsAny <Guid>()))
            .Returns(Task.FromResult(speech));

            IUpdateSpeechUseCase usecase = new SpeechUseCase(moqSpeechRepository.Object,
                                                             mockEventSourcingSubscriber.Object, moqEventStoreRepository.Object);
            //Act
            //Assert
            await Assert.ThrowsAsync <ConcurrencyException>(() => usecase.Handle(command));
        }
Esempio n. 2
0
        public async Task Verify_that_UpdateAsync_can_be_called_on_SpeechRepository_and_fire_RepositoryUpdateAsync_only_once()
        {
            //Arrange

            Guid   speechId    = Guid.NewGuid();
            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";
            var    speech      = new Domain.SpeechAggregate.Speech(speechId,
                                                                   new Title(newTitle), new UrlValue("http://mysite.com"), new Description(description),
                                                                   SpeechType.Conferences);

            Mock <IRepository <Domain.SpeechAggregate.Speech, Guid> > mockRepository =
                new Mock <IRepository <Domain.SpeechAggregate.Speech, Guid> >();

            mockRepository.Setup(x => x.UpdateAsync(It.IsAny <Domain.SpeechAggregate.Speech>()))
            .Returns(Task.CompletedTask)
            .Callback <Domain.SpeechAggregate.Speech>(x => { });

            var options = new DbContextOptionsBuilder <DataBaseContext>().UseInMemoryDatabase(databaseName: "InMemoryDB").Options;
            var context = new DataBaseContext(options);
            await context.Speech.AddAsync(speech);

            ISpeechRepository sut = new SpeechRepository(mockRepository.Object, context);

            //Act
            await sut.DeleteAsync(speech);

            //Assert
            mockRepository.Verify(x => x.UpdateAsync(It.IsAny <Domain.SpeechAggregate.Speech>()), Times.Once);
        }
Esempio n. 3
0
        public async Task HandlingUpdateWhenTheSpeechIsValidAndExistShouldPerformUpdate()
        {
            //Arrange
            Guid   speechId    = Guid.NewGuid();
            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";
            var    speech      = new Domain.SpeechAggregate.Speech(speechId,
                                                                   new Title(newTitle), new UrlValue("http://mysite.com"), new Description(description),
                                                                   SpeechType.Conferences);

            Mock <IRepository <Domain.SpeechAggregate.Speech, Guid> > mockRepository =
                new Mock <IRepository <Domain.SpeechAggregate.Speech, Guid> >();

            DbContextOptionsBuilder <DataBaseContext> optionsBuilder = new DbContextOptionsBuilder <DataBaseContext>();

            optionsBuilder.UseInMemoryDatabase("FakeInMemoryData");
            var context = new DataBaseContext(optionsBuilder.Options);
            //context.Speech.Add(speechToUpdate);
            //context.SaveChanges();
            ISpeechRepository sut = new SpeechRepository(mockRepository.Object, context);

            //Act
            await sut.UpdateAsync(speech);

            //Assert
            var result = context.Entry(speech).Entity.Title.Value;

            Assert.Equal(newTitle, result);
        }
        public async Task HandlingUpdateWhenTheSpeechDoesNotExistShouldRaiseRepositoryNotFoundException()
        {
            //Arrange
            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";

            var speech = new Domain.SpeechAggregate.Speech(Guid.NewGuid(),
                                                           new Title(newTitle),
                                                           new UrlValue("http://mysite.com"),
                                                           new Description(description), SpeechType.Conferences);

            Mock <IRepository <Domain.SpeechAggregate.Speech, Guid> > mockRepository =
                new Mock <IRepository <Domain.SpeechAggregate.Speech, Guid> >();

            DbContextOptionsBuilder <DataBaseContext> optionsBuilder
                = new DbContextOptionsBuilder <DataBaseContext>();

            optionsBuilder.UseInMemoryDatabase("FakeInMemoryData");
            var context = new DataBaseContext(optionsBuilder.Options);

            ISpeechRepository sut = new SpeechRepository(mockRepository.Object, context);

            //Act
            //Assert
            await Assert.ThrowsAsync <NotFoundRepositoryException>(() => sut.UpdateAsync(speech));
        }
Esempio n. 5
0
        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 newDescription = @"new 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";
            string newUrl         = "http://www.test_new.com";

            var type    = SpeechType.Conferences.Value.ToString();
            var newType = SpeechType.SelfPacedLabs.IntValue;
            UpdateSpeechCommandMessage command = new UpdateSpeechCommandMessage(Guid.NewGuid(),
                                                                                newTitle, newDescription, newUrl, newType, 0);

            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), new SpeechType(type));

            Mock <IEventStoreRepository> moqEventStoreRepository =
                new Mock <IEventStoreRepository>();

            moqEventStoreRepository.Setup(m =>
                                          m.GetByIdAsync <Domain.SpeechAggregate.Speech>(It.IsAny <Guid>()))
            .Returns(Task.FromResult(speech));

            IUpdateSpeechUseCase usecase = new SpeechUseCase(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.Value))
                                                                                           )), Times.Once);

            mockEventSourcingSubscriber.Verify(m =>
                                               m.Subscribe(It.IsAny <IEventSourcing>()), Times.Once);
        }
Esempio n. 6
0
        public async Task Handle([NotNull] RegisterSpeechCommandMessage command)
        {
            var title       = command.Title;
            var urlValue    = command.Url;
            var description = command.Description;
            var type        = command.Type;
            var speech      = new Domain.SpeechAggregate.Speech(title, urlValue, description, type);
            await _speechRepository.CreateAsync(speech);

            _unitOfWork.Commit();
        }
Esempio n. 7
0
        public async Task DeleteAsync(Domain.SpeechAggregate.Speech speech)
        {
            if (speech == null)
            {
                throw new ArgumentNullRepositoryException(nameof(speech));
            }
            _context.ChangeTracker.TrackGraph(speech, a =>
            {
                a.Entry.State = EntityState.Modified;
            });

            await _repository.UpdateAsync(speech);
        }
Esempio n. 8
0
        public async Task UpdateAsync(Domain.SpeechAggregate.Speech speech)
        {
            if (speech == null)
            {
                throw new ArgumentNullRepositoryException(nameof(speech));
            }

            var existingSpeech = await _context.Speech
                                 .Include(b => b.MediaFileItems)
                                 .FirstOrDefaultAsync(b => b.Id == speech.Id);

            _context.Entry(existingSpeech ?? throw new NotFoundRepositoryException(nameof(existingSpeech))).CurrentValues.SetValues(speech);
        }
Esempio n. 9
0
        public async Task Handle(RegisterSpeechCommandMessage command)
        {
            if (command == null)
            {
                throw new ArgumentNullApplicationException(nameof(command));
            }

            var title       = new Title(command.Title);
            var urlValue    = new UrlValue(command.Url);
            var description = new Description(command.Description);
            var type        = new SpeechType(command.Type);

            var speech = new Domain.SpeechAggregate.Speech(AggregateId.NewId(), title, urlValue, description, type);
            await _speechRepository.CreateAsync(speech);

            await _domainEventSubscriber.Subscribe(speech);
        }
        public async Task Handle(RegisterSpeechCommandMessage command)
        {
            if (command == null)
            {
                throw new ApplicationArgumentNullException(nameof(command));
            }

            var title       = new Title(command.Title);
            var urlValue    = new UrlValue(command.Url);
            var description = new Description(command.Description);
            var type        = new SpeechType(command.Type);

            var speech = new Domain.SpeechAggregate.Speech(title, urlValue, description, type);
            await _speechRepository.CreateAsync(speech);

            _unitOfWork.Commit();
        }
Esempio n. 11
0
        public async Task UpdateAsync(Domain.SpeechAggregate.Speech speech)
        {
            if (speech == null)
            {
                throw new ArgumentNullRepositoryException(nameof(speech));
            }
            _context.ChangeTracker.TrackGraph(speech, a =>
            {
                a.Entry.State = a.Entry.IsKeySet ? EntityState.Modified : EntityState.Added;
            });

            foreach (var item in _context.ChangeTracker.Entries())
            {
                Console.WriteLine(item.Entity.GetType().Name, item.State.ToString());
            }

            await Task.CompletedTask;
        }
Esempio n. 12
0
        public async Task DeleteSpeechUseCaseWithValidInputReturnSuccessTest()
        {
            //Arrange
            long   orignalVersion = 0;
            var    id             = Guid.NewGuid();
            string title          = @"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";
            var    speech         = new Domain.SpeechAggregate.Speech(id,
                                                                      new Title(title), new UrlValue("http://mysite.com"),
                                                                      new Description(description), SpeechType.Conferences);

            /* ------------ 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.DeleteAsync(It.IsAny <Domain.SpeechAggregate.Speech>()))
            .Returns(Task.FromResult <ISpeechRepository>(null)).Verifiable();

            var mockEventStoreRepository = new Mock <IEventStoreRepository>();

            mockEventStoreRepository.Setup(m => m.GetByIdAsync <Domain.SpeechAggregate.Speech>(It.IsAny <Guid>())).Returns(Task.FromResult(speech));

            // ------------ I'm on the command side of CQRS pattern, so I don't need an output port
            // ------------ I need a command to delete a new speech
            var deleteSpeechCommand = new DeleteSpeechCommandMessage(id, orignalVersion);

            var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>();
            //Act
            // ------------ DeleteSpeechUseCase is the object under test
            IDeleteSpeechUseCase usecase = new SpeechUseCase(moqSpeechRepository.Object, mockEventSourcingSubscriber.Object, mockEventStoreRepository.Object);

            await usecase.Handle(deleteSpeechCommand);

            //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.DeleteAsync(It.IsAny <Domain.SpeechAggregate.Speech>()), Times.Once,
                                       "DeleteAsync must be called only once");
        }
Esempio n. 13
0
 public async Task CreateAsync(Domain.SpeechAggregate.Speech speech)
 {
     await _repository.CreateAsync(speech);
 }