Ejemplo n.º 1
0
        public CalculationsController(
            ICalculationService calcsService,
            ICalculationsSearchService calcsSearchService,
            IPreviewService previewService,
            IBuildProjectsService buildProjectsService,
            IQueueReIndexSpecificationCalculationRelationships calculationRelationships,
            ICalculationFundingLineQueryService calculationFundingLineQueryService,
            ICodeContextCache codeContextCache)
        {
            Guard.ArgumentNotNull(calcsService, nameof(calcsService));
            Guard.ArgumentNotNull(calcsSearchService, nameof(calcsSearchService));
            Guard.ArgumentNotNull(previewService, nameof(previewService));
            Guard.ArgumentNotNull(buildProjectsService, nameof(buildProjectsService));
            Guard.ArgumentNotNull(calculationRelationships, nameof(calculationRelationships));
            Guard.ArgumentNotNull(calculationFundingLineQueryService, nameof(calculationFundingLineQueryService));
            Guard.ArgumentNotNull(codeContextCache, nameof(codeContextCache));

            _calcsService                       = calcsService;
            _previewService                     = previewService;
            _calcsSearchService                 = calcsSearchService;
            _buildProjectsService               = buildProjectsService;
            _calculationRelationships           = calculationRelationships;
            _calculationFundingLineQueryService = calculationFundingLineQueryService;
            _codeContextCache                   = codeContextCache;
        }
 public OnUpdateCodeContextCache(
     ILogger logger,
     ICodeContextCache codeContextCache,
     IMessengerService messengerService,
     IUserProfileProvider userProfileProvider, bool useAzureStorage = false)
     : base(logger, messengerService, FunctionName, QueueName, useAzureStorage, userProfileProvider, codeContextCache)
 {
 }
        public void SetUp()
        {
            _policies = Substitute.For <IPoliciesApiClient>();
            _specificationApiClient           = Substitute.For <ISpecificationsApiClient>();
            _graphRepository                  = Substitute.For <IGraphRepository>();
            _createCalculationService         = Substitute.For <ICreateCalculationService>();
            _calculationsRepository           = Substitute.For <ICalculationsRepository>();
            _instructionAllocationJobCreation = Substitute.For <IInstructionAllocationJobCreation>();
            _calculationService               = Substitute.For <ICalculationService>();
            _cacheProvider    = Substitute.For <ICacheProvider>();
            _codeContextCache = Substitute.For <ICodeContextCache>();
            _jobManagement    = Substitute.For <IJobManagement>();

            _userId          = $"{NewRandomString()}_userId";
            _userName        = $"{NewRandomString()}_userName";
            _correlationId   = $"{NewRandomString()}_correlationId";
            _specificationId = $"{NewRandomString()}_specificationId";
            _fundingStreamId = $"{NewRandomString()}_fundingStreamId";
            _fundingPeriodId = $"{NewRandomString()}_fundingPeriodId";
            _templateVersion = $"{NewRandomString()}_templateVersion";
            _jobId           = $"{NewRandomString()}_jobId";

            _calculationsRepository.UpdateTemplateMapping(Arg.Any <string>(),
                                                          Arg.Any <string>(),
                                                          Arg.Any <TemplateMapping>())
            .Returns(Task.CompletedTask);

            _service = new ApplyTemplateCalculationsService(_createCalculationService,
                                                            _policies,
                                                            new ResiliencePolicies
            {
                PoliciesApiClient       = Policy.NoOpAsync(),
                CalculationsRepository  = Policy.NoOpAsync(),
                CacheProviderPolicy     = Policy.NoOpAsync(),
                SpecificationsApiClient = Policy.NoOpAsync()
            },
                                                            _calculationsRepository,
                                                            _jobManagement,
                                                            _instructionAllocationJobCreation,
                                                            Substitute.For <ILogger>(),
                                                            _calculationService,
                                                            _cacheProvider,
                                                            _specificationApiClient,
                                                            _graphRepository,
                                                            _codeContextCache);
        }
