Пример #1
0
        public EditNews()
        {
            InitializeComponent();

            this.newsContext = new NewsContext();
            this.firstNews   = this.newsContext.News.OrderBy(n => n.Id).First();

            richTextBox1.Text = this.firstNews.Content;
        }
 public static NewsViewModel CreateMethod(News news)
 {
     return new NewsViewModel
     {
         Id = news.Id,
         Title = news.Title,
         Content = news.Content,
         PublishDate = news.PublishDate
     };
 }
        public void AddNews_WhenNewsIsInvalid_ShouldThrowException()
        {
            var repo = new NewsRepository(new NewsEntities());
            var news = new News.Models.News()
            {
                Content     = "TestNewsContent",
                PublishDate = new DateTime(2015, 05, 10)
            };

            repo.Add(news);
            repo.SaveChanges();
        }
        public void DeleteNews_WhenNewsIsNonExisting_ShouldThrowException()
        {
            var repo = new NewsRepository(new NewsEntities());
            var news = new News.Models.News
            {
                Title       = "title",
                Content     = "content",
                PublishDate = DateTime.Now
            };

            repo.Delete(news);
            repo.SaveChanges();
        }
Пример #5
0
 public static NewsViewModel CreateView(News n)
 {
     return  new NewsViewModel
     {
         Id = n.Id,
         Title = n.Title,
         Content = n.Content,
         PublishDate = n.PublishDate,
         Author = new AuthorViewModel
         {
             Id = n.Author.Id,
             UserName = n.Author.UserName,
             Email = n.Author.Email
         }
     };
 }
        public void CreateNewsItemWithCorrectData()
        {
            var repo = new RepositoryMock<News>();
            repo.IsSaveCalled = false;
            repo.Entities = new Dictionary<object, News>();
            var news = new News()
            {
                Id = 1,
                AuthorId = "valid",
                Title = "valid",
                Content = "valid",
                PublishDate = DateTime.Now
            };

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

            // Act
            var httpResponse = controller.CreateNews(new NewsBindingModel()
            {
                Id = news.Id,
                AuthorId = news.AuthorId,
                Title = news.Title,
                Content = news.Content,
                PublishDate = news.PublishDate
            }).ExecuteAsync(new CancellationToken()).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.Created, httpResponse.StatusCode);
            Assert.IsNotNull(httpResponse.Headers.Location);

            var newsFromService = httpResponse.Content.ReadAsAsync<News>().Result;
            Assert.AreEqual(newsFromService.Title, news.Title);
            Assert.AreEqual(newsFromService.AuthorId, news.AuthorId);
            Assert.AreEqual(newsFromService.Content, news.Content);
            Assert.AreEqual(newsFromService.Id, news.Id);
            Assert.IsNotNull(newsFromService.PublishDate);

            Assert.AreEqual(repo.Entities.Count, 1);
            var newsInRepo = repo.Entities.First().Value;
            Assert.AreEqual(news.Title, newsInRepo.Title);
            Assert.AreEqual(news.Id, newsInRepo.Id);
            Assert.AreEqual(news.Content, newsInRepo.Content);
            Assert.AreEqual(news.AuthorId, newsInRepo.AuthorId);
            Assert.IsNotNull(newsInRepo.PublishDate);
            Assert.IsTrue(repo.IsSaveCalled);
        }
Пример #7
0
        public void AddNews_WhenNewsIsInvalid_ShouldThrowException()
        {
            // Arrange -> prapare the objects
            var invalidNews = new News
            {
                Title = "T",
                Content = "Hope the test will succeed.",
                PublishDate = DateTime.Now,
                AuthorId = this.repo.Users.All().First().Id
            };

            // Act -> perform some logic
            this.repo.News.Add(invalidNews);
            this.repo.SaveChanges();

            // Assert -> expect an exception
        }
Пример #8
0
        public IHttpActionResult CreateNew(CreateNewInputModel inputModel)
        {
            if (inputModel == null)
            {
                return this.BadRequest();
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var news = new News { Content = inputModel.Content, Title = inputModel.Title, PublishedAt = DateTime.Now };

            this.Data.News.Add(news);
            this.Data.SaveChanges();
            var newOutputModel = new GetAllNewsViewModel() { Id = news.Id, Content = news.Content, Title = news.Title, PublishedDate = news.PublishedAt};
            return this.CreatedAtRoute("DefaultApi", new { controller = "news", id = newOutputModel.Id }, newOutputModel);
        }
        public void InsertNews_WhenInsertedCorrectly_ShouldReturnNews()
        {
            var repo = new NewsRepository(new NewsEntities());
            var news = new News.Models.News()
            {
                Title       = "TestNews",
                Content     = "TestNewsContent",
                PublishDate = new DateTime(2015, 05, 10)
            };

            repo.Add(news);
            repo.SaveChanges();

            var newsFromDb = repo.Find(news.Id);

            Assert.AreEqual(4, repo.All().Count());
            Assert.AreEqual(news.Title, newsFromDb.Title);
            Assert.AreEqual(news.Content, newsFromDb.Content);
            Assert.AreEqual(news.PublishDate.ToShortDateString(),
                            newsFromDb.PublishDate.ToShortDateString());
            Assert.AreNotEqual(0, newsFromDb.Id);
        }
        public IHttpActionResult CreateNews(NewsBindingModel newsDto)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (newsDto == null)
            {
                return this.BadRequest(this.ModelState);
            }

            var news = new News
                           {
                               Title = newsDto.Title, 
                               Content = newsDto.Content, 
                               PublishedDate = newsDto.PublishedDate ?? DateTime.Now
                           };
            this.Data.Add(news);
            this.Data.SaveChanges();

            return this.CreatedAtRoute("DefaultApi", new { id = news.Id }, news);
        }
