public Cliente Adicionar(Cliente cliente)
        {
            if (!cliente.IsValid)
            {
                _notificacoes.AdicionarListaNotificacao(cliente.ValidationResult.Errors.ToList());
            }

            if (_notificacoes.TemNotificacao())
            {
                return(cliente);
            }

            Cliente clienteExistente = _clienteRepository.ObterPorCpfCnpj(cliente.CpfCnpj);

            if (!string.IsNullOrEmpty(clienteExistente?.CpfCnpj))
            {
                _notificacoes.AdicionarNotificacao($"Cliente de CpfCnpj.: {clienteExistente.CpfCnpj} já existe no sistema!");
                return(cliente);
            }

            _clienteRepository.Adicionar(cliente);

            _clienteRepository.SaveChanges();

            return(cliente);
        }
Пример #2
0
        public void Cliente_Apagar_NaoEncontrou_RetornoInvalido()
        {
            //Arrange
            var             mockRepository = new Mock <IClienteRepository>(MockBehavior.Strict);
            Notificacoes    notificacoes   = new Notificacoes();
            IClienteService clienteService = new ClienteService(mockRepository.Object, notificacoes);

            mockRepository.Setup(s => s.ObterPorCpfCnpj(clienteNovo.CpfCnpj)).Returns(clienteNull);
            mockRepository.Setup(s => s.Apagar(clienteNovo.CpfCnpj));
            mockRepository.Setup(s => s.SaveChanges()).Returns(1);

            //Act
            clienteService.Apagar(clienteNovo.CpfCnpj);

            //Assert
            Assert.True(notificacoes.TemNotificacao());

            mockRepository.Verify(mock => mock.ObterPorCpfCnpj(clienteNovo.CpfCnpj), Times.Once);
            mockRepository.Verify(mock => mock.Apagar(clienteNovo.CpfCnpj), Times.Never);
            mockRepository.Verify(mock => mock.SaveChanges(), Times.Never);
        }
Пример #3
0
        public void Cliente_Alterar_RetornoValido()
        {
            //Arrange
            var             mockRepository = new Mock <IClienteRepository>(MockBehavior.Strict);
            Notificacoes    notificacoes   = new Notificacoes();
            IClienteService clienteService = new ClienteService(mockRepository.Object, notificacoes);

            mockRepository.Setup(s => s.ObterPorCpfCnpj(clienteValido.CpfCnpj)).Returns(clienteValido);
            mockRepository.Setup(s => s.Alterar(clienteValido)).Returns(clienteValido);
            mockRepository.Setup(s => s.SaveChanges()).Returns(1);

            //Act
            clienteValido = clienteService.Alterar(clienteValido);

            //Assert
            Assert.True(clienteValido.IsValid);
            Assert.True(!notificacoes.TemNotificacao());

            mockRepository.Verify(mock => mock.ObterPorCpfCnpj(clienteValido.CpfCnpj), Times.Once);
            mockRepository.Verify(mock => mock.Alterar(clienteValido), Times.Once);
            mockRepository.Verify(mock => mock.SaveChanges(), Times.Once);
        }
Пример #4
0
        public void Cliente_NaoDeveIncluir_CpfInvalido_RetornoValido()
        {
            //Arrange
            var             mockRepository = new Mock <IClienteRepository>(MockBehavior.Strict);
            Notificacoes    notificacoes   = new Notificacoes();
            IClienteService clienteService = new ClienteService(mockRepository.Object, notificacoes);

            mockRepository.Setup(s => s.ObterPorCpfCnpj(clienteInValido.CpfCnpj)).Returns(clienteNull);
            mockRepository.Setup(s => s.Adicionar(clienteInValido)).Returns(clienteInValido);
            mockRepository.Setup(s => s.SaveChanges()).Returns(1);

            //Act
            clienteValido = clienteService.Adicionar(clienteInValido);

            //Assert
            Assert.True(!clienteValido.IsValid);
            Assert.True(notificacoes.TemNotificacao());

            mockRepository.Verify(mock => mock.ObterPorCpfCnpj(clienteNovo.CpfCnpj), Times.Never);
            mockRepository.Verify(mock => mock.Adicionar(clienteNovo), Times.Never);
            mockRepository.Verify(mock => mock.SaveChanges(), Times.Never);
        }
 protected bool IsValidOperation()
 {
     return(!_notificacoes.TemNotificacao());
 }