public async Task CalculateAsync_WhenCalled_AssertMonthWasCalledOnEachCreditInfoInCreditInfoCollection()
        {
            ICreditInfoCollection sut = CreateSut();

            DateTime creditInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IEnumerable <Mock <ICreditInfo> > creditInfoMockCollection = new List <Mock <ICreditInfo> >
            {
                _fixture.BuildCreditInfoMock(creditInfoOffset, isLastYearOfStatusDate: true),
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-1), isLastYearOfStatusDate: true),
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-2))
            };

            sut.Add(creditInfoMockCollection.Select(creditInfoMock => creditInfoMock.Object).ToArray());

            await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            foreach (Mock <ICreditInfo> creditInfoMock in creditInfoMockCollection)
            {
                if (creditInfoMock.Object.IsLastYearOfStatusDate)
                {
                    creditInfoMock.Verify(m => m.Month, Times.Once);
                    continue;
                }

                creditInfoMock.Verify(m => m.Month, Times.Never);
            }
        }
        internal static void Populate(this ICreditInfoCollection creditInfoCollection, IAccount account, ICreditInfo[] creditInfos, DateTime statusDate, DateTime statusDateForCreditInfos)
        {
            NullGuard.NotNull(creditInfoCollection, nameof(creditInfoCollection))
            .NotNull(account, nameof(account))
            .NotNull(creditInfos, nameof(creditInfos));

            DateTime fromDate = new DateTime(statusDate.AddYears(-1).Year, 1, 1);

            if (creditInfos.Length == 0)
            {
                creditInfoCollection.Add(BuildCreditInfo(account, (short)fromDate.Year, (short)fromDate.Month));
                creditInfoCollection.Add(BuildCreditInfo(account, (short)statusDateForCreditInfos.Year, (short)statusDateForCreditInfos.Month));

                creditInfoCollection.EnsurePopulation(account);

                return;
            }

            ICreditInfo creditInfoForFromDate = creditInfos.FindInfoForFromDate(fromDate,
                                                                                (year, month, creditInfoBeforeFromDate) => BuildCreditInfo(account, year, month, creditInfoBeforeFromDate),
                                                                                (year, month) => BuildCreditInfo(account, year, month));

            creditInfoCollection.Add(creditInfoForFromDate);

            creditInfoCollection.Add(creditInfos.Between(creditInfoForFromDate.ToDate.AddDays(1), statusDateForCreditInfos));

            ICreditInfo lastCreditInfo = creditInfoCollection.Last();

            if (lastCreditInfo == null || lastCreditInfo.Year > (short)statusDateForCreditInfos.Year || lastCreditInfo.Year == (short)statusDateForCreditInfos.Year && lastCreditInfo.Month >= (short)statusDateForCreditInfos.Month)
            {
                creditInfoCollection.EnsurePopulation(account);

                return;
            }

            creditInfoCollection.Add(BuildCreditInfo(account, (short)statusDateForCreditInfos.Year, (short)statusDateForCreditInfos.Month, lastCreditInfo));

            creditInfoCollection.EnsurePopulation(account);
        }
        public async Task CalculateAsync_WhenCreditInfoCollectionDoesNotContainCreditInfoForEndOfLastYearFromStatusDate_ReturnsCreditInfoCollectionWhereBalanceInValuesAtEndOfLastYearFromStatusDateIsEqualToZero()
        {
            ICreditInfoCollection sut = CreateSut();

            DateTime creditInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IEnumerable <ICreditInfo> creditInfoCollection = new List <ICreditInfo>
            {
                _fixture.BuildCreditInfoMock(creditInfoOffset).Object,
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-1)).Object,
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-2)).Object
            };

            sut.Add(creditInfoCollection);

            ICreditInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ValuesAtEndOfLastYearFromStatusDate.Balance, Is.EqualTo(0M));
        }
        public async Task CalculateAsync_WhenCreditInfoCollectionContainsCreditInfoForEndOfLastYearFromStatusDate_ReturnsCreditInfoCollectionWhereCreditInValuesAtEndOfLastYearFromStatusDateIsEqualToCreditInCreditInfoForEndOfLastYearFromStatusDate()
        {
            ICreditInfoCollection sut = CreateSut();

            DateTime    creditInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            ICreditInfo creditInfoForEndOfLastYearFromStatusDate = _fixture.BuildCreditInfoMock(creditInfoOffset, isLastYearOfStatusDate: true).Object;
            IEnumerable <ICreditInfo> creditInfoCollection       = new List <ICreditInfo>
            {
                creditInfoForEndOfLastYearFromStatusDate,
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-1), isLastYearOfStatusDate: true).Object,
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-2), isLastYearOfStatusDate: true).Object
            };

            sut.Add(creditInfoCollection);

            ICreditInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ValuesAtEndOfLastYearFromStatusDate.Credit, Is.EqualTo(creditInfoForEndOfLastYearFromStatusDate.Credit));
        }
        public async Task CalculateAsync_WhenCreditInfoCollectionContainsCreditInfoForStatusDate_ReturnsCreditInfoCollectionWhereValuesAtStatusDateIsNotNull()
        {
            ICreditInfoCollection sut = CreateSut();

            DateTime    creditInfoOffset                   = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            ICreditInfo creditInfoForStatusDate            = _fixture.BuildCreditInfoMock(creditInfoOffset, true).Object;
            IEnumerable <ICreditInfo> creditInfoCollection = new List <ICreditInfo>
            {
                creditInfoForStatusDate,
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-1)).Object,
                _fixture.BuildCreditInfoMock(creditInfoOffset.AddMonths(-2)).Object
            };

            sut.Add(creditInfoCollection);

            ICreditInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ValuesAtStatusDate, Is.Not.Null);
        }