Ejemplo n.º 4
0
        public ApplyTemplateCalculationsService(
            ICreateCalculationService createCalculationService,
            IPoliciesApiClient policiesApiClient,
            ICalcsResiliencePolicies calculationsResiliencePolicies,
            ICalculationsRepository calculationsRepository,
            IJobManagement jobManagement,
            IInstructionAllocationJobCreation instructionAllocationJobCreation,
            ILogger logger,
            ICalculationService calculationService,
            ICacheProvider cacheProvider,
            ISpecificationsApiClient specificationsApiClient,
            IGraphRepository graphRepository,
            ICodeContextCache codeContextCache) : base(jobManagement, logger)
        {
            Guard.ArgumentNotNull(instructionAllocationJobCreation, nameof(instructionAllocationJobCreation));
            Guard.ArgumentNotNull(createCalculationService, nameof(createCalculationService));
            Guard.ArgumentNotNull(policiesApiClient, nameof(policiesApiClient));
            Guard.ArgumentNotNull(calculationsRepository, nameof(calculationsRepository));
            Guard.ArgumentNotNull(calculationsResiliencePolicies?.PoliciesApiClient, nameof(calculationsResiliencePolicies.PoliciesApiClient));
            Guard.ArgumentNotNull(calculationsResiliencePolicies?.CalculationsRepository, nameof(calculationsResiliencePolicies.CalculationsRepository));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(calculationService, nameof(calculationService));
            Guard.ArgumentNotNull(calculationsResiliencePolicies?.CacheProviderPolicy, nameof(calculationsResiliencePolicies.CacheProviderPolicy));
            Guard.ArgumentNotNull(cacheProvider, nameof(cacheProvider));
            Guard.ArgumentNotNull(graphRepository, nameof(graphRepository));
            Guard.ArgumentNotNull(specificationsApiClient, nameof(specificationsApiClient));

            _createCalculationService     = createCalculationService;
            _calculationsRepository       = calculationsRepository;
            _policiesApiClient            = policiesApiClient;
            _policiesResiliencePolicy     = calculationsResiliencePolicies.PoliciesApiClient;
            _calculationsRepositoryPolicy = calculationsResiliencePolicies.CalculationsRepository;
            _logger = logger;
            _instructionAllocationJobCreation = instructionAllocationJobCreation;
            _calculationService            = calculationService;
            _cachePolicy                   = calculationsResiliencePolicies.CacheProviderPolicy;
            _cacheProvider                 = cacheProvider;
            _specificationsApiClient       = specificationsApiClient;
            _codeContextCache              = codeContextCache;
            _specificationsApiClientPolicy = calculationsResiliencePolicies.SpecificationsApiClient;
        }
        private static CalculationService CreateCalculationService(
            ICalculationsRepository calculationsRepository = null,
            ILogger logger = null,
            ISearchRepository <CalculationIndex> searchRepository = null,
            IBuildProjectsService buildProjectsService            = null,
            IPoliciesApiClient policiesApiClient        = null,
            ICacheProvider cacheProvider                = null,
            ICalcsResiliencePolicies resiliencePolicies = null,
            IVersionRepository <CalculationVersion> calculationVersionRepository = null,
            IJobManagement jobManagement                     = null,
            ISourceCodeService sourceCodeService             = null,
            IFeatureToggle featureToggle                     = null,
            IBuildProjectsRepository buildProjectsRepository = null,
            ICalculationCodeReferenceUpdate calculationCodeReferenceUpdate      = null,
            IValidator <CalculationCreateModel> calculationCreateModelValidator = null,
            IValidator <CalculationEditModel> calculationEditModelValidator     = null,
            ISpecificationsApiClient specificationsApiClient = null,
            IGraphRepository graphRepository = null,
            ICalculationsFeatureFlag calculationsFeatureFlag = null,
            ICodeContextCache codeContextCache         = null,
            ISourceFileRepository sourceFileRepository = null,
            IResultsApiClient resultsApiClient         = null,
            IDatasetsApiClient datasetsApiClient       = null,
            IApproveAllCalculationsJobAction approveAllCalculationsJobAction = null)
        {
            CalculationNameInUseCheck calculationNameInUseCheck = new CalculationNameInUseCheck(calculationsRepository ?? CreateCalculationsRepository(),
                                                                                                specificationsApiClient ?? CreateSpecificationsApiClient(),
                                                                                                resiliencePolicies ?? CalcsResilienceTestHelper.GenerateTestPolicies());

            InstructionAllocationJobCreation instructionAllocationJobCreation =
                new InstructionAllocationJobCreation(
                    calculationsRepository ?? CreateCalculationsRepository(),
                    resiliencePolicies ?? CalcsResilienceTestHelper.GenerateTestPolicies(),
                    logger ?? CreateLogger(),
                    calculationsFeatureFlag ?? CreateCalculationsFeatureFlag(),
                    jobManagement ?? CreateJobManagement());

            return(new CalculationService
                   (
                       calculationsRepository ?? CreateCalculationsRepository(),
                       logger ?? CreateLogger(),
                       searchRepository ?? CreateSearchRepository(),
                       buildProjectsService ?? CreateBuildProjectsService(),
                       policiesApiClient ?? CreatePoliciesApiClient(),
                       cacheProvider ?? CreateCacheProvider(),
                       resiliencePolicies ?? CalcsResilienceTestHelper.GenerateTestPolicies(),
                       calculationVersionRepository ?? CreateCalculationVersionRepository(),
                       sourceCodeService ?? CreateSourceCodeService(),
                       featureToggle ?? CreateFeatureToggle(),
                       buildProjectsRepository ?? CreateBuildProjectsRepository(),
                       calculationCodeReferenceUpdate ?? CreateCalculationCodeReferenceUpdate(),
                       calculationCreateModelValidator ?? CreateCalculationCreateModelValidator(),
                       calculationEditModelValidator ?? CreateCalculationEditModelValidator(),
                       specificationsApiClient ?? CreateSpecificationsApiClient(),
                       calculationNameInUseCheck,
                       instructionAllocationJobCreation,
                       new CreateCalculationService(calculationNameInUseCheck,
                                                    calculationsRepository ?? CreateCalculationsRepository(),
                                                    calculationVersionRepository ?? CreateCalculationVersionRepository(),
                                                    resiliencePolicies ?? CalcsResilienceTestHelper.GenerateTestPolicies(),
                                                    calculationCreateModelValidator ?? CreateCalculationCreateModelValidator(),
                                                    cacheProvider ?? CreateCacheProvider(),
                                                    searchRepository ?? CreateSearchRepository(),
                                                    logger ?? CreateLogger(),
                                                    instructionAllocationJobCreation),
                       graphRepository ?? CreateGraphRepository(),
                       CreateJobManagement(),
                       codeContextCache ?? Substitute.For <ICodeContextCache>(),
                       resultsApiClient ?? Substitute.For <IResultsApiClient>(),
                       datasetsApiClient ?? Substitute.For <IDatasetsApiClient>(),
                       approveAllCalculationsJobAction ?? CreateApproveAllCalculationsJobAction()));
        }
