Example #1
0
        public virtual async Task <PaymentPlanSearchResult> SearchPlansAsync(PaymentPlanSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPlansAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(PaymentPlanSearchCacheRegion.CreateChangeToken());

                var retVal = AbstractTypeFactory <PaymentPlanSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = BuildQuery(repository, criteria);
                    var sortInfos = BuildSortExpression(criteria);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var paymentPlanIds = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                             .Select(x => x.Id)
                                             .Skip(criteria.Skip).Take(criteria.Take)
                                             .ToArrayAsync();

                        //Load plans with preserving sorting order
                        var unorderedResults = await _paymentPlanService.GetByIdsAsync(paymentPlanIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.OrderBy(x => Array.IndexOf(paymentPlanIds, x.Id)).ToArray();
                    }

                    return retVal;
                }
            }));
        }
        public GenericSearchResult <PaymentPlan> SearchPlans(PaymentPlanSearchCriteria criteria)
        {
            var retVal = new GenericSearchResult <PaymentPlan>();

            using (var repository = _subscriptionRepositoryFactory())
            {
                var query = repository.PaymentPlans;

                var sortInfos = criteria.SortInfos;
                if (sortInfos.IsNullOrEmpty())
                {
                    sortInfos = new[] { new SortInfo {
                                            SortColumn = ReflectionUtility.GetPropertyName <PaymentPlan>(x => x.CreatedDate), SortDirection = SortDirection.Descending
                                        } };
                }
                query = query.OrderBySortInfos(sortInfos);

                retVal.TotalCount = query.Count();

                var paymentPlanIds = query.Skip(criteria.Skip)
                                     .Take(criteria.Take)
                                     .ToArray()
                                     .Select(x => x.Id)
                                     .ToArray();

                //Load subscriptions with preserving sorting order
                retVal.Results = GetByIds(paymentPlanIds, criteria.ResponseGroup).OrderBy(x => Array.IndexOf(paymentPlanIds, x.Id)).ToArray();
                return(retVal);
            }
        }
Example #3
0
        protected virtual IList <SortInfo> BuildSortExpression(PaymentPlanSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[]
                {
                    new SortInfo
                    {
                        SortColumn    = nameof(PaymentPlanEntity.CreatedDate),
                        SortDirection = SortDirection.Descending
                    }
                };
            }
            return(sortInfos);
        }
