Exemplo n.º 1
0
        public void RegisterIpAddress_IpAddressIsAlreadyRegistered_GetsVisitorByIpAddress()
        {
            // Arrange
            Visitor visitor = new Visitor { VisitorId = 1, IpAddress = "127.0.0.1" };

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(r => r.GetByIpAddress(visitor.IpAddress))
            .Returns(visitor);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(unitOfWork => unitOfWork.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act
            Visitor registeredVisitor = target.RegisterIpAddress(visitor.IpAddress);

            // Assert
            Assert.AreSame(visitor, registeredVisitor);

            visitorRepositoryMock.Verify(r => r.GetByIpAddress(visitor.IpAddress), Times.Once);
            visitorRepositoryMock.Verify(r => r.Insert(It.Is<Visitor>(v => v.VisitorId == visitor.VisitorId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Exemplo n.º 2
0
        public Visitor RegisterIpAddress(string ipAddress)
        {
            Guard.NotNull(ipAddress, "ipAddress");

            if (!this._visitorValidation.IsValidIpAddress(ipAddress))
            {
                throw new InvalidIpAddressFormatException(String.Format("IP address '{0}' has invalid format.", ipAddress));
            }

            Visitor visitor = this._unitOfWork.VisitorRepository.GetByIpAddress(ipAddress);

            if (visitor == null)
            {
                // Ip address is not registered yet
                visitor = new Visitor { IpAddress = ipAddress };

                this._unitOfWork.VisitorRepository.Insert(visitor);
                this._unitOfWork.Save();
            }

            return visitor;
        }
Exemplo n.º 3
0
        public void ViewArticle_ArticleAlreadyViewed_UpdatesLastViewDate()
        {
            // Arrange
            int visitorId = 1;
            int articleId = 2;
            DateTime lastViewDate = DateTime.Now;
            ArticleVisitor[] articleVisitors =
            {
                new ArticleVisitor { ArticleId = articleId, VisitorId = visitorId, LastViewDate = lastViewDate },
                new ArticleVisitor { VisitorId = visitorId, ArticleId = articleId + 1 },
                new ArticleVisitor { VisitorId = visitorId, ArticleId = articleId + 2 }
            };

            Visitor visitor = new Visitor { VisitorId = visitorId, ArticleVisitors = articleVisitors.ToList() };

            Expression<Func<Visitor, object>>[] propertiesToInclude = { v => v.ArticleVisitors };

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(
            r =>
            r.GetById(visitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(visitor);

            IEnumerable<ArticleVisitor> newArticleVisitors = null;

            visitorRepositoryMock.Setup(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitorId)))
            .Callback((Visitor v) => newArticleVisitors = v.ArticleVisitors);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act
            target.ViewArticle(visitorId, articleId);

            // Assert
            Assert.IsNotNull(newArticleVisitors);
            Assert.AreEqual(articleVisitors.Count(), newArticleVisitors.Count());

            ArticleVisitor articleVisitor = newArticleVisitors.FirstOrDefault(v => v.ArticleId == articleId);

            Assert.IsNotNull(articleVisitor);
            Assert.IsTrue(articleVisitor.LastViewDate.Subtract(lastViewDate)
            .TotalMilliseconds > 0);

            visitorRepositoryMock.Verify(
            r =>
            r.GetById(visitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            visitorRepositoryMock.Verify(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitorId)), Times.Once);

            unitOfWorkMock.Verify(u => u.Save(), Times.Once);
        }
Exemplo n.º 4
0
        public void ViewArticle_NonexistentArticleId_ThrowsArticleNotFoundException()
        {
            // Arrange
            Visitor visitor = new Visitor { VisitorId = 1 };

            int articleId = 2;

            Expression<Func<Visitor, object>>[] propertiesToInclude = { v => v.ArticleVisitors };

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(
            r =>
            r.GetById(visitor.VisitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(visitor);

            // 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.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArticleNotFoundException>(() => target.ViewArticle(visitor.VisitorId, articleId));

            visitorRepositoryMock.Verify(
            r =>
            r.GetById(visitor.VisitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            visitorRepositoryMock.Verify(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitor.VisitorId)), Times.Never);

            articleRepositoryMock.Verify(r => r.GetById(articleId), Times.Once);

            unitOfWorkMock.Verify(u => u.Save(), Times.Never);
        }
Exemplo n.º 5
0
        public void ViewArticle_AllCredentialsAreValid_MarksArticleAsViewedByVisitor()
        {
            // Arrange
            int visitorId = 1;
            int articleId = 2;
            ArticleVisitor[] articleVisitors =
            {
                new ArticleVisitor { VisitorId = visitorId, ArticleId = articleId + 1 },
                new ArticleVisitor { VisitorId = visitorId, ArticleId = articleId + 2 }
            };

            Visitor visitor = new Visitor { VisitorId = visitorId, ArticleVisitors = articleVisitors.ToList() };
            Article article = new Article { ArticleId = articleId };

            Expression<Func<Visitor, object>>[] propertiesToInclude = { v => v.ArticleVisitors };

            // Arrange - mock visitorRepository
            Mock<IVisitorRepository> visitorRepositoryMock = new Mock<IVisitorRepository>();

            visitorRepositoryMock.Setup(
            r =>
            r.GetById(visitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))))
            .Returns(visitor);

            IEnumerable<ArticleVisitor> newArticleVisitors = null;

            visitorRepositoryMock.Setup(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitorId)))
            .Callback((Visitor v) => newArticleVisitors = v.ArticleVisitors);

            // Arrange - mock articleRepository
            Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>();

            articleRepositoryMock.Setup(r => r.GetById(articleId))
            .Returns(article);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.VisitorRepository)
            .Returns(visitorRepositoryMock.Object);

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            // Arrange - create target
            IVisitorService target = new VisitorService(unitOfWorkMock.Object, this._visitorValidationMock.Object);

            // Act
            target.ViewArticle(visitorId, articleId);

            // Assert
            Assert.IsNotNull(newArticleVisitors);
            Assert.AreEqual(articleVisitors.Count() + 1, newArticleVisitors.Count());

            ArticleVisitor articleVisitor = newArticleVisitors.FirstOrDefault(v => v.ArticleId == articleId);

            Assert.IsNotNull(articleVisitor);
            Assert.IsTrue(new DateTime() != articleVisitor.LastViewDate);

            visitorRepositoryMock.Verify(
            r =>
            r.GetById(visitorId,
            It.Is<Expression<Func<Visitor, object>>[]>(
            selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once);

            visitorRepositoryMock.Verify(r => r.Update(It.Is<Visitor>(v => v.VisitorId == visitorId)), Times.Once);

            articleRepositoryMock.Verify(r => r.GetById(articleId), Times.Once);

            unitOfWorkMock.Verify(u => u.Save(), Times.Once);
        }