public async Task ItShouldReturnAnEmptyListIfGetAccountsThrowsAnHttpRequestException() { AccountApiClient .Setup(x => x.GetPageOfAccounts(It.IsAny <int>(), 10, null)) .ReturnsAsync(_pagedApiResponseViewModel); var e = new HttpRequestException("Some exception message"); AccountApiClient .Setup(x => x.GetAccount(It.IsAny <string>())) .ThrowsAsync(e); _sut = new Services.AccountRepository( AccountApiClient.Object, PayeSchemeObfuscator.Object, DatetimeService.Object, Logger.Object, HashingService.Object); var actual = await _sut.FindAllDetails(10, 1); AccountApiClient.Verify(x => x.GetPageOfAccounts(It.IsAny <int>(), It.IsAny <int>(), null), Times.AtLeastOnce); AccountApiClient.Verify(x => x.GetAccount(It.IsAny <string>()), Times.AtLeastOnce); Logger.Verify(x => x.Error(e, $"Exception while retrieving details for account ID {_accountWithBalanceViewModels.First().AccountHashId}")); Assert.IsNotNull(actual); CollectionAssert.IsEmpty(actual.ToList()); }
public async Task ItShouldReturnTheEntireListOfAccounts() { AccountApiClient.Setup(x => x.GetPageOfAccounts(It.IsAny <int>(), 10, null)) .ReturnsAsync(_pagedApiResponseViewModel); AccountApiClient .Setup(x => x.GetAccount(It.IsAny <string>())) .ReturnsAsync(new AccountDetailViewModel()); _sut = new Services.AccountRepository( AccountApiClient.Object, PayeSchemeObfuscator.Object, DatetimeService.Object, Logger.Object, HashingService.Object); var actual = await _sut.FindAllDetails(10, 1); AccountApiClient.Verify(x => x.GetPageOfAccounts(It.IsAny <int>(), It.IsAny <int>(), null), Times.Once); AccountApiClient.Verify(x => x.GetAccount(It.IsAny <string>()), Times.Exactly(2)); Assert.IsNotNull(actual); var list = actual.ToList(); CollectionAssert.IsNotEmpty(list); Assert.AreEqual(2, list.Count()); }
public async Task ItShouldReturnTheMatchingAccountWithOutLegalEntitiesThatAreOutOfScope( EmployerAgreementStatus scope) { var id = "123"; var accountResponse = new AccountDetailViewModel { LegalEntities = new ResourceList( new List <ResourceViewModel> { new ResourceViewModel { Href = "https://tempuri.org/legalEntity/{id}", Id = "ABC" } }) }; AccountApiClient.Setup(x => x.GetResource <AccountDetailViewModel>($"/api/accounts/{id}")) .ReturnsAsync(accountResponse); var legalEntityResponse = new LegalEntityViewModel { AgreementStatus = scope }; var legalEntity = accountResponse.LegalEntities[0]; AccountApiClient.Setup(x => x.GetResource <LegalEntityViewModel>(legalEntity.Href)) .ReturnsAsync(legalEntityResponse); var actual = await _sut.Get(id, AccountFieldsSelection.Organisations); Logger.Verify( x => x.Debug( $"{nameof(IAccountApiClient)}.{nameof(IAccountApiClient.GetResource)}<{nameof(AccountDetailViewModel)}>(\"/api/accounts/{id}\");"), Times.Once); Logger.Verify( x => x.Debug( $"{nameof(IAccountApiClient)}.{nameof(IAccountApiClient.GetResource)}<{nameof(LegalEntityViewModel)}>(\"{legalEntity.Href}\");"), Times.Once); AccountApiClient.Verify(x => x.GetResource <LegalEntityViewModel>(It.IsAny <string>()), Times.Exactly(accountResponse.LegalEntities.Count)); Assert.IsNotNull(actual); Assert.IsNotNull(actual.LegalEntities); Assert.AreEqual(0, actual.LegalEntities.Count()); Assert.IsNull(actual.PayeSchemes); Assert.IsNull(actual.Transactions); Assert.IsNull(actual.TeamMembers); }
public async Task ThenTheLevyDeclarationsAreReturned() { var expectedLevyDeclarations = new AccountResourceList <LevyDeclarationViewModel>(new List <LevyDeclarationViewModel>()); var levyHref = $"/api/accounts/2385/levy/"; AccountApiClient.Setup(x => x.GetResource <AccountResourceList <LevyDeclarationViewModel> >(levyHref)).ReturnsAsync(expectedLevyDeclarations); var result = await AccountGateway.GetLevyDeclarations(levyHref); result.Should().BeSameAs(expectedLevyDeclarations); AccountApiClient.Verify(x => x.GetResource <AccountResourceList <LevyDeclarationViewModel> >(levyHref), Times.Once); }
public async Task ThenTheAccountDetailsAreReturned() { var expectedAccountInformation = new AccountDetailViewModelBuilder().Build(); var accountHref = $"/api/accounts/2385"; AccountApiClient.Setup(x => x.GetResource <AccountDetailViewModel>(accountHref)).ReturnsAsync(expectedAccountInformation); var result = await AccountGateway.GetAccount(accountHref); result.Should().Be(expectedAccountInformation); AccountApiClient.Verify(x => x.GetResource <AccountDetailViewModel>(accountHref), Times.Once); }
public async Task ThenThePayeSchemeIsReturned() { var expectedPayeScheme = new PayeSchemeViewModel(); var payeSchemeHref = $"/api/accounts/2385/payeschemes/123456"; AccountApiClient.Setup(x => x.GetResource <PayeSchemeViewModel>(payeSchemeHref)).ReturnsAsync(expectedPayeScheme); var result = await AccountGateway.GetPayeScheme(payeSchemeHref); result.Should().Be(expectedPayeScheme); AccountApiClient.Verify(x => x.GetResource <PayeSchemeViewModel>(payeSchemeHref), Times.Once); }
public async Task ThenTheAgreementIsReturned() { var expectedAgreement = new EmployerAgreementView(); var agreementHref = $"/api/accounts/2385/agreements/123456"; AccountApiClient.Setup(x => x.GetResource <EmployerAgreementView>(agreementHref)).ReturnsAsync(expectedAgreement); var result = await AccountGateway.GetEmployerAgreement(agreementHref); result.Should().Be(expectedAgreement); AccountApiClient.Verify(x => x.GetResource <EmployerAgreementView>(agreementHref), Times.Once); }
public async Task ThenTheLegalEntityIsReturned() { var expectedLegalEntity = new LegalEntityViewModel(); var legalEntityHref = $"/api/accounts/2385/legalentities/123456"; AccountApiClient.Setup(x => x.GetResource <LegalEntityViewModel>(legalEntityHref)).ReturnsAsync(expectedLegalEntity); var result = await AccountGateway.GetLegalEntity(legalEntityHref); result.Should().Be(expectedLegalEntity); AccountApiClient.Verify(x => x.GetResource <LegalEntityViewModel>(legalEntityHref), Times.Once); }
public async Task ItShouldThrowsAnExceptionWhenAccountHasNoResourceForPayeScheme() { //Arrange AccountApiClient.Setup(x => x.GetPageOfAccounts(It.IsAny <int>(), 10, null)) .ReturnsAsync(_pagedApiResponseViewModel); AccountApiClient .Setup(x => x.GetAccount(It.IsAny <string>())) .ReturnsAsync(new AccountDetailViewModel { PayeSchemes = new ResourceList(new[] { new ResourceViewModel { Id = "1", Href = "/api/payeschemes/test1" } }) }); var e = new Exception("Some exception message"); AccountApiClient.Setup(x => x.GetResource <PayeSchemeViewModel>(It.IsAny <string>())) .ThrowsAsync(e); var obscuredPayePayeScheme = "123/123456"; PayeSchemeObfuscator.Setup(x => x.ObscurePayeScheme(It.IsAny <string>())) .Returns(obscuredPayePayeScheme); _sut = new Services.AccountRepository( AccountApiClient.Object, PayeSchemeObfuscator.Object, DatetimeService.Object, Logger.Object, HashingService.Object); //Act var actual = await _sut.FindAllDetails(10, 1); //Assert AccountApiClient.Verify(x => x.GetPageOfAccounts(It.IsAny <int>(), It.IsAny <int>(), null), Times.Once); AccountApiClient.Verify(x => x.GetAccount(It.IsAny <string>()), Times.Exactly(2)); Logger.Verify(x => x.Error(e, $"Exception occured in Account API type of {nameof(PayeSchemeViewModel)} at /api/payeschemes/test1 id 1")); Assert.IsNotNull(actual); var list = actual.ToList(); CollectionAssert.IsNotEmpty(list); Assert.AreEqual(2, list.Count()); }
public async Task ItShouldReturnTheEntireListOfAccountsWhenAccountHasPayeScheme() { //Arrange AccountApiClient.Setup(x => x.GetPageOfAccounts(It.IsAny <int>(), 10, null)) .ReturnsAsync(_pagedApiResponseViewModel); AccountApiClient .Setup(x => x.GetAccount(It.IsAny <string>())) .ReturnsAsync(new AccountDetailViewModel { PayeSchemes = new ResourceList(new[] { new ResourceViewModel { Id = "1", Href = "/api/payeschemes/test1" } }) }); AccountApiClient.Setup(x => x.GetResource <PayeSchemeViewModel>(It.IsAny <string>())) .ReturnsAsync(new PayeSchemeViewModel { Name = "Test", Ref = "123" }); var obscuredPayePayeScheme = "123/123456"; PayeSchemeObfuscator.Setup(x => x.ObscurePayeScheme(It.IsAny <string>())) .Returns(obscuredPayePayeScheme); _sut = new Services.AccountRepository( AccountApiClient.Object, PayeSchemeObfuscator.Object, DatetimeService.Object, Logger.Object, HashingService.Object); //Act var actual = await _sut.FindAllDetails(10, 1); //Assert AccountApiClient.Verify(x => x.GetPageOfAccounts(It.IsAny <int>(), It.IsAny <int>(), null), Times.Once); AccountApiClient.Verify(x => x.GetAccount(It.IsAny <string>()), Times.Exactly(2)); Assert.IsNotNull(actual); var list = actual.ToList(); CollectionAssert.IsNotEmpty(list); Assert.AreEqual(2, list.Count()); }
public async Task ItShoudFailGracefullyAndLogErrorWhenClientThrowsExceptionOnGetResourceAccount() { var id = "123"; AccountApiClient.Setup(x => x.GetResource <AccountDetailViewModel>($"/api/accounts/{id}")) .ThrowsAsync(new Exception()); var actual = await _sut.Get(id, AccountFieldsSelection.Organisations); Logger.Verify(x => x.Debug(It.IsAny <string>()), Times.Once); Logger.Verify(x => x.Error(It.IsAny <Exception>(), It.IsAny <string>()), Times.Once); AccountApiClient.Verify(x => x.GetResource <AccountDetailViewModel>(It.IsAny <string>()), Times.Once); AccountApiClient.Verify(x => x.GetResource <LegalEntityViewModel>(It.IsAny <string>()), Times.Never); Assert.IsNull(actual); }
public async Task ItShoudFailGracefullyAndLogErrorWhenClientThrowsExceptionOnGetResourceLegalEntity() { var id = "123"; var accountResponse = new AccountDetailViewModel { LegalEntities = new ResourceList( new List <ResourceViewModel> { new ResourceViewModel { Href = "https://tempuri.org/legalEntity/{id}", Id = "ABC" } }) }; AccountApiClient.Setup(x => x.GetResource <AccountDetailViewModel>($"/api/accounts/{id}")) .ReturnsAsync(accountResponse); var legalEntity = accountResponse.LegalEntities[0]; AccountApiClient.Setup(x => x.GetResource <LegalEntityViewModel>(legalEntity.Href)) .ThrowsAsync(new Exception()); var actual = await _sut.Get(id, AccountFieldsSelection.Organisations); Logger.Verify(x => x.Debug(It.IsAny <string>()), Times.Exactly(2)); Logger.Verify(x => x.Error(It.IsAny <Exception>(), It.IsAny <string>()), Times.Once); AccountApiClient.Verify(x => x.GetResource <LegalEntityViewModel>(It.IsAny <string>()), Times.Exactly(accountResponse.LegalEntities.Count)); Assert.IsNotNull(actual); Assert.IsEmpty(actual.LegalEntities); }
public async Task ItShouldReturnTheMatchingAccountWithTransaction() { var id = "123"; var accountDetailViewModel = new AccountDetailViewModel { AccountId = 123, Balance = 0m, PayeSchemes = new ResourceList( new List <ResourceViewModel> { new ResourceViewModel { Id = "123/123456", Href = "https://tempuri.org/payescheme/{1}" } }), LegalEntities = new ResourceList( new List <ResourceViewModel> { new ResourceViewModel { Id = "TempUri Limited", Href = "https://tempuri.org/organisation/{1}" } }), HashedAccountId = "DFGH", DateRegistered = DateTime.Today.AddYears(-2), OwnerEmail = "*****@*****.**", DasAccountName = "Test Account 1" }; AccountApiClient.Setup(x => x.GetResource <AccountDetailViewModel>($"/api/accounts/{id}")) .ReturnsAsync(accountDetailViewModel); var obscuredPayePayeScheme = "123/123456"; PayeSchemeObfuscator.Setup(x => x.ObscurePayeScheme(It.IsAny <string>())) .Returns(obscuredPayePayeScheme); var payeSchemeViewModel = new PayeSchemeViewModel { AddedDate = DateTime.Today.AddMonths(-4), Ref = "123/123456", Name = "123/123456", DasAccountId = "123", RemovedDate = null }; AccountApiClient.Setup(x => x.GetResource <PayeSchemeViewModel>(It.IsAny <string>())) .ReturnsAsync(payeSchemeViewModel); /* * This is a testing HACK to avoid using a concrete datetime service * because our collegues used DateTime.Now in the code! * See ASCS-83 for a fix */ var now = DateTime.Now.Date; var startOfFirstFinancialYear = new DateTime(2017, 4, 1); var monthsToQuery = (now.Year - startOfFirstFinancialYear.Year) * 12 + (now.Month - startOfFirstFinancialYear.Month) + 1; DatetimeService.Setup(x => x.GetBeginningFinancialYear(startOfFirstFinancialYear)) .Returns(startOfFirstFinancialYear); var isNotZero = 100m; var isTxDateCreated = DateTime.Today; var transactionsViewModel = new TransactionsViewModel { new TransactionViewModel { Description = "Is Not Null", Amount = isNotZero, DateCreated = isTxDateCreated }, new TransactionViewModel { Description = "Is Not Null 2", Amount = isNotZero, DateCreated = isTxDateCreated } }; AccountApiClient.Setup(x => x.GetTransactions(accountDetailViewModel.HashedAccountId, It.IsAny <int>(), It.IsAny <int>())) .ReturnsAsync(transactionsViewModel ); var actual = await _sut.Get(id, AccountFieldsSelection.Finance); Logger.Verify(x => x.Debug(It.IsAny <string>()), Times.Exactly(2)); PayeSchemeObfuscator .Verify(x => x.ObscurePayeScheme(It.IsAny <string>()), Times.Exactly(2)); AccountApiClient .Verify(x => x.GetTransactions(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(monthsToQuery)); Assert.IsNotNull(actual); Assert.IsNotNull(actual.PayeSchemes); Assert.AreEqual(1, actual.PayeSchemes.Count()); Assert.IsNotNull(actual.Transactions); Assert.AreEqual(2 * monthsToQuery, actual.Transactions.Count()); Assert.IsNull(actual.LegalEntities); Assert.IsNull(actual.TeamMembers); }