Exemple #1
0
 public NewsController(
     INewsParserServices parserService,
     NewsDataService newsDataService)
 {
     _dataService    = newsDataService;
     _parserServices = parserService;
 }
Exemple #2
0
        public void ReturnTheUnImportantNews()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);


            var expectedResult = new List <Newsfeed>()
            {
                new Newsfeed()
                {
                    IsImportant = false,
                    Content     = "test content 1",
                    User        = new User()
                    {
                        AvatarPictureUrl = "test url 1"
                    }
                },
                new Newsfeed()
                {
                    IsImportant = true,
                    Content     = "test content 2",
                    CreatedOn   = DateTime.Now.AddDays(-1),
                    User        = new User()
                    {
                        AvatarPictureUrl = "test url 2"
                    }
                },
                new Newsfeed()
                {
                    IsImportant = false,
                    Content     = "test content 3",
                    CreatedOn   = DateTime.Now.AddDays(-2),
                    User        = new User()
                    {
                        AvatarPictureUrl = "test url 3"
                    }
                }
            };

            mockedNewsfeedRepository
            .Setup(x => x.GetAll(
                       It.IsAny <Expression <Func <Newsfeed, bool> > >(),
                       It.IsAny <Expression <Func <Newsfeed, NewsModel> > >()))
            .Returns((Expression <Func <Newsfeed, bool> > predicate,
                      Expression <Func <Newsfeed, NewsModel> > projection) =>
                     expectedResult.Where(predicate.Compile()).Select(projection.Compile()));

            var actual = newsDataService.GetNews();

            Assert.AreEqual(actual.Count(), 2);
        }
Exemple #3
0
        protected async override Task <FetchArticlesResult> FetchArticlesAsync(int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            System.Diagnostics.Debug.WriteLine($"{GetType().Name} FetchArticlesAsync for {Title} Source");
            var result   = new FetchArticlesResult(pageNumber, pageSize);
            var articles = await NewsDataService.FetchArticlesBySource(Source);

            if (articles?.Articles != null)
            {
                result.Articles   = articles.Articles.Select(a => new ArticleViewModel(a)).ToList();
                result.TotalCount = articles.TotalCount;
            }
            return(result);
        }
Exemple #4
0
        public void Throw_ArhumentException_When_ContentIsEmpty()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <Func <IUnitOfWork> >();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                mockedUnitOfWork.Object);

            Assert.Throws <ArgumentException>(() =>
            {
                newsDataService.AddNews("username", string.Empty, DateTime.Now, false);
            });
        }
Exemple #5
0
        public void Throw_When_UsernameIsNull()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <Func <IUnitOfWork> >();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                mockedUnitOfWork.Object);

            Assert.Throws <ArgumentNullException>(() =>
            {
                newsDataService.AddNews(null, "random string", DateTime.Now, false);
            });
        }
Exemple #6
0
        public void Call_UnitOfWork_Commit_MethodOnce()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            var user = new User();

            mockedUserRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <User, bool> > >())).Returns(user);

            newsDataService.AddNews("test", "content", DateTime.Now, false);
            mockedUnitOfWork.Verify(x => x.Commit(), Times.Once);
        }
Exemple #7
0
        public void MapDataCorrectly()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            var mockedData = new List <Newsfeed>()
            {
                new Newsfeed()
                {
                    IsImportant = false,
                    CreatedOn   = DateTime.Now,
                    Content     = "test content",
                    User        = new User()
                    {
                        AvatarPictureUrl = "test url",
                        UserName         = "******"
                    }
                }
            };

            mockedNewsfeedRepository
            .Setup(x => x.GetAll(
                       It.IsAny <Expression <Func <Newsfeed, bool> > >(),
                       It.IsAny <Expression <Func <Newsfeed, NewsModel> > >()))
            .Returns((Expression <Func <Newsfeed, bool> > predicate,
                      Expression <Func <Newsfeed, NewsModel> > projection) =>
                     mockedData.Where(predicate.Compile()).Select(projection.Compile()));

            var actual = newsDataService.GetNews().First();

            var expected = mockedData.First();

            Assert.AreSame(expected.Content, actual.Content);
            Assert.AreSame(expected.User.AvatarPictureUrl, actual.AvatarPictureUrl);
            Assert.AreSame(expected.User.UserName, actual.Creator);
            Assert.AreEqual(expected.CreatedOn, actual.CreatedOn);
        }
Exemple #8
0
        public void CorrectlyAddData()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            var user = new User();

            mockedUserRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <User, bool> > >())).Returns(user);

            newsDataService.AddNews("test", "content", DateTime.Now, false);

            Assert.AreEqual(user.NewsFeed.Count, 1);
        }
