public void List_All_Items_Should_Work_Correctly_200_OK()
        {
            // Arrange
            CleanDatabase();
            var database = new NewsContext();

            database.News.Add(new NewsRecord()
            {
                Title = "HopHop1",
                Content = "The hoppy hop",
                PublishDate = DateTime.Now
            });

            database.News.Add(new NewsRecord
            {
                Title = "HopHop2",
                Content = "The hoppy hop2",
                PublishDate = DateTime.Now.AddDays(+5)
            });

            database.SaveChanges();

            // Act
            var httpResponse = httpClient.GetAsync("/api/news").Result;
            var newsFromService = httpResponse.Content.ReadAsAsync<List<NewsRecord>>().Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
            Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json");
            Assert.AreEqual(2, newsFromService.Count);
        }
        public void GetAllNews_ShouldReturnAllNews()
        {
            var context = new NewsContext();
            context.News.Delete();
            var firstNews = new Models.News()
            {
                Title = "Murder",
                Content = "A man was slaughterd in Sofia last night.",
                PublishDate = DateTime.Now
            };
            context.News.Add(firstNews);
            var secNews = new Models.News()
            {
                Title = "Eros Ramazzotti",
                Content = "Ramazzotti will sing in Sofia in September",
                PublishDate = DateTime.Now
            };
            context.News.Add(secNews);
            context.SaveChanges();
            var repo = new NewsRepository(context);
            var newsFromDB = repo.All().ToArray();
            Assert.IsNotNull(newsFromDB[0]);
            Assert.AreEqual(firstNews.Title, newsFromDB[0].Title);
            Assert.AreEqual(firstNews.Content, newsFromDB[0].Content);
            Assert.AreEqual(firstNews.PublishDate, newsFromDB[0].PublishDate);
            Assert.IsTrue(newsFromDB[0].Id != 0);

            Assert.IsNotNull(newsFromDB[1]);
            Assert.AreEqual(secNews.Title, newsFromDB[1].Title);
            Assert.AreEqual(secNews.Content, newsFromDB[1].Content);
            Assert.AreEqual(secNews.PublishDate, newsFromDB[1].PublishDate);
            Assert.IsTrue(newsFromDB[1].Id != 0);
        }
        public void ListNews_NonEmptyDb_ShouldReturn_OkListNews()
        {
            var context = new NewsContext();
            var news1 = new News
            {
                Title = "Some new title 1.",
                Content = "Some freaky content.",
                PublishedDate = DateTime.Now
            };
            
            var news2 = new News
            {
                Title = "Some new title 2.",
                Content = "Some freaky content.",
                PublishedDate = DateTime.Now.AddDays(-10)
            };
            context.News.Add(news1);
            context.News.Add(news2);
            context.SaveChanges();

            var httpResponse = this.httpClient.GetAsync(NewsRequestUri).Result;
            var news = httpResponse.Content.ReadAsAsync<List<News>>().Result;
            
            Assert.AreEqual(httpResponse.StatusCode, HttpStatusCode.OK);
            Assert.AreEqual(news.Count, 2);
            Assert.AreEqual(news[0].Title, news1.Title);
            Assert.AreEqual(news[1].PublishedDate.ToString(), news2.PublishedDate.ToString());
        }
        public static void Update()
        {
            var context = new NewsContext();
            var news = context.News.FirstOrDefault();

            Console.WriteLine("System news content: {0}", news.Content);

            using(var dbContextTransaction = context.Database.BeginTransaction())
            {
                try
                {
                    Console.WriteLine("Update news with new content");
                    string updatedText = Console.ReadLine();

                    news.Content = updatedText;
                    context.SaveChanges();
                    dbContextTransaction.Commit();

                    Console.WriteLine("This news content will succesfully updated!");
                }
                catch(Exception ex)
                {
                    dbContextTransaction.Rollback();
                    Console.WriteLine("This news content not updated.");

                    Update();
                }
            }
        }
        private void CleanDb()
        {
            var newsDb = new NewsContext();
            newsDb.News.Delete();

            newsDb.SaveChanges();
        }
 private static void Seed()
 {
     var context = new NewsContext();
     var userStore = new UserStore<ApplicationUser>(context);
     var userManager = new ApplicationUserManager(userStore);
     var user = new ApplicationUser()
     {
         UserName = "******",
         Email = "*****@*****.**"
     };
     var result = userManager.CreateAsync(user, "Pass123$").Result;
     if (!result.Succeeded)
     {
         Assert.Fail(string.Join(Environment.NewLine, result.Errors));
     }
     var news = new News.Models.News()
     {
         Title = "First news",
         Content = "first news content",
         PublishDate = DateTime.Now,
         Author = user
     };
     var news2 = new News.Models.News()
     {
         Title = "Second news",
         Content = "second news content",
         PublishDate = DateTime.Now,
         Author = user
     };
     context.News.Add(news2);
     context.SaveChanges();
 }
 private static void ClearDatabase()
 {
     var context = new NewsContext();
     context.News.Delete();
     context.Users.Delete();
     context.SaveChanges();
 }
        private static void SeedDatabase()
        {
            var context = new NewsContext();
            context.News.Add(
                new News { Title = "Title 1", Content = "Content 11111", PublishDate = new DateTime(2015, 1, 1) });
            context.News.Add(
                new News { Title = "Title 2", Content = "Content 22222", PublishDate = new DateTime(2015, 2, 2) });
            context.News.Add(
                new News { Title = "Title 3", Content = "Content 33333", PublishDate = new DateTime(2015, 3, 3) });
            context.News.Add(
                new News { Title = "Title 4", Content = "Content 44444", PublishDate = new DateTime(2015, 4, 4) });
            context.News.Add(
                new News { Title = "Title 5", Content = "Content 55555", PublishDate = new DateTime(2015, 5, 5) });

            context.SaveChanges();
        }
 public void DeleteNews_WhenDeletingNonExistingNews_ShouldThrowException()
 {
     var context = new NewsContext();
     context.News.Delete();
     context.SaveChanges();
     var repo = new NewsRepository(context);
     var newsToDelete = repo.Find(1);
     repo.Delete(newsToDelete);
     repo.SaveChanges();
 }
        public void Modify_Existing_With_Incorrect_Data_Return_400_BadRequest()
        {
            // Arrange
            CleanDatabase();
            var database = new NewsContext();
            database.News.Add(new NewsRecord()
            {
                Title = "HopHop1",
                Content = "The hoppy hop",
                PublishDate = DateTime.Now
            });

            database.News.Add(new NewsRecord
            {
                Title = "HopHop2",
                Content = "The hoppy hop2",
                PublishDate = DateTime.Now.AddDays(+5)
            });

            database.SaveChanges();

            var id = database.News.First().Id;
            var postContent = new FormUrlEncodedContent(new[] 
            {
                new KeyValuePair<string, string>("Title", "ssssaaaa"),
                new KeyValuePair<string, string>("Content", "ssasasas"),

            //    new KeyValuePair<string, string>("Content", "Content"), 
            });


            var endpointUrl = string.Format("/api/news?id={0}", id);
            // Act

            var httpResponse = httpClient.PutAsync(endpointUrl, postContent).Result;
            var newsFromService = httpResponse.Content.ReadAsAsync<NewsRecord>().Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
        }
 public void ModifyNews_WhenModifingNonExistingNews_ShouldThrowException()
 {
     var context = new NewsContext();
     context.News.Delete();
     context.SaveChanges();
     var repo = new NewsRepository(context);
     var newsToChange = repo.Find(1);
     newsToChange.Title = "New title";
     repo.SaveChanges();
 }
 public void ModifyNews_WhenExistingNewsIsModifiedWithIncorrectData_ShouldThrowException()
 {
     var context = new NewsContext();
     context.News.Delete();
     var news = new Models.News()
     {
         Title = "Murder",
         Content = "A man was slaughterd in Sofia last night.",
         PublishDate = DateTime.Now
     };
     context.News.Add(news);
     context.SaveChanges();
     var repo = new NewsRepository(context);
     var newsToChange = repo.Find(news.Id);
     newsToChange.Title = null;
     repo.SaveChanges();
 }
 public void ModifyNews_WhenExistingNewsIsModifiedWithCorrectData_ShouldReturnNews()
 {
     var context = new NewsContext();
     context.News.Delete();
     var news = new Models.News()
     {
         Title = "Murder",
         Content = "A man was slaughterd in Sofia last night.",
         PublishDate = DateTime.Now
     };
     context.News.Add(news);
     context.SaveChanges();
     var repo = new NewsRepository(context);
     var newsToChange = repo.Find(news.Id);
     newsToChange.Title = "Murder committed last night";
     repo.SaveChanges();
     var newsFromDb = repo.Find(news.Id);
     Assert.IsNotNull(newsFromDb);
     Assert.AreEqual(newsToChange.Title, newsFromDb.Title);
     Assert.AreEqual(news.Content, newsFromDb.Content);
     Assert.AreEqual(news.PublishDate, newsFromDb.PublishDate);
     Assert.IsTrue(newsFromDb.Id != 0);
 }
        private void CleanDatabase()
        {
            // Clean all data in all database tables
            var dbContext = new NewsContext();

          

            foreach (var newsR in dbContext.News)
            {
                dbContext.News.Remove(newsR);
            }

            dbContext.SaveChanges();
        }
        public void Delete_Existing_With_Incorrect_Data_Return_400_BadRequest()
        {
            // Arrange
            CleanDatabase();
            var database = new NewsContext();
            database.News.Add(new NewsRecord()
            {
                Title = "HopHop1",
                Content = "The hoppy hop",
                PublishDate = DateTime.Now
            });

            database.News.Add(new NewsRecord
            {
                Title = "HopHop2",
                Content = "The hoppy hop2",
                PublishDate = DateTime.Now.AddDays(+5)
            });

            database.SaveChanges();

            var id = 0;

            var endpointUrl = string.Format("/api/news/{0}", id);
            // Act
            var httpResponse = httpClient.DeleteAsync(endpointUrl).Result;
            var newsFromService = httpResponse.Content.ReadAsAsync<NewsRecord>().Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.NotFound, httpResponse.StatusCode);
        }
        private void CleanDatabase()
        {
            // Clean all data in all database tables
            var dbContext = new NewsContext();

            dbContext.Newses.Count();

            TestingUser = dbContext.Users.First();

            foreach (var news in dbContext.Newses)
            {
                dbContext.Newses.Remove(news);
            }

            dbContext.SaveChanges();

            dbContext.Newses.Add(new News()
            {
                AuthorId = this.TestingUser.Id,
                Title = "Test Title",
                Content = "Test Content",
                PublishDate = DateTime.Now
            });

            dbContext.SaveChanges();
        }
 public void DeleteNews_WhenDeletingExistingNews_ShouldDeleteNews()
 {
     var context = new NewsContext();
     context.News.Delete();
     var news = new Models.News()
     {
         Title = "Murder",
         Content = "A man was slaughterd in Sofia last night.",
         PublishDate = DateTime.Now
     };
     context.News.Add(news);
     context.SaveChanges();
     var repo = new NewsRepository(context);
     repo.Delete(news);
     repo.SaveChanges();
     var newsFromDb = repo.All().ToArray();
     Assert.AreEqual(0, newsFromDb.Count());
 }