Example #4
0
        public virtual async Task <PaymentPlanSearchResult> SearchPlansAsync(PaymentPlanSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchPlansAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                cacheEntry.AddExpirationToken(PaymentPlanSearchCacheRegion.CreateChangeToken());

                var retVal = AbstractTypeFactory <PaymentPlanSearchResult> .TryCreateInstance();
                using (var repository = _subscriptionRepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = repository.PaymentPlans;

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <PaymentPlan>(x => x.CreatedDate), SortDirection = SortDirection.Descending
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);

                    retVal.TotalCount = await query.CountAsync();

                    if (criteria.Take > 0)
                    {
                        var paymentPlanEntities = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        var paymentPlanIds = paymentPlanEntities.Select(x => x.Id).ToArray();

                        //Load subscriptions with preserving sorting order
                        var unorderedResults = await _paymentPlanService.GetByIdsAsync(paymentPlanIds, criteria.ResponseGroup);
                        retVal.Results = unorderedResults.AsQueryable().OrderBySortInfos(sortInfos).ToArray();
                    }

                    return retVal;
                }
            }));
        }
        public async Task TestDataExportImport()
        {
            // Arrange
            var firstSubscriptionCriteria = new SubscriptionSearchCriteria {
                Take = 0, ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var firstSubscriptionResult = new SubscriptionSearchResult {
                TotalCount = TestSubscriptions.Count
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(firstSubscriptionCriteria))
            .ReturnsAsync(firstSubscriptionResult);

            var secondSubscriptionCriteria = new SubscriptionSearchCriteria {
                Skip = 0, Take = ExpectedBatchSize, ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var secondSubscriptionResult = new SubscriptionSearchResult {
                TotalCount = TestSubscriptions.Count, Results = TestSubscriptions
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(secondSubscriptionCriteria))
            .ReturnsAsync(secondSubscriptionResult);

            var firstPaymentPlanCriteria = new PaymentPlanSearchCriteria {
                Take = 0
            };
            var firstPaymentPlanResult = new PaymentPlanSearchResult {
                TotalCount = TestPaymentPlans.Count
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(firstPaymentPlanCriteria))
            .ReturnsAsync(firstPaymentPlanResult);

            var secondPaymentPlanCriteria = new PaymentPlanSearchCriteria {
                Skip = 0, Take = ExpectedBatchSize
            };
            var secondPaymentPlanResult = new PaymentPlanSearchResult {
                TotalCount = TestPaymentPlans.Count, Results = TestPaymentPlans
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(secondPaymentPlanCriteria))
            .ReturnsAsync(secondPaymentPlanResult);

            Subscription[] actualSubscriptions = null;
            _subscriptionService.Setup(service => service.SaveSubscriptionsAsync(It.IsAny <Subscription[]>()))
            .Callback <Subscription[]>(subscriptions => actualSubscriptions = subscriptions)
            .Returns(Task.CompletedTask);

            PaymentPlan[] actualPaymentPlans = null;
            _paymentPlanService.Setup(service => service.SavePlansAsync(It.IsAny <PaymentPlan[]>()))
            .Callback <PaymentPlan[]>(paymentPlans => actualPaymentPlans = paymentPlans)
            .Returns(Task.CompletedTask);

            // Act
            string actualJson;

            using (var targetStream = new MemoryStream())
            {
                await _subscriptionExportImport.DoExportAsync(targetStream, IgnoreProgressInfo, _cancellationToken.Object);

                // DoExportAsync() closes targetStream, so extracting data from it is a bit tricky...
                var streamContents = targetStream.ToArray();
                using (var copiedStream = new MemoryStream(streamContents))
                    using (var textReader = new StreamReader(copiedStream))
                    {
                        actualJson = await textReader.ReadToEndAsync();
                    }
            }

            var importStream = GetStreamFromString(actualJson);
            await _subscriptionExportImport.DoImportAsync(importStream, IgnoreProgressInfo, _cancellationToken.Object);

            //Assert
            TestSubscriptions.Should().BeEquivalentTo(actualSubscriptions);
            TestPaymentPlans.Should().BeEquivalentTo(actualPaymentPlans);
        }
Example #6
0
        protected virtual IQueryable <PaymentPlanEntity> BuildQuery(ISubscriptionRepository repository, PaymentPlanSearchCriteria criteria)
        {
            var query = repository.PaymentPlans;

            return(query);
        }
        public async Task TestDataExport()
        {
            // Arrange
            var firstSubscriptionCriteria = new SubscriptionSearchCriteria
            {
                Take          = 0,
                ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var firstSubscriptionResult = new SubscriptionSearchResult
            {
                TotalCount = TestSubscriptions.Count
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(firstSubscriptionCriteria))
            .ReturnsAsync(firstSubscriptionResult);

            var secondSubscriptionCriteria = new SubscriptionSearchCriteria
            {
                Skip          = 0,
                Take          = ExpectedBatchSize,
                ResponseGroup = SubscriptionResponseGroup.Default.ToString()
            };
            var secondSubscriptionResult = new SubscriptionSearchResult
            {
                TotalCount = TestSubscriptions.Count,
                Results    = TestSubscriptions
            };

            _subscriptionSearchService
            .Setup(subscriptionSearchService => subscriptionSearchService.SearchSubscriptionsAsync(secondSubscriptionCriteria))
            .ReturnsAsync(secondSubscriptionResult);

            var firstPaymentPlanCriteria = new PaymentPlanSearchCriteria
            {
                Take = 0,
            };

            var firstPaymentPlanResult = new PaymentPlanSearchResult
            {
                TotalCount = TestPaymentPlans.Count
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(firstPaymentPlanCriteria))
            .ReturnsAsync(firstPaymentPlanResult);

            var secondPaymentPlanCriteria = new PaymentPlanSearchCriteria
            {
                Skip = 0,
                Take = ExpectedBatchSize
            };

            var secondPaymentPlanResult = new PaymentPlanSearchResult
            {
                TotalCount = TestPaymentPlans.Count,
                Results    = TestPaymentPlans
            };

            _paymentPlanSearchService.Setup(service => service.SearchPlansAsync(secondPaymentPlanCriteria))
            .ReturnsAsync(secondPaymentPlanResult);

            string expectedJson;

            using (var resourceStream = ReadEmbeddedResource("Resources.SerializedSubscriptionData.json"))
                using (var textReader = new StreamReader(resourceStream))
                {
                    expectedJson = await textReader.ReadToEndAsync();
                }

            // Act
            string actualJson;

            using (var targetStream = new MemoryStream())
            {
                await _subscriptionExportImport.DoExportAsync(targetStream, IgnoreProgressInfo, _cancellationToken.Object);

                // DoExportAsync() closes targetStream, so extracting data from it is a bit tricky...
                var streamContents = targetStream.ToArray();
                using (var copiedStream = new MemoryStream(streamContents))
                    using (var textReader = new StreamReader(copiedStream))
                    {
                        actualJson = await textReader.ReadToEndAsync();
                    }
            }

            // Assert
            // NOTE: whitespace characters and indentation may vary, so comparing JSON as text will not work well.
            //       To overcome it, we compare JSON tokens here.
            var expectedJObject = JsonConvert.DeserializeObject <JObject>(expectedJson);
            var actualJObject   = JsonConvert.DeserializeObject <JObject>(actualJson);

            Assert.True(JToken.DeepEquals(expectedJObject, actualJObject));
        }