Ejemplo n.º 6
0
        public async Task CreateAdditionalCalculation_GivenCalcSaves_ReturnsOKObjectResult()
        {
            //Arrange
            string cacheKey = $"{CacheKeys.CalculationsMetadataForSpecification}{SpecificationId}";

            CalculationCreateModel model = CreateCalculationCreateModel();

            Reference author = CreateAuthor();

            ICalculationsRepository calculationsRepository = CreateCalculationsRepository();

            calculationsRepository
            .CreateDraftCalculation(Arg.Any <Calculation>())
            .Returns(HttpStatusCode.OK);

            IVersionRepository <CalculationVersion> versionRepository = CreateCalculationVersionRepository();

            ISearchRepository <CalculationIndex> searchRepository = CreateSearchRepository();

            IJobManagement jobManagement = CreateJobManagement();

            jobManagement
            .QueueJob(Arg.Any <JobCreateModel>())
            .Returns(new Job {
                Id = "job-id-1"
            });

            ISpecificationsApiClient specificationsApiClient = CreateSpecificationsApiClient();

            specificationsApiClient
            .GetSpecificationSummaryById(Arg.Is(SpecificationId))
            .Returns(new ApiResponse <SpecificationSummary>(
                         HttpStatusCode.OK,
                         new SpecificationSummary {
                Id            = SpecificationId,
                FundingPeriod = new FundingPeriod {
                    Id = FundingPeriodId
                }
            }
                         ));

            ILogger logger = CreateLogger();

            ICacheProvider cacheProvider = CreateCacheProvider();

            ICodeContextCache codeContextCache = Substitute.For <ICodeContextCache>();

            IResultsApiClient resultsApiClient = CreateResultsApiClient();

            CalculationService calculationService = CreateCalculationService(
                calculationsRepository: calculationsRepository,
                calculationVersionRepository: versionRepository,
                searchRepository: searchRepository,
                jobManagement: jobManagement,
                logger: logger,
                cacheProvider: cacheProvider,
                specificationsApiClient: specificationsApiClient,
                codeContextCache: codeContextCache,
                resultsApiClient: resultsApiClient);

            IEnumerable <CalculationIndex> indexedCalculations = null;

            await
            searchRepository
            .Index(Arg.Do <IEnumerable <CalculationIndex> >(m =>
                                                            indexedCalculations = m
                                                            ));

            CalculationVersion savedCalculationVersion = null;

            await
            versionRepository
            .SaveVersion(Arg.Do <CalculationVersion>(m => savedCalculationVersion = m));

            //Act
            IActionResult result = await calculationService.CreateAdditionalCalculation(SpecificationId, model, author, CorrelationId);

            //Assert
            result
            .Should()
            .BeAssignableTo <OkObjectResult>();

            Calculation calculation = (result as OkObjectResult).Value as Calculation;

            await
            jobManagement
            .Received(1)
            .QueueJob(Arg.Is <JobCreateModel>(
                          m =>
                          m.InvokerUserDisplayName == Username &&
                          m.InvokerUserId == UserId &&
                          m.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructAllocationJob &&
                          m.Properties["specification-id"] == SpecificationId
                          ));

            logger
            .Received(1)
            .Information(Arg.Is($"New job of type '{JobConstants.DefinitionNames.CreateInstructAllocationJob}' created with id: 'job-id-1'"));


            await
            versionRepository
            .Received(1)
            .SaveVersion(Arg.Is <CalculationVersion>(m =>
                                                     m.PublishStatus == Models.Versioning.PublishStatus.Draft &&
                                                     m.Author.Id == UserId &&
                                                     m.Author.Name == Username &&
                                                     m.Date.Date == DateTimeOffset.Now.Date &&
                                                     m.Version == 1 &&
                                                     m.SourceCode == model.SourceCode &&
                                                     m.Description == model.Description &&
                                                     m.ValueType == model.ValueType &&
                                                     m.CalculationType == CalculationType.Additional &&
                                                     m.WasTemplateCalculation == false &&
                                                     m.Namespace == CalculationNamespace.Additional &&
                                                     m.Name == model.Name &&
                                                     m.SourceCodeName == new VisualBasicTypeIdentifierGenerator().GenerateIdentifier(model.Name) &&
                                                     m.DataType == CalculationDataType.Decimal
                                                     ));


            await searchRepository
            .Received(1)
            .Index(Arg.Any <IEnumerable <CalculationIndex> >());

            indexedCalculations
            .Should()
            .BeEquivalentTo(new List <CalculationIndex>()
            {
                new CalculationIndex()
                {
                    CalculationType   = "Additional",
                    Description       = "test description",
                    FundingStreamId   = "fs-1",
                    FundingStreamName = model.FundingStreamName,
                    Id                     = model.Id,
                    Name                   = model.Name,
                    Namespace              = "Additional",
                    SpecificationId        = "spec-id-1",
                    SpecificationName      = "spec-id-1_specificationName",
                    Status                 = "Draft",
                    ValueType              = "Currency",
                    WasTemplateCalculation = false,
                    LastUpdatedDate        = savedCalculationVersion.Date,
                }
            });

            //!string.IsNullOrWhiteSpace(m.First().Id) &&
            //m.First().Name == model.Name &&
            //m.First().SpecificationId == SpecificationId &&
            //m.First().SpecificationName == model.SpecificationName &&
            //m.First().ValueType == model.ValueType.ToString() &&
            //m.First().CalculationType == CalculationType.Additional.ToString() &&
            //m.First().Namespace == CalculationNamespace.Additional.ToString() &&
            //m.First().FundingStreamId == model.FundingStreamId &&
            //m.First().FundingStreamName == model.FundingStreamName &&
            //m.First().WasTemplateCalculation == false &&
            //m.First().Description == model.Description &&
            //m.First().Status == calculation.Current.PublishStatus.ToString()

            await
            cacheProvider
            .Received(1)
            .RemoveAsync <List <CalculationMetadata> >(Arg.Is(cacheKey));

            await codeContextCache
            .Received(1)
            .QueueCodeContextCacheUpdate(SpecificationId);
        }