public void Listing_All_News_Should_Return_Them_Correctly()
        {
            var fakeNews = _mocks.NewsRepoMock.Object.All();

            var mockContext = new Mock<INewsData>();

            mockContext.Setup(c => c.News.All())
                .Returns(fakeNews);

            var newsController = new NewsController(mockContext.Object);
            SetupController(newsController);

            var response = newsController.GetAllNews().ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var newsResponse = response.Content.ReadAsAsync<IEnumerable<NewsViewModel>>()
                .Result
                .Select(n => n.Id)
                .ToList();

            var fakeNewsList = fakeNews
                .OrderByDescending(n => n.PublishDate)
                .Select(n => n.Id)
                .ToList();

            CollectionAssert.AreEqual(newsResponse, fakeNewsList);
        }
        public void CreateNews_WithIncorrectData_ShouldReturnBadRequestWithOutAddingTheNews()
        {
            // Arrange
            var controller = new NewsController(this.dataLayerMocked);
            this.SetupController(controller, "news");

            // Act
            var httpResponse = controller.CreateNew(null).ExecuteAsync(new CancellationToken()).Result;
            var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result;
            var newsFromService = httpGetResponse.Content.ReadAsAsync<News[]>().Result;

            // Arrange
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
            Assert.AreEqual(0, newsFromService.Count());
        }
        public void ListAllNewsShouldReturnOk()
        {
            var controller = new NewsController(this.mock.NewsRepositoryMock.Object);
            this.SetupController(controller);

            var response = controller.GetAllNews()
                .ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(
                this.mock.NewsRepositoryMock.Object.All().Count(),
                3,
                "Counts of news are not currect.");
            Assert.AreEqual(
                response.StatusCode, 
                HttpStatusCode.OK, 
                "Expect status code OK.");
        }
        public void ListAllNewsItemsCorrectly()
        {
            var repo = new RepositoryMock<News>();

            var news = new Dictionary<object, News>();

            news.Add(1, new News()
            {
                Id = 1,
                AuthorId = "test",
                Content = "News1 Content",
                Title = "News1 Title",
                PublishDate = DateTime.Now
            });

            news.Add(2, new News()
            {
                Id = 2,
                AuthorId = "test",
                Content = "News2 Content",
                Title = "News2 Title",
                PublishDate = DateTime.Now
            });

            news.Add(3, new News()
            {
                Id = 3,
                AuthorId = "test",
                Content = "News3 Content",
                Title = "News3 Title",
                PublishDate = DateTime.Now
            });

            repo.Entities = news;

            var controller = new NewsController(repo);

            var result = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);

            var theNews = JsonConvert.DeserializeObject<Dictionary<object, News>>(result.Content.ReadAsStringAsync().Result);

            // Assert
            CollectionAssert.AreEquivalent(news, theNews);
        }
        public void Delete_WithExistingNews_ShouldDeleteTheNewsAndReturn200Ok()
        {
            // Arrange
            var initialNew = new News() { Id = 1, Title = "Test news #1", Content = "Test content #1", };

            var controller = new NewsController(this.dataLayerMocked);
            this.SetupController(controller, "news");

            // Act
            this.dataLayerMocked.News.Add(initialNew);

            var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result;
            var newsFromService = httpGetResponse.Content.ReadAsAsync<List<News>>().Result;

            var newsId = newsFromService[0].Id;
            var httpDeleteResponse = controller.DeleteNewById(newsId).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(0, this.dataLayerMocked.News.All().Count());
            Assert.AreEqual(HttpStatusCode.OK, httpDeleteResponse.StatusCode);
        }
        public void UpdateNews_WithIncorrectData_ShouldReturnBadRequestWithOutUpdatingTheNews()
        {
            // Arrange
            var initialNew = new News() { Id = 1, Title = "Test news #1", Content = "Test content #1", };

            var controller = new NewsController(this.dataLayerMocked);
            this.SetupController(controller, "news");

            // Act
            this.dataLayerMocked.News.Add(initialNew);

            var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result;
            var newsFromService = httpGetResponse.Content.ReadAsAsync<List<News>>().Result;

            var newsId = newsFromService[0].Id;
            var httpPutResponse =
                controller.UpdateNewById(newsId, null).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, httpPutResponse.StatusCode);
            Assert.AreEqual(initialNew.Title, newsFromService[0].Title);
            Assert.AreEqual(initialNew.Content, newsFromService[0].Content);
        }
        public void UpdateNews_WithCorrectData_ShouldUpdateTheNewsAndReturn200Ok()
        {
            // Arrange
            var initialNew = new News() { Id = 1, Title = "Test news #1", Content = "Test content #1", };

            var controller = new NewsController(this.dataLayerMocked);
            this.SetupController(controller, "news");

            // Act
            this.dataLayerMocked.News.Add(initialNew);

            var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result;
            var newsFromService = httpGetResponse.Content.ReadAsAsync<List<News>>().Result;
            var updatedNew = new UpdateNewInputModel() { Title = "Updated", Content = "Tralala"};
            var newsId = newsFromService[0].Id;
            var httpPutResponse =
                controller.UpdateNewById(newsId, updatedNew).ExecuteAsync(new CancellationToken()).Result;

            var result = httpPutResponse.Content.ReadAsAsync<News>().Result;

            // Assert
            Assert.AreEqual(updatedNew.Title, result.Title);
            Assert.AreEqual(updatedNew.Content, result.Content);
        }
        public void GetAll_WhenValid_ShouldReturnBugsCollection()
        {
            // Arrange
            // var repo = new NewsRepositoryMock();
            var newsData = this.dataLayerMocked.News;
            var news = AddThreeNews();

            newsData.Add(new News()
                {
                    Title = "Test news #1",
                    Content = "Test content #1",
                    PublishedAt = new DateTime(2014,1,1)
                });
            newsData.Add(new News()
            {
                Title = "Test news #2",
                Content = "Test content #2",
                PublishedAt = new DateTime(2014, 1, 1)
            });
            newsData.Add(new News()
            {
                Title = "Test news #3",
                Content = "Test content #3",
                PublishedAt = new DateTime(2014, 1, 1)
            });

            var controller = new NewsController(dataLayerMocked);
            this.SetupController(controller, "news");

            // Act
            var httpResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result;
            var result = httpResponse.Content.ReadAsAsync<List<News>>().Result;

            // Assert
            Assert.AreEqual(news.Count, result.Count);
            Assert.AreEqual(news[0].Title, result[0].Title);
            Assert.AreEqual(news[1].Title, result[1].Title);
            Assert.AreEqual(news[2].Content, result[2].Content);
        }