예제 #1
0
        public async void Create_MarkSessionModelAlreadyExists_ThrowsException()
        {
            // Arrange
            var existingMarkSessionModel = new MarkSessionModel(
                "be69cb8c-45e4-4d80-8d55-419984aa2151",
                "be69cb8c-45e4-4d80-8d55-419984aa2151",
                ResourceTypeEnum.Project,
                MarkSessionTypeEnum.ToBeDeleted
                );
            var       dbMongoService        = new DbMongoService();
            var       markSessionRepository = new MarkSessionRepository(dbMongoService);
            Exception exception             = null;

            try
            {
                // Act
                await markSessionRepository.Create(existingMarkSessionModel);
            }
            catch (MarkSessionAlreadyExistsException e)
            {
                exception = e;
            }

            // Assert
            Assert.NotNull(exception);
        }
예제 #2
0
        public async Task UnmarkResourcesForMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            if (markSessionModel.SourceDependency != null)
            {
                await UnmarkResource(markSessionModel.SourceDependency, markSessionModel.ProjectId);

                markSessionModel.SourceDependency = null;
                await _markSessionRepository.Update(markSessionModel);
            }

            var dependantResourceModels = new List <DependantResourceModel>(markSessionModel.DependantResources);
            var taskList = dependantResourceModels.Select(markedResourceModel => Task.Run(
                                                              async() =>
            {
                await UnmarkResource(markedResourceModel, markSessionModel.ProjectId);

                lock (markSessionModel.DependantResources)
                {
                    markSessionModel.DependantResources.Remove(markedResourceModel);
                }
            }
                                                              )).ToList();

            await ExecuteTasksThenUpdateMarkSession(taskList, markSessionModel);
        }
        public async Task <MarkSessionModel> CreateMarkSession(
            string resourceId,
            string projectId,
            string resourceType,
            string markSessionType
            )
        {
            var markSessionModel = new MarkSessionModel(resourceId, projectId, resourceType, markSessionType);

            try
            {
                await _markSessionRepository.Create(markSessionModel);

                await _dependantResourceHandler.MarkResourcesForMarkSession(markSessionModel);

                markSessionModel.State = MarkSessionModel.StateComplete;
                await _markSessionRepository.Update(markSessionModel);
            }
            catch (Exception e)
            {
                if (!(e is FailedToCreateMarkSessionException || e is MarkSessionAlreadyExistsException))
                {
                    await DeleteMarkSession(markSessionModel.Id.ToString());
                }

                throw;
            }

            return(markSessionModel);
        }
예제 #4
0
        public async void Update_MarkSessionModelsExist_NoExceptionThrown()
        {
            // Arrange
            var markSessionModel = new MarkSessionModel(
                "2085eb4c-7a94-4cc8-9c46-58f5166d3c82",
                "2085eb4c-7a94-4cc8-9c46-58f5166d3c82",
                ResourceTypeEnum.Project,
                MarkSessionTypeEnum.ToBeArchived
                )
            {
                Id = new ObjectId("5b07def67aa54a0007b3db53")
            };
            var       dbMongoService        = new DbMongoService();
            var       markSessionRepository = new MarkSessionRepository(dbMongoService);
            Exception exception             = null;

            try
            {
                // Act
                await markSessionRepository.Update(markSessionModel);
            }
            catch (Exception e)
            {
                exception = e;
            }

            // Assert
            Assert.Null(exception);
        }
예제 #5
0
        public async void Delete_MarkSessionModelExists_NoExceptionThrown()
        {
            // Arrange
            var markSessionModel = new MarkSessionModel(
                "f05725ff-7da3-4dbe-83ce-184a585f47df",
                "f05725ff-7da3-4dbe-83ce-184a585f47df",
                ResourceTypeEnum.Project,
                MarkSessionTypeEnum.ToBeDeleted
                )
            {
                Id = new ObjectId("5b07dee57aa54a0007b3db52")
            };
            var       dbMongoService        = new DbMongoService();
            var       markSessionRepository = new MarkSessionRepository(dbMongoService);
            Exception exception             = null;

            try
            {
                // Act
                await markSessionRepository.Delete(markSessionModel);
            }
            catch (Exception e)
            {
                exception = e;
            }

            // Assert
            Assert.Null(exception);
        }
