public void PurchaseService_AddItemAsync_DeveAdicionarOsItensQuandoEmEstadoValido()
        {
            // Arange
            int totalItens = 5;

            Purchase             purchase      = PurchaseFaker.GenerateFaker().Generate();
            IList <PurchaseItem> purchaseItens = PurchaseItemFaker.GenerateFaker(purchase).Generate(totalItens);

            ConfigureMock();

            FactoryService();

            // Act
            foreach (PurchaseItem purchaseItem in purchaseItens)
            {
                _purchaseService.AddItemAsync(purchase, purchaseItem);
            }

            // Assert
            purchase.Items.Should().HaveCount(totalItens);
            purchase.TotalValue.Should().Be(purchaseItens.Sum(x => x.CalculteValue()));
            _purchaseRepositoryMock.Verify(x => x.Update(It.IsAny <Purchase>()), Times.Exactly(totalItens));
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Exactly(totalItens));
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseItemAdded>()), Times.Exactly(totalItens));
        }
        public void PurchaseService_RemoveItemAsync_DeveRemoverOItemQuandoOMesmoExistir()
        {
            // Arange
            int totalItens = 5;

            Purchase             purchase      = PurchaseFaker.GenerateFaker().Generate();
            IList <PurchaseItem> purchaseItens = PurchaseItemFaker.GenerateFaker(purchase).Generate(totalItens);

            ConfigureMock();

            FactoryService();

            foreach (PurchaseItem purchaseItem in purchaseItens)
            {
                _purchaseService.AddItemAsync(purchase, purchaseItem);
            }

            // Act
            _purchaseService.RemoveItemAsync(purchase, purchaseItens.First());

            // Assert
            purchase.Items.Should().HaveCount(totalItens - 1);
            purchase.TotalValue.Should().Be(purchaseItens.Skip(1).Sum(x => x.CalculteValue()));
            _purchaseRepositoryMock.Verify(x => x.Remove(It.IsAny <Purchase>()), Times.Once);
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Exactly(totalItens + 1));
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseItemRemoved>()), Times.Once);
        }
        public void PurchaseService_CloseAsync_DeveMudarOEstadoParaFechadoCorretamenteQuandoEstiverAberto()
        {
            // Arange
            int totalItens = 5;

            Purchase             purchase      = PurchaseFaker.GenerateFaker().Generate();
            IList <PurchaseItem> purchaseItens = PurchaseItemFaker.GenerateFaker(purchase).Generate(totalItens);

            ConfigureMock();

            FactoryService();

            foreach (PurchaseItem purchaseItem in purchaseItens)
            {
                _purchaseService.AddItemAsync(purchase, purchaseItem);
            }

            // Act
            _purchaseService.CloseAsync(purchase);

            // Assert
            purchase.State.Should().Be(PurchaseState.Closed);
            purchase.Items.Should().HaveCount(totalItens);
            purchase.TotalValue.Should().Be(purchaseItens.Sum(x => x.CalculteValue()));
            _purchaseRepositoryMock.Verify(x => x.Update(It.IsAny <Purchase>()), Times.Exactly(totalItens + 1));
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Exactly(totalItens + 1));
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseClosedEvent>()), Times.Once);
        }
Example #4
0
        public void Purchase_AddItem_DeveSetarOStatusParaFechadoCorretamenteSeEstiverAberto()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            // Act
            purchase.Close();

            // Assert
            purchase.State.Should().Be(PurchaseState.Closed);
        }
Example #5
0
        public void Purchase_UpdateItem_DeveGerarDomainExceptionQuandoTentarAtualizarUmItemInexistente()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            PurchaseItem purchaseItem = PurchaseItemFaker.GenerateFaker(purchase).Generate();

            // Act
            Action act = () => purchase.UpdateItem(purchaseItem);

            // Assert
            act.Should().Throw <DomainException>();
        }
Example #6
0
        public void PurchaseItem_Validate_DeveRetornarDomainExceptionAoTentarCriarComValoresInvalidos()
        {
            // Arange && Act
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();
            Product  product  = ProductFaker.GenerateFaker().Generate();

            Func <PurchaseItem> func1 = () => new PurchaseItem(0, 1, purchase, product);
            Func <PurchaseItem> func2 = () => new PurchaseItem(1, 0, purchase, product);

            // Assert
            func1.Should().Throw <DomainException>();
            func2.Should().Throw <DomainException>();
        }
Example #7
0
        public void Purchase_RemoveItem_DeveGerarDomainExceptionAoFecharORegistroQueEstaFechado()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            purchase.Close();

            // Act
            Action act = () => purchase.Close();

            // Assert
            act.Should().Throw <DomainException>();
        }
Example #8
0
        public void PurchaseItem_CalculteValue_DeveCalculaCorretamenteOValorDoItem()
        {
            // Arange && Act
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();
            Product  product  = ProductFaker.GenerateFaker().Generate();

            PurchaseItem item = new PurchaseItem(10, 2, purchase, product);

            // Act
            decimal value = item.CalculteValue();

            // Assert
            value.Should().Be(20);
        }
Example #9
0
        public void Purchase_UpdateItem_DeveAtualizarCorretamenteUmItemQuandoOMesmoExistir()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            PurchaseItem purchaseItem = PurchaseItemFaker.GenerateFaker(purchase).Generate();

            purchase.AddItem(purchaseItem);

            // Act
            purchase.UpdateItem(purchaseItem);

            // Assert
            purchase.Items.Should().HaveCount(1);
        }
