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(); } } }
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); }
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(); }
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 static void Main(string[] args) { var context = new NewsContext(); Console.WriteLine("Connecting to database ...\n"); context.News.Count(); Console.WriteLine("Application started.\n"); ConcurencyUpdates.Update(); }
public void List_All_News_Should_Return_200OK_And_Return_All_News_As_Json() { var context = new NewsContext(); var newsInDb = context.News.Select(n => n.Id).ToList(); var response = httpClient.GetAsync(Endpoint).Result; var newsJson = response.Content.ReadAsAsync<List<News>>().Result .Select(n => n.Id).ToList(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); CollectionAssert.AreEqual(newsInDb, newsJson); }
public void Create_News_With_Correct_Data_Should_Return_201Created_And_Created_Item() { var context = new NewsContext(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", "Title 10"), new KeyValuePair<string, string>("content", "Content 1010101010"), new KeyValuePair<string, string>("publishDate", "2015-9-1"), }); var response = httpClient.PostAsync(Endpoint, data).Result; var returnedNews = response.Content.ReadAsAsync<News>().Result; var lastNewsInDbId = context.News.Select(n => n.Id).ToList().Last(); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.AreEqual(lastNewsInDbId, returnedNews.Id); }
private static void ClearDatabase() { var context = new NewsContext(); context.News.Delete(); context.Users.Delete(); context.SaveChanges(); }
public void DeleteNewsWhenIdIsInvalid_ShouldReturnBadRequest() { var context = new NewsContext(); int id = 100; var dbNews = context.News.FirstOrDefault(n => n.Id == id); var endpoint = string.Format("api/news/{0}", id); var response = client.DeleteAsync(endpoint).Result; Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void DeleteNewsWhenIdIsCorrect_ShouldReturnOK_DeleteNews() { var context = new NewsContext(); var news = context.News.First(n => n.Title.Contains("Second")); var newsId = news.Id; var endpoint = string.Format("api/news/{0}", newsId); var response = client.DeleteAsync(endpoint).Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var dbNews = context.News.FirstOrDefault(n => n.Title.Contains("Second")); Assert.IsNull(dbNews); }
public void ModifyNewsWithIncorrectData_ShouldReturnBadRequest() { var context = new NewsContext(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", "New news") }); var newsId = context.News.First(n => n.Title.Contains("Second")); var endpoint = string.Format("api/news/{0}", newsId); var response = client.PutAsync(endpoint, data).Result; Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); var dbNews = context.News.FirstOrDefault(n => n.Title == "New news"); Assert.IsNull(dbNews); }
public void Modify_Existing_News_With_Correct_Data_Should_Return_200OK_And_Modify_News_In_Db() { var context = new NewsContext(); var newsToModify = context.News.First(); var endpointWithId = Endpoint + "/" + newsToModify.Id; var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", "Modified Title"), new KeyValuePair<string, string>("content", "Modified Content"), new KeyValuePair<string, string>("publishDate", "2015-9-3"), }); var response = httpClient.PutAsync(endpointWithId, data).Result; var returnedNews = response.Content.ReadAsAsync<News>().Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual("Modified Title", returnedNews.Title); Assert.AreEqual("Modified Content", returnedNews.Content); Assert.AreEqual(new DateTime(2015, 9, 3).ToString(), returnedNews.PublishDate.ToString()); }
private void CleanDb() { var newsDb = new NewsContext(); newsDb.News.Delete(); newsDb.SaveChanges(); }
public void EditExistingNews_WithWrongModel_ShouldReturn_BadRequest() { var newsContent = string.Empty; this.CreateNews_ShouldReturn_Created(); var context = new NewsContext(); var news = context.News.First(); var postContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Content", newsContent) }); var httpResponse = this.httpClient.PutAsync(NewsRequestUri + "/" + news.Id, postContent).Result; var newsFromService = httpResponse.Content.ReadAsAsync<News>().Result; Assert.AreEqual(httpResponse.StatusCode, HttpStatusCode.BadRequest); Assert.AreNotEqual(newsFromService.Content, news.Content); }
public void CreateNews_ShouldReturn_Created() { var newsTitle = "New news title"; var newsContent = "News content"; var postContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", newsTitle), new KeyValuePair<string, string>("Content", newsContent) }); var httpResponse = this.httpClient.PostAsync(NewsRequestUri, postContent).Result; var newsFromService = httpResponse.Content.ReadAsAsync<News>().Result; Assert.AreEqual(httpResponse.StatusCode, HttpStatusCode.Created); Assert.IsNotNull(httpResponse.Headers.Location); Assert.AreEqual(newsFromService.Title, newsTitle); Assert.AreEqual(newsFromService.Content, newsContent); Assert.IsNotNull(newsFromService.PublishedDate); var context = new NewsContext(); var newsFromDb = context.News.FirstOrDefault(); Assert.IsNotNull(newsFromDb); Assert.AreEqual(newsFromDb.Id, newsFromService.Id); Assert.AreEqual(newsFromDb.Content, newsFromService.Content); Assert.AreEqual(newsFromDb.PublishedDate.ToString(), newsFromService.PublishedDate.ToString()); }
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 Delete_NonExisting_News_Should_Return_400BadRequest() { var context = new NewsContext(); var newsInDbBeforeDeleting = context.News.ToList(); var nonExistingNewsId = -1; Random rnd = new Random(); while (true) { int id = rnd.Next(1, int.MaxValue); var newsInDb = newsInDbBeforeDeleting .FirstOrDefault(n => n.Id == id); if (newsInDb == null) { nonExistingNewsId = id; break; } } var endpointWithId = Endpoint + "/" + nonExistingNewsId; var response = httpClient.DeleteAsync(endpointWithId).Result; var newsInDbAfterDeleting = context.News.ToList(); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); Assert.AreEqual(newsInDbBeforeDeleting.Count, newsInDbAfterDeleting.Count); }
public void Delete_Existing_News_Should_Return_200OK() { var context = new NewsContext(); var newsInDbBeforeDeleting = context.News.ToList(); var newsToDelete = context.News.First(); var endpointWithId = Endpoint + "/" + newsToDelete.Id; var response = httpClient.DeleteAsync(endpointWithId).Result; var newsInDbAfterDeleting = context.News.ToList(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual(newsInDbBeforeDeleting.Count - 1, newsInDbAfterDeleting.Count); }
public void Modify_Existing_News_With_InCorrect_Data_Should_Return_400BadRequest() { var context = new NewsContext(); var newsToModify = context.News.First(); var endpointWithId = Endpoint + "/" + newsToModify.Id; var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", "T"), // invalid title MinLength new KeyValuePair<string, string>("content", "Modified Content"), new KeyValuePair<string, string>("publishDate", "2015-9-3"), }); var response = httpClient.PutAsync(endpointWithId, data).Result; Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
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()); }
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 EditExistingNews_ShouldReturn_OkEdited() { var newsTitle = "edit news title"; var newsContent = "edit content"; this.CreateNews_ShouldReturn_Created(); var context = new NewsContext(); var news = context.News.First(); var postContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", newsTitle), new KeyValuePair<string, string>("Content", newsContent) }); var httpResponse = this.httpClient.PutAsync(NewsRequestUri + "/" + news.Id, postContent).Result; var newsFromService = httpResponse.Content.ReadAsAsync<News>().Result; Assert.AreEqual(httpResponse.StatusCode, HttpStatusCode.OK); Assert.AreEqual(newsFromService.Title, newsTitle); Assert.AreEqual(newsFromService.Content, newsContent); }
public void CreateNewsWithIncorrectData_ShouldReturnBadRequest() { var dbContext = new NewsContext(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", "new news") }); var response = this.PostNews(data); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void DeleteExistingNews_ShouldReturn_Ok() { this.CreateNews_ShouldReturn_Created(); var context = new NewsContext(); var newsFromDb = context.News.First(); var httpResponse = this.httpClient.DeleteAsync(NewsRequestUri + "/" + newsFromDb.Id).Result; Assert.AreEqual(httpResponse.StatusCode, HttpStatusCode.OK); }
public void ModifyNewsWithCorrectData_ShouldReturnOK_ModifyNews() { var context = new NewsContext(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Title", "New news"), new KeyValuePair<string, string>("Content", "New news content") }); var news = context.News.First(n => n.Title.Contains("Second")); var newsId = news.Id; var endpoint = string.Format("api/news/{0}", newsId); var response = client.PutAsync(endpoint, data).Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var modifiedNews = response.Content.ReadAsStringAsync().Result; var result = JsonConvert.DeserializeObject<News.Models.News>(modifiedNews); Assert.AreEqual("New news", result.Title); Assert.AreEqual("New news content", result.Content); }
public void Create_News_With_InCorrect_Data_Should_Return_400BadRequest() { var context = new NewsContext(); var newsInDbBeforeAdding = context.News.ToList(); var data = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("title", "T"), // invalid title MinLength new KeyValuePair<string, string>("content", "Content 1010101010"), new KeyValuePair<string, string>("publishDate", "2015-9-1"), }); var response = httpClient.PostAsync(Endpoint, data).Result; var newsInDbAfterAdding = context.News.ToList(); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); CollectionAssert.AreEqual(newsInDbBeforeAdding, newsInDbAfterAdding); }
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(); }
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 GetAllNews_ShouldReturn200OK() { var dbContext = new NewsContext(); var allNews = dbContext.News .OrderByDescending(n => n.PublishDate) .Select(NewsViewModel.Create) .Select(n => n.Title) .ToList(); var response = client.GetAsync("api/news").Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var news = response.Content.ReadAsAsync<IEnumerable<NewsViewModel>>() .Result .Select(n => n.Title) .ToList(); CollectionAssert.AreEqual(allNews, news); Assert.AreEqual(allNews.Count, news.Count()); }