예제 #6
0
        private async Task ExecuteTasksThenUpdateMarkSession(
            IEnumerable <Task> tasks,
            MarkSessionModel markSessionModel
            )
        {
            Exception exception = null;

            try
            {
                await TaskUtil.ExecuteTasksInParallel(tasks);
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
                await _markSessionRepository.Update(markSessionModel);

                if (exception != null)
                {
                    throw exception;
                }
            }
        }
예제 #7
0
        private async Task MarkResourcesForScenarioMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var scenarioId = markSessionModel.ResourceId;
            var projectId  = markSessionModel.ProjectId;

            var markedSourceScenario = await _scenarioClient.MarkScenario(scenarioId);

            markSessionModel.DependantResources.Add(markedSourceScenario);
            await _markSessionRepository.Update(markSessionModel);

            var simPlansForScenario = await _simPlanClient.GetSimPlansForScenario(scenarioId, projectId);

            await MarkResourcesThenUpdateMarkSession(simPlansForScenario, projectId, markSessionModel);

            var simRunsForSimPlans = new List <SimRunModel>();

            foreach (var simPlanModel in simPlansForScenario)
            {
                simRunsForSimPlans.AddRange(
                    await _simRunClient.GetSimRunsForSimPlan(simPlanModel.Id, projectId)
                    );
            }
            await MarkResourcesThenUpdateMarkSession(simRunsForSimPlans, projectId, markSessionModel);

            await MarkResultDataThenUpdateMarkSession(simRunsForSimPlans, markSessionModel);
        }
    public async Task Create(
        MarkSessionModel markSessionModel
        )
    {
        try
        {
            await EnsureUniqueFieldForMarkSession(MarkSessionModel.BsonElementDefinitionResourceId);

            await _dbMongoService.GetMarkSessionCollection().InsertOneAsync(markSessionModel);
        }
        catch (MongoWriteException e)
        {
            if (e.WriteError.Category == ServerErrorCategory.DuplicateKey)
            {
                throw new MarkSessionAlreadyExistsException(
                          $"Cannot create {markSessionModel}, it already exists!"
                          );
            }

            throw new FailedToCreateMarkSessionException(
                      $"Failed to create {markSessionModel}",
                      e
                      );
        }
        catch (Exception e)
        {
            throw new FailedToCreateMarkSessionException(
                      $"Failed to create {markSessionModel}",
                      e
                      );
        }
    }
예제 #9
0
        public async Task DeleteDependantResourcesForMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var taskList = new List <Task>();

            foreach (var dependantResourceModel in markSessionModel.DependantResources)
            {
                switch (dependantResourceModel.ResourceType)
                {
                case ResourceTypeEnum.Project:
                    // The project must not be deleted !
                    break;

                case ResourceTypeEnum.Metadata:
                    taskList.Add(
                        _fileClient.DeleteResource(dependantResourceModel)
                        );
                    break;

                case ResourceTypeEnum.Scenario:
                    taskList.Add(
                        _scenarioClient.DeleteResource(dependantResourceModel)
                        );
                    break;

                case ResourceTypeEnum.ResultConfig:
                    taskList.Add(
                        _resultConfigClient.DeleteResource(dependantResourceModel)
                        );
                    break;

                case ResourceTypeEnum.SimPlan:
                    taskList.Add(
                        _simPlanClient.DeleteResource(dependantResourceModel, markSessionModel.ProjectId)
                        );
                    break;

                case ResourceTypeEnum.SimRun:
                    taskList.Add(
                        _simRunClient.DeleteResource(dependantResourceModel, markSessionModel.ProjectId)
                        );
                    break;

                case ResourceTypeEnum.ResultData:
                    taskList.Add(
                        _resultDataClient.DeleteResource(dependantResourceModel)
                        );
                    break;

                default:
                    throw new UnknownResourceTypeExcetion(
                              $"{dependantResourceModel.ResourceType} is unknown!"
                              );
                }
            }

            await TaskUtil.ExecuteTasksInParallel(taskList);
        }
 public async Task <string> DeleteMarkSessionAndDependantResources(
     MarkSessionModel markSessionModel
     )
 {
     return(await _backgroundJobsHandler.CreateBackgroundJob(
                () => StartDeletionProcess(markSessionModel.MarkSessionId)
                ));
 }
