public void When_a_second_pipeline_using_newer_schema_revision_is_started_a_new_project_is_created_and_linked_with_the_old_one() { var fixture = new Fixture(); fixture.Customize(new NumericSequencePerTypeCustomization()); var pipelineSchemaName = fixture.Create <string>(); var firstPipelineId = fixture.Create <string>(); var secondPipelineId = fixture.Create <string>(); var firstOccurenceDate = fixture.Create <DateTime>(); var secondOccurenceDate = fixture.Create <DateTime>(); var firstVersion = fixture.Create <int>(); var secondVersion = fixture.Create <int>(); var sut = new ProjectReadModel(); //First pipeline ProcessCreatedEvent(firstPipelineId, pipelineSchemaName, firstVersion, sut, firstOccurenceDate); //Ack: Second pipeline ProcessCreatedEvent(secondPipelineId, pipelineSchemaName, secondVersion, sut, secondOccurenceDate); var assertSession = SessionFactory.OpenSession(); var projects = assertSession.QueryOver <Project>() .Where(x => x.Name == pipelineSchemaName) .List(); Assert.AreEqual(2, projects.Count); var newProject = projects.First(x => x.IsLatestVersion); var oldProject = projects.First(x => !x.IsLatestVersion); Assert.AreSame(oldProject, newProject.PreviousSchemaView); Assert.AreEqual(secondVersion, newProject.SchemaVersion); Assert.AreEqual(firstVersion, oldProject.SchemaVersion); }
public void When_a_second_pipeline_using_same_schema_revision_is_started_nothing_happens() { var fixture = new Fixture(); var pipelineSchemaName = fixture.Create <string>(); var firstPipelineId = fixture.Create <string>(); var secondPipelineId = fixture.Create <string>(); var firstOccurenceDate = fixture.Create <DateTime>(); var secondOccurenceDate = fixture.Create <DateTime>(); var version = fixture.Create <int>(); var sut = new ProjectReadModel(); //First pipeline ProcessCreatedEvent(firstPipelineId, pipelineSchemaName, version, sut, firstOccurenceDate); //Act: Second pipeline ProcessCreatedEvent(secondPipelineId, pipelineSchemaName, version, sut, secondOccurenceDate); var assertSession = SessionFactory.OpenSession(); var projects = assertSession.QueryOver <Project>() .Where(x => x.Name == pipelineSchemaName) .List(); Assert.AreEqual(1, projects.Count); var project = projects[0]; Assert.AreEqual(version, project.SchemaVersion); Assert.IsNull(project.PreviousSchemaView); }
public async Task <List <ProjectReadModel> > ReadAllProjectsWithChildren() { try { var results = new List <ProjectReadModel>(); var result = await _connection.QueryAsync <ProjectModel, OrganisationModel, CategoryModel, ProjectResearcherModel, ProjectReadModel> ("USP_ReadAllProjects", (project, organisation, category, researcher) => { var lookupProject = results.FirstOrDefault(x => x.ProjectModel.ProjectId == project.ProjectId); if (lookupProject == null) { var projectReadModel = new ProjectReadModel(); projectReadModel.CategoryModels = projectReadModel.CategoryModels ?? new List <CategoryModel>(); projectReadModel.ResearcherModels = projectReadModel.ResearcherModels ?? new List <ProjectResearcherModel>(); projectReadModel.OrganisationsModels = projectReadModel.OrganisationsModels ?? new List <OrganisationModel>(); projectReadModel.ProjectModel = project; results.Add(projectReadModel); lookupProject = projectReadModel; } if (!lookupProject.OrganisationsModels.Any(x => x.OrganisationId == organisation.OrganisationId)) { lookupProject.OrganisationsModels.Add(organisation); } if (!lookupProject.CategoryModels.Any(x => x.CategoryId == category.CategoryId)) { lookupProject.CategoryModels.Add(category); } if (!lookupProject.ResearcherModels.Any(x => x.ReseacherId == researcher.ReseacherId)) { lookupProject.ResearcherModels.Add(researcher); } return(null); }, splitOn : "OrganisationId,CategoryId,ReseacherId", transaction : this._transaction); if (results == null) { return(null); } return(results); } catch (Exception e) { return(null); } }
public async void Handle_Exception_Test() { ProjectReadModel returnn = new ProjectReadModel(); this._unitOfUnitMock.Setup(mock => mock.ProjectRepository.ReadProjectWithChildren(It.IsAny <int>())) .Throws(new Exception()); ReadProjectCommand command = new ReadProjectCommand(1); ReadProjectHandler handler = new ReadProjectHandler(this._unitOfUnitMock.Object); var result = await handler.Handle(command, new CancellationTokenSource().Token); Assert.False((bool)result); }
public async void Handle_Succes_Test() { ProjectReadModel returnn = new ProjectReadModel(); this._unitOfUnitMock.Setup(mock => mock.ProjectRepository.ReadProjectWithChildren(It.IsAny <int>())) .Returns(Task.FromResult(returnn)); ReadProjectCommand command = new ReadProjectCommand(1); ReadProjectHandler handler = new ReadProjectHandler(this._unitOfUnitMock.Object); var result = await handler.Handle(command, new CancellationTokenSource().Token); Assert.Equal(result, returnn); }
public void ApplyEvent(ProjectCreated @event) { var readModel = new ProjectReadModel { ProjectId = @event.ProjectId, ProjectName = @event.ProjectName, ProjectAbbreviation = @event.ProjectAbbreviation, Description = @event.Description, ProjectType = @event.ProjectType.ToProjectTypeString(), IsCompleted = false }; _projectRepository.Save(readModel, justCreated: true); }
public void Save(ProjectReadModel readModel, bool justCreated = false) { var redisDatabase = _redisConnection.GetDatabase(); var transaction = redisDatabase.CreateTransaction(); transaction.StringIncrementAsync(_redisKeys.VersionKey); transaction.HashSetAsync(_redisKeys.GetHashKey(readModel.ProjectId), readModel.ToRedis()); if (justCreated) { // guid set transaction.SetAddAsync(_redisKeys.SetKey, readModel.ProjectId.ToString()); // name index transaction.SortedSetAddAsync(_redisKeys.ProjectNameIndexKey, $"{readModel.ProjectName.ToLowerInvariant()}:{readModel.ProjectName}:{readModel.ProjectId}", 0); // abbr index transaction.SortedSetAddAsync(_redisKeys.ProjectAbbreviationIndexKey, $"{readModel.ProjectAbbreviation.ToLowerInvariant()}:{readModel.ProjectAbbreviation}:{readModel.ProjectId}", 0); } if (!transaction.Execute()) { _logger.LogError("Could not save the readmodel to the database."); } }
public void Whren_a_pipeline_is_created_for_the_first_time_it_creates_a_new_project() { var fixture = new Fixture(); var pipelineSchemaName = fixture.Create <string>(); var pipelineId = fixture.Create <string>(); var occurenceDate = fixture.Create <DateTime>(); var version = fixture.Create <int>(); var sut = new ProjectReadModel(); ProcessCreatedEvent(pipelineId, pipelineSchemaName, version, sut, occurenceDate); var assertSession = SessionFactory.OpenSession(); var projects = assertSession.QueryOver <Project>() .Where(x => x.Name == pipelineSchemaName) .List(); Assert.AreEqual(1, projects.Count); var project = projects[0]; Assert.AreEqual(version, project.SchemaVersion); Assert.IsNull(project.PreviousSchemaView); }
public async Task <ProjectReadModel> ReadProjectWithChildren(int id) { try { var projectReadModel = new ProjectReadModel(); projectReadModel.CategoryModels = new List <CategoryModel>(); projectReadModel.OrganisationsModels = new List <OrganisationModel>(); projectReadModel.ResearcherModels = new List <ProjectResearcherModel>(); var parameter = new DynamicParameters(); parameter.Add("@ProjectId", id); var response = await _connection.QueryAsync <Project, OrganisationModel, CategoryModel, ProjectResearcherModel, Project> ("USP_ReadProjectWithChildren @ProjectId", (project, organisation, category, researcherCategory) => { if (projectReadModel.ProjectModel == null) { var projectModel = new ProjectModel(); projectReadModel.ProjectModel = projectModel; projectReadModel.ProjectModel.ProjectId = project.Id; projectReadModel.ProjectModel.ProjectCreated = project.Created; projectReadModel.ProjectModel.ProjectName = project.ProjectName; projectReadModel.ProjectModel.StartDate = project.StartDate; projectReadModel.ProjectModel.EndDate = project.EndDate; projectReadModel.OrganisationsModels = projectReadModel.OrganisationsModels ?? new List <OrganisationModel>(); projectReadModel.CategoryModels = projectReadModel.CategoryModels ?? new List <CategoryModel>(); projectReadModel.ResearcherModels = projectReadModel.ResearcherModels ?? new List <ProjectResearcherModel>(); } if (researcherCategory != null) { if (!projectReadModel.ResearcherModels.Any(x => x.ProjectResearcherCatId == researcherCategory.ProjectResearcherCatId && x.ReseacherId == researcherCategory.ReseacherId)) { projectReadModel.ResearcherModels.Add(researcherCategory); } } if (category != null) { if (!projectReadModel.CategoryModels.Any(x => x.CategoryId == category.CategoryId)) { projectReadModel.CategoryModels.Add(category); } } if (organisation != null) { if (!projectReadModel.OrganisationsModels.Any(x => x.OrganisationId == organisation.OrganisationId)) { projectReadModel.OrganisationsModels.Add(organisation); } } return(null); }, splitOn : "OrganisationId, CategoryId, ReseacherId", transaction : this._transaction, param : parameter); if (projectReadModel == null) { return(null); } return(projectReadModel); } catch (Exception e) { return(null); } }
private void ProcessCreatedEvent(string pipelineId, string schemaName, int schemaVersion, ProjectReadModel sut, DateTime occurenceDate) { using (var session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { var evnt = new PipelineCreatedEvent(new UniquePipelineId(pipelineId, schemaName), CreatePipelineSchema(schemaName, schemaVersion)); sut.On(new EventEnvelope <PipelineCreatedEvent>(pipelineId, evnt, occurenceDate, session)); transaction.Commit(); } }
public ActionResult Edit(ProjectReadModel model) { _projectReadStore.Update(model); return RedirectToAction("Index"); }