Exemple #9
0
        protected async override Task <FetchArticlesResult> FetchArticlesAsync(int pageNumber = 1, int pageSize = Constants.DefaultArticlesPageSize)
        {
            var result = new FetchArticlesResult(pageNumber, pageSize);

            if (string.IsNullOrWhiteSpace(SearchTerm))
            {
                return(result);
            }

            System.Diagnostics.Debug.WriteLine($"{GetType().Name} FetchArticlesAsync for [{SearchTerm}] Search Term");
            var articles = await NewsDataService.FetchArticlesBySearchQuery(SearchTerm);

            if (articles?.Articles != null)
            {
                result.Articles   = articles.Articles.Select(a => new ArticleViewModel(a)).ToList();
                result.TotalCount = articles.TotalCount;
            }

            return(result);
        }
        public void Call_newsfeedRepo_GetAllMethodOnce()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            newsDataService.GetImportantNews();

            mockedNewsfeedRepository
            .Verify(
                x => x.GetAll(
                    It.IsAny <Expression <Func <Newsfeed, bool> > >(),
                    It.IsAny <Expression <Func <Newsfeed, NewsModel> > >()),
                Times.Exactly(1));
        }
Exemple #11
0
        public void Throw_When_UserIsNotFound()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            var user = new User();

            mockedUserRepo.Setup(x => x.GetFirst(null)).Returns(user);


            Assert.Throws <ArgumentNullException>(() =>
            {
                newsDataService.AddNews("test", "content", DateTime.Now, false);
            });
        }
Exemple #12
0
        public void ReturnThe_20_ItemsByDefault()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            var expectedResult = new List <Newsfeed>();

            for (int i = 0; i < 40; i++)
            {
                expectedResult.Add(
                    new Newsfeed()
                {
                    IsImportant = false,
                    Content     = "test content " + i,
                    User        = new User()
                    {
                        AvatarPictureUrl = "test url " + i
                    }
                });
            }

            mockedNewsfeedRepository
            .Setup(x => x.GetAll(
                       It.IsAny <Expression <Func <Newsfeed, bool> > >(),
                       It.IsAny <Expression <Func <Newsfeed, NewsModel> > >()))
            .Returns((Expression <Func <Newsfeed, bool> > predicate,
                      Expression <Func <Newsfeed, NewsModel> > projection) =>
                     expectedResult.Where(predicate.Compile()).Select(projection.Compile()));

            var actual = newsDataService.GetNews();

            Assert.AreEqual(actual.Count(), 20);
        }
Exemple #13
0
 public NewsController(UserManager <ApplicationUser> userManager, NewsDataService newsViewModelService)
 {
     _userManager          = userManager;
     _newsViewModelService = newsViewModelService;
 }
Exemple #14
0
        public void OrderItemsDescendingCorrectly()
        {
            var mockedNewsfeedRepository = new Mock <IRepository <Newsfeed> >();
            var mockedUserRepo           = new Mock <IRepository <User> >();
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();

            var newsDataService = new NewsDataService(
                mockedNewsfeedRepository.Object,
                mockedUserRepo.Object,
                () => mockedUnitOfWork.Object);

            var item1 = new Newsfeed()
            {
                IsImportant = false,
                CreatedOn   = DateTime.Now.AddDays(-2),
                Content     = "test content 1",
                User        = new User()
                {
                    AvatarPictureUrl = "test url 1"
                }
            };
            var item2 = new Newsfeed()
            {
                IsImportant = false,
                Content     = "test content 2",
                CreatedOn   = DateTime.Now.AddDays(-1),
                User        = new User()
                {
                    AvatarPictureUrl = "test url 2"
                }
            };
            var item3 = new Newsfeed()
            {
                IsImportant = false,
                Content     = "test content 3",
                CreatedOn   = DateTime.Now,
                User        = new User()
                {
                    AvatarPictureUrl = "test url 3"
                }
            };

            var expectedResult = new List <Newsfeed>()
            {
                item1, item2, item3
            };

            mockedNewsfeedRepository
            .Setup(x => x.GetAll(
                       It.IsAny <Expression <Func <Newsfeed, bool> > >(),
                       It.IsAny <Expression <Func <Newsfeed, NewsModel> > >()))
            .Returns((Expression <Func <Newsfeed, bool> > predicate,
                      Expression <Func <Newsfeed, NewsModel> > projection) =>
                     expectedResult.Where(predicate.Compile()).Select(projection.Compile()));

            var actualResult = newsDataService.GetNews().ToList();

            Assert.AreEqual(actualResult[2].CreatedOn.ToString("dd:HH:mm:ss"), expectedResult[0].CreatedOn.ToString("dd:HH:mm:ss"));
            Assert.AreEqual(actualResult[1].CreatedOn.ToString("dd:HH:mm:ss"), expectedResult[1].CreatedOn.ToString("dd:HH:mm:ss"));
            Assert.AreEqual(actualResult[0].CreatedOn.ToString("dd:HH:mm:ss"), expectedResult[2].CreatedOn.ToString("dd:HH:mm:ss"));
        }