Exemplo n.º 1
0
        public async Task <CheckWebsiteStatusResponse> CheckWebsiteStatusAsync(CheckWebsiteStatusRequest request)
        {
            var key = request.GenerateCacheKey();

            var cachedResponse = cacheHandler.GetCacheObject <CheckWebsiteStatusResponse>(key);

            if (cachedResponse != null)
            {
                return(cachedResponse);
            }

            if (!Enum.TryParse <SearchEngineType>(request.SearchEngine, true, out var engineType))
            {
                throw new SearchEngineHandlerNotFound(request.SearchEngine);
            }

            var searchEngine = searchEngineHandlers.Select(engineType);

            var links = (await searchEngine.SelectLinksAsync(request.Query, request.Count)).ToList();

            var resultItems = new List <SearchResultItem>();

            for (var i = 0; i < links.Count; i++)
            {
                if (links[i].Contains(request.Link, StringComparison.InvariantCultureIgnoreCase))
                {
                    resultItems.Add(new SearchResultItem()
                    {
                        Position = i + 1
                    });
                }
            }

            var response = new CheckWebsiteStatusResponse()
            {
                ResultItems = resultItems
            };

            cacheHandler.SetCacheObject(key, response);

            return(response);
        }
Exemplo n.º 2
0
        public async Task CheckWebsiteStatusAsyncUnitTest(
            IEnumerable <ISearchEngineHandler> searchEngineHandlers,
            CheckWebsiteStatusRequest request,
            CheckWebsiteStatusResponse response,
            Type exceptionType)
        {
            var cacheHandler = new Mock <ICacheHandler>();

            var searchService = new SearchService(searchEngineHandlers, cacheHandler.Object);

            if (exceptionType == null)
            {
                var result = await searchService.CheckWebsiteStatusAsync(request);

                Assert.Equal(response.ResultItems.Count(), result.ResultItems.Count());
            }
            else
            {
                await Assert.ThrowsAsync(exceptionType, () => searchService.CheckWebsiteStatusAsync(request));
            }
        }