public async Task build_correct_stream_id_when_handle_generic_command() { var regionId = "x"; var context = "ctx"; var aggregateRootName = "ar"; var aggregateRootId = "1"; var mockCommand = new Mock <ICommand>(); var mockCommandValidationResult = new Mock <ICommandValidationResult>(); var mockStreamIdBuilder = new Mock <IStreamIdBuilder>(); var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, null, mockStreamIdBuilder.Object, null, null); var ar = new TestAggregateRoot(dependencies, context, aggregateRootName); mockCommandValidationResult.Setup(x => x.IsValid).Returns(true);; mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object); mockCommand.Setup(x => x.GetRegionId()).Returns(regionId); mockCommand.Setup(x => x.GetAggregateRootId()).Returns(aggregateRootId); try { await ar.HandleGenericCommandAsync(mockCommand.Object, CancellationToken.None); } catch (Exception) { // Ignore exception. } mockStreamIdBuilder.Verify(x => x.Build(regionId, context, aggregateRootName, aggregateRootId)); }
public async Task return_false_when_concurrency_conflict_when_commit_events() { var mockEvent = new Mock <IBusinessEvent>(); var events = new List <IBusinessEvent>() { mockEvent.Object }.ToImmutableList(); var eventType = mockEvent.Object.GetType().Name; var dataText = "{}"; var dataBytes = Encoding.Unicode.GetBytes(dataText); var unresolvedEvent = new UnresolvedBusinessEvent(eventType, dataBytes); var mockResolver = new Mock <IBusinessEventResolver>(); var mockState = new Mock <IAggregateRootState>(); var mockStreamClientFactory = new Mock <IStreamClientFactory>(); var mockStreamClient = new Mock <IStreamClient>(); var ar = new TestAggregateRoot(new AggregateRootDependencies <IAggregateRootState>(null, null, null, mockStreamClientFactory.Object, mockResolver.Object), null, null); mockResolver.Setup(x => x.CanUnresolve(mockEvent.Object)).Returns(true); mockResolver.Setup(x => x.Unresolve(mockEvent.Object)).Returns(unresolvedEvent); mockStreamClientFactory.Setup(x => x.Create(It.IsAny <string>())).Returns(mockStreamClient.Object); mockStreamClient.Setup(x => x.CommitEventsToStreamAsync(It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <IEnumerable <CommitEvent> >())).ReturnsAsync(CommitResult.ConcurrencyConflict); var result = await ar.TestCommitEventsAsync(events, null, null, null); Assert.False(result); }
public async Task SaveAggregateAsync_save_new_aggregate() { var aggregate = new TestAggregateRoot() { Id = ObjectId.Parse(_aggregateId) }; aggregate.ApplyChange(new AnotherTestEventV1() { Id = ObjectId.GenerateNewId(), FirstName = "Joe", SurName = "Bloggs", IsValid = true }); await _aggregateRepository.SaveAggregateAsync(aggregate); var events = (await _eventStore.GetDomainEventsAsync(_aggregateId)).ToList(); Assert.That(events.Count, Is.EqualTo(1)); var lastEvent = events.Last(); Assert.That(lastEvent.AggregateId, Is.EqualTo(_aggregateId)); Assert.That(lastEvent.Type, Is.EqualTo("AnotherTestEvent")); Assert.That(lastEvent.Version, Is.EqualTo(1)); Assert.That(lastEvent.Commit, Is.EqualTo(1)); Assert.That(lastEvent.Index, Is.EqualTo(1)); }
public async Task Handle(IUserSession userIssuingCommand, CreateTest command) { var test = new TestAggregateRoot( Guid.NewGuid(), command.Name); await _repository.SaveChanges(test); }
private DomainEvent ApplyNewEvent(TestAggregateRoot aggregateRoot) { var domainEvent = new TestDomainEvent(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent); return(domainEvent); }
public void ShouldAppendDomainEventsToEndOfStream() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); IAggregateRoot explicitCast = aggregateRoot; // Apply 3 domain events. aggregateRoot.ChangeMe(Guid.NewGuid()); aggregateRoot.ChangeMe(Guid.NewGuid()); aggregateRoot.ChangeMe(Guid.NewGuid()); DomainEventStream stream1 = (DomainEventStream)explicitCast.GetDomainEventsMarkedForCommit(); // Clear domain events. explicitCast.MarkDomainEventsAsCommitted(); // Apply 3 domain events. aggregateRoot.ChangeMe(Guid.NewGuid()); aggregateRoot.ChangeMe(Guid.NewGuid()); aggregateRoot.ChangeMe(Guid.NewGuid()); DomainEventStream stream2 = (DomainEventStream)explicitCast.GetDomainEventsMarkedForCommit(); // Append 2 streams. DomainEventStream result = stream1.AppendDomainEventStream(stream2); result.Should().HaveCount(6); }
public async Task handle_command_should_create_state() { var regionId = "x"; var streamId = "sId"; var mockCommand = new Mock <ICommand>(); var mockCommandValidationResult = new Mock <ICommandValidationResult>(); var mockStreamIdBuilder = new Mock <IStreamIdBuilder>(); var mockStateRepo = new Mock <IAggregateRootStateRepo <IAggregateRootState> >(); var cancelSource = new CancellationTokenSource(); var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, mockStateRepo.Object, mockStreamIdBuilder.Object, null, null); var ar = new TestAggregateRoot(dependencies, null, null); mockCommandValidationResult.Setup(x => x.IsValid).Returns(true);; mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object); mockCommand.Setup(x => x.GetRegionId()).Returns(regionId); mockCommand.Setup(x => x.GetAggregateRootId()).Returns(string.Empty); mockStreamIdBuilder.Setup(x => x.Build(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(streamId); try { await ar.HandleGenericCommandAsync(mockCommand.Object, cancelSource.Token); } catch (Exception) { // Ignore exception. } mockStateRepo.Verify(x => x.LoadAsync(regionId, streamId, cancelSource.Token)); }
public void ResolvedEventHandlersAreExecutedInCorrectOrder() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent1); TestAggregateRoot2 aggregateRoot2 = new TestAggregateRoot2(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent2); sut.ApplyChanges(aggregateRoot, aggregateRoot2); Received.InOrder(() => { unitOfWorkDomainEventHandler1.Execute(domainEvent1, unitOfWork); unitOfWorkDomainEventHandler2.Execute(domainEvent1, unitOfWork); unitOfWorkDomainEventHandler1.Execute(domainEvent2, unitOfWork); unitOfWorkDomainEventHandler2.Execute(domainEvent2, unitOfWork); unitOfWork.SaveChanges(); domainEventHandler3.Execute(domainEvent1); domainEventHandler4.Execute(domainEvent1); domainEventHandler3.Execute(domainEvent2); domainEventHandler4.Execute(domainEvent2); }); }
public void MultipleChangesInUnitOfWorkAreSavedOnce() { TestAggregateRoot aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot1.ApplyEvent(domainEvent); TestDomainEvent domainEvent2 = new TestDomainEvent(Guid.NewGuid()); unitOfWorkEventHandlerResolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >().Returns(unitOfWorkDomainEventHandlers); TestAggregateRoot aggregateRoot2 = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot2.ApplyEvent(domainEvent2); sut.ApplyChanges(new List <TestAggregateRoot> { aggregateRoot1, aggregateRoot2 }); Received.InOrder(() => { unitOfWorkDomainEventHandler1.Execute(domainEvent, unitOfWork); unitOfWorkDomainEventHandler1.Execute(domainEvent2, unitOfWork); unitOfWork.SaveChanges(); }); }
public void OnlyUnitOfWorkEventsAreHandledIfCommitFails() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent); unitOfWork .When(u => u.SaveChanges()) .Do(callInfo => { throw new InvalidOperationException(); }); try { sut.ApplyChanges(aggregateRoot); Assert.Fail("No exception thrown."); } catch (InvalidOperationException) { } unitOfWorkDomainEventHandler1.Received().Execute(domainEvent, unitOfWork); unitOfWorkDomainEventHandler2.Received().Execute(domainEvent, unitOfWork); unitOfWork.Received().SaveChanges(); domainEventHandler3.DidNotReceive().Execute(domainEvent); domainEventHandler4.DidNotReceive().Execute(domainEvent); }
public void Aggregate_reconstruct_a_valid() { var be = BusinessEntity.From(EntityTestId.GetNext(), Version.New()); var agg = TestAggregateRoot.ReconstructFrom(be); Assert.True(agg.ValidationResults.IsValid); }
public void OnlyUnitOfWorkEventsAreHandledIfCommitFails() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent); unitOfWork .When(u => u.SaveChanges()) .Do(callInfo => { throw new InvalidOperationException(); }); try { sut.ApplyChangesAsync(aggregateRoot).Wait(); Assert.Fail("No exception thrown."); } catch (AggregateException e) { if (e.InnerException == null) { Assert.Fail("Expected inner exception of type AggregateException. No inner exception received."); } if (!(e.InnerException is InvalidOperationException)) { Assert.Fail("Expected inner exception of type AggregateException. Received '{0}'.", e.InnerException); } } unitOfWorkDomainEventHandler1.Received().ExecuteAsync(domainEvent, unitOfWork); unitOfWorkDomainEventHandler2.Received().ExecuteAsync(domainEvent, unitOfWork); unitOfWork.Received().SaveChanges(); domainEventHandler3.DidNotReceive().ExecuteAsync(domainEvent); domainEventHandler4.DidNotReceive().ExecuteAsync(domainEvent); }
public async Task return_duplicate_command_id_validation_error_when_handle_generic_command() { var commandId = "cId"; var mockCommand = new Mock <ICommand>(); var mockCommandValidationResult = new Mock <ICommandValidationResult>(); var mockStreamIdBuilder = new Mock <IStreamIdBuilder>(); var mockStreamClientFactory = new Mock <IStreamClientFactory>(); var mockStreamClient = new Mock <IStreamClient>(); var mockStateRepo = new Mock <IAggregateRootStateRepo <IAggregateRootState> >(); var mockState = new Mock <IAggregateRootState>(); var cancelSource = new CancellationTokenSource(); var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, mockStateRepo.Object, mockStreamIdBuilder.Object, mockStreamClientFactory.Object, null); var ar = new TestAggregateRoot(dependencies, null, null); mockCommandValidationResult.Setup(x => x.IsValid).Returns(true); mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object); mockCommand.Setup(x => x.GetRegionId()).Returns(string.Empty); mockCommand.Setup(x => x.GetAggregateRootId()).Returns(string.Empty); mockCommand.Setup(x => x.GetCommandId()).Returns(commandId); mockStreamIdBuilder.Setup(x => x.Build(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(string.Empty); mockStreamClientFactory.Setup(x => x.Create(It.IsAny <string>())).Returns(mockStreamClient.Object); mockStreamClient.Setup(x => x.FirstPositionInStream).Returns(0); mockStateRepo.Setup(x => x.LoadAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockState.Object); mockState.Setup(x => x.IsCausalIdInHistoryAsync(commandId)).ReturnsAsync(true); var result = await ar.HandleGenericCommandAsync(mockCommand.Object, cancelSource.Token); Assert.False(result.IsSuccess); Assert.Contains("Duplicate command id.", result.Errors); }
public void AggregateRoot_ConstructorShouldAssignValues() { var guid = Guid.NewGuid(); var aggregateBus = new Mock<IAggregateBus>(); var testAggreageRoot = new TestAggregateRoot(aggregateBus.Object, guid); testAggreageRoot.Id.Should().Be(guid); }
public override void SetUp() { base.SetUp(); var id = Guid.NewGuid(); this.Aggregate = new TestAggregateRoot(); this.Aggregate.Execute(new TestCommand1{ AggregateId = id, Value1 = "1", Value2 = 1, }); }
public void NoEventsExecutedIfNoAppliedEventsExist() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); sut.ApplyChanges(aggregateRoot); Assert.AreEqual(0, sut.EventBroker.ExecutedEvents.Count); }
public async Task do_nothing_with_empty_events_and_return_true_when_commit_events() { var ar = new TestAggregateRoot(new AggregateRootDependencies <IAggregateRootState>(null, null, null, null, null), null, null); // Will throw null reference if attempts to process events because dependencies are null. var result = await ar.TestCommitEventsAsync(ImmutableList <IBusinessEvent> .Empty, null, null, null); Assert.True(result); }
public async Task rethrow_exceptions_when_handle_generic_command() { var mockCommand = new Mock <ICommand>(); var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, null, null, null, null); var ar = new TestAggregateRoot(dependencies, null, null); mockCommand.Setup(x => x.ValidateSemantics()).Throws(new TestException()); await Assert.ThrowsAsync <TestException>(() => ar.HandleGenericCommandAsync(mockCommand.Object, CancellationToken.None)); }
public void Aggregate_Should_ApplyChange_For_NewEvents() { var aggregate = new TestAggregateRoot(); aggregate.ApplyChange(new TestEventV1()); aggregate.ApplyChange(new AnotherTestEventV1()); Assert.That(aggregate.TestEventFired, Is.True); Assert.That(aggregate.AnotherTestEventFired, Is.True); }
public void AddEvent_SameEventTypeWithoutAllowMultiple_AddedOnlyOnceToCollection() { var entity = new TestAggregateRoot(); entity.AddEvent <TestDomainEventOne>(false); entity.AddEvent <TestDomainEventTwo>(false); entity.AddEvent <TestDomainEventOne>(false); entity.DomainEvents.Count().Should().Be(2); }
public void ShouldApplyDomainEvent() { //Given var aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); Guid changeId = Guid.NewGuid(); aggregateRoot.ChangeMe(changeId); aggregateRoot.HasHandledChangeId(changeId).Should().BeTrue(); }
public void ChangesInUnitOfWorkAreSaved() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent); sut.ApplyChanges(aggregateRoot); unitOfWork.Received().SaveChanges(); }
public async Task SavedChangesAsync_Success_Should_ThrowException_Detail_AggregateRootAttached() { var aggregateRoot = new TestAggregateRoot(null); _Context.Add(aggregateRoot); Func <Task> act = () => _Context.SaveChangesAsync(); (await act.Should().ThrowExactlyAsync <ValidationException>()) .Subject.Should().Contain(e => e.Message == "Test string es nulo"); }
public override void SetUp() { base.SetUp(); var id = Guid.NewGuid(); this.Aggregate = new TestAggregateRoot(); this.Aggregate.Execute(new TestCommand1 { AggregateId = id, Value1 = "1", Value2 = 1, }); }
public async Task ShouldSaveAggregateRoot() { var aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); var repository = new InMemoryAggregateRootRepository <TestAggregateRoot>(); await repository.SaveAsync(aggregateRoot); var result = await repository.GetByIdAsync(aggregateRoot.Id); result.Should().Be(aggregateRoot); }
public void ResolvedUnitOfWorkEventHandlersAreExecutedInCorrectOrder() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); aggregateRoot.ApplyEvent(domainEvent); sut.ApplyChanges(aggregateRoot); unitOfWorkDomainEventHandler1.Received().Execute(domainEvent, unitOfWork); unitOfWorkDomainEventHandler2.Received().Execute(domainEvent, unitOfWork); }
public void AddDomainEvent() { var ar = new TestAggregateRoot(); ar.DomainEvents.ShouldBeEmpty(); ar.AddDomainEvent(1); ar.DomainEvents.ShouldContain(1); ar.DomainEvents.ShouldHaveSingleItem(); ar.ClearDomainEvents(); ar.DomainEvents.ShouldBeEmpty(); }
public void AllAppliedEventsAreExecuted() { TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); var domainEvent1 = ApplyNewEvent(aggregateRoot); var domainEvent2 = ApplyNewEvent(aggregateRoot); sut.ApplyChanges(aggregateRoot); Assert.AreEqual(2, sut.EventBroker.ExecutedEvents.Count); Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent1)); Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent2)); }
public void AggregateRoot_PublishAndApplyEventAsyncShouldCallApplyEventIfExists() { var guid = Guid.NewGuid(); var @event = new TestEvent2(); var aggregateBus = new Mock<IAggregateBus>(); aggregateBus.Setup(x => x.PublishAsync<TestAggregateRoot, TestEvent2>(guid, @event)).Returns(Task.FromResult<object>(null)).Verifiable(); var testAggregateRoot = new TestAggregateRoot(aggregateBus.Object, guid); testAggregateRoot.Awaiting(x => x.PublishAndApplyEventAsync<TestAggregateRoot, TestEvent>(new TestEvent())).ShouldNotThrow(); testAggregateRoot.WasApplyEventCalled.Should().BeTrue(); }
public void AggregateRoot_PublishAndApplyEventAsync2ShouldThrowExceptionIfApplyEventMethodDoesntExist() { var guid = Guid.NewGuid(); var @event = new TestEvent2(); var aggregateBus = new Mock<IAggregateBus>(); aggregateBus.Setup(x => x.PublishAsync<TestAggregateRoot, TestEvent2>(guid, @event)).Returns(Task.FromResult<object>(null)).Verifiable(); var testAggregateRoot = new TestAggregateRoot(aggregateBus.Object, guid); testAggregateRoot.Awaiting(x => x.PublishAndApplyEventAsync<TestEvent2>(@event)).ShouldThrow<ApplicationException>(); testAggregateRoot.WasApplyEventCalled.Should().BeFalse(); }
public void ClearMessages_WithSomePendingMessages_MessagesCleared() { var entity = new TestAggregateRoot(); entity.AddEvent <TestDomainEventOne>(); entity.AddEvent <TestDomainEventTwo>(); entity.AddEvent <TestDomainEventOne>(); entity.ClearMessages(); entity.DomainEvents.Should().NotBeNull(); entity.DomainEvents.Should().BeEmpty(); }
public void ShouldChangeUpdatedPropertyWhenADomainEventIsAppliedByDefault() { // Given. var aggregateRoot = new TestAggregateRoot(Guid.NewGuid()); DateTime aggregateRootUpdated = aggregateRoot.Updated; // When. aggregateRoot.ChangeMe(Guid.NewGuid()); // Then. aggregateRoot.Updated.Should().NotBe(aggregateRootUpdated); }
public void AddEvent_EventType_AddedToCollection() { var entity = new TestAggregateRoot(); entity.AddEvent <TestDomainEventOne>(); entity.AddEvent <TestDomainEventTwo>(); entity.AddEvent <TestDomainEventOne>(); entity.DomainEvents.Should().NotBeNull(); entity.DomainEvents.Count().Should().Be(3); entity.DomainEvents.Should().OnlyContain(e => e.Source == entity); }
public async Task DeleteAsync_TwoGenericVersion_Success_Should_HaveZeroCountInTrackedEntities() { const int expCount = 0; IRepositoryAsync <TestAggregateRoot, Guid> repository = new TestAggregateRootRepository2(_Context); var aggregateRoot = new TestAggregateRoot("testing"); var entry = await repository.DeleteAsync(aggregateRoot.Id); entry.Should().BeNull(); _Context.ChangeTracker.Entries <TestAggregateRoot>() .Should().HaveCount(expCount); }
public override void SetUp() { base.SetUp(); var id = Guid.NewGuid(); this.Aggregate = new TestAggregateRoot(); for (int i = 0; i < 1000; i++) { this.Aggregate.Execute(new TestCommand1{ AggregateId = id, Value1 = "1", Value2 = i, }); } this.Repository.Save(this.Aggregate); }
public override void SetUp() { base.SetUp(); this.Aggregate = new TestAggregateRoot(); }