예제 #11
0
        public static bool IsMarkSessionRecentlyUpdated(
            this MarkSessionModel markSessionModel
            )
        {
            var timePassedInTicks = DateTime.Now.Ticks - markSessionModel.LatestUpdateTimestampInTicks;

            return(timePassedInTicks < Constants.Constants.MarkSessionUpdateReferenceTimeInTicks);
        }
예제 #12
0
        private async Task MarkResourcesForResultDataMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var markedResulrData = await _resultDataClient.MarkResultData(markSessionModel.ResourceId);

            markSessionModel.DependantResources.Add(markedResulrData);
            await _markSessionRepository.Update(markSessionModel);
        }
    public async Task Update(
        MarkSessionModel markSessionModel
        )
    {
        markSessionModel.LatestUpdateTimestampInTicks = DateTime.Now.Ticks;

        await _dbMongoService.GetMarkSessionCollection().ReplaceOneAsync(
            GetFilterDefinitionForResourceId(markSessionModel.ResourceId),
            markSessionModel
            );
    }
예제 #14
0
        public async Task MarkResourcesForMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            switch (markSessionModel.ResourceType)
            {
            case ResourceTypeEnum.Project:
                await MarkResourcesForProjectMarkSession(markSessionModel);

                break;

            case ResourceTypeEnum.Metadata:
                await MarkResourcesForMetadataMarkSession(markSessionModel);

                break;

            case ResourceTypeEnum.Scenario:
                await MarkResourcesForScenarioMarkSession(markSessionModel);

                break;

            case ResourceTypeEnum.ResultConfig:
                await MarkResourcesForResultConfigMarkSession(markSessionModel);

                break;

            case ResourceTypeEnum.SimPlan:
                await MarkResourcesForSimPlanMarkSession(markSessionModel);

                break;

            case ResourceTypeEnum.SimRun:
                await MarkResourcesForSimRunMarkSession(markSessionModel);

                break;

            case ResourceTypeEnum.ResultData:
                await MarkResourcesForResultDataMarkSession(markSessionModel);

                break;

            default:
                throw new UnknownResourceTypeException(
                          $"{markSessionModel.ResourceType} is unknown!"
                          );
            }
        }
예제 #15
0
        private async Task MarkResourcesForSimRunMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var simRunId     = markSessionModel.ResourceId;
            var projectId    = markSessionModel.ProjectId;
            var sourceSimRun = await _simRunClient.GetSimRun(simRunId, projectId);

            var markedSourceSimRun = await _simRunClient.MarkSimRun(simRunId, projectId);

            markSessionModel.DependantResources.Add(markedSourceSimRun);
            await _markSessionRepository.Update(markSessionModel);

            var markedResultData = await _resultDataClient.MarkResultData(sourceSimRun);

            markSessionModel.DependantResources.Add(markedResultData);
            await _markSessionRepository.Update(markSessionModel);
        }
예제 #16
0
        private async Task MarkResourcesForSimPlanMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var simPlanId = markSessionModel.ResourceId;
            var projectId = markSessionModel.ProjectId;

            var markedSourceSimPlan = await _simPlanClient.MarkSimPlan(simPlanId, projectId);

            markSessionModel.DependantResources.Add(markedSourceSimPlan);
            await _markSessionRepository.Update(markSessionModel);

            var simRunsForSimPlan = await _simRunClient.GetSimRunsForSimPlan(simPlanId, projectId);

            await MarkResourcesThenUpdateMarkSession(simRunsForSimPlan, projectId, markSessionModel);

            await MarkResultDataThenUpdateMarkSession(simRunsForSimPlan, markSessionModel);
        }
