Пример #1
0
        public void Init()
        {
            //Arrange

            var rssSourcesMock = new Mock<IRssSourcesProvider>();

            rssSourcesMock.Setup(sourcesProvider => sourcesProvider.GetRssSources(It.IsAny<RssSources>()))
                .Returns(() => new RssSources
                {
                    Sources = new List<RssSource> {_rssSource}
                });

            _logo = File.ReadAllBytes("..\\..\\TestData\\logo.png");
            _enclosure = File.ReadAllBytes("..\\..\\TestData\\enclosure.jpg");
            _rssSource.Logo = _logo;

            _header1 = new NewsHeader
            {
                Guid = new Guid("10D93F92-B3E3-41F7-9E72-03A88883F6AD"),
                Description = "07/01 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor...",
                HasEnclosure = true,
                HasLogo = true,
                Link = "http://lenta.ru/loren_ipsum_1",
                PublishDate = new DateTime(2015, 07, 01),
                Title = "Lorem ipsum 1",
                Source = "Lenta.ru",
                Enclosure = _enclosure
            };

            _header2 = new NewsHeader
            {
                Guid = new Guid("51B06028-F579-40EE-860B-A74A498A9987"),
                Description = "07/02 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor...",
                HasEnclosure = false,
                HasLogo = true,
                Link = "http://lenta.ru/loren_ipsum_2",
                PublishDate = new DateTime(2015, 07, 02),
                Title = "Lorem ipsum 2",
                Source = "Lenta.ru"
            };

            var newsProviderMock = new Mock<INewsProvider>();
            newsProviderMock.Setup(newsProvider => newsProvider.GetNews(It.IsAny<RssSources>(), 1, true)).Returns(
                new NewsHeaders
                {
                    TotalArticlesCount = 2,
                    Headers = new List<NewsHeader> {_header1, _header2}
                });

            _article1 = new ArticleContainer
            {
                Guid = _header1.Guid,
                Header = _header1,
                RssSource = _rssSource,
                Body = new BodyContainer
                {
                    HasPicture = true,
                    Body =
                        @"01.07.2015 12:00 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
                            Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
                            Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                }
            };

            _article2 = new ArticleContainer
            {
                Guid = _header2.Guid,
                Header = _header2,
                RssSource = _rssSource,
                Body = new BodyContainer
                {
                    HasPicture = false,
                    Body =
                        @"02.07.2015 12:00 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
                            Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
                            Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                }
            };

            var contentStorageMock = new Mock<IContentStorage>();
            contentStorageMock.Setup(contentStorage => contentStorage.SaveArticle(It.IsAny<ArticleContainer>()))
                .Returns(true);
            contentStorageMock.Setup(contentStorage => contentStorage.GetArticleByUrl("http://lenta.ru/loren_ipsum_1"))
                .Returns(() =>
                {
                    var task = new Task<ArticleContainer>(() => _article1);
                    task.Start();
                    return task;
                });

            contentStorageMock.Setup(contentStorage => contentStorage.GetArticleByUrl("http://lenta.ru/loren_ipsum_2"))
                .Returns(() =>
                {
                    var task = new Task<ArticleContainer>(() => _article2);
                    task.Start();
                    return task;
                });


            contentStorageMock.Setup(contentStorage => contentStorage.GetArticleByGuid(It.IsAny<Guid>()))
                .Returns((Guid guid) =>
                {
                    ArticleContainer article = null;
                    if (guid == _header1.Guid)
                        article = _article1;
                    else if (guid == _header2.Guid)
                        article = _article2;
                    var task = new Task<ArticleContainer>(() => article);
                    task.Start();
                    return task;
                });

            contentStorageMock.Setup(contentStorage => contentStorage.GetArticlesBySource(It.IsAny<RssSource>()))
                .Returns(() => new List<ArticleContainer> {_article1, _article2});


            var parserProviderMock = new Mock<IParserProvider>();

            parserProviderMock.Setup(parserProvider => parserProvider.GetArticleBody(It.IsAny<Guid>()))
                .Returns((Guid guid) =>
                {
                    ArticleContainer article = null;
                    if (guid == _header1.Guid)
                        article = _article1;
                    else if (guid == _header2.Guid)
                        article = _article2;

                    return article;
                });

            _controller = new SourcesController(rssSourcesMock.Object, newsProviderMock.Object,
                contentStorageMock.Object, parserProviderMock.Object);
        }
