public void Create_AllCredentialsAreValid_CreatesAndReturnsComment() { // Arrange User user = new User { UserId = 1 }; Article article = new Article { ArticleId = 2 }; string commentText = "comment_text"; string validatedCommentText = "validated_comment_text"; // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock articleRepository Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>(); articleRepositoryMock.Setup(r => r.GetById(article.ArticleId)) .Returns(article); // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.ArticleRepository) .Returns(articleRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); // Arrange - mock commentValidation this._commentValidationMock.Setup(v => v.ValidateCommentText(commentText)) .Returns(validatedCommentText); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act Comment comment = target.Create(user.UserId, article.ArticleId, commentText); // Assert Assert.IsNotNull(comment); Assert.AreEqual(user.UserId, comment.UserId); Assert.AreEqual(article.ArticleId, comment.ArticleId); Assert.AreEqual(validatedCommentText, comment.Text); Assert.IsTrue(new DateTime() != comment.Date); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once); commentRepositoryMock.Verify( r => r.Insert( It.Is<Comment>(c => c.UserId == user.UserId && c.ArticleId == article.ArticleId && c.Text == validatedCommentText)), Times.Once); unitOfWorkMock.Verify(u => u.Save(), Times.Once); this._commentValidationMock.Verify(v => v.ValidateCommentText(commentText), Times.Once); }
public void Create_NonexistentArticleId_ThrowsArticleNotFoundException() { // Arrange User user = new User { UserId = 1 }; int articleId = 2; string commentText = "comment_text"; // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock articleRepository Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>(); articleRepositoryMock.Setup(r => r.GetById(articleId)) .Returns((Article)null); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.ArticleRepository) .Returns(articleRepositoryMock.Object); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArticleNotFoundException>(() => target.Create(user.UserId, articleId, commentText)); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); articleRepositoryMock.Verify(r => r.GetById(articleId), Times.Once); }
public void AddVote_UserHasNotVotedYet_AddsVote() { // Arrange int commentId = 1; int userId = 1; bool voteIsPositive = true; CommentVote[] commentVotes = { new CommentVote { CommentId = commentId, UserId = userId + 1 }, new CommentVote { CommentId = commentId, UserId = userId + 2 } }; User user = new User { UserId = userId }; Comment comment = new Comment { CommentId = commentId, CommentVotes = commentVotes.ToList() }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(commentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); IEnumerable<CommentVote> newCommentVotes = null; commentRepositoryMock.Setup(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId))) .Callback((Comment c) => newCommentVotes = c.CommentVotes); // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(userId)) .Returns(user); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act target.AddVote(commentId, userId, voteIsPositive); // Assert Assert.IsNotNull(newCommentVotes); Assert.AreEqual(commentVotes.Count() + 1, newCommentVotes.Count()); CommentVote commentVote = newCommentVotes.FirstOrDefault(v => v.UserId == userId && v.IsPositive == voteIsPositive); Assert.IsNotNull(commentVote); commentRepositoryMock.Verify( r => r.GetById(commentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Once); userRepositoryMock.Verify(r => r.GetById(userId), Times.Once()); unitOfWorkMock.Verify(r => r.Save(), Times.Once); }
public void Reply_AllCredentialsAreValid_CreatesAndReturnsReply() { // Arrange User user = new User { UserId = 1 }; string commentText = "comment_text"; string validatedCommentText = "validated_comment_text"; Comment[] commentReplies = { new Comment { CommentId = 2 }, new Comment { CommentId = 3 } }; Comment comment = new Comment { CommentId = 4, ArticleId = 5, Comment1 = commentReplies.ToList() }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.CommentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); IEnumerable<Comment> newCommentReplies = null; commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>())) .Callback((Comment c) => newCommentReplies = c.Comment1); // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); // Arrange - mock commentValidation this._commentValidationMock.Setup(v => v.ValidateCommentText(commentText)) .Returns(validatedCommentText); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act Comment reply = target.Reply(comment.CommentId, user.UserId, commentText); // Assert Assert.IsNotNull(reply); Assert.AreEqual(user.UserId, reply.UserId); Assert.AreEqual(comment.ArticleId, reply.ArticleId); Assert.AreEqual(validatedCommentText, reply.Text); Assert.IsTrue(new DateTime() != reply.Date); Assert.IsNotNull(newCommentReplies); Assert.AreEqual(commentReplies.Count() + 1, newCommentReplies.Count()); Assert.IsTrue(newCommentReplies.Contains(reply)); commentRepositoryMock.Verify( r => r.GetById(comment.CommentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Once); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Once); this._commentValidationMock.Verify(v => v.ValidateCommentText(commentText), Times.Once); }
public User Create(string email, string firstName, string lastName, string password) { Guard.NotNullOrEmpty(email, "email"); Guard.NotNullOrEmpty(firstName, "firstName"); Guard.NotNullOrEmpty(lastName, "lastName"); Guard.NotNullOrEmpty(password, "password"); if (!this._userValidation.IsValidEmail(email)) { throw new InvalidEmailFormatException(String.Format("Email '{0}' has invalid format.", email)); } if (!this._userValidation.IsValidName(firstName)) { throw new InvalidFirstNameFormatException(String.Format("First name '{0}' has invalid format.", firstName)); } if (!this._userValidation.IsValidName(lastName)) { throw new InvalidLastNameFormatException(String.Format("Last name '{0}' has invalid format.", lastName)); } PasswordStrength passwordStrength = this._userValidation.GetPasswordStrength(password); if (passwordStrength < PasswordStrength.Strong) { throw new PasswordIsNotStrongEnoughException(String.Format("Password '{0}' is not strong enough.", password)); } User user = this._unitOfWork.UserRepository.GetByEmail(email); if (user != null) { throw new EmailAlreadyExistsException(String.Format("User with email '{0}' already exists.", email)); } SecuredPassword securedPassword = this._securedPasswordHelper.GetSecuredPassword(password); user = new User { Email = email, FirstName = firstName, LastName = lastName, Password = securedPassword.Hash, Salt = securedPassword.Salt, RegistrationDate = DateTime.Now }; this._unitOfWork.UserRepository.Insert(user); this._unitOfWork.Save(); return user; }
private bool IsPasswordCorrect(string password, User user) { SecuredPassword securedPassword = new SecuredPassword(user.Password, user.Salt); return this._securedPasswordHelper.ArePasswordsEqual(password, securedPassword); }
public void Create_TagIdsArrayIsEmpty_DoNotGetTagsFromRepository() { // Arrange User user = new User { UserId = 1 }; string articleTitle = "article_title"; string articleText = "article_text"; string validatedArticleTitle = "validated_article_title"; string validatedArticleText = "validated_article_text"; Topic topic = new Topic { TopicId = 2 }; int[] tagIds = { }; // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock topicRepository Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>(); topicRepositoryMock.Setup(r => r.GetById(topic.TopicId)) .Returns(topic); // Arrange - mock tagRepository Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>(); // Arrange - mock articleRepository Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>(); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.TopicRepository) .Returns(topicRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.TagRepository) .Returns(tagRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.ArticleRepository) .Returns(articleRepositoryMock.Object); // Arrange - mock articleValidation this._articleValidationMock.Setup(v => v.ValidateTitle(articleTitle)) .Returns(validatedArticleTitle); this._articleValidationMock.Setup(v => v.ValidateArticleText(articleText)) .Returns(validatedArticleText); // Arrange - create target IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object); // Act Article article = target.Create(user.UserId, articleTitle, articleText, topic.TopicId, tagIds); // Assert Assert.IsNotNull(article); Assert.AreEqual(user.UserId, article.UserId); Assert.AreEqual(topic.TopicId, article.TopicId); Assert.AreEqual(validatedArticleTitle, article.Title); Assert.AreEqual(validatedArticleText, article.Text); Assert.IsFalse(article.Tags.Any()); Assert.IsTrue(new DateTime() != article.PublicationDate); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); topicRepositoryMock.Verify(r => r.GetById(topic.TopicId), Times.Once); tagRepositoryMock.Verify( r => r.Get(It.IsAny<Expression<Func<Tag, bool>>>(), It.IsAny<Func<IQueryable<Tag>, IOrderedQueryable<Tag>>>(), It.IsAny<Expression<Func<Tag, object>>[]>()), Times.Never); articleRepositoryMock.Verify( r => r.Insert( It.Is<Article>(a => a.UserId == user.UserId && a.Title == validatedArticleTitle && a.Text == validatedArticleText)), Times.Once); unitOfWorkMock.Verify(u => u.Save(), Times.Once); this._articleValidationMock.Verify(v => v.ValidateTitle(articleTitle), Times.Once); this._articleValidationMock.Verify(v => v.ValidateArticleText(articleText), Times.Once); }
public void Create_AllCredentialsAreValid_CreatesArticle() { // Arrange User user = new User { UserId = 1 }; string articleTitle = "article_title"; string articleText = "article_text"; string validatedArticleTitle = "validated_article_title"; string validatedArticleText = "validated_article_text"; Topic topic = new Topic { TopicId = 2 }; int[] tagIds = { 1, 2, 3, 4 }; IList<Tag> tags = new List<Tag>(); foreach (int tagId in tagIds) { tags.Add(new Tag { TagId = tagId }); } // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock topicRepository Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>(); topicRepositoryMock.Setup(r => r.GetById(topic.TopicId)) .Returns(topic); // Arrange - mock tagRepository Mock<ITagRepository> tagRepositoryMock = new Mock<ITagRepository>(); tagRepositoryMock.Setup( r => r.Get(It.IsAny<Expression<Func<Tag, bool>>>(), null, It.Is<Expression<Func<Tag, object>>[]>(e => !e.Any()))) .Returns( (Expression<Func<Tag, bool>> filter, Func<IQueryable<Tag>, IOrderedQueryable<Tag>> orderBy, Expression<Func<Tag, object>>[] selector) => CollectionHelper.FilterCollection(tags, filter, orderBy)); // Arrange - mock articleRepository Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>(); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.TopicRepository) .Returns(topicRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.TagRepository) .Returns(tagRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.ArticleRepository) .Returns(articleRepositoryMock.Object); // Arrange - mock articleValidation this._articleValidationMock.Setup(v => v.ValidateTitle(articleTitle)) .Returns(validatedArticleTitle); this._articleValidationMock.Setup(v => v.ValidateArticleText(articleText)) .Returns(validatedArticleText); // Arrange - create target IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object); // Act Article article = target.Create(user.UserId, articleTitle, articleText, topic.TopicId, tagIds); // Assert Assert.IsNotNull(article); Assert.AreEqual(user.UserId, article.UserId); Assert.AreEqual(topic.TopicId, article.TopicId); Assert.AreEqual(validatedArticleTitle, article.Title); Assert.AreEqual(validatedArticleText, article.Text); Assert.AreEqual(tags, article.Tags); Assert.IsTrue(new DateTime() != article.PublicationDate); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); topicRepositoryMock.Verify(r => r.GetById(topic.TopicId), Times.Once); tagRepositoryMock.Verify( r => r.Get(It.IsAny<Expression<Func<Tag, bool>>>(), null, It.Is<Expression<Func<Tag, object>>[]>(e => !e.Any())), Times.Once); articleRepositoryMock.Verify( r => r.Insert( It.Is<Article>(a => a.UserId == user.UserId && a.Title == validatedArticleTitle && a.Text == validatedArticleText)), Times.Once); unitOfWorkMock.Verify(u => u.Save(), Times.Once); this._articleValidationMock.Verify(v => v.ValidateTitle(articleTitle), Times.Once); this._articleValidationMock.Verify(v => v.ValidateArticleText(articleText), Times.Once); }
public void Create_NonexistentTopicId_ThrowsTopicNotFoundException() { // Arrange User user = new User { UserId = 1 }; string articleTitle = "article_title"; string articleText = "article_text"; int topicId = 2; int[] tagIds = { 1, 2, 3, 4 }; // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock topicRepository Mock<ITopicRepository> topicRepositoryMock = new Mock<ITopicRepository>(); topicRepositoryMock.Setup(r => r.GetById(topicId)) .Returns((Topic)null); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.TopicRepository) .Returns(topicRepositoryMock.Object); // Arrange - create target IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object); // Act and Assert Assert.Throws<TopicNotFoundException>( () => target.Create(user.UserId, articleTitle, articleText, topicId, tagIds)); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); topicRepositoryMock.Verify(r => r.GetById(topicId), Times.Once); }