예제 #17
0
        private async Task MarkResourcesThenUpdateMarkSession <TResourceModel>(
            IEnumerable <TResourceModel> resourceModels,
            string projectId,
            MarkSessionModel markSessionModel
            )
        {
            if (resourceModels == null)
            {
                return;
            }

            var taskList = resourceModels.Select(resourceModel => MarkResourceThenAddToMarkSession(
                                                     resourceModel,
                                                     projectId,
                                                     markSessionModel
                                                     )).ToList();

            await ExecuteTasksThenUpdateMarkSession(taskList, markSessionModel);
        }
예제 #18
0
        private async Task MarkResourceThenAddToMarkSession <TResourceModel>(
            TResourceModel resourceModel,
            string projectId,
            MarkSessionModel markSessionModel
            )
        {
            DependantResourceModel markedResource;

            if (resourceModel is MetadataModel)
            {
                markedResource = await _metadataClient.MarkMetadata(resourceModel as MetadataModel);
            }
            else if (resourceModel is ScenarioModel)
            {
                markedResource = await _scenarioClient.MarkScenario(resourceModel as ScenarioModel);
            }
            else if (resourceModel is ResultConfigModel)
            {
                // ResultConfigs obey the mark of the metadata!
                markedResource =
                    await _resultConfigClient.CreateDependantResultConfigResource(resourceModel as ResultConfigModel);
            }
            else if (resourceModel is SimPlanModel)
            {
                markedResource = await _simPlanClient.MarkSimPlan(resourceModel as SimPlanModel, projectId);
            }
            else if (resourceModel is SimRunModel)
            {
                markedResource = await _simRunClient.MarkSimRun(resourceModel as SimRunModel, projectId);
            }
            else
            {
                throw new UnknownResourceModelException(
                          $"{resourceModel.GetType()} is unknown!"
                          );
            }

            lock (markSessionModel.DependantResources)
            {
                markSessionModel.DependantResources.Add(markedResource);
            }
        }
예제 #19
0
        private async Task MarkResourcesForMetadataMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var metadataId = markSessionModel.ResourceId;
            var projectId  = markSessionModel.ProjectId;

            var markedSourceMetadata = await _metadataClient.MarkMetadata(metadataId);

            markSessionModel.DependantResources.Add(markedSourceMetadata);
            await _markSessionRepository.Update(markSessionModel);

            var scenariosForMetadata = await _scenarioClient.GetScenariosForMetadata(metadataId);

            await MarkResourcesThenUpdateMarkSession(scenariosForMetadata, projectId, markSessionModel);

            var resultConfigsForMetadata = await _resultConfigClient.GetResultConfigsForMetadata(metadataId);

            await MarkResourcesThenUpdateMarkSession(resultConfigsForMetadata, projectId, markSessionModel);

            var simPlansForScenarios = new List <SimPlanModel>();

            foreach (var scenarioModel in scenariosForMetadata)
            {
                simPlansForScenarios.AddRange(
                    await _simPlanClient.GetSimPlansForScenario(scenarioModel.ScenarioId, projectId)
                    );
            }
            await MarkResourcesThenUpdateMarkSession(simPlansForScenarios, projectId, markSessionModel);

            var simRunsForSimPlans = new List <SimRunModel>();

            foreach (var simPlanModel in simPlansForScenarios)
            {
                simRunsForSimPlans.AddRange(
                    await _simRunClient.GetSimRunsForSimPlan(simPlanModel.Id, projectId)
                    );
            }
            await MarkResourcesThenUpdateMarkSession(simRunsForSimPlans, projectId, markSessionModel);

            await MarkResultDataThenUpdateMarkSession(simRunsForSimPlans, markSessionModel);
        }
    public async Task Delete(
        MarkSessionModel markSessionModel
        )
    {
        if (markSessionModel.DependantResources.Count != 0)
        {
            throw new CannotDeleteMarkSessionException(
                      $"{markSessionModel} cannot be deleted because it has unmarked dependant resources!"
                      );
        }

        if (markSessionModel.SourceDependency != null)
        {
            throw new CannotDeleteMarkSessionException(
                      $"{markSessionModel} cannot be deleted because it has unmarked source dependant resources!"
                      );
        }

        await _dbMongoService.GetMarkSessionCollection().DeleteOneAsync(
            GetFilterDefinitionForResourceId(markSessionModel.ResourceId)
            );
    }
