public RepositoryCommandHandler(IRepository repository)
 {
     this.repository                 = repository;
     addCustomerCommandHandler       = new AddCustomerCommandHandler(repository);
     addOrderCommandHandler          = new AddOrderCommandHandler(repository);
     addProductCommandHandler        = new AddProductCommandHandler(repository);
     addProductToOrderCommandHandler = new AddProductToOrderCommandHandler(repository);
 }
Exemple #2
0
 public AddProductCommandHandlerTest()
 {
     basketRedisService = new Mock <IBasketRedisService>();
     command            = new AddProductCommand {
         UserId = It.IsAny <string>(), Product = new BasketProduct()
     };
     commandHandler = new AddProductCommandHandler(basketRedisService.Object);
     basketDto      = new UserBasketDto();
 }
 public AddProductCommandHandlerTest()
 {
     productRepository = new Mock <IProductRepository>();
     bus            = new Mock <IBus>();
     mapper         = new Mock <IMapper>();
     command        = new AddProductCommand();
     commandHandler = new AddProductCommandHandler(productRepository.Object, mapper.Object, bus.Object);
     product        = new Product();
 }
 public void Should_be_Able_to_Add_the_Product()
 {
     var baseAddress = new Uri("http://localhost:8082/");
     var httpSelfhostConfiguration = new HttpSelfHostConfiguration(baseAddress);
     new MusicStoreBootStrap(httpSelfhostConfiguration).ConfigureDatabaseForTest();
     AddProductCommand command = new AddProductCommand { Name = "Reebok Shoe", Categories = "Shoes", Id = 1 };
     ICommandHandler<AddProductCommand> productCommand = new AddProductCommandHandler();
     productCommand.Execute(command);
     ProductViewModel ViewModel = Database.Open().Products.FindById(1);
     ViewModel.Name.Should().Be(command.Name);
 }
Exemple #5
0
        public async Task Should_ThrowException_When_InputIsNull()
        {
            var dbContext = new Mock <CleanArchWriteDbContext>();
            var logger    = new Mock <ILogger <AddProductCommandHandler> >();

            var commandHandler = new AddProductCommandHandler(dbContext.Object, logger.Object);

            var request = new Mock <AddProductCommand>();

            //await Assert.ThrowsAsync<InvalidNullInputException>(() => commandHandler.Handle(request.Object, CancellationToken.None));
            await Assert.ThrowsAsync <InvalidNullInputException>(() => commandHandler.Handle(null, CancellationToken.None));
        }
Exemple #6
0
        public void WhenUserAddsTheProduct(string userName, Table table)
        {
            var user = sessionFactory.OpenSession().Query <User>().First(u => u.UserName == userName);

            var command = new AddProductCommand();

            command.UserId       = user.Id;
            command.ProductName  = table.Rows[0]["Name"];
            command.ProductNotes = table.Rows[0]["Notes"];

            var handler = new AddProductCommandHandler(sessionFactory.OpenSession());
            var result  = handler.Handle(command);

            ScenarioContext.Current.Set(command);
        }
Exemple #7
0
        public void AddProduct_IfIsCorrect_ShouldSuccess()
        {
            SystemTime.NowFunc = () => new DateTime(2019, 4, 1);

            var command = new AddProductCommand
            {
                Name                = "Mleko",
                ExpirationDate      = new DateTime(2019, 4, 17),
                MaxDaysAfterOpening = 2
            };

            var handler = new AddProductCommandHandler(ServiceFactory.ProductsRepository);
            var result  = handler.Handle(command);

            result.IsSuccess.Should().BeTrue();
        }
Exemple #8
0
        public void AddProduct_IfExpirationDateIsLessThanCurrent_ShouldFailure()
        {
            SystemTime.NowFunc = () => new DateTime(2019, 4, 20);

            var command = new AddProductCommand
            {
                Name                = "Mleko",
                ExpirationDate      = new DateTime(2019, 4, 17),
                MaxDaysAfterOpening = 2
            };

            var handler = new AddProductCommandHandler(ServiceFactory.ProductsRepository);
            var result  = handler.Handle(command);

            result.IsSuccess.Should().BeFalse();
            result.ErrorMessage.Should().BeEquivalentTo("Expiration date should be greater than current date.");
        }
Exemple #9
0
        public void AddProduct_IfNameIsEmpty_ShouldFailure()
        {
            SystemTime.NowFunc = () => new DateTime(2019, 4, 1);

            var command = new AddProductCommand
            {
                Name                = "",
                ExpirationDate      = new DateTime(2019, 4, 17),
                MaxDaysAfterOpening = 2
            };

            var handler = new AddProductCommandHandler(ServiceFactory.ProductsRepository);
            var result  = handler.Handle(command);

            result.IsSuccess.Should().BeFalse();
            result.ErrorMessage.Should().BeEquivalentTo("Product name can not be empty.");
        }