Пример #11
0
        public void AddNews_WhenNewsIsValid_ShouldBeAdded()
        {
            // Arrange -> prapare the objects
            var news = new News
            {
                Title = "Testing repositories",
                Content = "Hope the test will succeed.",
                PublishDate = DateTime.Now,
                AuthorId = this.repo.Users.All().First().Id
            };

            // Act -> perform some logic
            this.repo.News.Add(news);
            this.repo.SaveChanges();

            // Assert -> validate the results
            var newsFromDb = this.repo.News.Find(news.Id);

            Assert.IsNotNull(newsFromDb);
            Assert.AreEqual(news.Title, newsFromDb.Title);
            Assert.AreEqual(news.Content, newsFromDb.Content);
            Assert.AreEqual(news.PublishDate, newsFromDb.PublishDate);
            Assert.IsTrue(newsFromDb.Id != 0);
        }
        public void ModifyExistingNewsItemWithIncorrectData()
        {
            var repo = new RepositoryMock<News>();
            var controller = new NewsController(repo);
            this.SetupController(controller, "news");

            // Act
            var news = new News()
            {
                Id = 1,
                Title = "valid",
                AuthorId = "valid",
                PublishDate = DateTime.Now,
                Content = "valid"
            };

            var result = controller.CreateNews(new NewsBindingModel()
            {
                Id = news.Id,
                AuthorId = news.AuthorId,
                Title = news.Title,
                Content = news.Content,
                PublishDate = news.PublishDate
            }).ExecuteAsync(new CancellationToken()).Result;

            var theCreatedNews = repo.Entities.First().Value;

            theCreatedNews.Content = null;

            var editResult = controller.EditNews(theCreatedNews.Id, new NewsBindingModel()
            {
                Id = theCreatedNews.Id,
                AuthorId = theCreatedNews.AuthorId,
                Title = theCreatedNews.Title,
                Content = theCreatedNews.Content,
                PublishDate = theCreatedNews.PublishDate
            }).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
Пример #13
0
        public void DeleteNonExistingNews_ShouldThrowException()
        {
            // Arrange -> prapare the objects
            var user = new ApplicationUser
            {
                UserName = "******",
                Email = "*****@*****.**"
            };
            this.repo.Users.Add(user);
            this.repo.SaveChanges();

            var news = new News
            {
                Title = "Seeding repositoriesasd",
                Content = "Some content asdasd.",
                PublishDate = DateTime.Parse("2000-01-19"),
                AuthorId = user.Id,
                Author = user
            };

            // Act -> perform some logic
            this.repo.News.Delete(news);
            this.repo.SaveChanges();

            // Assert -> validate the results
        }
 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 DeleteExistingNewsItem()
        {
            var repo = new RepositoryMock<News>();
            var controller = new NewsController(repo);
            this.SetupController(controller, "news");

            // Act
            var news = new News()
            {
                Id = 1,
                Title = "valid",
                AuthorId = "valid",
                PublishDate = DateTime.Now,
                Content = "valid"
            };

            var result = controller.CreateNews(new NewsBindingModel()
            {
                Id = news.Id,
                AuthorId = news.AuthorId,
                Title = news.Title,
                Content = news.Content,
                PublishDate = news.PublishDate
            }).ExecuteAsync(new CancellationToken()).Result;

            var theCreatedNews = repo.Entities.First().Value;

            var deleteResult = controller.DeletelNews(theCreatedNews.Id).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.AreEqual(repo.Entities.Count, 0);
        }
        public void ModifyNonExistingNewsItem()
        {
            var repo = new RepositoryMock<News>();
            var controller = new NewsController(repo);
            this.SetupController(controller, "news");

            // Act
            // Not added
            var news = new News()
            {
                Id = 1,
                Title = "valid",
                AuthorId = "valid",
                PublishDate = DateTime.Now,
                Content = "valid"
            };

            var result = controller.EditNews(news.Id, new NewsBindingModel()
            {
                Id = news.Id,
                AuthorId = news.AuthorId,
                Title = news.Title,
                Content = news.Content,
                PublishDate = news.PublishDate
            }).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public void CreateNewsItemWithIncorrectData()
        {
            var repo = new RepositoryMock<News>();
            var controller = new NewsController(repo);
            this.SetupController(controller, "news");

            // Act
            var news = new News()
            {
                Id = 1,
                Title = "valid",
                AuthorId = "valid",
                PublishDate = DateTime.Now,
                Content = null
            };

            var result = controller.CreateNews(new NewsBindingModel()
            {
                Id = news.Id,
                AuthorId = news.AuthorId,
                Title = news.Title,
                Content = news.Content,
                PublishDate = news.PublishDate
            }).ExecuteAsync(new CancellationToken()).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public IHttpActionResult PostNewNews([FromBody] NewsBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var @new = new News.Models.News()
            {
                Title = model.Title,
                Content = model.Content,
                PublishedData = model.PublishDate
            };


            this.Data.News.Add(@new);
            this.Data.SaveChanges();

            var newView = new NewsViewModel()
            {
                Title = @new.Title,
                Content = @new.Content,
                PublishData = @new.PublishedData
            };

            return this.Created(Uri.UriSchemeHttp, newView);
        }
 public NewsViewModel(News news)
 {
     this.Title = news.Title;
     this.Content = news.Content;
     this.PublishedDate = news.PublishedDate;
 }