public async Task When_getting_all_ships()
        {
            //arrange
            using (var contextFactory = new FakeShipRegistryContextFactory(_options))
            {
                var uow        = contextFactory.Create();
                var repository = new ShipRepositoryAsync(uow);
                var shipOne    = new Ship(new Id(), new ShipName("Majestic"), ShipType.Container, new Capacity(50000), new Id());
                var shipTwo    = new Ship(new Id(), new ShipName("Royal"), ShipType.Container, new Capacity(80000), new Id());

                await repository.AddAsync(shipOne);

                await repository.AddAsync(shipTwo);

                var query = new ShipsAllQuery();

                var queryHandler = new ShipsAllQueryHandlerAsync(contextFactory);

                //act
                var result = await queryHandler.ExecuteAsync(query);

                //asert
                Assert.That(2, Is.EqualTo(result.Ships.Length));
            }
        }
        public async Task When_getting_a_ship()
        {
            //arrange
            using (var contextFactory = new FakeShipRegistryContextFactory(_options))
            {
                var uow        = contextFactory.Create();
                var repository = new ShipRepositoryAsync(uow);
                var ship       = new Ship(new Id(), new ShipName("Majestic"), ShipType.Container, new Capacity(50000), new Id());

                await repository.AddAsync(ship);

                var query = new ShipByIdQuery(ship.Id);

                var queryHandler = new ShipByIdQueryHandlerAsync(contextFactory);

                //act
                var foundShip = await queryHandler.ExecuteAsync(query);

                //asert
                Assert.That(foundShip.Id, Is.EqualTo(ship.Id.Value));
                Assert.That(foundShip.Capacity, Is.EqualTo(ship.Capacity.Value));
                Assert.That(foundShip.ShipName, Is.EqualTo(ship.ShipName.ToString()));
                Assert.That(foundShip.ShipType, Is.EqualTo(ship.ShipType.ToString()));
                Assert.That(foundShip.ShippingLineId, Is.EqualTo(ship.ShippingLineId.Value));
            }
        }
        public async Task When_removing_a_ship_registration()
        {
            //arrange
            using (var contextFactory = new FakeShipRegistryContextFactory(_options))
            {
                var uow        = contextFactory.Create();
                var repository = new ShipRepositoryAsync(uow);
                var ship       = new Ship(new Id(), new ShipName("Majestic"), ShipType.Container, new Capacity(50000), new Id());

                await repository.AddAsync(ship);

                var commandProcessor = new FakeCommandProcessor();

                var command = new RemoveShipCommand(ship.Id);

                var handler = new RemoveShipHandlerAsync(contextFactory, commandProcessor);

                //act
                await handler.HandleAsync(command);

                //assert
                var removedShip = await uow.Ships.SingleOrDefaultAsync(s => s.Id == ship.Id);

                Assert.That(removedShip, Is.Null);

                var domainEvent = commandProcessor.Messages.SingleOrDefault(m => m.Action == Action.Post);
                Assert.That(domainEvent, Is.Not.Null);

                var shipRemovedEvent = (ShipRemovedEvent)domainEvent.Message;
                Assert.That(shipRemovedEvent.ShipRemoved, Is.EqualTo(ship.Id));
            }
        }
        public override async Task <AddShipCommand> HandleAsync(AddShipCommand command, CancellationToken cancellationToken = new CancellationToken())
        {
            using (var uow = _contextFactory.Create())
            {
                var repo = new ShipRepositoryAsync(uow);
                var ship = new Ship(new Id(command.Id), command.Name, command.Type, command.Capacity, command.ShippingLine);

                await repo.AddAsync(ship);

                await _commandProcessor.PostAsync(
                    new NewShipAddedEvent(ship.Id, ship.ShipType, ship.ShipName, ship.Capacity, ship.ShippingLineId),
                    cancellationToken : cancellationToken
                    );
            }


            return(await base.HandleAsync(command, cancellationToken));
        }
        public async Task When_updating_an_existing_ship_name()
        {
            //arrange
            using (var contextFactory = new FakeShipRegistryContextFactory(_options))
            {
                var uow        = contextFactory.Create();
                var repository = new ShipRepositoryAsync(uow);
                var ship       = new Ship(new Id(), new ShipName("Majestic"), ShipType.Container, new Capacity(50000), new Id());

                await repository.AddAsync(ship);

                var commandProcessor = new FakeCommandProcessor();

                var command = new UpdateShipNameCommand(ship.Id, name: new ShipName("Toronto Star"));

                var handler = new UpdateShipNameHandlerAsync(contextFactory, commandProcessor);

                //act
                await handler.HandleAsync(command);

                //assert
                var updatedShip = await uow.Ships.SingleOrDefaultAsync(s => s.Id == ship.Id);

                Assert.That(updatedShip, Is.Not.Null);
                Assert.That(updatedShip.ShipName, Is.EqualTo(command.Name));
                Assert.That(updatedShip.Version, Is.EqualTo(1));
                Assert.That(ship.Version, Is.EqualTo(1));

                var domainEvent = commandProcessor.Messages.SingleOrDefault(m => m.Action == Action.Post);
                Assert.That(domainEvent, Is.Not.Null);

                var updatedNameMessage = (ShipNameUpdatedEvent)domainEvent.Message;
                Assert.That(updatedNameMessage.ShipId, Is.EqualTo(updatedShip.Id));
                Assert.That(updatedNameMessage.ShipName, Is.EqualTo(updatedShip.ShipName));
                Assert.That(updatedNameMessage.Version, Is.EqualTo(1));
            }
        }