<commit_before>using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using SFA.DAS.EAS.Portal.Client;
using SFA.DAS.EAS.Portal.Client.Application.Queries;
using SFA.DAS.EAS.Portal.Client.Services.DasRecruit;
using SFA.DAS.EAS.Portal.Client.Types;
using SFA.DAS.Testing;
using StructureMap;

namespace SFA.DAS.EAS.Portal.UnitTests.Client
{
    [TestFixture, Parallelizable]
    public class PortalClientTests : FluentTest<PortalClientTestsFixture>
    {
        [Test]
        public Task GetAccount_WhenAccountDoesNotExistAndHasNoPayeScheme_ThenNullIsReturned()
        {
            return TestAsync(f => f.ArrangeAccountDoesNotExist().ArrangeHasNoPayeScheme(),
                f => f.GetAccount(),
                (f, r) => f.AssertNullIsReturned(r));
        }

        [Test]
        public Task GetAccount_WhenAccountDoesNotExistAndHasPayeScheme_ThenNullIsReturned()
        {
            return TestAsync(f => f.ArrangeAccountDoesNotExist(),
                f => f.GetAccount(),
                (f, r) => f.AssertNullIsReturned(r));
        }

        [Test]
        public Task GetAccount_WhenAccountDoesNotExistAndHasPayeSchemeAndHasVacancies_ThenNullIsReturned()
        {
            return TestAsync(f => f.ArrangeAccountDoesNotExist(),
                f => f.GetAccount(),
                (f, r) => f.AssertNullIsReturned(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasNoPayeScheme_ThenVacancyCardinalityIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeHasNoPayeScheme(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasNoPayeScheme_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeHasNoPayeScheme(),
                f => f.GetAccount(), (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallFails_ThenVacancyCardinalityIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallFails(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallFails_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallFails(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsNoVacancies_ThenVacancyCardinalityIsSetToNone()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallSucceedsAndReturnsNoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsSet(r, Cardinality.None));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsNoVacancies_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallSucceedsAndReturnsNoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }
        
        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsOneVacancies_ThenVacancyCardinalityIsSetToOne()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallSucceedsAndReturnsOneVacancy(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsSet(r, Cardinality.One));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsOneVacancies_ThenSingleVacancyIsSetCorrectly()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallSucceedsAndReturnsOneVacancy(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsSetCorrectly(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsTwoVacancies_ThenVacancyCardinalityIsSetToMany()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallSucceedsAndReturnsTwoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsSet(r, Cardinality.Many));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsTwoVacancies_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountDoesExist().ArrangeRecruitApiCallSucceedsAndReturnsTwoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }
    }

    public class PortalClientTestsFixture
    {
        PortalClient PortalClient { get; set; }
        Mock<IContainer> MockContainer { get; set; } = new Mock<IContainer>();
        Mock<IGetAccountQuery> MockGetAccountQuery { get; set; } = new Mock<IGetAccountQuery>();
        Mock<IDasRecruitService> MockDasRecruitService { get; set; } = new Mock<IDasRecruitService>();
        bool HasPayeScheme { get; set; } = true;
        Account Account { get; set; }
        Vacancy Vacancy { get; set; }
        Vacancy OriginalVacancy { get; set; }
        IEnumerable<Vacancy> Vacancies { get; set; }
        const long AccountId = 999L;
        const string PublicHashedAccountId = "HASH12";

        public PortalClientTestsFixture()
        {
            Vacancy = new Fixture().Create<Vacancy>();
            
            Account = JsonConvert.DeserializeObject<Account>($"{{\"Id\": {AccountId} }}");

            MockContainer.Setup(c => c.GetInstance<IGetAccountQuery>())
                .Returns(MockGetAccountQuery.Object);
            MockContainer.Setup(c => c.GetInstance<IDasRecruitService>())
                .Returns(MockDasRecruitService.Object);
            
            PortalClient = new PortalClient(MockContainer.Object);
        }

        public PortalClientTestsFixture ArrangeAccountDoesExist()
        {
            MockGetAccountQuery.Setup(q => q.Get(AccountId, It.IsAny<CancellationToken>()))
                .ReturnsAsync(Account);
            
            return this;
        }

        public PortalClientTestsFixture ArrangeAccountDoesNotExist()
        {
            MockGetAccountQuery.Setup(q => q.Get(AccountId, It.IsAny<CancellationToken>()))
                .ReturnsAsync(default(Account));
            
            return this;<commit_msg>portal client unit tests wip<commit_after>using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using SFA.DAS.EAS.Portal.Client;
using SFA.DAS.EAS.Portal.Client.Application.Queries;
using SFA.DAS.EAS.Portal.Client.Services.DasRecruit;
using SFA.DAS.EAS.Portal.Client.Types;
using SFA.DAS.Testing;
using StructureMap;

namespace SFA.DAS.EAS.Portal.UnitTests.Client
{
    /// <summary>
    /// These tests don't cover every permutation, only a (sensible) subset
    /// </summary>
    [TestFixture, Parallelizable]
    public class PortalClientTests : FluentTest<PortalClientTestsFixture>
    {
        [Test]
        public Task GetAccount_WhenAccountDoesNotExistAndHasNoPayeScheme_ThenNullIsReturned()
        {
            return TestAsync(f => f.ArrangeAccountDoesNotExist().ArrangeHasNoPayeScheme(),
                f => f.GetAccount(),
                (f, r) => f.AssertNullIsReturned(r));
        }

        [Test]
        public Task GetAccount_WhenAccountDoesNotExistAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsNoVacancies_ThenNullIsReturned()
        {
            return TestAsync(f => f.ArrangeAccountDoesNotExist().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsNoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertNullIsReturned(r));
        }

        [Test]
        public Task GetAccount_WhenAccountDoesNotExistAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsOneVacancies_ThenNullIsReturned()
        {
            return TestAsync(f => f.ArrangeAccountDoesNotExist().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsOneVacancy(),
                f => f.GetAccount(),
                (f, r) => f.AssertNullIsReturned(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasNoPayeScheme_ThenVacancyCardinalityIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasNoPayeScheme(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasNoPayeScheme_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasNoPayeScheme(),
                f => f.GetAccount(), (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallFails_ThenVacancyCardinalityIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallFails(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallFails_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallFails(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsNoVacancies_ThenVacancyCardinalityIsSetToNone()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsNoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsSet(r, Cardinality.None));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsNoVacancies_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsNoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }
        
        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsOneVacancies_ThenVacancyCardinalityIsSetToOne()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsOneVacancy(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsSet(r, Cardinality.One));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsOneVacancies_ThenSingleVacancyIsSetCorrectly()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsOneVacancy(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsSetCorrectly(r));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsTwoVacancies_ThenVacancyCardinalityIsSetToMany()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsTwoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertVacancyCardinalityIsSet(r, Cardinality.Many));
        }

        [Test]
        public Task GetAccount_WhenAccountExistsAndHasPayeSchemeAndRecruitApiCallSucceedsAndReturnsTwoVacancies_ThenSingleVacancyIsNotSet()
        {
            return TestAsync(f => f.ArrangeAccountExists().ArrangeHasPayeScheme().ArrangeRecruitApiCallSucceedsAndReturnsTwoVacancies(),
                f => f.GetAccount(),
                (f, r) => f.AssertSingleVacancyIsNotSet(r));
        }
    }

    public class PortalClientTestsFixture
    {
        PortalClient PortalClient { get; set; }
        Mock<IContainer> MockContainer { get; set; } = new Mock<IContainer>();
        Mock<IGetAccountQuery> MockGetAccountQuery { get; set; } = new Mock<IGetAccountQuery>();
        Mock<IDasRecruitService> MockDasRecruitService { get; set; } = new Mock<IDasRecruitService>();
        bool HasPayeScheme { get; set; } = true;
        Account Account { get; set; }
        Vacancy Vacancy { get; set; }
        Vacancy OriginalVacancy { get; set; }
        IEnumerable<Vacancy> Vacancies { get; set; }
        const long AccountId = 999L;
        const string PublicHashedAccountId = "HASH12";

        public PortalClientTestsFixture()
        {
            Vacancy = new Fixture().Create<Vacancy>();
            
            Account = JsonConvert.DeserializeObject<Account>($"{{\"Id\": {AccountId} }}");

            MockContainer.Setup(c => c.GetInstance<IGetAccountQuery>())
                .Returns(MockGetAccountQuery.Object);
            MockContainer.Setup(c => c.GetInstance<IDasRecruitService>())
                .Returns(MockDasRecruitService.Object);
            
            PortalClient = new PortalClient(MockContainer.Object);
        }

        public PortalClientTestsFixture ArrangeAccountExists()
        {
            MockGetAccountQuery.Setup(q => q.Get(AccountId, It.IsAny<CancellationToken>()))
                .ReturnsAsync(Account);
            
            return this;
        }

        public PortalClientTestsFixture ArrangeAccountDoesNotExist()
        {
            MockGetAccountQuery.Setup(q => q.Get(AccountId, It.IsAny<CancellationToken>()))
                .ReturnsAsync(default(Account));
            
            return this;
        }

        public PortalClientTestsFixture ArrangeHasPayeScheme()
        {
            HasPayeScheme = true;
            
            return this;
{'fraction_non_alphanumeric': 0.0944022770398482, 'fraction_numerical': 0.0006776904310111142, 'mean_word_length': 2.758787570045848, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2402266', 'n_tokens_mistral': 4402, 'n_tokens_neox': 4298, 'n_words': 594}