Example #10
0
        public void Purchase_AddItem_DeveGerarDomainExceptionQuandoAdicionarUmItemEEstiverFechado()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            purchase.Close();

            PurchaseItem item1 = PurchaseItemFaker.GenerateFaker(purchase).Generate();

            // Act
            Action act = () => purchase.AddItem(item1);

            // Assert
            act.Should().Throw <DomainException>();
        }
Example #11
0
        public void Purchase_CalculateTotalValue_DeveSomarCorretamenteValorTotal()
        {
            // Arange && Act
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(10);

            foreach (PurchaseItem itemMock in itemsMock)
            {
                purchase.AddItem(itemMock);
            }

            // Assert
            purchase.TotalValue.Should().Be(itemsMock.Sum(x => x.CalculteValue()));
        }
Example #12
0
        public void Purchase_RemoveItem_DeveGerarDomainExceptionQuandoRemoverUmItemInexistente()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(2);

            purchase.AddItem(itemsMock.First());

            // Act
            Action act = () => purchase.RemoveItem(itemsMock.Last());

            // Assert
            act.Should().Throw <DomainException>();
        }
Example #13
0
        public void Purchase_AddItem_DeveAdicionarOsItensCorretamenteQuandoValidos()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            // Act
            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(10);

            foreach (PurchaseItem itemMock in itemsMock)
            {
                purchase.AddItem(itemMock);
            }

            // Assert
            purchase.Items.Should().HaveCount(10);
        }
Example #14
0
        public void PurchaseItem_Update_DeveAtualizarCorretamenteOItemQuandoInformadoValoresValidos()
        {
            // Arange && Act
            Purchase purchase   = PurchaseFaker.GenerateFaker().Generate();
            Product  product    = ProductFaker.GenerateFaker().Generate();
            Product  newProduct = ProductFaker.GenerateFaker().Generate();

            PurchaseItem item = new PurchaseItem(10, 2, purchase, product);

            // Act
            item.Update(10, 5, newProduct);

            // Assert
            item.Amount.Should().Be(10);
            item.Value.Should().Be(5);
        }
Example #15
0
        public void PurchaseItem_Update_DeveRetornarDomainExceptionAoTentarAtualizarComValoresInvalidos()
        {
            // Arange && Act
            Purchase purchase   = PurchaseFaker.GenerateFaker().Generate();
            Product  product    = ProductFaker.GenerateFaker().Generate();
            Product  newProduct = ProductFaker.GenerateFaker().Generate();

            PurchaseItem item = new PurchaseItem(10, 2, purchase, product);

            // Act
            Action action1 = () => item.Update(0, 5, newProduct);
            Action action2 = () => item.Update(1, 0, newProduct);

            // Assert
            action1.Should().Throw <DomainException>();
            action2.Should().Throw <DomainException>();
        }
Example #16
0
        public void Purchase_RemoveItem_DeveRemoverCorretamenteQuandoItemExistir()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(2);

            purchase.AddItem(itemsMock.First());
            purchase.AddItem(itemsMock.Last());

            // Act
            purchase.RemoveItem(itemsMock.First());

            // Assert
            purchase.Items.Should().HaveCount(1);
            purchase.Items.Should().NotContain(x => x.Id == itemsMock.First().Id);
        }
        public void PurchaseService_CloseAsync_DeveGerarDomainExceptionQuandoQuandoEstiverFechadoETentarFecharNovamente()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            purchase.Close();

            ConfigureMock();

            FactoryService();

            // Act
            Action act = () => purchase.Close();

            // Assert
            act.Should().Throw <DomainException>();
            _purchaseRepositoryMock.Verify(x => x.Update(It.IsAny <Purchase>()), Times.Never);
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Never);
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseClosedEvent>()), Times.Never);
        }
Example #18
0
        private void ConfigureMock()
        {
            _purchaseRepositoryMock = new Mock <IPurchaseRepository>();
            _productRepositoryMock  = new Mock <IProductRepository>();
            _unitOfWork             = new Mock <IUnitOfWork>();
            _busHandlerMock         = new Mock <IBus>();

            _purchaseRepositoryMock.SetupProperty(x => x.UnitOfWork, _unitOfWork.Object);

            _unitOfWork.Setup(x => x.CommitAsync())
            .Returns(Task.FromResult(true));

            _purchaseRepositoryMock.Setup(x => x.FindAsync(It.IsAny <Guid>()))
            .Returns(Task.FromResult(PurchaseFaker.GenerateFaker().Generate()));

            _purchaseRepositoryMock.Setup(x => x.FindAllAsync())
            .Returns(Task.FromResult(PurchaseFaker.GenerateFaker().Generate(100)));

            _purchaseRepositoryMock.Setup(x => x.AddAsync(It.IsAny <Purchase>()))
            .Returns(Task.CompletedTask);

            _purchaseRepositoryMock.Setup(x => x.Update(It.IsAny <Purchase>()));

            _purchaseRepositoryMock.Setup(x => x.Remove(It.IsAny <Purchase>()));

            _busHandlerMock.Setup(x => x.PublishDomainEvent <PurchaseItemAdded>(It.IsAny <PurchaseItemAdded>()))
            .Returns(Task.CompletedTask);

            _busHandlerMock.Setup(x => x.PublishDomainEvent <PurchaseItemRemoved>(It.IsAny <PurchaseItemRemoved>()))
            .Returns(Task.CompletedTask);

            _busHandlerMock.Setup(x => x.PublishDomainEvent <PurchaseClosedEvent>(It.IsAny <PurchaseClosedEvent>()))
            .Returns(Task.CompletedTask);

            _productRepositoryMock.Setup(x => x.FindAsync(It.IsAny <Guid>()))
            .Returns(Task.FromResult(ProductFaker.GenerateFaker().Generate()));
        }