public void A_call_not_requiring_a_merge_commits_successfully() { var aggregateId = Guid.NewGuid(); //Set up existing aggregate state - will be 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 expected version which matches the current aggregate version var command = new RenameInventoryItem(aggregateId, "Another new name", 3); var specificCommandHandler = new RenameInventoryItemHandler(_repository); var mergingChain = new MergingContextCommitHandler<RenameInventoryItem>(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); }