예제 #21
0
        private async Task MarkResourcesForProjectMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var projectId = markSessionModel.ProjectId;

            markSessionModel.SourceDependency = await _projectClient.MarkProject(projectId);

            await _markSessionRepository.Update(markSessionModel);

            var metadataForProject = await _metadataClient.GetMetadataForProject(projectId);

            await MarkResourcesThenUpdateMarkSession(metadataForProject, projectId, markSessionModel);

            var scenariosForProject = await _scenarioClient.GetScenariosForProject(projectId);

            await MarkResourcesThenUpdateMarkSession(scenariosForProject, projectId, markSessionModel);

            var resultConfigsForMetadata = new List <ResultConfigModel>();

            foreach (var metadataModel in metadataForProject)
            {
                resultConfigsForMetadata.AddRange(
                    await _resultConfigClient.GetResultConfigsForMetadata(metadataModel.DataId)
                    );
            }
            await MarkResourcesThenUpdateMarkSession(resultConfigsForMetadata, projectId, markSessionModel);

            var simPlansForProject = await _simPlanClient.GetSimPlansForProject(projectId);

            await MarkResourcesThenUpdateMarkSession(simPlansForProject, projectId, markSessionModel);

            var simRunsForProject = await _simRunClient.GetSimRunsForProject(projectId);

            await MarkResourcesThenUpdateMarkSession(simRunsForProject, projectId, markSessionModel);

            await MarkResultDataThenUpdateMarkSession(simRunsForProject, markSessionModel);
        }
예제 #22
0
        private async Task MarkResultDataThenUpdateMarkSession(
            IEnumerable <SimRunModel> simRunModels,
            MarkSessionModel markSessionModel
            )
        {
            if (simRunModels == null)
            {
                return;
            }

            var taskList = simRunModels.Select(simRunModel => Task.Run(
                                                   async() =>
            {
                var markedResultData = await _resultDataClient.MarkResultData(simRunModel);

                lock (markSessionModel.DependantResources)
                {
                    markSessionModel.DependantResources.Add(markedResultData);
                }
            }
                                                   )).ToList();

            await ExecuteTasksThenUpdateMarkSession(taskList, markSessionModel);
        }
예제 #23
0
        private async Task MarkResourcesForResultConfigMarkSession(
            MarkSessionModel markSessionModel
            )
        {
            var resultConfigId = markSessionModel.ResourceId;
            var projectId      = markSessionModel.ProjectId;

            var sourceResultConfig = await _resultConfigClient.GetResultConfig(resultConfigId);

            markSessionModel.SourceDependency = await _metadataClient.MarkMetadata(sourceResultConfig.ModelId);

            await _markSessionRepository.Update(markSessionModel);

            var markedSourceResultConfig =
                await _resultConfigClient.CreateDependantResultConfigResource(resultConfigId);

            markSessionModel.DependantResources.Add(markedSourceResultConfig);
            await _markSessionRepository.Update(markSessionModel);

            var simPlansForResultConfig =
                await _simPlanClient.GetSimPlansForResultConfig(resultConfigId, projectId);

            await MarkResourcesThenUpdateMarkSession(simPlansForResultConfig, projectId, markSessionModel);

            var simRunsForSimPlans = new List <SimRunModel>();

            foreach (var simPlanModel in simPlansForResultConfig)
            {
                simRunsForSimPlans.AddRange(
                    await _simRunClient.GetSimRunsForSimPlan(simPlanModel.Id, projectId)
                    );
            }
            await MarkResourcesThenUpdateMarkSession(simRunsForSimPlans, projectId, markSessionModel);

            await MarkResultDataThenUpdateMarkSession(simRunsForSimPlans, markSessionModel);
        }