<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CprBroker.Providers.ServicePlatform;
using CprBroker.Providers.CprServices;
using CprBroker.Providers.ServicePlatform.Responses;
using CprBroker.Providers.CprServices.Responses;
using CprBroker.Schemas.Part;
using NUnit.Framework;

namespace CprBroker.Tests.ServicePlatform
{
    namespace ServicePlatformDataProviderTests
    {

        [TestFixture]
        public class PutSubscription : BaseResponseTests
        {
            [SetUp]
            public void InitContext()
            {
                CprBroker.Engine.BrokerContext.Initialize(CprBroker.Utilities.Constants.BaseApplicationToken.ToString(), "NUnit");
            }

            [Test]
            [TestCaseSource("PNRs")]
            public void PutSubscription_OK(string pnr)
            {
                var prov = ServicePlatformDataProviderFactory.Create();
                var ret = prov.PutSubscription(new Schemas.PersonIdentifier() { CprNumber = pnr, UUID = Guid.NewGuid() });
                Assert.True(ret);
            }
        }

        [TestFixture]
        public class ToRegistreringType1 : BaseResponseTests
        {
            public RegistreringType1 CallMethod(string pnr)
            {
                var cache = UuidCacheFactory.Create();

                var stamPlus = new StamPlusResponse(GetResponse(pnr, ServiceInfo.StamPlus_Local.Name));
                var familyPlus = new FamilyPlusResponse(GetResponse(pnr, ServiceInfo.FamilyPlus_Local.Name));

                var prov = new ServicePlatformDataProvider();
                return prov.ToRegistreringType1(stamPlus, familyPlus, null, cpr => cache.GetUuid(cpr));
            }

            [TestCaseSource("PNRs")]
            public void ToRegistreringType1_NotNull(string pnr)
            {
                Assert.NotNull(CallMethod(pnr));
            }

            [TestCaseSource("PNRs")]
            public void ToRegistreringType1_GenderOk(string pnr)
            {
                var ret = CallMethod(pnr);

                PersonGenderCodeType gender = (long.Parse(pnr) % 2) == 1 ? PersonGenderCodeType.male : PersonGenderCodeType.female;
                Assert.AreEqual(gender, ret.AttributListe.Egenskab.First().PersonGenderCode);
            }

            [Test]
            public void ToRegistreringType1_SomeHaveFather()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Fader != null
                        && reg.RelationListe.Fader.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Fader.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveMother()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Moder != null
                        && reg.RelationListe.Moder.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Moder.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveChildren()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Boern != null
                        && reg.RelationListe.Boern.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Boern.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveGuardian()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.RetligHandleevneVaergemaalsindehaver != null
                        && reg.RelationListe.RetligHandleevneVaergemaalsindehaver.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.RetligHandleevneVaergemaalsindehaver.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveSpouse()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Aegtefaelle != null
                        && reg.RelationListe.Aegtefaelle.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Aegtefaelle.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveParentalAuthority()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Foraeldremyndighedsindehaver != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Foraeldremyndighedsindehaver.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveParentalAuthorityFromParents()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(
                        reg => reg.RelationListe.Foraeldremyndighedsindehaver != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.FirstOrDefault() != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.Where(
                            p=>                            
                                (reg.RelationListe.Fader.FirstOrDefault()!=null &&  p.ReferenceID.Item.Equals(reg.RelationListe.Fader.First().ReferenceID.Item))
                                || (reg.RelationListe.Moder.FirstOrDefault()!=null &&  p.ReferenceID.Item.Equals(reg.RelationListe.Moder.First().ReferenceID.Item))
                          ).Count()>0
                        );

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveParentalAuthorityFromNonParents()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(
                        reg => reg.RelationListe.Foraeldremyndighedsindehaver != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.FirstOrDefault() != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.Where(
                            p =>
                                (reg.RelationListe.Fader.FirstOrDefault() != null && !p.ReferenceID.Item.Equals(reg.RelationListe.Fader.First().ReferenceID.Item))
                                || (reg.RelationListe.Moder.FirstOrDefault() != null && !p.ReferenceID.Item.Equals(reg.RelationListe.Moder.First().ReferenceID.Item))
                          ).Count() > 0
                        );

                Assert.NotNull(ret.FirstOrDefault());
            }

            
        }
    }
}<commit_msg>Test fix for parental authority holders in service platform<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CprBroker.Providers.ServicePlatform;
using CprBroker.Providers.CprServices;
using CprBroker.Providers.ServicePlatform.Responses;
using CprBroker.Providers.CprServices.Responses;
using CprBroker.Schemas.Part;
using NUnit.Framework;

namespace CprBroker.Tests.ServicePlatform
{
    namespace ServicePlatformDataProviderTests
    {

        [TestFixture]
        public class PutSubscription : BaseResponseTests
        {
            [SetUp]
            public void InitContext()
            {
                CprBroker.Engine.BrokerContext.Initialize(CprBroker.Utilities.Constants.BaseApplicationToken.ToString(), "NUnit");
            }

