private Controller CreateSut(bool hasBudgetAccount = true, IBudgetAccount budgetAccount = null)
        {
            _queryBusMock.Setup(m => m.QueryAsync <IGetBudgetAccountQuery, IBudgetAccount>(It.IsAny <IGetBudgetAccountQuery>()))
            .Returns(Task.FromResult(hasBudgetAccount ? budgetAccount ?? _fixture.BuildBudgetAccountMock().Object : null));

            return(new Controller(_commandBusMock.Object, _queryBusMock.Object));
        }
 private Mock<ICreateBudgetAccountCommand> CreateCommandMock(IBudgetAccount budgetAccount = null)
 {
     Mock<ICreateBudgetAccountCommand> commandMock = new Mock<ICreateBudgetAccountCommand>();
     commandMock.Setup(m => m.ToDomain(It.IsAny<IAccountingRepository>()))
         .Returns(budgetAccount ?? _fixture.BuildBudgetAccountMock().Object);
     return commandMock;
 }
        private QueryHandler CreateSut(bool hasBudgetAccount = true, IBudgetAccount budgetAccount = null)
        {
            _accountingRepositoryMock.Setup(m => m.GetBudgetAccountAsync(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <DateTime>()))
            .Returns(Task.FromResult(hasBudgetAccount? budgetAccount ?? _fixture.BuildBudgetAccountMock().Object : null));

            return(new QueryHandler(_validatorMock.Object, _accountingRepositoryMock.Object));
        }
Example #4
0
        public async Task ApplyCalculationAsync_WhenCalled_AssertAccountingWasCalledOnEachPostingLine()
        {
            IPostingLineCollection sut = CreateSut();

            int            accountingNumber = _fixture.Create <int>();
            string         accountNumber    = _fixture.Create <string>();
            IAccounting    accounting       = _fixture.BuildAccountingMock(accountingNumber).Object;
            IBudgetAccount budgetAccount    = _fixture.BuildBudgetAccountMock(accounting, accountNumber).Object;
            IEnumerable <Mock <IPostingLine> > postingLineMockCollection = new List <Mock <IPostingLine> >
            {
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount),
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount),
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount),
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount),
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount),
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount),
                _fixture.BuildPostingLineMock(account: _fixture.BuildAccountMock(accounting).Object, budgetAccount: budgetAccount)
            };

            sut.Add(postingLineMockCollection.Select(postingLineMock => postingLineMock.Object).ToArray());

            IBudgetAccount calculatedBudgetAccount = _fixture.BuildBudgetAccountMock(accounting, accountNumber).Object;
            await sut.ApplyCalculationAsync(calculatedBudgetAccount);

            foreach (Mock <IPostingLine> postingLineMock in postingLineMockCollection)
            {
                postingLineMock.Verify(m => m.Accounting, Times.Once);
            }
        }
        public async Task <ActionResult <BudgetAccountModel> > BudgetAccountAsync(int accountingNumber, string accountNumber, DateTimeOffset?statusDate = null)
        {
            if (string.IsNullOrWhiteSpace(accountNumber))
            {
                throw new IntranetExceptionBuilder(ErrorCode.ValueCannotBeNullOrWhiteSpace, nameof(accountNumber))
                      .WithValidatingType(typeof(string))
                      .WithValidatingField(nameof(accountNumber))
                      .Build();
            }

            IGetBudgetAccountQuery query = new GetBudgetAccountQuery
            {
                AccountingNumber = accountingNumber,
                AccountNumber    = accountNumber,
                StatusDate       = statusDate?.LocalDateTime.Date ?? DateTime.Today
            };
            IBudgetAccount budgetAccount = await _queryBus.QueryAsync <IGetBudgetAccountQuery, IBudgetAccount>(query);

            if (budgetAccount == null)
            {
                throw new IntranetExceptionBuilder(ErrorCode.ValueShouldBeKnown, nameof(accountNumber))
                      .WithValidatingType(typeof(string))
                      .WithValidatingField(nameof(accountNumber))
                      .Build();
            }

            BudgetAccountModel budgetAccountModel = _accountingModelConverter.Convert <IBudgetAccount, BudgetAccountModel>(budgetAccount);

            return(Ok(budgetAccountModel));
        }
