public async Task Handle_ConfigurationEntryModifiedEvent_UpdatesValues()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();
            var initialEntry   = initialEntity.Entries.First();

            var message = new ConfigurationEntryModified
            {
                Id      = initialProject.Id,
                EntryId = initialEntry.Id,
                Values  = new Dictionary <Guid, object>
                {
                    { initialEntity.Properties.ElementAt(0).Id, "TestValue1" },
                    { initialEntity.Properties.ElementAt(1).Id, "TestValue2" }
                },
                Version   = 2,
                TimeStamp = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var values = this.context.DbContext.Values.Where(p => p.EntryId == message.EntryId);

            Assert.Equal(2, values.Count());
            Assert.Contains(values, v => v.Value == "TestValue1");
            Assert.Contains(values, v => v.Value == "TestValue2");
        }
        public async Task Handle_EntityDefinitionModifiedEvent_UpdatesEntity()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();

            var message = new EntityDefinitionModified
            {
                Id          = initialProject.Id,
                EntityId    = initialEntity.Id,
                Version     = 2,
                TimeStamp   = DateTimeOffset.Now,
                Name        = "New Entity Name",
                Description = "new Entity Description"
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var entity = this.context.DbContext.Entities.FirstOrDefault(p => p.Id == message.EntityId);

            Assert.NotNull(entity);
            Assert.Equal(message.EntityId, entity.Id);
            Assert.Equal(message.Id, entity.ProjectId);
            Assert.Equal(message.Name, entity.Name);
            Assert.Equal(message.Description, entity.Description);
        }
        public async Task Handle_ConfigurationEntryDeletedEvent_DeletesValues()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();
            var initialEntry   = initialEntity.Entries.First();

            var message = new ConfigurationEntryDeleted
            {
                Id        = initialProject.Id,
                EntryId   = initialEntry.Id,
                Version   = 2,
                TimeStamp = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var values = this.context.DbContext.Values.Where(e => e.EntryId == initialEntry.Id);

            Assert.Empty(values);
        }
        public async Task Handle_EntityDefinitionAddedEvent_UpdatesProjectVersionTracking()
        {
            // arrange
            var initialProject = this.CreateProject();

            var message = new EntityDefinitionAdded
            {
                Id          = initialProject.Id,
                EntityId    = Guid.NewGuid(),
                Version     = 2,
                TimeStamp   = DateTimeOffset.Now,
                Name        = "New Entity Name",
                Description = "new Entity Description"
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var project = this.context.DbContext.Projects.First(p => p.Id == message.Id);

            Assert.Equal(message.Id, project.Id);
            Assert.Equal(message.Version, project.Version);
            Assert.Equal(message.TimeStamp, project.TimeStamp);
        }
        public async Task Handle_ProjectCreatedEvent_CreatesProject()
        {
            // arrange
            var message = new ProjectCreated
            {
                Id          = Guid.NewGuid(),
                Version     = 1,
                TimeStamp   = DateTimeOffset.Now,
                Name        = "New Project",
                Description = "A new Project Scription"
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var project = this.context.DbContext.Projects.FirstOrDefault(p => p.Id == message.Id);

            Assert.NotNull(project);
            Assert.Equal(message.Version, project.Version);
            Assert.Equal(message.TimeStamp, project.TimeStamp);
            Assert.Equal(message.Name, project.Name);
            Assert.Equal(message.Description, project.Description);
            Assert.Null(project.Description);
        }
        public async Task Handle_PropertyDefinitionAddedEvent_AddsValueToEntry()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();
            var initialEntry   = initialEntity.Entries.First();

            var message = new PropertyDefinitionAdded
            {
                Id             = initialProject.Id,
                ParentEntityId = initialEntity.Id,
                PropertyId     = Guid.NewGuid(),
                Name           = "New property name",
                Description    = "New property description",
                Version        = 2,
                TimeStamp      = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var values = this.context.DbContext.Values.Where(p => p.EntryId == initialEntry.Id);

            Assert.Equal(3, values.Count());

            var propertyValue = values.FirstOrDefault(v => v.PropertyId == message.PropertyId);

            Assert.NotNull(propertyValue);
            Assert.Equal(propertyValue.Value, null);
        }
        public async Task Handle_ConfigurationEntryAddedEvent_CreatesValues()
        {
            // arrange
            var initialProject  = this.CreateProject();
            var initialEntity   = initialProject.Entities.First();
            var initialProperty = initialEntity.Properties.First();

            var message = new ConfigurationEntryAdded
            {
                Id       = initialProject.Id,
                EntryId  = Guid.NewGuid(),
                EntityId = initialEntity.Id,
                Values   = new Dictionary <Guid, object> {
                    { initialProperty.Id, "TestValue" }
                },
                Version   = 2,
                TimeStamp = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var value = this.context.DbContext.Values.FirstOrDefault(p => p.EntryId == message.EntryId);

            Assert.NotNull(value);
            Assert.Equal(initialProperty.Id, value.PropertyId);
            Assert.Equal("TestValue", value.Value);
        }
        public async Task Handle_ConfigurationEntryModifiedEvent_UpdatesProjectVersionTracking()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();
            var initialEntry   = initialEntity.Entries.First();

            var message = new ConfigurationEntryModified
            {
                Id      = initialProject.Id,
                EntryId = initialEntry.Id,
                Values  = new Dictionary <Guid, object>
                {
                    { initialEntity.Properties.ElementAt(0).Id, "TestValue1" },
                    { initialEntity.Properties.ElementAt(1).Id, "TestValue2" }
                },
                Version   = 2,
                TimeStamp = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var project = this.context.DbContext.Projects.First(p => p.Id == message.Id);

            Assert.Equal(message.Id, project.Id);
            Assert.Equal(message.Version, project.Version);
            Assert.Equal(message.TimeStamp, project.TimeStamp);
        }
        public async Task Handle_ProjectModifiedEvent_UpdatesProject()
        {
            // arrange
            var initialProject = this.CreateProject();

            var message = new ProjectModified
            {
                Id          = initialProject.Id,
                Version     = 2,
                TimeStamp   = DateTimeOffset.Now,
                Name        = "New Project name",
                Description = "New Project description"
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var project = this.context.DbContext.Projects.FirstOrDefault(p => p.Id == message.Id);

            Assert.NotNull(project);
            Assert.Equal(message.Version, project.Version);
            Assert.Equal(message.TimeStamp, project.TimeStamp);
            Assert.Equal(message.Name, project.Name);
            Assert.Equal(message.Description, project.Description);
        }
        public async Task Handle_PropertyDefinitionModifiedEvent_UpdatesProperty()
        {
            // arrange
            var initialProject  = this.CreateProject();
            var initialProperty = initialProject.Entities.First().Properties.First();

            var message = new PropertyDefinitionModified
            {
                Id           = initialProject.Id,
                PropertyId   = initialProperty.Id,
                Name         = "New property name",
                Description  = "New property description",
                PropertyType = "Zuehlke.Eacm.String",
                Version      = 2,
                TimeStamp    = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var property = this.context.DbContext.Properties.FirstOrDefault(p => p.Id == message.PropertyId);

            Assert.NotNull(property);
            Assert.Equal(message.Name, property.Name);
            Assert.Equal(message.Description, property.Description);
            Assert.Equal(message.PropertyType, property.PropertyType);
        }
        public async Task Handle_ConfigurationEntryDeletedEvent_UpdatesProjectVersionTracking()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();
            var initialEntry   = initialEntity.Entries.First();

            var message = new ConfigurationEntryDeleted
            {
                Id        = initialProject.Id,
                EntryId   = initialEntry.Id,
                Version   = 2,
                TimeStamp = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var project = this.context.DbContext.Projects.First(p => p.Id == message.Id);

            Assert.Equal(message.Id, project.Id);
            Assert.Equal(message.Version, project.Version);
            Assert.Equal(message.TimeStamp, project.TimeStamp);
        }
        public async Task UpdateReadmodelHandler()
        {
            EntityGuid = Guid.NewGuid();

            var queryRepository = new ReadModelRepositoryMongoDb(EventMongoDb);

            var readModelHandler = new ReadModelEventHandler <TestReadModelQuerries>(queryRepository,
                                                                                     new VersionRepositoryMongoDb(EventMongoDb), new FeedMock2());
            await readModelHandler.UpdateAsync();

            var result = await queryRepository.LoadAsync <TestReadModelQuerries>(EntityGuid.ToString());

            Assert.AreEqual(EntityGuid.ToString(), result.Value.Id);
            Assert.AreEqual(14, result.Value.Version);
            Assert.AreEqual("testName", result.Value.Name);
        }
        public async Task UpdateModel_TwoEntities()
        {
            EntityGuid  = Guid.NewGuid();
            EntityGuid2 = Guid.NewGuid();

            var queryRepository = new ReadModelRepositoryMongoDb(EventMongoDb);

            var readModelHandler = new ReadModelEventHandler <TestReadModelQuerries>(queryRepository,
                                                                                     new VersionRepositoryMongoDb(EventMongoDb), new FeedMock3());

            await readModelHandler.UpdateAsync();

            var result = await queryRepository.LoadAsync <TestReadModelQuerries>(EntityGuid.ToString());

            var result2 = await queryRepository.LoadAsync <TestReadModelQuerries>(EntityGuid2.ToString());

            Assert.AreEqual(EntityGuid.ToString(), result.Value.Id);
            Assert.AreEqual(EntityGuid2.ToString(), result2.Value.Id);
        }
        public async Task UpdateModel_VersionUpdatedExplicitly()
        {
            EntityGuid = Guid.NewGuid();

            var queryRepository = new ReadModelRepositoryMongoDb(EventMongoDb);

            var readModelHandler = new ReadModelEventHandler <TestReadModelQuerries_VerionedHandle>(
                queryRepository,
                new VersionRepositoryMongoDb(EventMongoDb),
                new FeedMockVersioned());

            await readModelHandler.UpdateAsync();

            var result = await queryRepository.LoadAsync <TestReadModelQuerries_VerionedHandle>(EntityGuid.ToString());

            Assert.AreEqual(EntityGuid.ToString(), result.Value.EntityId);
            Assert.AreEqual(12, result.Value.InnerVersion);
            Assert.AreEqual(14, result.Value.Version);
        }
        public async Task UpdateModel_EventsNotAppliedStillUpdatesVersion()
        {
            EntityGuid = Guid.NewGuid();

            var queryRepository = new ReadModelRepositoryMongoDb(EventMongoDb);

            var readModelHandler = new ReadModelEventHandler <TestReadModelQuerries_OnlyOneEventAndVersionIsCounted>(
                queryRepository,
                new VersionRepositoryMongoDb(EventMongoDb),
                new FeedMock5());

            await readModelHandler.UpdateAsync();

            var result = await queryRepository.LoadAsync <TestReadModelQuerries_OnlyOneEventAndVersionIsCounted>
                             (EntityGuid.ToString());

            Assert.AreEqual(14, result.Value.Version);
            Assert.AreEqual(null, result.Value.Name);
            Assert.AreEqual(EntityGuid.ToString(), result.Value.Id);
        }
        public async Task UpdateModel_EventsPresentThatAreNotHandleble()
        {
            EntityGuid  = Guid.NewGuid();
            EntityGuid2 = Guid.NewGuid();

            var queryRepository = new ReadModelRepositoryMongoDb(EventMongoDb);

            var readModelHandler = new ReadModelEventHandler <TestReadModelQuerries>(queryRepository, new VersionRepositoryMongoDb(EventMongoDb), new FeedMock4());

            await readModelHandler.UpdateAsync();

            var result = await queryRepository.LoadAsync <TestReadModelQuerries>(EntityGuid.ToString());

            var result2 = await queryRepository.LoadAsync <TestReadModelQuerries>(EntityGuid2.ToString());

            Assert.AreEqual(EntityGuid.ToString(), result.Value.Id);
            var condition = result2.Is <NotFound>();

            Assert.IsTrue(condition);
        }
        public async Task UpdateModel_TwoParallelReadModelHandler_SerializationBug()
        {
            EntityGuid  = Guid.NewGuid();
            EntityGuid2 = Guid.NewGuid();

            var queryRepository = new ReadModelRepositoryMongoDb(EventMongoDb);

            var readModelHandler = new ReadModelEventHandler <TestReadModelQuerries_TwoParallelFeeds1>(queryRepository, new VersionRepositoryMongoDb(EventMongoDb), new FeedMock6());

            var readModelHandler2 = new ReadModelEventHandler <TestReadModelQuerries_TwoParallelFeeds2>(queryRepository, new VersionRepositoryMongoDb(EventMongoDb), new FeedMock7());

            await readModelHandler.UpdateAsync();

            await readModelHandler2.UpdateAsync();

            var result = await queryRepository.LoadAsync <TestReadModelQuerries_TwoParallelFeeds1>(EntityGuid.ToString());

            var result2 = await queryRepository.LoadAsync <TestReadModelQuerries_TwoParallelFeeds2>(EntityGuid2.ToString());

            Assert.AreEqual(EntityGuid.ToString(), result.Value.Id);
            Assert.AreEqual(EntityGuid2.ToString(), result2.Value.IdTotallyDifferenzt);
        }
        public async Task Handle_EntityDefinitionDeletedEvent_RemovesEntriesOfEntity()
        {
            // arrange
            var initialProject = this.CreateProject();
            var initialEntity  = initialProject.Entities.First();

            var message = new EntityDefinitionDeleted
            {
                Id        = initialProject.Id,
                EntityId  = initialEntity.Id,
                Version   = 2,
                TimeStamp = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var entries = this.context.DbContext.Entries.Where(p => p.EntityId == message.EntityId);

            Assert.Empty(entries);
        }
        public async Task Handle_PropertyDefinitionDeletedEvent_DeletesProperty()
        {
            // arrange
            var initialProject  = this.CreateProject();
            var initialProperty = initialProject.Entities.First().Properties.First();

            var message = new PropertyDefinitionDeleted
            {
                Id         = initialProject.Id,
                PropertyId = initialProperty.Id,
                Version    = 2,
                TimeStamp  = DateTimeOffset.Now
            };

            var target = new ReadModelEventHandler(this.context.DbContext, this.context.Mapper);

            // act
            await target.Handle(message);

            // assert
            var property = this.context.DbContext.Properties.FirstOrDefault(p => p.Id == message.PropertyId);

            Assert.Null(property);
        }