Ejemplo n.º 1
0
        public ICommandResult CreateTodo([FromBody] CreateTodoCommand command)
        {
            command.User = "******";
            var result = (ResponseCommand)_handler.Handle(command);

            return(result);
        }
        public void CreateTodoCommandValid()
        {
            var command = new CreateTodoCommand("title", DateTime.Now, "userbygoogle");

            command.Validate();
            Assert.True(command.Valid);
        }
        public void CreateTodoCommandInvalidTitle()
        {
            var command = new CreateTodoCommand("ti", DateTime.Now, "userbygoogle");

            command.Validate();
            Assert.False(command.Valid);
        }
        public void CreateTodoCommandInvalidUser()
        {
            var command = new CreateTodoCommand("title", DateTime.Now, "");

            command.Validate();
            Assert.False(command.Valid);
        }
Ejemplo n.º 5
0
        public ActionResult Create(CreateViewModel vm)
        {
            var command = new CreateTodoCommand(vm.Id, vm.Title);

            _bus.Send(command);
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 6
0
 public CreateTodoHandlerTests()
 {
     _invalidComand = new CreateTodoCommand("", "", DateTime.Now);
     _validComand   = new CreateTodoCommand("criando tarefa", "douglas", DateTime.Now);
     _handler       = new TodoHandler(new FakeTodoRepository());
     _result        = new GenericCommandResult();
 }
        public async Task <IActionResult> Post(CreateTodoCommand command, [FromHeader(Name = "user-id")] Guid userId)
        {
            command.UserId = userId;
            await mediator.Send(command);

            return(StatusCode(StatusCodes.Status201Created));
        }
Ejemplo n.º 8
0
 public void Handle(CreateTodoCommand command, IEventPublisher publisher)
 {
     if (State != TodoState.CLOSED)
     {
         publisher.Publish(new TodoIsCreatedEvent(command.UUID, command.Title));
     }
 }
Ejemplo n.º 9
0
        public void Dado_um_Comando_Invalido_deve_interromper_a_execucao()
        {
            var command = new CreateTodoCommand("", "", DateTime.Now);

            _result = (GenericCommandResult)_handler.Handle(_invalidCommand);
            Assert.AreEqual(_result.Sucess, false);
        }
Ejemplo n.º 10
0
        public void CreateTodoCommandInvalid()
        {
            var command = new CreateTodoCommand("", DateTime.Now, "");
            var result  = (GenericCommandResult)_handler.Handle(command);

            Assert.False(result.Success);
        }
Ejemplo n.º 11
0
 public GenericCommandResult Create(
     [FromBody] CreateTodoCommand command,
     [FromServices] TodoHandler handler)
 {
     command.User = User.Claims.FirstOrDefault(x => x.Type == "user_id")?.Value;
     return((GenericCommandResult)handler.Handle(command));
 }
        public async Task Handle_WhenUserHasPermission_ShouldCreateTodo()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new CreateTodoCommand {
                UserId = userId, Name = "todo one", IsComplete = true
            };

            var dbOptions = new DbContextOptionsBuilder <TodoDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            long todoId = 0;

            using (var context = new TodoDbContext(dbOptions))
            {
                var sut = new CreateTodoCommandHandler(context, mapper, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient);
                // Act
                todoId = await sut.Handle(command, new CancellationToken(false));

                // Assert
                todoId.ShouldBeGreaterThan(0);

                var createdTodo = await context.Todos.FirstOrDefaultAsync(t => t.Id == todoId);

                createdTodo.ShouldNotBeNull();
                createdTodo.Name.ShouldBe(command.Name);
                createdTodo.IsComplete.ShouldBe(command.IsComplete);
            }
        }
        public async Task Handle_WhenUserDoesNotHavePermission_ShouldThrowException()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new CreateTodoCommand {
                UserId = userId, Name = "todo one", IsComplete = true
            };
            var rpcResponse = new IsUserAllowedResponse {
                IsAllowed = false
            };

            rpcPermissionsMock.ClientMock.Setup(x => x.IsUserAllowed(It.IsAny <IsUserAllowedRequest>(), It.IsAny <CallOptions>())).Returns(rpcResponse);

            var dbOptions = new DbContextOptionsBuilder <TodoDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            using (var context = new TodoDbContext(dbOptions))
            {
                // Act
                var sut = new CreateTodoCommandHandler(context, mapper, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient);
                try
                {
                    var result = await sut.Handle(command, new CancellationToken(false));

                    Assert.True(false, "Should throw exception");
                }
                catch (InvalidOperationException ex)
                {
                    // Assert
                    Assert.NotNull(ex);
                }
            }
        }
Ejemplo n.º 14
0
        public async Task <ActionResult <TodoReadDTO> > CreateTodo([FromBody] TodoCreateDTO todoCreateDto)
        {
            var request     = new CreateTodoCommand(User.Identity?.Name, todoCreateDto);
            var createdTodo = await _mediator.Send(request);

            return(CreatedAtRoute(nameof(GetTodoById), new { Id = createdTodo.Id }, createdTodo));
        }
