Пример #1
0
 private static CalculationService CreateCalculationService(
     ICalculationsRepository calculationsRepository = null,
     ILogger logger = null,
     ISearchRepository <CalculationIndex> searchRepository = null,
     IValidator <Calculation> calcValidator           = null,
     IBuildProjectsService buildProjectsService       = null,
     ISpecificationRepository specificationRepository = null,
     ICacheProvider cacheProvider = null,
     ICalcsResiliencePolicies resiliencePolicies = null,
     IVersionRepository <CalculationVersion> calculationVersionRepository = null,
     IJobsApiClient jobsApiClient                     = null,
     ISourceCodeService sourceCodeService             = null,
     IFeatureToggle featureToggle                     = null,
     IBuildProjectsRepository buildProjectsRepository = null,
     ICalculationCodeReferenceUpdate calculationCodeReferenceUpdate = null)
 {
     return(new CalculationService
                (calculationsRepository ?? CreateCalculationsRepository(),
                logger ?? CreateLogger(),
                searchRepository ?? CreateSearchRepository(),
                calcValidator ?? CreateCalculationValidator(),
                buildProjectsService ?? CreateBuildProjectsService(),
                specificationRepository ?? CreateSpecificationRepository(),
                cacheProvider ?? CreateCacheProvider(),
                resiliencePolicies ?? CalcsResilienceTestHelper.GenerateTestPolicies(),
                calculationVersionRepository ?? CreateCalculationVersionRepository(),
                jobsApiClient ?? CreateJobsApiClient(),
                sourceCodeService ?? CreateSourceCodeService(),
                featureToggle ?? CreateFeatureToggle(),
                buildProjectsRepository ?? CreateBuildProjectsRepository(),
                calculationCodeReferenceUpdate ?? CreateCalculationCodeReferenceUpdate()));
 }
Пример #2
0
        private ICalculationCodeReferenceUpdate FakeCalculationCodeReferenceUpdate()
        {
            ICalculationCodeReferenceUpdate calculationCodeReferenceUpdate = CreateCalculationCodeReferenceUpdate();

            calculationCodeReferenceUpdate
            .ReplaceSourceCodeReferences(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>())
            .Returns(x =>
            {
                string source  = x.ArgAt <string>(0);
                string oldName = x.ArgAt <string>(1);
                string newName = x.ArgAt <string>(2);

                return(source.Replace(oldName, newName));
            });
            return(calculationCodeReferenceUpdate);
        }
        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()));
        }
Пример #4
0
        public async Task UpdateCalculationCodeOnCalculationSpecificationChange_WhenNoCalculationsFoundReferencingCalculationToBeUpdated_ThenNoCalculationsUpdated()
        {
            // Arrange
            ICalculationsRepository  calculationsRepository           = CreateCalculationsRepository();
            ISpecificationRepository specificationRepository          = CreateSpecificationRepository();
            IVersionRepository <CalculationVersion> versionRepository = CreateCalculationVersionRepository();
            ICalculationCodeReferenceUpdate         calculationCodeReferenceUpdate = FakeCalculationCodeReferenceUpdate();

            CalculationService service = CreateCalculationService(calculationsRepository: calculationsRepository,
                                                                  specificationRepository: specificationRepository,
                                                                  calculationVersionRepository: versionRepository,
                                                                  calculationCodeReferenceUpdate: calculationCodeReferenceUpdate);

            const string specificationId = "specId";
            const string calculationId   = "updatedCalc";

            Models.Specs.CalculationVersionComparisonModel comparison = new Models.Specs.CalculationVersionComparisonModel()
            {
                CalculationId = calculationId,
                Current       = new Models.Specs.Calculation
                {
                    Id              = "calcSpec1",
                    Name            = "Calculation to update",
                    CalculationType = Models.Specs.CalculationType.Funding,
                },
                Previous = new Models.Specs.Calculation
                {
                    Id              = "calcSpec1",
                    Name            = "Original Name",
                    CalculationType = Models.Specs.CalculationType.Funding,
                },
                SpecificationId = specificationId,
            };

            Reference user = new Reference("userId", "User Name");

            List <Calculation> calculations = new List <Calculation>()
            {
                new Calculation
                {
                    Id                       = calculationId,
                    Name                     = "Calculation to Update",
                    SpecificationId          = specificationId,
                    FundingPeriod            = new Reference("fp1", "Funding Period"),
                    CalculationSpecification = new Reference("calcSpec1", "Calculation to Update"),
                    CalculationType          = CalculationType.Funding,
                    Description              = "Calculation Description",
                    BuildProjectId           = "bpC1",
                    Policies                 = new List <Reference>(),
                    Current                  = new CalculationVersion
                    {
                        SourceCode    = "Return 10",
                        DecimalPlaces = 6,
                    }
                },
                new Calculation
                {
                    Id                       = "referenceCalc",
                    Name                     = "Calling Calculation To Update",
                    SpecificationId          = specificationId,
                    FundingPeriod            = new Reference("fp1", "Funding Period"),
                    CalculationSpecification = new Reference("calcSpec1", "Calculation to Update"),
                    CalculationType          = CalculationType.Funding,
                    Description              = "Calculation Description",
                    BuildProjectId           = "bpC1",
                    Policies                 = new List <Reference>(),
                    Current                  = new CalculationVersion
                    {
                        SourceCode    = "Return 50",
                        DecimalPlaces = 6,
                    }
                }
            };

            calculationsRepository
            .GetCalculationsBySpecificationId(Arg.Is(specificationId))
            .Returns(calculations);

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

            calculationsRepository
            .GetCalculationById(Arg.Is(calculations[0].Id))
            .Returns(calculations[0]);

            Models.Specs.SpecificationSummary specification = new Models.Specs.SpecificationSummary()
            {
                Id   = specificationId,
                Name = "Specification Name",
            };

            specificationRepository
            .GetSpecificationSummaryById(Arg.Is(specificationId))
            .Returns(specification);

            CalculationVersion calculationVersion = new CalculationVersion
            {
                SourceCode = "Return CalculationToUpdate()",
                Version    = 2
            };

            versionRepository
            .CreateVersion(Arg.Any <CalculationVersion>(), Arg.Any <CalculationVersion>())
            .Returns(calculationVersion);

            // Act
            IEnumerable <Calculation> updatedCalculations = await service.UpdateCalculationCodeOnCalculationSpecificationChange(comparison, user);

            // Assert
            updatedCalculations
            .Should()
            .HaveCount(0);

            calculationCodeReferenceUpdate
            .Received(calculations.Count)
            .ReplaceSourceCodeReferences(Arg.Any <string>(),
                                         VisualBasicTypeGenerator.GenerateIdentifier(comparison.Previous.Name),
                                         VisualBasicTypeGenerator.GenerateIdentifier(comparison.Current.Name));

            foreach (Calculation calculation in calculations)
            {
                calculationCodeReferenceUpdate
                .Received(1)
                .ReplaceSourceCodeReferences(calculation.Current.SourceCode,
                                             VisualBasicTypeGenerator.GenerateIdentifier(comparison.Previous.Name),
                                             VisualBasicTypeGenerator.GenerateIdentifier(comparison.Current.Name));
            }
        }