Example #6
0
        private static Task <IPostingWarning> CalculateAsync(IBudgetAccount budgetAccount, IBudgetInfoValues budgetInfoValues, IPostingLine postingLine)
        {
            NullGuard.NotNull(postingLine, nameof(postingLine));

            return(Task.Run(() =>
            {
                if (budgetAccount == null || budgetInfoValues == null)
                {
                    return null;
                }

                decimal budget = budgetInfoValues.Budget;
                decimal posted = budgetInfoValues.Posted;

                if (budget > 0M && posted < budget)
                {
                    return new PostingWarning(PostingWarningReason.ExpectedIncomeHasNotBeenReachedYet, budgetAccount, budget - posted, postingLine);
                }

                if (budget < 0M && posted < budget)
                {
                    return new PostingWarning(PostingWarningReason.ExpectedExpensesHaveAlreadyBeenReached, budgetAccount, Math.Abs(posted) - Math.Abs(budget), postingLine);
                }

                return (IPostingWarning)null;
            }));
        }
Example #7
0
        public async Task CreateBudgetAccountAsync_WhenCalled_CreatesBudgetAccount()
        {
            IAccountingRepository sut = CreateSut();

            DateTime today = DateTime.Today;

            IAccounting accounting = await sut.GetAccountingAsync(WithExistingAccountingNumber(), today);

            IBudgetAccountGroup[] budgetAccountGroupCollection = (await sut.GetBudgetAccountGroupsAsync()).ToArray();

            IBudgetAccount budgetAccount = new BudgetAccount(accounting, WithExistingAccountNumberForBudgetAccount(), _fixture.Create <string>(), budgetAccountGroupCollection[_random.Next(0, budgetAccountGroupCollection.Length - 1)])
            {
                Description = _fixture.Create <string>()
            };

            decimal income   = _random.Next(50, 70) * 1000;
            decimal expenses = _random.Next(25, 35) * 1000;

            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(-3), income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(-2), income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(-1), income, expenses));

            income   += _random.Next(5, 10) * 1000;
            expenses += _random.Next(5, 10) * 1000;

            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today, income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(1), income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(2), income, expenses));

            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(3), 0M, 0M));

            IBudgetAccount result = await sut.CreateBudgetAccountAsync(budgetAccount);

            Assert.That(result, Is.Not.Null);
        }
        public PostingLine(Guid identifier, DateTime postingDate, string reference, IAccount account, string details, IBudgetAccount budgetAccount, decimal debit, decimal credit, IContactAccount contactAccount, int sortOrder, ICreditInfoValues accountValuesAtPostingDate = null, IBudgetInfoValues budgetAccountValuesAtPostingDate = null, IContactInfoValues contactAccountValuesAtPostingDate = null)
        {
            NullGuard.NotNull(account, nameof(account))
            .NotNullOrWhiteSpace(details, nameof(details));

            if (postingDate.Year < InfoBase <ICreditInfo> .MinYear || postingDate.Year > InfoBase <ICreditInfo> .MaxYear)
            {
                throw new ArgumentException($"Year for the posting data should be between {InfoBase<ICreditInfo>.MinYear} and {InfoBase<ICreditInfo>.MaxYear}.", nameof(postingDate));
            }

            if (postingDate.Month < InfoBase <ICreditInfo> .MinMonth || postingDate.Month > InfoBase <ICreditInfo> .MaxMonth)
            {
                throw new ArgumentException($"Month for the posting data should be between {InfoBase<ICreditInfo>.MinMonth} and {InfoBase<ICreditInfo>.MaxMonth}.", nameof(postingDate));
            }

            if (budgetAccount != null && budgetAccount.Accounting.Number != account.Accounting.Number)
            {
                throw new ArgumentException("Accounting on the given budget account does not match the accounting on the given account.", nameof(budgetAccount));
            }

            if (debit < 0M)
            {
                throw new ArgumentException("Debit cannot be lower than 0.", nameof(debit));
            }

            if (credit < 0M)
            {
                throw new ArgumentException("Credit cannot be lower than 0.", nameof(credit));
            }

            if (contactAccount != null && contactAccount.Accounting.Number != account.Accounting.Number)
            {
                throw new ArgumentException("Accounting on the given contact account does not match the accounting on the given account.", nameof(contactAccount));
            }

            if (sortOrder < 0)
            {
                throw new ArgumentException("Sort order cannot be lower than 0.", nameof(sortOrder));
            }

            Identifier  = identifier;
            Accounting  = account.Accounting;
            PostingDate = postingDate.Date;
            Reference   = string.IsNullOrWhiteSpace(reference) ? null : reference.Trim();
            Account     = account;
            AccountValuesAtPostingDate = accountValuesAtPostingDate ?? new CreditInfoValues(0M, 0M);
            Details       = details.Trim();
            BudgetAccount = budgetAccount;
            BudgetAccountValuesAtPostingDate = budgetAccount != null ? budgetAccountValuesAtPostingDate ?? new BudgetInfoValues(0M, 0M) : null;
            Debit          = debit;
            Credit         = credit;
            ContactAccount = contactAccount;
            ContactAccountValuesAtPostingDate = contactAccount != null ? contactAccountValuesAtPostingDate ?? new ContactInfoValues(0M) : null;
            SortOrder = sortOrder;

            _calculateAccountValuesAtPostingDate        = accountValuesAtPostingDate == null;
            _calculateBudgetAccountValuesAtPostingDate  = budgetAccountValuesAtPostingDate == null;
            _calculateContactAccountValuesAtPostingDate = contactAccountValuesAtPostingDate == null;
        }
