public void A_merge_which_is_allowed_succeeds()
        {
            var aggregateId = Guid.NewGuid();

            //Set up existing aggregate state - will be at aggregate version 3
            var existingAggregateEvents = new List<Event>();
            existingAggregateEvents.Add(new InventoryItemCreated(aggregateId, "A Name"));
            existingAggregateEvents.Add(new InventoryItemReceivedIntoStock(aggregateId, 100));
            existingAggregateEvents.Add(new InventoryItemRenamed(aggregateId, "Some other name"));

            _eventStore.SaveEvents(aggregateId, existingAggregateEvents, 0);

            //Command has an earlier expected version than the current aggregate version
            var command = new DeactivateInventoryItem(aggregateId, 1);

            var specificCommandHandler = new DeactivateInventoryItemHandler(_repository);
            var mergingChain = new MergingContextCommitHandler<DeactivateInventoryItem>(specificCommandHandler,
                                                                                        _eventStore, _allowedMerges);

            mergingChain.Handle(command, new CommandExecutionContext());

            var newAggregate = _repository.GetById<InventoryItem>(aggregateId);
            Assert.AreEqual(4, newAggregate.AggregateVersion);
        }
        public void A_merge_which_is_not_allowed_throws_a_RealConcurrencyException()
        {
            var aggregateId = Guid.NewGuid();

            //Set up existing aggregate state - will be at aggregate version 4
            var existingAggregateEvents = new List<Event>();
            existingAggregateEvents.Add(new InventoryItemCreated(aggregateId, "A Name"));
            existingAggregateEvents.Add(new InventoryItemReceivedIntoStock(aggregateId, 100));
            existingAggregateEvents.Add(new InventoryItemDeactivated(aggregateId));

            _eventStore.SaveEvents(aggregateId, existingAggregateEvents, 0);

            //Command has an earlier expected version and conflicts with changes between expected and actual version
            var command = new RenameInventoryItem(aggregateId, "A new name", 1);

            var specificCommandHandler = new RenameInventoryItemHandler(_repository);
            var mergingChain = new MergingContextCommitHandler<RenameInventoryItem>(specificCommandHandler, _eventStore,
                                                                                    _allowedMerges);

            Exception caughtException = new ThereWasNoExceptionButOneWasExpectedException();
            try
            {
                mergingChain.Handle(command, new CommandExecutionContext());
            } catch (Exception e)
            {
                caughtException = e;
            }

            Assert.That(caughtException is RealConcurrencyException);
        }