/// <summary> /// Gets the contract. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="contractId">The contract identifier.</param> /// <param name="includeProducts">if set to <c>true</c> [include products].</param> /// <param name="includeProductsWithFees">if set to <c>true</c> [include products with fees].</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="NotFoundException">No contract found in read model with Id [{contractId}]</exception> public async Task <ContractModel> GetContract(Guid estateId, Guid contractId, Boolean includeProducts, Boolean includeProductsWithFees, CancellationToken cancellationToken) { EstateReportingContext context = await this.ContextFactory.GetContext(estateId, cancellationToken); Contract contract = await context.Contracts.SingleOrDefaultAsync(c => c.EstateId == estateId && c.ContractId == contractId, cancellationToken); if (contract == null) { throw new NotFoundException($"No contract found in read model with Id [{contractId}]"); } List <ContractProduct> contractProducts = null; List <ContractProductTransactionFee> contractProductFees = null; if (includeProducts || includeProductsWithFees) { contractProducts = await context.ContractProducts.Where(cp => cp.EstateId == estateId && cp.ContractId == contractId).ToListAsync(cancellationToken); } if (includeProductsWithFees) { contractProductFees = await context.ContractProductTransactionFees.Where(f => f.EstateId == estateId && f.ContractId == contractId) .ToListAsync(cancellationToken); } return(this.ModelFactory.ConvertFrom(contract, contractProducts, contractProductFees)); }
private static async Task <EstateReportingContext> GetContext(String databaseName, TestDatabaseType databaseType = TestDatabaseType.InMemory) { EstateReportingContext context = null; if (databaseType == TestDatabaseType.InMemory) { DbContextOptionsBuilder <EstateReportingContext> builder = new DbContextOptionsBuilder <EstateReportingContext>().UseInMemoryDatabase(databaseName) .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning)); context = new EstateReportingContext(builder.Options); } else if (databaseType == TestDatabaseType.SqliteInMemory) { SqliteConnection inMemorySqlite = new SqliteConnection("Data Source=:memory:"); inMemorySqlite.Open(); DbContextOptionsBuilder <EstateReportingContext> builder = new DbContextOptionsBuilder <EstateReportingContext>().UseSqlite(inMemorySqlite); context = new EstateReportingContext(builder.Options); await context.Database.MigrateAsync(); } else { throw new NotSupportedException($"Database type [{databaseType}] not supported"); } return(context); }
/// <summary> /// Gets the merchants. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <List <MerchantModel> > GetMerchants(Guid estateId, CancellationToken cancellationToken) { EstateReportingContext context = await this.ContextFactory.GetContext(estateId, cancellationToken); List <Merchant> merchants = await(from m in context.Merchants where m.EstateId == estateId select m).ToListAsync(cancellationToken); List <MerchantAddress> merchantAddresses = await(from a in context.MerchantAddresses where a.EstateId == estateId select a).ToListAsync(cancellationToken); List <MerchantContact> merchantContacts = await(from c in context.MerchantContacts where c.EstateId == estateId select c).ToListAsync(cancellationToken); List <MerchantDevice> merchantDevices = await(from d in context.MerchantDevices where d.EstateId == estateId select d).ToListAsync(cancellationToken); List <MerchantSecurityUser> merchantSecurityUsers = await(from u in context.MerchantSecurityUsers where u.EstateId == estateId select u).ToListAsync(cancellationToken); List <MerchantOperator> merchantOperators = await(from o in context.MerchantOperators where o.EstateId == estateId select o).ToListAsync(cancellationToken); if (merchants.Any() == false) { return(null); } List <MerchantModel> models = new List <MerchantModel>(); foreach (Merchant merchant in merchants) { List <MerchantAddress> addresses = merchantAddresses.Where(a => a.MerchantId == merchant.MerchantId).ToList(); List <MerchantContact> contacts = merchantContacts.Where(c => c.MerchantId == merchant.MerchantId).ToList(); List <MerchantDevice> devices = merchantDevices.Where(d => d.MerchantId == merchant.MerchantId).ToList(); List <MerchantSecurityUser> securityUsers = merchantSecurityUsers.Where(s => s.MerchantId == merchant.MerchantId).ToList(); List <MerchantOperator> operators = merchantOperators.Where(o => o.MerchantId == merchant.MerchantId).ToList(); models.Add(this.ModelFactory.ConvertFrom(merchant, addresses, contacts, operators, devices, securityUsers)); } return(models); }
public async Task EstateManagementRepository_GetMerchants_MerchantRetrieved(TestDatabaseType testDatabaseType) { EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType); context.Merchants.Add(TestData.MerchantEntity); context.MerchantContacts.Add(TestData.MerchantContactEntity); context.MerchantAddresses.Add(TestData.MerchantAddressEntity); context.MerchantDevices.Add(TestData.MerchantDeviceEntity); context.MerchantOperators.Add(TestData.MerchantOperatorEntity); context.MerchantSecurityUsers.Add(TestData.MerchantSecurityUserEntity); await context.SaveChangesAsync(); var dbContextFactory = this.GetMockDbContextFactory(); Mock <IModelFactory> modelFactory = new Mock <IModelFactory>(); dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context); modelFactory.Setup(m => m.ConvertFrom(It.IsAny <EstateReporting.Database.Entities.Merchant>(), It.IsAny <List <MerchantAddress> >(), It.IsAny <List <MerchantContact> >(), It.IsAny <List <MerchantOperator> >(), It.IsAny <List <MerchantDevice> >(), It.IsAny <List <MerchantSecurityUser> >())); EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object); List <Merchant> merchantListModel = await estateManagementRepository.GetMerchants(TestData.EstateId, CancellationToken.None); merchantListModel.ShouldNotBeNull(); merchantListModel.ShouldNotBeEmpty(); merchantListModel.ShouldHaveSingleItem(); }
/// <summary> /// Gets the merchant balance history. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="merchantId">The merchant identifier.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <List <MerchantBalanceHistoryModel> > GetMerchantBalanceHistory(Guid estateId, Guid merchantId, CancellationToken cancellationToken) { EstateReportingContext context = await this.ContextFactory.GetContext(estateId, cancellationToken); List <MerchantBalanceView> merchantBalanceHistories = await context.MerchantBalanceView.Where(m => m.MerchantId == merchantId).OrderByDescending(o => o.EntryDateTime).Take(100).ToListAsync(cancellationToken); List <MerchantBalanceHistoryModel> merchantBalanceHistoryModels = this.ModelFactory.ConvertFrom(merchantBalanceHistories); return(merchantBalanceHistoryModels); }
public async Task EstateManagementRepository_GetEstate_EstateNotFound_ErrorThrown(TestDatabaseType testDatabaseType) { EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType); var dbContextFactory = this.GetMockDbContextFactory(); Mock <IModelFactory> modelFactory = new Mock <IModelFactory>(); dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context); modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Estate>(), It.IsAny <List <EstateOperator> >(), It.IsAny <List <EstateSecurityUser> >())) .Returns(new Models.Estate.Estate()); EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object); Should.Throw <NotFoundException>(async() => { await estateManagementRepository.GetEstate(TestData.EstateId, CancellationToken.None); }); }
private async Task RemoveEstateReadModel() { List <Guid> estateIdList = this.TestingContext.GetAllEstateIds(); foreach (Guid estateId in estateIdList) { String databaseName = $"EstateReportingReadModel{estateId}"; // Build the connection string (to master) String connectionString = Setup.GetLocalConnectionString(databaseName); await Retry.For(async() => { EstateReportingContext context = new EstateReportingContext(connectionString); await context.Database.EnsureDeletedAsync(CancellationToken.None); }, retryFor : TimeSpan.FromMinutes(2), retryInterval : TimeSpan.FromSeconds(30)); } }
/// <summary> /// Gets the transaction fees for product. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="merchantId">The merchant identifier.</param> /// <param name="contractId">The contract identifier.</param> /// <param name="productId">The product identifier.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <List <TransactionFeeModel> > GetTransactionFeesForProduct(Guid estateId, Guid merchantId, Guid contractId, Guid productId, CancellationToken cancellationToken) { EstateReportingContext context = await this.ContextFactory.GetContext(estateId, cancellationToken); List <ContractProductTransactionFee> transactionFees = await context .ContractProductTransactionFees .Where(c => c.EstateId == estateId && c.ContractId == contractId && c.ProductId == productId && c.IsEnabled == true) .ToListAsync(cancellationToken); List <TransactionFeeModel> transactionFeeModels = this.ModelFactory.ConvertFrom(transactionFees); return(transactionFeeModels); }
/// <summary> /// Gets the estate. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="NotFoundException">No estate found with Id [{estateId}]</exception> public async Task <EstateModel> GetEstate(Guid estateId, CancellationToken cancellationToken) { EstateReportingContext context = await this.ContextFactory.GetContext(estateId, cancellationToken); Estate estate = await context.Estates.SingleOrDefaultAsync(e => e.EstateId == estateId, cancellationToken); if (estate == null) { throw new NotFoundException($"No estate found in read model with Id [{estateId}]"); } List <EstateOperator> estateOperators = await context.EstateOperators.Where(eo => eo.EstateId == estateId).ToListAsync(cancellationToken); List <EstateSecurityUser> estateSecurityUsers = await context.EstateSecurityUsers.Where(esu => esu.EstateId == estateId).ToListAsync(cancellationToken); return(this.ModelFactory.ConvertFrom(estate, estateOperators, estateSecurityUsers)); }
public async Task EstateManagementRepository_GetEstate_EstateRetrieved(TestDatabaseType testDatabaseType) { EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType); context.Estates.Add(TestData.EstateEntity); context.EstateOperators.Add(TestData.EstateOperatorEntity); context.EstateSecurityUsers.Add(TestData.EstateSecurityUserEntity); await context.SaveChangesAsync(); var dbContextFactory = this.GetMockDbContextFactory(); Mock <IModelFactory> modelFactory = new Mock <IModelFactory>(); dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context); modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Estate>(), It.IsAny <List <EstateOperator> >(), It.IsAny <List <EstateSecurityUser> >())) .Returns(new Models.Estate.Estate()); EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object); Models.Estate.Estate estateModel = await estateManagementRepository.GetEstate(TestData.EstateId, CancellationToken.None); estateModel.ShouldNotBeNull(); }
public async Task EstateManagementRepository_GetContract_ContractNotFound_ErrorThrown(TestDatabaseType testDatabaseType) { EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType); var dbContextFactory = this.GetMockDbContextFactory(); Mock <IModelFactory> modelFactory = new Mock <IModelFactory>(); dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context); modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Contract>(), It.IsAny <List <ContractProduct> >(), It.IsAny <List <ContractProductTransactionFee> >())) .Returns(TestData.ContractModel); EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object); Should.Throw <NotFoundException>(async() => { await estateManagementRepository.GetContract(TestData.EstateId, TestData.ContractId, false, false, CancellationToken.None); }); }
public async Task EstateManagementRepository_GetContracts_ContractsRetrieved(TestDatabaseType testDatabaseType) { EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType); context.Contracts.Add(TestData.ContractEntity); context.ContractProducts.Add(TestData.ContractProductEntity); context.EstateOperators.Add(TestData.EstateOperatorEntity); await context.SaveChangesAsync(); var dbContextFactory = this.GetMockDbContextFactory(); Mock <IModelFactory> modelFactory = new Mock <IModelFactory>(); dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context); modelFactory.Setup(m => m.ConvertFrom(It.IsAny <Contract>(), It.IsAny <List <ContractProduct> >(), It.IsAny <List <ContractProductTransactionFee> >())) .Returns(TestData.ContractModel); EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object); List <Models.Contract.Contract> contractModelList = await estateManagementRepository.GetContracts(TestData.EstateId, CancellationToken.None); contractModelList.ShouldNotBeNull(); contractModelList.ShouldNotBeEmpty(); }
public async Task EstateManagementRepository_GetTransactionFeesForProduct_TransactionFeesForProductRetrieved(TestDatabaseType testDatabaseType) { EstateReportingContext context = await EstateManagementRepositoryTests.GetContext(Guid.NewGuid().ToString("N"), testDatabaseType); context.ContractProductTransactionFees.Add(TestData.ContractProductTransactionFeeEntity); await context.SaveChangesAsync(); var dbContextFactory = this.GetMockDbContextFactory(); Mock <IModelFactory> modelFactory = new Mock <IModelFactory>(); dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context); modelFactory.Setup(m => m.ConvertFrom(It.IsAny <List <ContractProductTransactionFee> >())).Returns(TestData.ProductTransactionFees); EstateManagementRepository estateManagementRepository = new EstateManagementRepository(dbContextFactory.Object, modelFactory.Object); List <ContractProductTransactionFeeModel> transactionFeesModel = await estateManagementRepository.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ProductId, CancellationToken.None); transactionFeesModel.ShouldNotBeNull(); transactionFeesModel.ShouldHaveSingleItem(); }
/// <summary> /// Gets the merchant contracts. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="merchantId">The merchant identifier.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <List <ContractModel> > GetMerchantContracts(Guid estateId, Guid merchantId, CancellationToken cancellationToken) { EstateReportingContext context = await this.ContextFactory.GetContext(estateId, cancellationToken); var x = await(from c in context.Contracts join cp in context.ContractProducts on c.ContractId equals cp.ContractId join eo in context.EstateOperators on c.OperatorId equals eo.OperatorId join m in context.Merchants on c.EstateId equals m.EstateId where m.MerchantId == merchantId && m.EstateId == estateId select new { Contract = c, Product = cp, Operator = eo }).ToListAsync(cancellationToken); List <ContractModel> contracts = new List <ContractModel>(); foreach (var test in x) { // attempt to find the contract ContractModel contract = contracts.SingleOrDefault(c => c.ContractId == test.Contract.ContractId); if (contract == null) { // create the contract contract = new ContractModel { EstateId = test.Contract.EstateId, OperatorId = test.Contract.OperatorId, OperatorName = test.Operator.Name, Products = new List <Product>(), Description = test.Contract.Description, IsCreated = true, ContractId = test.Contract.ContractId }; contracts.Add(contract); } // Now add the product if not already added Boolean productFound = contract.Products.Any(p => p.ProductId == test.Product.ProductId); if (productFound == false) { // Not already there so need to add it contract.Products.Add(new Product { ProductId = test.Product.ProductId, TransactionFees = null, Value = test.Product.Value, Name = test.Product.ProductName, DisplayText = test.Product.DisplayText }); } } return(contracts); }