Example #9
0
        public void ToDomain_WhenCalled_ReturnsBudgetAccount()
        {
            IBudgetAccountDataCommand sut = CreateSut();

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount, Is.TypeOf <BudgetAccount>());
        }
Example #10
0
        public void ToDomain_WhenCalled_ReturnsBudgetAccountWhereBudgetInfoCollectionIsNotEmpty()
        {
            IBudgetAccountDataCommand sut = CreateSut(budgetInfoCommandCollection: _fixture.CreateMany <IBudgetInfoCommand>(_random.Next(5, 10)).ToArray());

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.BudgetInfoCollection, Is.Not.Empty);
        }
Example #11
0
        public void ToDomain_WhenCalled_ReturnsBudgetAccountWhereBudgetInfoCollectionIsNotNull()
        {
            IBudgetAccountDataCommand sut = CreateSut();

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.BudgetInfoCollection, Is.Not.Null);
        }
Example #12
0
        public void ToDomain_WhenNoteWasNotGivenInBudgetAccountDataCommand_ReturnsBudgetAccountWhereNoteIsNull()
        {
            IBudgetAccountDataCommand sut = CreateSut(hasNote: false);

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.Note, Is.Null);
        }
Example #13
0
        public void ValuesForMonthOfStatusDate_WhenCalled_ReturnsNotNull()
        {
            IBudgetAccount sut = CreateSut();

            IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate;

            Assert.That(result, Is.Not.Null);
        }
        public async Task CalculateAsync_WhenCalled_ReturnsSameBudgetAccount()
        {
            IBudgetAccount sut = CreateSut();

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

            Assert.That(result, Is.SameAs(sut));
        }
        public async Task DeleteBudgetAccountAsync_WhenCalled_DeletesBudgetAccount()
        {
            IAccountingRepository sut = CreateSut();

            IBudgetAccount result = await sut.DeleteBudgetAccountAsync(WithExistingAccountingNumber(), WithExistingAccountNumberForBudgetAccount());

            Assert.That(result, Is.Null);
        }
Example #16
0
        public async Task BudgetAccountExistsAsync_WhenAccountingNumberDoesNotExist_ReturnsNull()
        {
            IAccountingRepository sut = CreateSut();

            IBudgetAccount result = await sut.GetBudgetAccountAsync(WithNonExistingAccountingNumber(), WithExistingAccountNumberForBudgetAccount(), DateTime.Today);

            Assert.That(result, Is.Null);
        }
        protected override Task ManageRepositoryAsync(IUpdateBudgetAccountCommand command)
        {
            NullGuard.NotNull(command, nameof(command));

            IBudgetAccount budgetAccount = command.ToDomain(AccountingRepository);

            return(AccountingRepository.UpdateBudgetAccountAsync(budgetAccount));
        }
        internal static IBudgetInfo ToDomain(this BudgetInfoModel budgetInfoModel, IConverter accountingModelConverter)
        {
            NullGuard.NotNull(budgetInfoModel, nameof(budgetInfoModel))
            .NotNull(accountingModelConverter, nameof(accountingModelConverter));

            IBudgetAccount budgetAccount = accountingModelConverter.Convert <BudgetAccountModel, IBudgetAccount>(budgetInfoModel.BudgetAccount);

            return(budgetInfoModel.ToDomain(budgetAccount));
        }
        public async Task ApplyCalculationAsync_WhenCalled_ReturnsSamePostingLineWhereBudgetAccountIsEqualToCalculatedBudgetAccount()
        {
            IPostingLine sut = CreateSut();

            IBudgetAccount calculatedBudgetAccount = _fixture.BuildBudgetAccountMock().Object;
            IPostingLine   result = await sut.ApplyCalculationAsync(calculatedBudgetAccount);

            Assert.That(result.BudgetAccount, Is.EqualTo(calculatedBudgetAccount));
        }
