public void GetByIdTest_ValidId_ShouldReturnValidEntity() { Comment entity = new Comment() { Content = "Test content", DateCreated = DateTime.Now }; this.commentRepository.Add(entity); var foundEntity = this.commentEntities.Find(entity.Id); Assert.AreSame(entity, foundEntity); }
public void AddTest_EntityIsValid_ShouldBeAddedSuccessfuly() { Comment entity = new Comment() { Content = "Test content", DateCreated = DateTime.Now }; this.commentRepository.Add(entity); var foundEntity = this.commentEntities.Find(entity.Id); Assert.IsTrue(entity.Id != 0); Assert.IsNotNull(foundEntity); Assert.AreSame(entity, foundEntity); }
public HttpResponseMessage CommentForPost([FromBody]CommentModel commentModel, [FromUri]int postId, string sessionKey) { var response = this.PerformOperationAndHandleExceptions(() => { var user = this.userRepository.GetAll() .Where(usr => usr.SessionKey == sessionKey).FirstOrDefault(); if (user == null) { throw new InvalidOperationException("No logged user or invalid sessionKey"); } var post = this.postRepository.GetById(postId); if (post == null) { throw new ArgumentException(string.Format("No post with id = {0}", postId)); } if (commentModel.Content == null) { throw new ArgumentException("The comment content cannot be null."); } Comment comment = new Comment() { Content = commentModel.Content, DateCreated = commentModel.CommentDate, Post = post, User = user }; user.Comments.Add(comment); this.commentRepository.Add(comment); var responseMessage = this.Request.CreateResponse( HttpStatusCode.Created, new { id = comment.Id, commentedBy = user.Nickname, content = comment.Content }); return responseMessage; }); return response; }
public void UpdateTest_InvalidEntityId() { int invalidId = -1; Comment entity = new Comment() { Content = "Test content", DateCreated = DateTime.Now }; this.commentRepository.Add(entity); Comment commentToUpgradeWith = new Comment() { Content = "Updated test content", DateCreated = DateTime.Now.AddDays(1) }; Comment upgradedComment = this.commentRepository.Update(invalidId, commentToUpgradeWith); Assert.IsNull(upgradedComment); }
public void UpdateTest_ValidUpdate() { Comment entity = new Comment() { Content = "Test content", DateCreated = DateTime.Now }; this.commentRepository.Add(entity); Comment commentToUpgradeWith = new Comment() { Content = "Updated test content", DateCreated = DateTime.Now.AddDays(1) }; Comment upgradedComment = this.commentRepository.Update(entity.Id, commentToUpgradeWith); Assert.AreSame(entity, upgradedComment); Assert.AreEqual(upgradedComment.Content, commentToUpgradeWith.Content); }
public void UpdateTest_InvalidUpdateEntity_ShouldThrowArgumentNullException() { Comment entity = new Comment() { Content = "Test content", DateCreated = DateTime.Now }; this.commentRepository.Add(entity); Comment commentToUpgradeWith = null; Comment upgradedComment = this.commentRepository.Update(entity.Id, commentToUpgradeWith); }