Пример #2
0
        /// <summary>
        /// Obtains article headers from RSS source
        /// </summary>
        /// <param name="source">Rss source</param>
        /// <param name="refresh">Flad indicating news should be refreshed</param>
        /// <returns>News headers</returns>
        public List<NewsHeader> GetArticlesHeaders(RssSource source, bool refresh)
        {
            var articleHeaders = new List<NewsHeader>();
            var rssFeed = new XmlDocument();

            #region new data loading

            if (refresh)
            {
                try
                {
                    rssFeed.Load(source.Url); // todo refactor this with HttpClient

                    // Load logo if present

                    if (source.Logo == null)
                    {
                        var logoNodes = rssFeed.SelectNodes("rss/channel/image");
                        if (logoNodes != null)
                        {
                            foreach (var logoUrl in 
                                from XmlNode inner
                                    in logoNodes
                                select inner.SelectSingleNode("url")
                                into urlNode
                                where urlNode != null
                                select urlNode.InnerText)
                            {
                                var url = logoUrl;
                                if (!logoUrl.Contains("http"))
                                    url = "http:" + url;
                                GetContent(url).ContinueWith(response =>
                                {
                                    if (response.Result != null)
                                    {
                                        var content = response.Result.Content as StreamContent;
                                        if (content != null)
                                            content.ReadAsByteArrayAsync()
                                                .ContinueWith(bytes => source.Logo = bytes.Result);
                                    }
                                }).Wait();
                                break;
                            }
                        }
                    }

                    var rssNodes = rssFeed.SelectNodes("rss/channel/item");
                    if (rssNodes != null)
                    {
                        foreach (XmlNode rssNode in rssNodes)
                        {
                            var rssSubNode = rssNode.SelectSingleNode("link");
                            var link = rssSubNode != null ? rssSubNode.InnerText : "";

                            var task = _contentStorage.GetArticleByUrl(link);
                            task.Wait();

                            if (task.Result != null)
                            {
                                articleHeaders.Add(task.Result.Header);
                                continue;
                            }

                            rssSubNode = rssNode.SelectSingleNode("title");
                            var title = rssSubNode != null ? rssSubNode.InnerText.Replace("&mdash;", "-") : "";

                            rssSubNode = rssNode.SelectSingleNode("description");
                            var description = rssSubNode != null ? rssSubNode.InnerText.StripTagsRegex() : "";
                            if (description.Length > 150)
                                description = description.Substring(0, 147) + "..."; // trim too long description for pop-over

                            rssSubNode = rssNode.SelectSingleNode("pubDate");
                            var date = rssSubNode != null ? rssSubNode.InnerText : "";

                            rssSubNode = rssNode.SelectSingleNode("enclosure");
                            var enclosure = rssSubNode != null && rssSubNode.Attributes != null
                                ? rssSubNode.Attributes["url"].Value
                                : "";
                            byte[] enclosured = null;
                            if (enclosure != "")
                            {
                                GetContent(enclosure).ContinueWith(response =>
                                {
                                    var content = response.Result.Content as StreamContent;
                                    if (content != null)
                                        content.ReadAsByteArrayAsync()
                                            .ContinueWith(bytes => enclosured = bytes.Result);
                                }).Wait();
                            }
                            var newsHeader = new NewsHeader
                            {
                                Description = description,
                                Link = link,
                                Title = title,
                                Guid = Guid.NewGuid(),
                                PublishDate = DateTime.Parse(date),
                                Enclosure = enclosured,
                                Source = source.SiteName,
                                HasLogo = source.Logo != null,
                                HasEnclosure = enclosured != null
                            };
                            articleHeaders.Add(newsHeader);
                            _contentStorage.SaveArticle(new ArticleContainer
                            {
                                Guid = newsHeader.Guid,
                                RssSource = source,
                                Header = newsHeader
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    _errorDescription = new ErrorDescription
                    {
                        Level = ApplicationLevel.RssParser,
                        Description = ex.Message
                    };
                }
            }

            #endregion
            #region obtaining loaded data
            else
                articleHeaders = _contentStorage.GetArticlesBySource(source).Select(art => art.Header).ToList();
            #endregion

            return articleHeaders;
        }