Example #20
0
        internal static IBudgetAccount ToDomain(this BudgetAccountModel budgetAccountModel, IAccounting accounting, MapperCache mapperCache, IConverter accountingModelConverter)
        {
            NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel))
            .NotNull(accounting, nameof(accounting))
            .NotNull(mapperCache, nameof(mapperCache))
            .NotNull(accountingModelConverter, nameof(accountingModelConverter));

            lock (mapperCache.SyncRoot)
            {
                IBudgetAccount budgetAccount = budgetAccountModel.Resolve(mapperCache.BudgetAccountDictionary);
                if (budgetAccount != null)
                {
                    return(budgetAccount);
                }

                IBudgetAccountGroup budgetAccountGroup = accountingModelConverter.Convert <BudgetAccountGroupModel, IBudgetAccountGroup>(budgetAccountModel.BudgetAccountGroup);

                budgetAccount = new BudgetAccount(accounting, budgetAccountModel.AccountNumber, budgetAccountModel.BasicAccount.AccountName, budgetAccountGroup)
                {
                    Description = budgetAccountModel.BasicAccount.Description,
                    Note        = budgetAccountModel.BasicAccount.Note
                };
                budgetAccountModel.CopyAuditInformationTo(budgetAccount);
                budgetAccount.SetDeletable(budgetAccountModel.Deletable);

                mapperCache.BudgetAccountDictionary.Add(budgetAccountModel.BudgetAccountIdentifier, budgetAccount);

                accounting.BudgetAccountCollection.Add(budgetAccount);

                if (budgetAccountModel.BudgetInfos != null)
                {
                    budgetAccount.BudgetInfoCollection.Populate(budgetAccount,
                                                                budgetAccountModel.BudgetInfos
                                                                .Where(budgetInfoModel => budgetInfoModel.Convertible() &&
                                                                       (budgetInfoModel.YearMonth.Year < budgetAccountModel.StatusDateForInfos.Year ||
                                                                        budgetInfoModel.YearMonth.Year == budgetAccountModel.StatusDateForInfos.Year &&
                                                                        budgetInfoModel.YearMonth.Month <= budgetAccountModel.StatusDateForInfos.Month))
                                                                .Select(budgetInfoModel => budgetInfoModel.ToDomain(budgetAccount))
                                                                .ToArray(),
                                                                budgetAccountModel.StatusDate,
                                                                budgetAccountModel.StatusDateForInfos);
                }

                if (budgetAccountModel.PostingLines != null)
                {
                    budgetAccount.PostingLineCollection.Add(budgetAccountModel.PostingLines
                                                            .Where(postingLineModel => postingLineModel.Convertible() &&
                                                                   postingLineModel.PostingDate >= budgetAccountModel.GetFromDateForPostingLines() &&
                                                                   postingLineModel.PostingDate < budgetAccountModel.GetToDateForPostingLines(1))
                                                            .Select(postingLineModel => postingLineModel.ToDomain(accounting, budgetAccount, mapperCache, accountingModelConverter))
                                                            .Where(postingLine => budgetAccount.PostingLineCollection.Contains(postingLine) == false)
                                                            .ToArray());
                }

                return(budgetAccount);
            }
        }
Example #21
0
        public void ToDomain_WhenCalled_ReturnsBudgetAccountWhereAccountingIsEqualToAccountingFromAccountingRepository()
        {
            IAccounting accounting        = _fixture.BuildAccountingMock().Object;
            IBudgetAccountDataCommand sut = CreateSut(accounting: accounting);

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.Accounting, Is.EqualTo(accounting));
        }
Example #22
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfoWhereBudgetAccountIsEqualToArgument()
        {
            IBudgetInfoCommand sut = CreateSut();

            IBudgetAccount budgetAccount = _fixture.BuildBudgetAccountMock().Object;
            IBudgetInfo    result        = sut.ToDomain(budgetAccount);

            Assert.That(result.BudgetAccount, Is.EqualTo(budgetAccount));
        }
