Esempio n. 1
0
        public ICommandResult Handle(UpdateServerCommand command)
        {
            command.Validate();
            if (command.IsInvalid)
            {
                return(new DefaultCommandResult(CommandResultStatus.InvalidCommand, command.Notifications));
            }

            try
            {
                var server = _serverRepository.GetById(command.Id);
                if (server == null)
                {
                    return(new DefaultCommandResult(CommandResultStatus.InvalidData, "Nenhum servidor foi localizado"));
                }

                server.Name = command.Name;
                server.Ip   = command.Ip;
                server.Port = command.Port;
                _serverRepository.Update(server);
                return(new DefaultCommandResult(server.Id));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Fail to execute UpdateServerHandler. Fail stack ===> {e.ToString()}");
                return(new DefaultCommandResult(CommandResultStatus.Exception));
            }
        }
Esempio n. 2
0
        public async Task Validar_comando_correto()
        {
            var command = new UpdateServerCommand(Guid.NewGuid(), "teste", "127.0.0.1", 80);

            var result = await _validator.ValidateAsync(command);

            result.IsValid.Should().BeTrue();
        }
Esempio n. 3
0
        public async Task Retornar_erro_quando_comando_tiver_porta_zerada()
        {
            var command = new UpdateServerCommand(Guid.NewGuid(), "teste", "127.0.0.1", 0);

            var result = await _validator.ValidateAsync(command);

            result.IsValid.Should().BeFalse();
            result.Errors.Should().Contain(x => x.ErrorMessage == "Porta informada deve ser maior que 0");
        }
Esempio n. 4
0
        public async Task Retornar_erro_quando_comando_tiver_ip_invalido()
        {
            var command = new UpdateServerCommand(Guid.Empty, "teste", "google.com.br", 80);

            var result = await _validator.ValidateAsync(command);

            result.IsValid.Should().BeFalse();
            result.Errors.Should().Contain(x => x.ErrorMessage == "IP inválido");
        }
Esempio n. 5
0
        public async Task Retornar_erro_quando_comando_nao_tiver_ip()
        {
            var command = new UpdateServerCommand(Guid.Empty, "teste", null, 80);

            var result = await _validator.ValidateAsync(command);

            result.IsValid.Should().BeFalse();
            result.Errors.Should().Contain(x => x.ErrorMessage == "IP é obrigatório");
        }
Esempio n. 6
0
        public async Task Retornar_erro_quando_comando_for_invalido()
        {
            var command = new UpdateServerCommand(Guid.Empty, "", "abc.0.0.1", 0);

            var result = await _handler.Handle(command, CancellationToken.None);

            result.IsFail.Should().BeTrue();
            _serverRepositoryMock.Verify(x => x.SaveChangesAsync(), Times.Never());
        }
Esempio n. 7
0
        public async Task Retornar_erro_quando_servidor_nao_existir()
        {
            var command = new UpdateServerCommand(Guid.NewGuid(), "novo", "127.0.0.1", 4321);

            var result = await _handler.Handle(command, CancellationToken.None);

            result.IsFail.Should().BeTrue();
            _serverRepositoryMock.Verify(x => x.SaveChangesAsync(), Times.Never());
        }
Esempio n. 8
0
        public async Task <IActionResult> Put(int id, [FromBody] CreateUpdateServerDto server, CancellationToken cancellationToken)
        {
            var command = new UpdateServerCommand {
                Id = id, Server = server
            };
            await Mediator.Send(command, cancellationToken);

            return(NoContent());
        }
Esempio n. 9
0
        public async Task <IActionResult> Update([FromBody] UpdateServerCommand command)
        {
            var result = await _mediator.Send(command);

            if (result.IsSuccess)
            {
                return(NoContent());
            }

            return(BadRequest(result.FailData));
        }
Esempio n. 10
0
        public async Task Retornar_servidor_atualizado_quando_sucesso()
        {
            _serverRepositoryMock.Setup(x => x.GetAsync(It.IsAny <Guid>(), false)).ReturnsAsync(new Server("velho", "127.0.0.1", 1234));
            var command = new UpdateServerCommand(Guid.NewGuid(), "novo", "127.0.0.1", 4321);

            var result = await _handler.Handle(command, CancellationToken.None);

            result.SuccessData.Should().NotBeNull();
            _serverRepositoryMock.Verify(x => x.Update(It.Is <Server>(s => s.Name == "novo" && s.Port == 4321)), Times.Once());
            _serverRepositoryMock.Verify(x => x.SaveChangesAsync(), Times.Once());
        }
Esempio n. 11
0
        public void Given_A_Valid_Command_Should_Be_Update_A_Server()
        {
            _serverHandler = new ServerHandler(new FakeServerRepository(_dataContextMock));
            var serverSalvedId   = _dataContextMock.Servers.FirstOrDefault().Id;
            var commandForUpdate = new UpdateServerCommand()
            {
                Id   = serverSalvedId,
                Name = "Updated Server",
                Ip   = "10.1.1.15",
                Port = 55
            };

            var handlerResponse = (DefaultCommandResult)_serverHandler.Handle(commandForUpdate);

            Assert.True(handlerResponse.IsSuccess);
        }
Esempio n. 12
0
        public IActionResult Update([FromServices] ServerHandler handler, [FromBody] UpdateServerCommand command)
        {
            var response = (DefaultCommandResult)handler.Handle(command);

            return(this.DefaultCommandResultToActionResult(response));
        }