Ejemplo n.º 1
0
 public IEnumerator GetEnumerator()
 {
     return(Platforms?.Select(platform => new SaucePlatform(Sanitiser.SanitisePlatformField(platform.Os),
                                                            Sanitiser.SanitisePlatformField(platform.Platform),
                                                            Sanitiser.SanitisePlatformField(platform.Browser),
                                                            Sanitiser.SanitisePlatformField(platform.BrowserVersion),
                                                            Sanitiser.SanitisePlatformField(platform.LongName),
                                                            Sanitiser.SanitisePlatformField(platform.LongVersion),
                                                            Sanitiser.SanitisePlatformField(platform.Url),
                                                            platform.Device ?? SauceryConstants.NULL_STRING,
                                                            platform.DeviceOrientation ?? SauceryConstants.NULL_STRING)).GetEnumerator());
 }
Ejemplo n.º 2
0
        //public static IEnumerable<PlatformTestData> GetPlatforms {
        //    get {
        //        var validator = new ActivationValidator();
        //        validator.CheckActivation();
        //        //var platforms = new JsonDeserializer().Deserialize<List<SaucePlatform>>(new RestResponse { Content = ondemand });
        //        var platforms = JsonConvert.DeserializeObject(Enviro.SauceOnDemandBrowsers, typeof(List<SaucePlatform>)) as List<SaucePlatform>;

        //        return platforms != null
        //            ? platforms.Select(platform => new PlatformTestData {
        //                Os = Sanitiser.SanitisePlatformField(platform.Os),
        //                Platform = Sanitiser.SanitisePlatformField(platform.Platform),
        //                BrowserName = Sanitiser.SanitisePlatformField(platform.Browser),
        //                BrowserVersion = Sanitiser.SanitisePlatformField(platform.BrowserVersion),
        //                LongName = Sanitiser.SanitisePlatformField(platform.LongName),
        //                LongVersion = Sanitiser.SanitisePlatformField(platform.LongVersion),
        //                Url = Sanitiser.SanitisePlatformField(platform.Url),
        //                Device = platform.Device ?? SauceryConstants.NULL_STRING,
        //                DeviceType = platform.DeviceType ?? SauceryConstants.NULL_STRING,
        //                DeviceOrientation = platform.DeviceOrientation ?? SauceryConstants.NULL_STRING})
        //            : null;
        //    }
        //}

        #endregion

        public IEnumerator GetEnumerator()
        {
            //var platforms = new JsonDeserializer().Deserialize<List<SaucePlatform>>(new RestResponse { Content = Enviro.SauceOnDemandBrowsers });
            var platforms = JsonConvert.DeserializeObject(Enviro.SauceOnDemandBrowsers, typeof(List <SaucePlatform>)) as List <SaucePlatform>;

            return(platforms != null
                    ? platforms.Select(platform => new SaucePlatform(Sanitiser.SanitisePlatformField(platform.Os),
                                                                     Sanitiser.SanitisePlatformField(platform.Platform),
                                                                     Sanitiser.SanitisePlatformField(platform.Browser),
                                                                     Sanitiser.SanitisePlatformField(platform.BrowserVersion),
                                                                     Sanitiser.SanitisePlatformField(platform.LongName),
                                                                     Sanitiser.SanitisePlatformField(platform.LongVersion),
                                                                     Sanitiser.SanitisePlatformField(platform.Url),
                                                                     platform.Device ?? SauceryConstants.NULL_STRING,
                                                                     platform.DeviceOrientation ?? SauceryConstants.NULL_STRING)).GetEnumerator()
                    : null);
        }
Ejemplo n.º 3
0
        public void Sanitise_Removes_Whitespace_Lines()
        {
            // arrange
            string[] inputLines        = { "blorg", "fester", "    ", "framistan" };
            var      whitespaceRemover = Substitute.For <IWhitespaceRemover>();

            whitespaceRemover.RemoveWhiteSpace(Arg.Any <string>()).Returns(param => string.IsNullOrWhiteSpace((string)param[0]) ? "" : param[0]);
            var commentRemover = Substitute.For <ICommentRemover>();

            commentRemover.RemoveComments(Arg.Any <string>()).Returns(param => param[0]);
            var sanitiser = new Sanitiser(whitespaceRemover, commentRemover);

            // act
            string[] cleanedLines = sanitiser.Sanitise(inputLines);

            // assert
            Assert.AreEqual(3, cleanedLines.Length);
        }
