public void MapsAllStrategiesInLocatorIntoResponses()
        {
            IReProfilingStrategy strategyOne   = NewStrategyStub();
            IReProfilingStrategy strategyTwo   = NewStrategyStub();
            IReProfilingStrategy strategyThree = NewStrategyStub();
            IReProfilingStrategy strategyFour  = NewStrategyStub();

            GivenTheSupportedStrategies(strategyOne, strategyTwo, strategyThree, strategyFour);

            ActionResult <IEnumerable <ReProfilingStrategyResponse> > responses = WhenTheReProfilingStrategiesAreQueried();

            responses?
            .Value
            .Should()
            .BeEquivalentTo(new[]
            {
                strategyOne,
                strategyTwo,
                strategyThree,
                strategyFour
            }.Select(_ => new ReProfilingStrategyResponse
            {
                StrategyKey = _.StrategyKey,
                Description = _.Description,
                DisplayName = _.DisplayName
            }));
        }
        public void HasKeyIsTrueIfHasStrategyWithSuppliedKey()
        {
            string key = NewRandomString();

            IReProfilingStrategy expectedStrategy = NewStrategyStub(_ => _.WithKey(key));

            GivenTheSupportedStrategies(NewStrategyStub(),
                                        NewStrategyStub(),
                                        expectedStrategy,
                                        NewStrategyStub());

            bool hasStrategy = WhenTheStrategyKeyIsChecked(key);

            hasStrategy
            .Should()
            .BeTrue();
        }
        public void LocatesStrategiesByStrategyKey()
        {
            string key = NewRandomString();

            IReProfilingStrategy expectedStrategy = NewStrategyStub(_ => _.WithKey(key));

            GivenTheSupportedStrategies(NewStrategyStub(),
                                        NewStrategyStub(),
                                        expectedStrategy,
                                        NewStrategyStub());

            IReProfilingStrategy actualStrategy = WhenTheStrategyIsLocated(key);

            actualStrategy
            .Should()
            .BeSameAs(expectedStrategy);
        }
        public async Task <ActionResult <ReProfileResponse> > ReProfile(ReProfileRequest reProfileRequest)
        {
            FundingStreamPeriodProfilePattern profilePattern = await _profilePatternService.GetProfilePattern(reProfileRequest.FundingStreamId,
                                                                                                              reProfileRequest.FundingPeriodId,
                                                                                                              reProfileRequest.FundingLineCode,
                                                                                                              reProfileRequest.ProfilePatternKey);

            if (profilePattern == null)
            {
                return(new NotFoundObjectResult("Profile pattern not found"));
            }

            if (profilePattern.ReProfilingConfiguration == null || !profilePattern.ReProfilingConfiguration.ReProfilingEnabled)
            {
                return(new BadRequestObjectResult("Re-profiling is not enabled or has not been configured"));
            }

            IReProfilingStrategy strategy = GetReProfilingStrategy(reProfileRequest, profilePattern);

            if (strategy == null)
            {
                return(new BadRequestObjectResult("Re-profiling is not enabled for this scenario or the strategy was not found"));
            }

            ReProfileContext context = CreateReProfilingContext(reProfileRequest, profilePattern);

            ReProfileStrategyResult strategyResult = strategy.ReProfile(context);

            VerifyProfileAmountsReturnedMatchRequestedFundingLineValue(reProfileRequest, strategyResult);

            return(new ReProfileResponse
            {
                DeliveryProfilePeriods = strategyResult.DeliveryProfilePeriods,
                DistributionPeriods = strategyResult.DistributionPeriods,
                ProfilePatternDisplayName = profilePattern.ProfilePatternDisplayName,
                ProfilePatternKey = profilePattern.ProfilePatternKey,
                CarryOverAmount = strategyResult.CarryOverAmount
            });
        }