Exemple #10
0
        public async Task Execute_CorrectCommand_ReturnsSuccess()
        {
            //Arrange
            var command = new AddProductCommand {
                Name = "TestProduct", CarbsGrams = 12, FatsGrams = 22, ProteinsGrams = 31
            };
            var validator = new Mock <AbstractValidator <AddProductCommand> >();

            validator.Setup(x => x.Validate(It.IsAny <ValidationContext <AddProductCommand> >())).Returns(new ValidationResult());
            var handler = new AddProductCommandHandler(_config.Object, _logger.Object, validator.Object, _context);

            //Act
            var result = await handler.Handle(command);

            //Assert
            Assert.AreEqual(result.Success, true);
        }
        public void CreateProductSuccessfully(Product product)
        {
            _productService.Setup(ps => ps.CreateProduct(It.IsAny <Product>())).ReturnsAsync(product.Id);

            var handler = new AddProductCommandHandler(_productService.Object);

            var result = handler.Handle(new AddProductCommand()
            {
                Product = new ProductVm()
                {
                    Name  = product.Name,
                    Price = product.Price
                }
            }, default);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <Task <Response <ProductVm> > >(result);
            Assert.AreEqual(StatusCodes.Status200OK, result.Result.StatusCode);
        }
Exemple #12
0
        public void AddProduct_IfIsCorrect_ShouldIncreaseProductsCount()
        {
            SystemTime.NowFunc = () => new DateTime(2019, 4, 1);

            var command = new AddProductCommand
            {
                Name                = "Mleko",
                ExpirationDate      = new DateTime(2019, 4, 17),
                MaxDaysAfterOpening = 2
            };

            var productsRepository = ServiceFactory.ProductsRepository;

            productsRepository.Query().Count().Should().Be(0);

            var handler = new AddProductCommandHandler(productsRepository);

            handler.Handle(command);

            productsRepository.Query().Count().Should().Be(1);
        }
Exemple #13
0
        public async Task Execute_IncorrectCommand_ReturnsFailure()
        {
            //Arrange
            var command = new AddProductCommand {
                Name = "TestProduct3", CarbsGrams = 12, FatsGrams = 22, ProteinsGrams = 31
            };
            var validator = new Mock <AbstractValidator <AddProductCommand> >();
            var incorrectValidationResultStub = new ValidationResult()
            {
                Errors = { new ValidationFailure("Name", "IncorrectName") }
            };

            validator.Setup(x => x.Validate(It.IsAny <ValidationContext <AddProductCommand> >()))
            .Returns(incorrectValidationResultStub);
            var handler = new AddProductCommandHandler(_config.Object, _logger.Object, validator.Object, _context);

            //Act
            var result = await handler.Handle(command);

            //Assert
            Assert.AreEqual(result.Success, false);
        }
        public async Task CommandIsValid_Executed_Success()
        {
            // Arrange
            var productRepository = new Mock <IProductRepository>();
            var addProductCommand = new AddProductCommand("titulo", "descricao", 1000);

            var product = new Product("", "", 1000);

            // productRepository.Setup(pr => pr.Add(It.IsAny<Product>())).Verifiable();
            productRepository.Setup(pr => pr.Add(It.IsAny <Product>())).Returns(Task.FromResult(product));

            var addProductCommandHandler = new AddProductCommandHandler(productRepository.Object);

            // Act
            var productResult = await addProductCommandHandler.Handle(addProductCommand, new CancellationToken());

            // Assert
            productRepository.Verify(pr => pr.Add(It.IsAny <Product>()), Times.Once);
            Assert.NotNull(productResult);
            Assert.Equal(product.Price, productResult.Price);
            Assert.Equal(product.Title, productResult.Title);
            Assert.Equal(product.Description, productResult.Description);
        }
Exemple #15
0
        public void GivenUserHasSavedTheFollowingProducts(string userName, Table table)
        {
            using (var session = sessionFactory.OpenSession())
            {
                var user = session.Query <User>().First(u => u.UserName == userName);

                foreach (var row in table.Rows)
                {
                    var command = new AddProductCommand
                    {
                        UserId       = user.Id,
                        ProductName  = row["Name"],
                        ProductNotes = row["Notes"]
                    };

                    var handler  = new AddProductCommandHandler(session);
                    var response = handler.Handle(command);

                    if (row.ContainsKey("Tags") && !string.IsNullOrWhiteSpace(row["Tags"]))
                    {
                        var addTagHandler = new AddTagToProductCommandHandler(session);
                        var tagNames      = row["Tags"].Split(',').Select(t => t.Trim());
                        foreach (var tagName in tagNames)
                        {
                            var addTagCommand = new AddTagToProductCommand
                            {
                                UserId    = user.Id,
                                ProductId = response.NewProductId,
                                TagName   = tagName
                            };
                            addTagHandler.Handle(addTagCommand);
                        }
                    }
                }
            }
        }