コード例 #1
0
 private async Task CachePublishedFundingDocument(PublishedFundingIndex publishedFundingIndex, SemaphoreSlim cacheThrottle)
 {
     try
     {
         await _retrievalService.GetFundingFeedDocument(publishedFundingIndex.DocumentPath, true);
     }
     finally
     {
         cacheThrottle.Release();
     }
 }
        private async Task <AtomFeed <AtomEntry> > CreateAtomFeed(SearchFeedV3 <PublishedFundingIndex> searchFeed, HttpRequest request)
        {
            const string fundingEndpointName       = "notifications";
            string       baseRequestPath           = request.Path.Value.Substring(0, request.Path.Value.IndexOf(fundingEndpointName, StringComparison.Ordinal) + fundingEndpointName.Length);
            string       fundingTrimmedRequestPath = baseRequestPath.Replace(fundingEndpointName, string.Empty).TrimEnd('/');

            string queryString = request.QueryString.Value;

            string fundingUrl = $"{request.Scheme}://{request.Host.Value}{baseRequestPath}{{0}}{(!string.IsNullOrWhiteSpace(queryString) ? queryString : "")}";

            AtomFeed <AtomEntry> atomFeed = CreateAtomFeedEntry(searchFeed, fundingUrl);

            ConcurrentDictionary <string, object> feedContentResults = new ConcurrentDictionary <string, object>();

            List <Task>   allTasks  = new List <Task>();
            SemaphoreSlim throttler = new SemaphoreSlim(initialCount: _externalEngineOptions.BlobLookupConcurrencyCount);

            foreach (PublishedFundingIndex feedIndex in searchFeed.Entries)
            {
                await throttler.WaitAsync();

                allTasks.Add(
                    Task.Run(async() =>
                {
                    try
                    {
                        //TODO; sort out the full document url as just the blob name is no good

                        string contents = await _publishedFundingRetrievalService.GetFundingFeedDocument(feedIndex.DocumentPath);

                        // Need to convert to an object, so JSON.NET can reserialise the contents, otherwise the string is escaped.
                        // Future TODO: change whole feed to output via text, instead of objects
                        object contentsObject = JsonConvert.DeserializeObject(contents);

                        feedContentResults.TryAdd(feedIndex.Id, contentsObject);
                    }
                    finally
                    {
                        throttler.Release();
                    }
                }));
            }

            await TaskHelper.WhenAllAndThrow(allTasks.ToArray());

            foreach (PublishedFundingIndex feedIndex in searchFeed.Entries)
            {
                AddAtomEntry(request, fundingTrimmedRequestPath, feedIndex, feedContentResults, atomFeed);
            }

            return(atomFeed);
        }
        public async Task GetFundingByFundingResultId_GivenResultFound_ReturnsContentResult()
        {
            //Arrange
            string resultId        = "12345";
            string documentPath    = "Round the ragged rocks the ragged rascal ran";
            string fundingDocument = "Now is the time for all good men to come to the aid of the party.";

            PublishedFundingIndex publishedFundingIndex = CreatePublishedFundingResult();

            publishedFundingIndex.DocumentPath = documentPath;

            ISearchRepository <PublishedFundingIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .SearchById(Arg.Is(resultId))
            .Returns(publishedFundingIndex);

            IPublishedFundingRetrievalService publishedFundingRetrievalService = Substitute.For <IPublishedFundingRetrievalService>();

            publishedFundingRetrievalService
            .GetFundingFeedDocument(documentPath)
            .Returns(fundingDocument);

            FundingFeedItemByIdService service = CreateService(searchRepository: searchRepository,
                                                               publishedFundingRetrievalService: publishedFundingRetrievalService);

            //Act
            IActionResult result = await service.GetFundingByFundingResultId(resultId);

            //Assert
            result
            .Should().BeOfType <ContentResult>()
            .Which
            .StatusCode
            .Should().Be((int)HttpStatusCode.OK);

            await searchRepository
            .Received(1)
            .SearchById(resultId);

            await publishedFundingRetrievalService
            .Received(1)
            .GetFundingFeedDocument(documentPath);

            ContentResult contentResult = result as ContentResult;

            contentResult.Content
            .Should().Be(fundingDocument);
            contentResult.ContentType
            .Should().Be("application/json");
        }
        public void SetUp()
        {
            _settings                   = new FeedItemPreLoaderSettings();
            _retrievalService           = Substitute.For <IPublishedFundingRetrievalService>();
            _searchService              = Substitute.For <IFundingFeedSearchService>();
            _cache                      = Substitute.For <IFileSystemCache>();
            _apiFileSystemCacheSettings = Substitute.For <IExternalApiFileSystemCacheSettings>();

            _preLoader = new FeedItemPreLoader(_settings,
                                               _retrievalService,
                                               _searchService,
                                               _cache,
                                               _apiFileSystemCacheSettings);

            _retrievalService
            .GetFundingFeedDocument(Arg.Any <string>(), Arg.Any <bool>())
            .Returns((string)null);
        }
        public async Task <IActionResult> GetFundingByFundingResultId(string id)
        {
            Guard.IsNullOrWhiteSpace(id, nameof(id));

            PublishedFundingIndex fundingIndexedDocument = await _fundingSearchRepositoryPolicy.ExecuteAsync(() => _fundingSearchRepository.SearchById(id));

            if (fundingIndexedDocument == null)
            {
                return(new NotFoundResult());
            }

            string fundingDocument = await _publishedFundingRetrievalService.GetFundingFeedDocument(fundingIndexedDocument.DocumentPath);

            if (string.IsNullOrWhiteSpace(fundingDocument))
            {
                _logger.Error("Failed to find blob with id {id} and document path: {documentPath}", id, fundingIndexedDocument.DocumentPath);
                return(new NotFoundResult());
            }

            return(new ContentResult()
            {
                Content = fundingDocument, ContentType = "application/json", StatusCode = (int)HttpStatusCode.OK
            });
        }
        public async Task GetFundingByFundingResultId_GivenEmptyResultFound_ReturnsNotFoundResult(string document)
        {
            //Arrange
            string resultId     = "12345";
            string documentPath = "Round the ragged rocks the ragged rascal ran";

            PublishedFundingIndex publishedFundingIndex = CreatePublishedFundingResult();

            publishedFundingIndex.DocumentPath = documentPath;

            ISearchRepository <PublishedFundingIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .SearchById(Arg.Is(resultId))
            .Returns(publishedFundingIndex);

            IPublishedFundingRetrievalService publishedFundingRetrievalService = Substitute.For <IPublishedFundingRetrievalService>();

            publishedFundingRetrievalService
            .GetFundingFeedDocument(documentPath)
            .Returns(document);

            FundingFeedItemByIdService service = CreateService(searchRepository: searchRepository,
                                                               publishedFundingRetrievalService: publishedFundingRetrievalService);

            //Act
            IActionResult result = await service.GetFundingByFundingResultId(resultId);

            //Assert
            result
            .Should().BeOfType <NotFoundResult>();

            await searchRepository
            .Received(1)
            .SearchById(resultId);
        }
        public async Task GetNotifications_GivenAQueryStringForWhichThereAreResults_ReturnsAtomFeedWithCorrectLinks()
        {
            //Arrange
            string fundingFeedDocument = JsonConvert.SerializeObject(new
            {
                FundingStreamId = "PES"
            });

            int pageRef  = 2;
            int pageSize = 3;

            List <PublishedFundingIndex>         searchFeedEntries = CreateFeedIndexes().ToList();
            SearchFeedV3 <PublishedFundingIndex> feeds             = new SearchFeedV3 <PublishedFundingIndex>
            {
                PageRef    = pageRef,
                Top        = 2,
                TotalCount = 8,
                Entries    = searchFeedEntries
            };
            PublishedFundingIndex firstFeedItem = feeds.Entries.ElementAt(0);

            IFundingFeedSearchService feedsSearchService = CreateSearchService();

            feedsSearchService
            .GetFeedsV3(Arg.Is(pageRef), Arg.Is(pageSize))
            .ReturnsForAnyArgs(feeds);

            IPublishedFundingRetrievalService publishedFundingRetrievalService = Substitute.For <IPublishedFundingRetrievalService>();

            publishedFundingRetrievalService
            .GetFundingFeedDocument(Arg.Any <string>())
            .Returns(fundingFeedDocument);

            Mock <IExternalEngineOptions> externalEngineOptions = new Mock <IExternalEngineOptions>();

            externalEngineOptions
            .Setup(_ => _.BlobLookupConcurrencyCount)
            .Returns(10);

            FundingFeedService service = CreateService(
                searchService: feedsSearchService,
                publishedFundingRetrievalService: publishedFundingRetrievalService,
                externalEngineOptions.Object);

            IHeaderDictionary headerDictionary = new HeaderDictionary
            {
                { "Accept", new StringValues("application/json") }
            };

            IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues>
            {
                { "pageRef", new StringValues(pageRef.ToString()) },
                { "allocationStatuses", new StringValues("Published,Approved") },
                { "pageSize", new StringValues(pageSize.ToString()) }
            });

            string scheme      = "https";
            string path        = "/api/v3/fundings/notifications";
            string host        = "wherever.naf:12345";
            string queryString = "?pageSize=2";

            HttpRequest request = Substitute.For <HttpRequest>();

            request.Scheme.Returns(scheme);
            request.Path.Returns(new PathString(path));
            request.Host.Returns(new HostString(host));
            request.QueryString.Returns(new QueryString(queryString));
            request.Headers.Returns(headerDictionary);
            request.Query.Returns(queryStringValues);

            //Act
            IActionResult result = await service.GetFunding(request, pageRef : pageRef, pageSize : pageSize);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            OkObjectResult contentResult = result as OkObjectResult;

            Models.External.V3.AtomItems.AtomFeed <AtomEntry> atomFeed = contentResult.Value as Models.External.V3.AtomItems.AtomFeed <AtomEntry>;

            atomFeed
            .Should()
            .NotBeNull();

            atomFeed.Id.Should().NotBeEmpty();
            atomFeed.Title.Should().Be("Calculate Funding Service Funding Feed");
            atomFeed.Author.Name.Should().Be("Calculate Funding Service");
            atomFeed.Link.First(m => m.Rel == "next-archive").Href.Should().Be($"{scheme}://{host}{path}/3{queryString}");
            atomFeed.Link.First(m => m.Rel == "prev-archive").Href.Should().Be($"{scheme}://{host}{path}/1{queryString}");
            atomFeed.Link.First(m => m.Rel == "self").Href.Should().Be($"{scheme}://{host}{path}{queryString}");
            atomFeed.Link.First(m => m.Rel == "current").Href.Should().Be($"{scheme}://{host}{path}/2{queryString}");

            atomFeed.AtomEntry.Count.Should().Be(3);

            for (int i = 0; i < 3; i++)
            {
                string text = $"id-{i + 1}";
                atomFeed.AtomEntry.ElementAt(i).Id.Should().Be($"{scheme}://{host}/api/v3/fundings/byId/{text}");
                atomFeed.AtomEntry.ElementAt(i).Title.Should().Be(text);
                atomFeed.AtomEntry.ElementAt(i).Summary.Should().Be(text);
                atomFeed.AtomEntry.ElementAt(i).Content.Should().NotBeNull();
            }

            JObject content = atomFeed.AtomEntry.ElementAt(0).Content as JObject;

            content.TryGetValue("FundingStreamId", out JToken token);
            ((JValue)token).Value <string>().Should().Be("PES");

            await feedsSearchService
            .Received(1)
            .GetFeedsV3(pageRef, pageSize, null, null, null);

            await publishedFundingRetrievalService
            .Received(searchFeedEntries.Count)
            .GetFundingFeedDocument(Arg.Any <string>());

            foreach (PublishedFundingIndex index in searchFeedEntries)
            {
                await publishedFundingRetrievalService
                .Received(1)
                .GetFundingFeedDocument(index.DocumentPath);
            }
        }