Ejemplo n.º 15
0
        public void CreateTodoCommand_TodoValido_DeveRetornarTodo()
        {
            var command = new CreateTodoCommand("Teste", "kelvin", DateTime.Now);

            command.Validate();
            Assert.True(command.Valid);
        }
Ejemplo n.º 16
0
 public GenericCommandResult Create(     // converte automaticamente em JSON, por causa do FromBody (ModelBind)
     [FromBody] CreateTodoCommand command,
     [FromServices] TodoHandler handler) // From services vem do startup, ve o que tem dentro do AddTransient e instancia a dependencia
 {
     command.User = "******";
     return((GenericCommandResult)handler.Handle(command)); // retorna se O.K ou não
 }
Ejemplo n.º 17
0
        public GenericCommandResult Create([FromBody] CreateTodoCommand command,
                                           [FromServices] TodoHandler todoHandler)
        {
            command.User = User.Claims.FirstOrDefault(us => us.Type.Equals("user_id"))?.Value;

            return((GenericCommandResult)todoHandler.Handle(command));
        }
Ejemplo n.º 18
0
        public static TodoAggregateRoot Register(CreateTodoCommand command)
        {
            var root = new TodoAggregateRoot(command.AggregateId);

            root.Create(command);
            return(root);
        }
Ejemplo n.º 19
0
        public void Dado_um_Comando_valido_deve_executar()
        {
            var command = new CreateTodoCommand("Titulo da Tarefa", "MatheusMunhoz", DateTime.Now);

            _result = (GenericCommandResult)_handler.Handle(_validCommand);
            Assert.AreEqual(_result.Sucess, true);
        }
Ejemplo n.º 20
0
        public void GivenAValidCommand_ShouldCreateTodo()
        {
            var command = new CreateTodoCommand("Study CSharp", "leandropicoli", DateTime.Now.AddDays(2));
            var result  = (GenericCommandResult)_handler.Handle(command);

            Assert.IsTrue(result.Success);
        }
Ejemplo n.º 21
0
        public void GivenAnInvalidCommand_ShouldNotCreateTodo()
        {
            var command = new CreateTodoCommand("", "", DateTime.Now);
            var result  = (GenericCommandResult)_handler.Handle(command);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 22
0
        public void CreateTodoCommandValid()
        {
            var command = new CreateTodoCommand("title", DateTime.Now, "userbygoogle");
            var result  = (GenericCommandResult)_handler.Handle(command);

            Assert.True(result.Success);
        }
Ejemplo n.º 23
0
        public CreateCommandTests()
        {
            _invalidComand = new CreateTodoCommand("", "", DateTime.Now);
            _invalidComand.Validate();

            _validComand = new CreateTodoCommand("criando tarefa", "douglas", DateTime.Now);
            _validComand.Validate();
        }
Ejemplo n.º 24
0
        public void CreateTodoCommand_TodoInvalido_DeveRetornarErros()
        {
            var command = new CreateTodoCommand("", "", DateTime.Now);

            command.Validate();
            Assert.False(command.Valid);
            //testar quantidade de notificações
        }
Ejemplo n.º 25
0
        public void Dado_um_command_invalido()
        {
            var command = new CreateTodoCommand("", DateTime.Now, "");

            command.Validate();

            Assert.AreEqual(command.Valid, false);
        }
Ejemplo n.º 26
0
 public GenericCommandResult Create(
     [FromBody] CreateTodoCommand command,
     [FromServices] TodoHandler handler
     )
 {
     command.User = "******";
     return((GenericCommandResult)handler.Handle(command));
 }
Ejemplo n.º 27
0
        public void DadoUmComandoInvalidoDeveInterromperExecucao()
        {
            var             command = new CreateTodoCommand("", DateTime.Now, "");
            var             handle  = new TodoHandler(new FakeTodoRepository());
            ResponseCommand result  = (ResponseCommand)handle.Handle(command);

            Assert.AreEqual(result.Success, false);
        }
Ejemplo n.º 28
0
        public void DadoUmComandoValidoDeveCriarTarefa()
        {
            var             command = new CreateTodoCommand("Titulo da Descrição", DateTime.Now, "Usuário");
            var             handle  = new TodoHandler(new FakeTodoRepository());
            ResponseCommand result  = (ResponseCommand)handle.Handle(command);

            Assert.AreEqual(result.Success, true);
        }
Ejemplo n.º 29
0
        public void Dado_um_command_valido()
        {
            var command = new CreateTodoCommand("titulo da tarefa", DateTime.Now, "Matheus Rodrigues");

            command.Validate();

            Assert.AreEqual(command.Valid, true);
        }
Ejemplo n.º 30
0
 public GenericCommandResult Create(
     [FromBody] CreateTodoCommand command,
     [FromServices] TodoHandler handler
     )
 {
     command.User = "******";
     return((GenericCommandResult)handler.Handle(command));
 }