            [Test]
            [TestCaseSource("PNRs")]
            public void PutSubscription_OK(string pnr)
            {
                var prov = ServicePlatformDataProviderFactory.Create();
                var ret = prov.PutSubscription(new Schemas.PersonIdentifier() { CprNumber = pnr, UUID = Guid.NewGuid() });
                Assert.True(ret);
            }
        }

        [TestFixture]
        public class ToRegistreringType1 : BaseResponseTests
        {
            public RegistreringType1 CallMethod(string pnr)
            {
                var cache = UuidCacheFactory.Create();

                var stamPlus = new StamPlusResponse(GetResponse(pnr, ServiceInfo.StamPlus_Local.Name));
                var familyPlus = new FamilyPlusResponse(GetResponse(pnr, ServiceInfo.FamilyPlus_Local.Name));

                var prov = new ServicePlatformDataProvider();
                return prov.ToRegistreringType1(stamPlus, familyPlus, null, cpr => cache.GetUuid(cpr));
            }

            [TestCaseSource("PNRs")]
            public void ToRegistreringType1_NotNull(string pnr)
            {
                Assert.NotNull(CallMethod(pnr));
            }

            [TestCaseSource("PNRs")]
            public void ToRegistreringType1_GenderOk(string pnr)
            {
                var ret = CallMethod(pnr);

                PersonGenderCodeType gender = (long.Parse(pnr) % 2) == 1 ? PersonGenderCodeType.male : PersonGenderCodeType.female;
                Assert.AreEqual(gender, ret.AttributListe.Egenskab.First().PersonGenderCode);
            }

            [Test]
            public void ToRegistreringType1_SomeHaveFather()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Fader != null
                        && reg.RelationListe.Fader.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Fader.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveMother()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Moder != null
                        && reg.RelationListe.Moder.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Moder.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveChildren()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Boern != null
                        && reg.RelationListe.Boern.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Boern.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveGuardian()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.RetligHandleevneVaergemaalsindehaver != null
                        && reg.RelationListe.RetligHandleevneVaergemaalsindehaver.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.RetligHandleevneVaergemaalsindehaver.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveSpouse()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Aegtefaelle != null
                        && reg.RelationListe.Aegtefaelle.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Aegtefaelle.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveParentalAuthority()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(reg => reg.RelationListe.Foraeldremyndighedsindehaver != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.FirstOrDefault() != null
                        && !string.IsNullOrEmpty(reg.RelationListe.Foraeldremyndighedsindehaver.First().ReferenceID.Item));

                Assert.NotNull(ret.FirstOrDefault());
            }

            string[] GetParentUuids(RegistreringType1 reg)
            {
                var parentUuids = new List<string>();
                if (reg.RelationListe.Fader.FirstOrDefault() != null)
                    parentUuids.Add(reg.RelationListe.Fader.First().ReferenceID.Item);
                if (reg.RelationListe.Moder.FirstOrDefault() != null)
                    parentUuids.Add(reg.RelationListe.Moder.First().ReferenceID.Item);
                return parentUuids.ToArray();

            }

            [Test]
            public void ToRegistreringType1_SomeHaveParentalAuthorityFromParents()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(
                        reg => reg.RelationListe.Foraeldremyndighedsindehaver != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.FirstOrDefault() != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.Where(
                            p =>
                                GetParentUuids(reg).Contains(p.ReferenceID.Item)
                          ).Count() > 0
                        );
                Console.WriteLine("Parents " + ret.Count());
                foreach (var reg in ret)
                    Console.WriteLine((reg.AttributListe.RegisterOplysning.First().Item as CprBorgerType).PersonCivilRegistrationIdentifier);
                Assert.NotNull(ret.FirstOrDefault());
            }

            [Test]
            public void ToRegistreringType1_SomeHaveParentalAuthorityFromNonParents()
            {
                var ret = PNRs
                    .Select(pnr => CallMethod(pnr))
                    .Where(
                        reg => reg.RelationListe.Foraeldremyndighedsindehaver != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.FirstOrDefault() != null
                        && reg.RelationListe.Foraeldremyndighedsindehaver.Where(
                            p =>
                                !GetParentUuids(reg).Contains(p.ReferenceID.Item)
                          ).Count() > 0
                        );
                Console.WriteLine("Non parents " + ret.Count());
                foreach (var reg in ret)
                    Console.WriteLine((reg.AttributListe.RegisterOplysning.First().Item as CprBorgerType).PersonCivilRegistrationIdentifier); Assert.NotNull(ret.FirstOrDefault());
            }


        }
    }
}
{'fraction_non_alphanumeric': 0.09496486364515505, 'fraction_numerical': 0.002256463155180195, 'mean_word_length': 1.647098976109215, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2147781', 'n_tokens_mistral': 4264, 'n_tokens_neox': 4078, 'n_words': 545}