Example #23
0
        public void ToDomain_WhenCalled_ReturnsBudgetAccountWhereAccountNameIsEqualToAccountNameFromBudgetAccountDataCommand()
        {
            string accountName            = _fixture.Create <string>();
            IBudgetAccountDataCommand sut = CreateSut(accountName: accountName);

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.AccountName, Is.EqualTo(accountName));
        }
        private Controller CreateSut(bool hasBudgetAccount = true, IBudgetAccount budgetAccount = null, IEnumerable <IBudgetAccountGroup> budgetAccountGroupCollection = null)
        {
            _queryBusMock.Setup(m => m.QueryAsync <IGetBudgetAccountQuery, IBudgetAccount>(It.IsAny <IGetBudgetAccountQuery>()))
            .Returns(Task.FromResult(hasBudgetAccount ? budgetAccount ?? _fixture.BuildBudgetAccountMock().Object : null));
            _queryBusMock.Setup(m => m.QueryAsync <EmptyQuery, IEnumerable <IBudgetAccountGroup> >(It.IsAny <EmptyQuery>()))
            .Returns(Task.FromResult(budgetAccountGroupCollection ?? _fixture.CreateMany <IBudgetAccountGroup>(_random.Next(5, 10)).ToArray()));

            return(new Controller(_commandBusMock.Object, _queryBusMock.Object, _claimResolverMock.Object));
        }
Example #25
0
        public void ToDomain_WhenDescriptionWasGivenInBudgetAccountDataCommand_ReturnsBudgetAccountWhereDescriptionIsEqualToDescriptionFromBudgetAccountDataCommand()
        {
            string description            = _fixture.Create <string>();
            IBudgetAccountDataCommand sut = CreateSut(description: description);

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.Description, Is.EqualTo(description));
        }
        private static IBudgetInfo BuildBudgetInfo(IBudgetAccount budgetAccount, short year, short month)
        {
            NullGuard.NotNull(budgetAccount, nameof(budgetAccount));

            IBudgetInfo budgetInfo = new BudgetInfo(budgetAccount, year, month, 0M, 0M);

            budgetInfo.AddAuditInformation(budgetAccount.CreatedDateTime.ToUniversalTime(), budgetAccount.CreatedByIdentifier, budgetAccount.ModifiedDateTime.ToUniversalTime(), budgetAccount.ModifiedByIdentifier);
            return(budgetInfo);
        }
Example #27
0
        public void ToDomain_WhenNoteWasGivenInBudgetAccountDataCommand_ReturnsBudgetAccountWhereNoteIsEqualToNoteFromBudgetAccountDataCommand()
        {
            string note = _fixture.Create <string>();
            IBudgetAccountDataCommand sut = CreateSut(note: note);

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.Note, Is.EqualTo(note));
        }
Example #28
0
 private IBudgetInfo CreateSut(IBudgetAccount budgetAccount = null, short?year = null, short?month = null)
 {
     return(new Domain.Accounting.BudgetInfo(
                budgetAccount ?? _fixture.BuildBudgetAccountMock().Object,
                year ?? (short)_random.Next(InfoBase <IBudgetInfo> .MinYear, InfoBase <IBudgetInfo> .MaxYear),
                month ?? (short)_random.Next(InfoBase <IBudgetInfo> .MinMonth, InfoBase <IBudgetInfo> .MaxMonth),
                Math.Abs(_fixture.Create <decimal>()),
                Math.Abs(_fixture.Create <decimal>())));
 }
Example #29
0
        public void ToDomain_WhenCalled_ReturnsBudgetAccountWhereBudgetAccountGroupIsEqualToBudgetAccountGroupFromAccountingRepository()
        {
            IBudgetAccountGroup       budgetAccountGroup = _fixture.BuildBudgetAccountGroupMock().Object;
            IBudgetAccountDataCommand sut = CreateSut(budgetAccountGroup: budgetAccountGroup);

            IBudgetAccount budgetAccount = sut.ToDomain(_accountingRepositoryMock.Object);

            Assert.That(budgetAccount.BudgetAccountGroup, Is.EqualTo(budgetAccountGroup));
        }
Example #30
0
        public void ValuesForMonthOfStatusDate_WhenCalled_ReturnsSameBudgetInfoValuesAsValuesForMonthOfStatusDateOnBudgetInfoCollection()
        {
            IBudgetInfoCollection budgetInfoCollection = _fixture.BuildBudgetInfoCollectionMock().Object;
            IBudgetAccount        sut = CreateSut(budgetInfoCollection);

            IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate;

            Assert.That(result, Is.SameAs(budgetInfoCollection.ValuesForMonthOfStatusDate));
        }