Ejemplo n.º 4
0
        public string GenerateContents(PublishedProviderVersion publishedProviderVersion,
                                       TemplateMetadataContents templateMetadataContents,
                                       TemplateMapping templateMapping)
        {
            Guard.ArgumentNotNull(publishedProviderVersion, nameof(publishedProviderVersion));
            Guard.ArgumentNotNull(templateMetadataContents, nameof(templateMetadataContents));
            Guard.ArgumentNotNull(templateMapping, nameof(templateMapping));

            Guard.ArgumentNotNull(publishedProviderVersion.Provider, nameof(publishedProviderVersion.Provider));

            IEnumerable <TemplateFundingLine> fundingLines = templateMetadataContents.RootFundingLines?.Flatten(x => x.FundingLines);
            IEnumerable <Calculation>         calculations = fundingLines?.SelectMany(fl => fl.Calculations.Flatten(cal => cal.Calculations));

            dynamic contents = new
            {
                Id             = publishedProviderVersion.FundingId,
                FundingVersion = $"{publishedProviderVersion.MajorVersion}_{publishedProviderVersion.MinorVersion}",
                Provider       = new
                {
                    Identifier = publishedProviderVersion.ProviderId,
                    publishedProviderVersion.Provider.Name,
                    SearchableName   = Sanitiser.SanitiseName(publishedProviderVersion.Provider.Name),
                    OtherIdentifiers = GetOtherIdentifiers(publishedProviderVersion.Provider),
                    publishedProviderVersion.Provider.ProviderVersionId,
                    publishedProviderVersion.Provider.ProviderType,
                    publishedProviderVersion.Provider.ProviderSubType,
                    ProviderDetails = new
                    {
                        publishedProviderVersion.Provider.DateOpened,
                        publishedProviderVersion.Provider.DateClosed,
                        publishedProviderVersion.Provider.Status,
                        publishedProviderVersion.Provider.PhaseOfEducation,
                        LocalAuthorityName = publishedProviderVersion.Provider.Authority,
                        OpenReason         = publishedProviderVersion.Provider.ReasonEstablishmentOpened,
                        CloseReason        = publishedProviderVersion.Provider.ReasonEstablishmentClosed,
                        TrustStatus        = publishedProviderVersion.Provider.TrustStatus.ToEnumMemberAttrValue(),
                        publishedProviderVersion.Provider.TrustName,
                        publishedProviderVersion.Provider.Town,
                        publishedProviderVersion.Provider.Postcode,
                        publishedProviderVersion.Provider.CompaniesHouseNumber,
                        publishedProviderVersion.Provider.GroupIdNumber,
                        publishedProviderVersion.Provider.RscRegionName,
                        publishedProviderVersion.Provider.RscRegionCode,
                        publishedProviderVersion.Provider.GovernmentOfficeRegionName,
                        publishedProviderVersion.Provider.GovernmentOfficeRegionCode,
                        publishedProviderVersion.Provider.DistrictName,
                        publishedProviderVersion.Provider.DistrictCode,
                        publishedProviderVersion.Provider.WardName,
                        publishedProviderVersion.Provider.WardCode,
                        publishedProviderVersion.Provider.CensusWardName,
                        publishedProviderVersion.Provider.CensusWardCode,
                        publishedProviderVersion.Provider.MiddleSuperOutputAreaName,
                        publishedProviderVersion.Provider.MiddleSuperOutputAreaCode,
                        publishedProviderVersion.Provider.LowerSuperOutputAreaName,
                        publishedProviderVersion.Provider.LowerSuperOutputAreaCode,
                        publishedProviderVersion.Provider.ParliamentaryConstituencyName,
                        publishedProviderVersion.Provider.ParliamentaryConstituencyCode,
                        publishedProviderVersion.Provider.CountryCode,
                        publishedProviderVersion.Provider.CountryName
                    }
                },
                FundingStreamCode = publishedProviderVersion.FundingStreamId,
                publishedProviderVersion.FundingPeriodId,
                FundingValue = new
                {
                    TotalValue   = publishedProviderVersion.TotalFunding,
                    FundingLines = fundingLines?.DistinctBy(x => x.TemplateLineId).Select(x => ToFundingLine(x,
                                                                                                             publishedProviderVersion.FundingLines)).ToDictionary(_ => _.TemplateLineId),
                    Calculations = calculations?.DistinctBy(x => x.TemplateCalculationId).Select(calculation => BuildCalculations(publishedProviderVersion.Calculations,
                                                                                                                                  calculation,
                                                                                                                                  publishedProviderVersion.ProviderId)).Where(_ => _ != null).ToDictionary(_ => _.TemplateCalculationId)
                },
                publishedProviderVersion.VariationReasons,
                Successors = string.IsNullOrWhiteSpace(publishedProviderVersion.Provider.Successor)
                    ? null
                    : new List <string>
                {
                    publishedProviderVersion.Provider.Successor
                },
                publishedProviderVersion.Predecessors
            };

            JsonSerializerSettings serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            return(JsonConvert.SerializeObject(contents, serializerSettings));
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <OrganisationGroupResult> > GenerateOrganisationGroup(
            FundingConfiguration fundingConfiguration,
            IEnumerable <Provider> scopedProviders,
            string providerVersionId,
            int?providerSnapshotId = null)
        {
            Guard.ArgumentNotNull(fundingConfiguration, nameof(fundingConfiguration));
            Guard.ArgumentNotNull(scopedProviders, nameof(scopedProviders));
            Guard.IsNullOrWhiteSpace(providerVersionId, nameof(providerVersionId));

            List <OrganisationGroupResult> results = new List <OrganisationGroupResult>();

            Dictionary <string, FdzPaymentOrganisation> paymentOrganisations = new Dictionary <string, FdzPaymentOrganisation>();

            if (fundingConfiguration.ProviderSource == ProviderSource.FDZ &&
                fundingConfiguration.OrganisationGroupings.Any(g => g.GroupingReason == GroupingReason.Payment ||
                                                               g.GroupingReason == GroupingReason.Contracting ||
                                                               g.GroupingReason == GroupingReason.Indicative))
            {
                if (!providerSnapshotId.HasValue)
                {
                    throw new InvalidOperationException("No provider snapshot ID provided, but it is required fto lookup Payment Organisations from FDZ");
                }

                ApiResponse <IEnumerable <FdzPaymentOrganisation> > paymentOrganisationsResponse = await _fundingDataZoneApiClient.GetAllOrganisations(providerSnapshotId.Value);

                if (paymentOrganisationsResponse.StatusCode == System.Net.HttpStatusCode.OK && paymentOrganisationsResponse.Content != null)
                {
                    foreach (FdzPaymentOrganisation fdzPaymentOrganisation in paymentOrganisationsResponse.Content)
                    {
                        if (paymentOrganisations.ContainsKey(fdzPaymentOrganisation.Ukprn))
                        {
                            throw new Exception($"The payment organisation group: '{fdzPaymentOrganisation.Ukprn}' needs to be unique for provider snapshot ID '{providerSnapshotId}'.");
                        }
                        else
                        {
                            paymentOrganisations.Add(fdzPaymentOrganisation.Ukprn, fdzPaymentOrganisation);
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Unable to retreive payment organisations from provider snapshot ID of {providerSnapshotId}");
                }
            }

            foreach (OrganisationGroupingConfiguration grouping in fundingConfiguration.OrganisationGroupings)
            {
                // Get the provider attribute required to group
                Func <Provider, string> providerFilterAttribute = GetProviderFieldForGrouping(grouping.GroupTypeIdentifier, grouping.OrganisationGroupTypeCode, grouping.GroupingReason, fundingConfiguration.PaymentOrganisationSource);

                // Filter providers based on provider type and subtypes
                IEnumerable <Provider> providersForGroup = grouping.ProviderTypeMatch.IsNullOrEmpty() ? scopedProviders : scopedProviders.Where(_ => ShouldIncludeProvider(_, grouping.ProviderTypeMatch));

                // Filter providers based on provider status
                providersForGroup = grouping.ProviderStatus.IsNullOrEmpty() ? providersForGroup : providersForGroup.Where(_ => ShouldIncludeProvider(_, grouping.ProviderStatus));

                // Group providers by the fields and discard any providers with null values for that field
                IEnumerable <IGrouping <string, Provider> > groupedProviders = providersForGroup.GroupBy(providerFilterAttribute);

                // Common values for all groups
                Enums.OrganisationGroupTypeClassification organisationGroupTypeClassification = grouping.GroupingReason.IsForProviderPayment() ? Enums.OrganisationGroupTypeClassification.LegalEntity : Enums.OrganisationGroupTypeClassification.GeographicalBoundary;
                Enums.OrganisationGroupTypeCode           organisationGroupTypeCode           = grouping.OrganisationGroupTypeCode.AsMatchingEnum <Enums.OrganisationGroupTypeCode>();

                // Generate Organisation Group results based on the grouped providers
                foreach (IGrouping <string, Provider> providerGrouping in groupedProviders)
                {
                    // Ignore providers without the matching data in the key
                    if (string.IsNullOrWhiteSpace(providerGrouping.Key))
                    {
                        continue;
                    }

                    TargetOrganisationGroup targetOrganisationGroup = null;

                    if (fundingConfiguration.PaymentOrganisationSource == PaymentOrganisationSource.PaymentOrganisationFields &&
                        grouping.GroupingReason.IsForProviderPayment())
                    {
                        IEnumerable <OrganisationIdentifier> identifiers;

                        // lookup alternative identifier and name from FDZ's PaymentOrganisation table via FDZ service
                        if (fundingConfiguration.ProviderSource == ProviderSource.FDZ &&
                            paymentOrganisations.TryGetValue(providerGrouping.Key, out FdzPaymentOrganisation fdzPaymentOrganisation))
                        {
                            identifiers = GetIdentifiers(fdzPaymentOrganisation);
                        }
                        else
                        {
                            identifiers = new OrganisationIdentifier[0];
                        }

                        // Will use providerGrouping.Key as the identifier of the PaymentOrganisation
                        targetOrganisationGroup = new TargetOrganisationGroup()
                        {
                            Identifier  = providerGrouping.First().PaymentOrganisationIdentifier,
                            Name        = providerGrouping.First().PaymentOrganisationName,
                            Identifiers = identifiers,
                        };
                    }
                    else if (fundingConfiguration.PaymentOrganisationSource == PaymentOrganisationSource.PaymentOrganisationAsProvider ||
                             (fundingConfiguration.PaymentOrganisationSource == PaymentOrganisationSource.PaymentOrganisationFields &&
                              !grouping.GroupingReason.IsForProviderPayment())
                             )
                    {
                        targetOrganisationGroup = await ObtainTargetOrganisationGroupFromProviderData(fundingConfiguration, providerVersionId, grouping, providerGrouping, targetOrganisationGroup);
                    }


                    if (targetOrganisationGroup == null)
                    {
                        // TODO: improve logging
                        throw new Exception($"Target Organisation Group could not be found for identifier '{providerGrouping.Key}'");
                    }

                    OrganisationGroupResult organisationGroupResult = new OrganisationGroupResult()
                    {
                        GroupTypeClassification = organisationGroupTypeClassification,
                        GroupTypeCode           = organisationGroupTypeCode,
                        GroupTypeIdentifier     = grouping.GroupTypeIdentifier.AsMatchingEnum <Enums.OrganisationGroupTypeIdentifier>(),
                        GroupReason             = grouping.GroupingReason.AsMatchingEnum <Enums.OrganisationGroupingReason>(),
                        IdentifierValue         = targetOrganisationGroup.Identifier,
                        Name           = targetOrganisationGroup.Name,
                        Identifiers    = targetOrganisationGroup.Identifiers,
                        SearchableName = Sanitiser.SanitiseName(targetOrganisationGroup.Name),
                        Providers      = providerGrouping,
                    };

                    results.Add(organisationGroupResult);
                }
            }

            return(results);
        }
Ejemplo n.º 6
0
        private PublishedFundingVersion GetPublishedFundingVersion(IGrouping <string, Provider> laGroup, TargetOrganisationGroup targetOrganisationGroup, int fundingIndex)
        {
            Provider anyProvider = laGroup.FirstOrDefault();
            PublishedFundingVersion publishedFundingVersion = new PublishedFundingVersion
            {
                FundingId         = $"{anyProvider?.FundingStreamId}-{(PublishedFundingPeriodType)Enum.Parse(typeof(PublishedFundingPeriodType), anyProvider?.FundingStreamPeriodTypeId)}-{anyProvider?.FundingPeriodId}-{GroupingReason.Payment.ToString()}-{Common.ApiClient.Policies.Models.OrganisationGroupTypeCode.LocalAuthority.ToString()}-{targetOrganisationGroup?.Identifier}-{MajorVersion}_{MinorVersion}",
                SchemaVersion     = "1.0",
                TemplateVersion   = "1.0",
                MajorVersion      = MajorVersion,
                MinorVersion      = MinorVersion,
                ProviderFundings  = laGroup.Select(x => GetFundingId(x)),
                GroupingReason    = GroupingReason.Payment,
                FundingStreamId   = anyProvider?.FundingStreamId,
                FundingStreamName = anyProvider?.FundingStreamName,
                FundingPeriod     = new PublishedFundingPeriod
                {
                    Type      = (PublishedFundingPeriodType)Enum.Parse(typeof(PublishedFundingPeriodType), anyProvider?.FundingStreamPeriodTypeId),
                    Period    = anyProvider?.FundingPeriodId,
                    Name      = anyProvider?.FundingStreamPeriodTypeName,
                    StartDate = new DateTime(2000 + int.Parse(anyProvider?.FundingPeriodId.Substring(0, 2)), int.Parse(anyProvider?.FundingStreamPeriodTypeStartMonth), int.Parse(anyProvider?.FundingStreamPeriodTypeStartDay)),
                    EndDate   = new DateTime(2000 + int.Parse(anyProvider?.FundingPeriodId.Substring(2, 2)), int.Parse(anyProvider?.FundingStreamPeriodTypeEndMoth), int.Parse(anyProvider?.FundingStreamPeriodTypeEndDay))
                },
                OrganisationGroupTypeIdentifier = targetOrganisationGroup?.Identifier,
                OrganisationGroupName           = targetOrganisationGroup?.Name,
                OrganisationGroupIdentifiers    = targetOrganisationGroup?.Identifiers?.Select(x => new PublishedOrganisationGroupTypeIdentifier
                {
                    Value = x.Value,
                    Type  = Enum.GetName(typeof(OrganisationGroupTypeIdentifier), x.Type)
                }),
                OrganisationGroupTypeClassification = "LegalEntity",
                OrganisationGroupIdentifierValue    = targetOrganisationGroup?.Identifiers?.Where(x => x.Type == OrganisationGroupTypeIdentifier.UKPRN).FirstOrDefault()?.Value,
                OrganisationGroupSearchableName     = Sanitiser.SanitiseName(targetOrganisationGroup?.Name),
                FundingLines = new List <FundingLine>
                {
                    new FundingLine
                    {
                        TemplateLineId      = TemplateLineId,
                        Value               = decimal.ToInt64(laGroup.Sum(x => decimal.Parse(x.OctoberProfileValue) + decimal.Parse(x.AprilProfileValue))),
                        DistributionPeriods = laGroup
                                              .GroupBy(la => la.OctoberDistributionPeriod)
                                              .Select(dp => new DistributionPeriod {
                            DistributionPeriodId = dp.Key,
                            Value          = dp.Sum(p => decimal.Parse(p.OctoberProfileValue)),
                            ProfilePeriods = new List <ProfilePeriod>
                            {
                                new ProfilePeriod
                                {
                                    Type                 = ProfilePeriodType.CalendarMonth,
                                    TypeValue            = dp.FirstOrDefault()?.OctoberPeriod,
                                    Year                 = int.Parse(dp.FirstOrDefault()?.OctoberPeriodYear),
                                    Occurrence           = 1,
                                    ProfiledValue        = dp.Sum(p => decimal.Parse(p.OctoberProfileValue)),
                                    DistributionPeriodId = dp.Key
                                }
                            }
                        })
                                              .Concat(laGroup
                                                      .GroupBy(la => la.AprilDistributionPeriod)
                                                      .Select(dp => new DistributionPeriod {
                            DistributionPeriodId = dp.Key,
                            Value          = dp.Sum(p => decimal.Parse(p.AprilProfileValue)),
                            ProfilePeriods = new List <ProfilePeriod>
                            {
                                new ProfilePeriod
                                {
                                    Type                 = ProfilePeriodType.CalendarMonth,
                                    TypeValue            = dp.FirstOrDefault()?.AprilPeriod,
                                    Year                 = int.Parse(dp.FirstOrDefault()?.AprilPeriodYear),
                                    Occurrence           = 1,
                                    ProfiledValue        = dp.Sum(p => decimal.Parse(p.AprilProfileValue)),
                                    DistributionPeriodId = dp.Key
                                }
                            }
                        })
                                                      )
                    }
                },
                StatusChangedDate            = hardcodedStatusDate.AddMinutes(fundingIndex),
                ExternalPublicationDate      = hardcodedStatusDate.AddMinutes(fundingIndex),
                EarliestPaymentAvailableDate = hardcodedStatusDate.AddMinutes(fundingIndex)
            };

            if (publishedFundingVersion.FundingLines != null)
            {
                publishedFundingVersion.TotalFunding = publishedFundingVersion.FundingLines
                                                       .Where(fundingLine => fundingLine != null)
                                                       .Aggregate(
                    (decimal?)null,
                    (current, fundingLineTotal) => current.AddValueIfNotNull(fundingLineTotal.Value));
            }

            return(publishedFundingVersion);
        }