Пример #1
0
        public async Task Task_Delete()
        {
            var connection = TestHelper.GetConnection();
            var options    = TestHelper.GetMockDBOptions(connection);

            try
            {
                using (var context = new AppcentTodoContext(options))
                {
                    var service = new DeleteTodoCommandHandler(context);
                    var command = new DeleteTodoCommand();
                    command.Data = new DeleteTodoRequest
                    {
                        TodoId   = 2,
                        UserName = "******"
                    };
                    var result = await service.Execute(command);

                    Assert.True(result.Result.IsSuccess);
                }

                using (var context = new AppcentTodoContext(options))
                {
                    var task = context.AcTasks.FirstOrDefault(e => e.TaskId == 2);
                    Assert.True(task.IsDeleted);
                }
            }
            finally
            {
                connection.Close();
            }
        }
Пример #2
0
        public async Task Handle_WhenTodoDoesNotExist_ShouldThrowException()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new DeleteTodoCommand {
                UserId = userId, Id = -1
            };

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

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

                    Assert.True(false, "Should throw exception");
                }
                catch (EntityNotFoundException <Todo> ex)
                {
                    // Assert
                    Assert.NotNull(ex);
                }
            }
        }
Пример #3
0
        public async Task Handle_WhenUserHasPermission_ShouldNotThrowException()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new DeleteTodoCommand {
                UserId = userId, Id = 1
            };
            var rpcResponse = new IsUserAllowedResponse {
                IsAllowed = true
            };

            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))
            {
                context.Todos.Add(new Todo {
                    Id = 1, Name = "todo 1"
                });
                context.SaveChanges();

                var sut = new DeleteTodoCommandHandler(context, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient);
                // Act
                var result = await sut.Handle(command, new CancellationToken(false));
            }
        }
Пример #4
0
        public async Task Handle_WhenUserDoesNotHavePermission_ShouldThrowException()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new DeleteTodoCommand {
                UserId = userId, Id = -1
            };
            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 DeleteTodoCommandHandler(context, 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);
                }
            }
        }
Пример #5
0
        public async Task Handle_WhenTodoDeleted_ShouldNotifiyUser()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new DeleteTodoCommand {
                UserId = userId, Id = 1
            };

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

            using (var context = new TodoDbContext(dbOptions))
            {
                context.Todos.Add(new Todo {
                    Id = 1, Name = "todo 1"
                });
                context.SaveChanges();

                var sut = new DeleteTodoCommandHandler(context, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient);

                // Act
                await sut.Handle(command, new CancellationToken(false));
            }

            // Assert
            rpcNotificationsMock.ClientMock.Verify(x => x.SendPush(It.IsAny <SendPushRequest>(), It.IsAny <CallOptions>()), Times.Once);
        }
Пример #6
0
        public async Task Handle_WhenUserHasPermission_ShouldDeleteTodo()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var command = new DeleteTodoCommand {
                UserId = userId, Id = 2
            };

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

            using (var context = new TodoDbContext(dbOptions))
            {
                context.Todos.Add(new Todo {
                    Id = 1, Name = "todo 1"
                });
                context.Todos.Add(new Todo {
                    Id = 2, Name = "todo 2"
                });
                context.SaveChanges();

                var sut = new DeleteTodoCommandHandler(context, rpcNotificationsMock.ServiceClient, rpcPermissionsMock.ServiceClient);
                // Act
                await sut.Handle(command, new CancellationToken(false));

                var detetedTodo = await context.Todos.FirstOrDefaultAsync(t => t.Id == command.Id);

                // Assert
                detetedTodo.ShouldBeNull();
            }
        }
Пример #7
0
        private static void WorkWithCommandsWithoutIoc()
        {
            System.Console.WriteLine("\n-- No IoC --");

            // Line up some commands to execute
            var itemToCreateThenChange = new Todo {
                Caption = "Feed cat", Category = "Etc"
            };
            var cmd1 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "I SHOULD BE DELETED", Category = "Etc"
                }
            };                                                                                                          // id will become 1
            var cmd2 = new CreateTodoCommand {
                Data = itemToCreateThenChange
            };                                                                  // id will become 2
            var cmd3 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Pay bills", Category = "Etc"
                }
            };                                                                                                // id will become 3
            var cmd4 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "I DO NOT PASS THE FILTER", Category = "Uncategorized"
                }
            };                                                                                                                        // id will become 3
            var updateCommand = new UpdateTodoCommand {
                Data = new Todo {
                    Id = itemToCreateThenChange.Id, Category = itemToCreateThenChange.Category, Caption = "Feed BOTH cats"
                }
            };
            var deleteCommand = new DeleteTodoCommand {
                Id = 1
            };                                                    // delete milk

            // Prepare the handlers
            var createHandler = new CreateTodoCommandHandler(new CachedRepository <Todo>());
            var updateHandler = new UpdateTodoCommandHandler(new CachedRepository <Todo>());
            var deleteHandler = new DeleteTodoCommandHandler(new CachedRepository <Todo>());
            // Example using a decorator. We're instantiating the decorator manually here but the idea is to it with IoC.
            // This decorator outputs some logging to to Console.Out.
            var decorating_deleteHandler = new LoggingCommandHandlerDecorator <DeleteTodoCommand>(deleteHandler, (ILogger) new ConsoleLogger());

            // Create something then queue it for deletion
            createHandler.Handle(cmd1).Wait();
            deleteCommand.Id = cmd1.Output;

            Task.WaitAll(
                createHandler.Handle(cmd2)
                , createHandler.Handle(cmd3)
                , createHandler.Handle(cmd4)
                );

            updateHandler.Handle(updateCommand).Wait();
            decorating_deleteHandler.Handle(deleteCommand).Wait();

            // Example using a queryhandler to deliver results
            var qhandler = new FindTodosByCategoryQueryHandler(new CachedRepository <Todo>());
            var query    = new FindTodosByCategoryQuery {
                Category = "Etc"
            };
            var queryResult = qhandler.Handle(query);

            // Only two items should show up since we deleted one, and one doesn't match the query.
            PrintResults(queryResult.Result);
        }
 public DeleteTodoCommandHandlerTests()
 {
     fixture            = new Fixture();
     todoRepositoryMock = new Mock <ITodoRepository>();
     sut = new DeleteTodoCommandHandler(todoRepositoryMock.Object);
 }