Beispiel #1
0
        public void FixtureArrangerWithEvents_CanBeReplayed()
        {
            using (var testKit = new TestKit(_config, "fixture-tests-3"))
            {
                var fixture           = new AggregateFixture <TestAggregate, TestAggregateId>(testKit);
                var aggregateIdentity = TestAggregateId.New;
                var events            = new List <IAggregateEvent <TestAggregate, TestAggregateId> >();
                events.Add(new TestCreatedEvent(aggregateIdentity));
                events.AddRange(Enumerable.Range(0, 10).Select(x => new TestAddedEvent(new Test(TestId.New))));
                var journal       = Persistence.Instance.Apply(testKit.Sys).JournalFor(null);
                var receiverProbe = testKit.CreateTestProbe("journal-probe");
                fixture
                .For(aggregateIdentity)
                .Given(events.ToArray());


                journal.Tell(new ReplayMessages(1, long.MaxValue, long.MaxValue, aggregateIdentity.ToString(), receiverProbe.Ref));


                var from = 1;
                foreach (var _ in events)
                {
                    var index = from;
                    receiverProbe.ExpectMsg <ReplayedMessage>(x =>
                                                              x.Persistent.SequenceNr == index &&
                                                              x.Persistent.Payload is ICommittedEvent <TestAggregate, TestAggregateId, IAggregateEvent <TestAggregate, TestAggregateId> >);

                    from++;
                }

                receiverProbe.ExpectMsg <RecoverySuccess>();
            }
        }
        public void TestEventMultipleEmitSourcing_AfterManyMultiCommand_TestStateSignalled()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;

            var command = new CreateTestCommand(aggregateId).WithSourceId(commandId);

            var test         = new TestEntity(TestId.New);
            var testSourceId = SourceId.New;
            var testCommand  = new AddFourTestsCommand(aggregateId, test).WithSourceId(testSourceId);

            var poisonCommand = new PoisonTestAggregateCommand(aggregateId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command, testCommand, poisonCommand)
            .ThenExpectDomainEvent <TestCreatedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectState(
                x => x.TestCollection.Count == 4);
        }
Beispiel #3
0
        public void FixtureArrangerWithSnapshot_CanBeLoaded()
        {
            using (var testKit = new TestKit(_config, "fixture-tests-4"))
            {
                var fixture           = new AggregateFixture <TestAggregate, TestAggregateId>(testKit);
                var aggregateIdentity = TestAggregateId.New;
                var snapshot          = new TestAggregateSnapshot(Enumerable.Range(0, 10)
                                                                  .Select(x => new TestAggregateSnapshot.TestModel(Guid.NewGuid())).ToList());
                var snapshotStore          = Persistence.Instance.Apply(testKit.Sys).SnapshotStoreFor(null);
                var receiverProbe          = testKit.CreateTestProbe("snapshot-probe");
                var snapshotSequenceNumber = 1L;
                fixture
                .For(aggregateIdentity)
                .Given(snapshot, snapshotSequenceNumber);

                snapshotStore.Tell(new LoadSnapshot(aggregateIdentity.Value, new SnapshotSelectionCriteria(long.MaxValue, DateTime.MaxValue), long.MaxValue), receiverProbe.Ref);

                receiverProbe.ExpectMsg <LoadSnapshotResult>(x =>
                                                             x.Snapshot.Snapshot is ComittedSnapshot <TestAggregate, TestAggregateId, IAggregateSnapshot <TestAggregate, TestAggregateId> > &&
                                                             x.Snapshot.Metadata.SequenceNr == snapshotSequenceNumber &&
                                                             x.Snapshot.Metadata.PersistenceId == aggregateIdentity.Value &&
                                                             x.Snapshot.Snapshot
                                                             .As <ComittedSnapshot <TestAggregate, TestAggregateId, IAggregateSnapshot <TestAggregate, TestAggregateId> > >().AggregateSnapshot
                                                             .As <TestAggregateSnapshot>().Tests.Count == snapshot.Tests.Count &&
                                                             x.ToSequenceNr == long.MaxValue);
            }
        }
        public void TestCommandTwice_AfterAggregateCreation_TestEventEmitted()
        {
            var aggregateId    = TestAggregateId.New;
            var commandId      = SourceId.New;
            var command        = new CreateTestCommand(aggregateId).WithSourceId(commandId);
            var testId         = TestId.New;
            var test           = new TestEntity(testId);
            var nextCommandId  = SourceId.New;
            var nextCommand    = new AddTestCommand(aggregateId, test).WithSourceId(nextCommandId);
            var test2Id        = TestId.New;
            var test2          = new TestEntity(test2Id);
            var nextCommandId2 = SourceId.New;
            var nextCommand2   = new AddTestCommand(aggregateId, test2).WithSourceId(nextCommandId2);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command, nextCommand, nextCommand2)
            .ThenExpectDomainEvent <TestCreatedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>(
                x => x.AggregateEvent.Test.Equals(test))
            .ThenExpectDomainEvent <TestAddedEvent>(
                x => x.AggregateEvent.Test.Equals(test2));
        }
Beispiel #5
0
        public void With_Test_Kit_InitialSnapshot_After_TestCreatedEventEmitted()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;

            fixture
            .For(aggregateId)
            .Given(new TestAggregateSnapshot(Enumerable.Range(0, 10).Select(x => new TestAggregateSnapshot.TestModel(Guid.NewGuid())).ToList()), 10)
            .When(new PublishTestStateCommand(aggregateId))
            .ThenExpect <TestStateSignalEvent>(x => x.AggregateState.FromHydration);
        }
Beispiel #6
0
        public void With_Test_Kit_InitialState_AfterAggregateCreation_TestCreatedEventEmitted()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;

            fixture
            .For(aggregateId)
            .GivenNothing()
            .When(new CreateTestCommand(aggregateId))
            .ThenExpect <TestCreatedEvent>(x => x.TestAggregateId == aggregateId);
        }
Beispiel #7
0
        public void With_Test_Kit_InitialCommand_AfterAggregateCreation_TestCreatedEventEmitted()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;
            var testId      = TestId.New;

            fixture
            .For(aggregateId)
            .Given(new CreateTestCommand(aggregateId))
            .When(new AddTestCommand(aggregateId, new Test(testId)))
            .ThenExpect <TestAddedEvent>(x => x.Test.Id == testId);
        }
        public void InitialState_TestingForbidden3()
        {
            var aggregateId = TestAggregateId.New;
            var command     = new TestForbiddenCommand(aggregateId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenUser("test", "Test User", new [] { "Admin" })
            .When(command)
            .ThenExpectResult(r => r.Should().BeEquivalentTo(Result.Success).And.Ok());
        }
        public void InitialState_TestingForbidden2()
        {
            var aggregateId = TestAggregateId.New;
            var command     = new TestForbiddenCommand2(aggregateId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectResult(r => r.Should().BeOfType <ForbiddenResult>().And.Ok());
        }
Beispiel #10
0
        public void With_Test_Kit_TestCommand_AfterAggregateCreation_TestEventEmitted()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;
            var testId      = TestId.New;
            var test        = new Test(testId);

            fixture
            .For(aggregateId)
            .GivenNothing()
            .When(new CreateTestCommand(aggregateId), new AddTestCommand(aggregateId, test))
            .ThenExpect <TestAddedEvent>(x => x.Test.Equals(test));
        }
Beispiel #11
0
        public void FixtureArrangerWithAggregateManager_CreatesAggregateManagerRef()
        {
            using (var testKit = new TestKit(_config, "fixture-tests-2"))
            {
                var fixture           = new AggregateFixture <TestAggregate, TestAggregateId>(testKit);
                var aggregateIdentity = TestAggregateId.New;

                fixture.Using(() => new TestAggregateManager(), aggregateIdentity);

                fixture.AggregateRef.Path.Name.Should().Be("aggregate-manager");
                fixture.AggregateId.Should().Be(aggregateIdentity);
            }
        }
        public void FixtureArrangerWithIdentity_CreatesAggregateRef()
        {
            //TODO - https://dev.azure.com/lutando/EventFly/_workitems/edit/23/
            using var testKit = new TestKit(Configuration.Default, "fixture-tests-1");
            var fixture           = new AggregateFixture <TestAggregate, TestAggregateId>(testKit);
            var aggregateIdentity = TestAggregateId.New;

            fixture
            .For(aggregateIdentity)
            .GivenNothing();

            fixture.AggregateId.Should().Be(aggregateIdentity);
        }
Beispiel #13
0
        public void With_Test_Kit_InitialState_AfterAggregateCreation_TestStateSignalled()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;

            fixture
            .For(aggregateId)
            .GivenNothing()
            .When(new CreateTestCommand(aggregateId), new PublishTestStateCommand(aggregateId))
            .ThenExpectDomainEvent <TestStateSignalEvent>(
                x => x.AggregateEvent.LastSequenceNr == 1 &&
                x.AggregateEvent.Version == 1 &&
                x.AggregateEvent.AggregateState.TestCollection.Count == 0);
        }
Beispiel #14
0
        public void InitialState_TestingBadCommand_SuccessResultReplied()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;
            var command     = new BadCommand(aggregateId).WithSourceId(commandId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectResult(r => !r.IsSuccess);
        }
Beispiel #15
0
        public void SendingCommand_ToAggregateRoot_ShouldReplyWithProperMessage()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;
            var command     = new CreateTestCommand(aggregateId).WithSourceId(commandId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectResult(result => result.IsSuccess);
        }
Beispiel #16
0
        public void InitialState_AfterAggregateCreation_TestStateSignalled()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;
            var command     = new CreateTestCommand(aggregateId).WithSourceId(commandId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectDomainEvent <TestCreatedEvent>()
            .ThenExpectState(
                x => x.TestCollection.Count == 0);
        }
Beispiel #17
0
        public void With_Test_Kit_TestEventMultipleEmitSourcing_AfterManyMultiCommand_TestStateSignalled()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;

            fixture
            .Using(() => new TestAggregateManager(), aggregateId)
            .Given(new TestCreatedEvent(aggregateId))
            .When(new AddFourTestsCommand(aggregateId, new Test(TestId.New)))
            .AndWhen(new PoisonTestAggregateCommand(aggregateId))
            .AndWhen(new PublishTestStateCommand(aggregateId))
            .ThenExpectDomainEvent <TestStateSignalEvent>(
                x => x.AggregateEvent.LastSequenceNr == 5 &&
                x.AggregateEvent.Version == 5 &&
                x.AggregateEvent.AggregateState.TestCollection.Count == 4);
        }
Beispiel #18
0
        public void InitialState_AfterAggregateCreation_TestCreatedEventEmitted()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;
            var command     = new CreateTestCommand(aggregateId).WithSourceId(commandId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectDomainEvent <TestCreatedEvent>(
                x => x.AggregateEvent.Id.Equals(aggregateId) &&
                x.Metadata.ContainsKey("some-key"));
        }
Beispiel #19
0
        public void FixtureArrangerWithIdentity_CreatesAggregateRef()
        {
            //Issue - https://dev.azure.com/lutando/Akkatecture/_workitems/edit/23/
            using (var testKit = new TestKit(_config, "fixture-tests-1"))
            {
                var fixture           = new AggregateFixture <TestAggregate, TestAggregateId>(testKit);
                var aggregateIdentity = TestAggregateId.New;

                fixture
                .For(aggregateIdentity)
                .GivenNothing();

                fixture.AggregateRef.Path.Name.Should().Be(aggregateIdentity.Value);
                fixture.AggregateId.Should().Be(aggregateIdentity);
            }
        }
Beispiel #20
0
        public void With_Test_Kit_TestEventSourcing_AfterManyTests_TestStateSignalled()
        {
            var fixture = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New; var commands = new List <ICommand <TestAggregate, TestAggregateId> >();

            commands.AddRange(Enumerable.Range(0, 5).Select(x => new AddTestCommand(aggregateId, new Test(TestId.New))));

            fixture
            .Using(() => new TestAggregateManager(), aggregateId)
            .Given(new TestCreatedEvent(aggregateId))
            .When(commands.ToArray())
            .AndWhen(new PoisonTestAggregateCommand(aggregateId))
            .AndWhen(new PublishTestStateCommand(aggregateId))
            .ThenExpectDomainEvent <TestStateSignalEvent>(
                x => x.AggregateEvent.LastSequenceNr == 6 &&
                x.AggregateEvent.Version == 6 &&
                x.AggregateEvent.AggregateState.TestCollection.Count == 5);
        }
Beispiel #21
0
        public void TestEventMultipleEmitSourcing_AfterManyMultiCreateCommand_EventsEmitted()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;
            var firstTest   = new TestEntity(TestId.New);
            var secondTest  = new TestEntity(TestId.New);
            var command     = new CreateAndAddTwoTestsCommand(aggregateId, firstTest, secondTest)
                              .WithSourceId(commandId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectDomainEvent <TestCreatedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>();
        }
Beispiel #22
0
        public void With_Test_Kit_EventContainerMetadata_AfterAggregateCreation_TestCreatedEventEmitted()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;

            fixture
            .For(aggregateId)
            .GivenNothing()
            .When(new CreateTestCommand(aggregateId))
            .ThenExpectDomainEvent <TestCreatedEvent>(
                x => x.AggregateIdentity.Equals(aggregateId) &&
                x.IdentityType == typeof(TestAggregateId) &&
                x.AggregateType == typeof(TestAggregate) &&
                x.EventType == typeof(TestCreatedEvent) &&
                x.Metadata.EventName == "TestCreated" &&
                x.Metadata.AggregateId == aggregateId.Value &&
                x.Metadata.EventVersion == 1 &&
                x.Metadata.AggregateSequenceNumber == 1);
        }
        public void TestCommandTwice_AfterAggregateCreation_TestEventEmitted()
        {
            var fixture     = new AggregateFixture <TestAggregate, TestAggregateId>(this);
            var aggregateId = TestAggregateId.New;
            var commandId   = CommandId.New;
            var commandId2  = CommandId.New;
            var commandId3  = CommandId.New;
            var testId      = TestId.New;
            var test        = new Test(testId);
            var test2Id     = TestId.New;
            var test2       = new Test(test2Id);

            fixture
            .For(aggregateId)
            .GivenNothing()
            .When(new CreateTestCommand(aggregateId, commandId), new AddTestCommand(aggregateId, commandId2, test), new AddTestCommand(aggregateId, commandId3, test2))
            .ThenExpectDomainEvent <TestAddedEvent>(x => x.AggregateEvent.Test.Equals(test) && x.AggregateSequenceNumber == 2)
            .ThenExpectDomainEvent <TestAddedEvent>(x => x.AggregateEvent.Test.Equals(test2) && x.AggregateSequenceNumber == 3);
        }
Beispiel #24
0
        public void EventContainerMetadata_AfterAggregateCreation_TestCreatedEventEmitted()
        {
            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;
            var command     = new CreateTestCommand(aggregateId).WithSourceId(commandId);

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(command)
            .ThenExpectDomainEvent <TestCreatedEvent>(
                x => x.AggregateEvent.Id.Equals(aggregateId) &&
                x.IdentityType == typeof(TestAggregateId) &&
                x.EventType == typeof(TestCreatedEvent) &&
                x.Metadata.EventName == "TestCreated" &&
                x.Metadata.EventVersion == 1 &&
                x.Metadata.SourceId.Value == commandId.Value);
        }
        public void FixtureArrangerWithAggregateManager_CreatesAggregateManagerRef()
        {
            using var testKit = new TestKit(Configuration.Default, "fixture-tests-2");
            testKit.Sys.RegisterDependencyResolver(
                new ServiceCollection()
                .AddEventFly(testKit.Sys)
                .WithContext <TestContext>()
                .Services
                .AddScoped <TestSaga>()
                .BuildServiceProvider()
                .UseEventFly()
                );

            var fixture           = new AggregateFixture <TestAggregate, TestAggregateId>(testKit);
            var aggregateIdentity = TestAggregateId.New;

            fixture.Using(aggregateIdentity);

            fixture.AggregateId.Should().Be(aggregateIdentity);
        }
Beispiel #26
0
        public void TestEventSourcing_AfterManyTests_TestStateSignalled()
        {
            IEnumerable <ICommand> GetCommands(TestAggregateId testAggregateId, ISourceId commandId1)
            {
                var command = new CreateTestCommand(testAggregateId).WithSourceId(commandId1);

                yield return(command);

                for (var i = 0; i < 5; i++)
                {
                    var test          = new TestEntity(TestId.New);
                    var testCommandId = SourceId.New;
                    var testCommand   = new AddTestCommand(testAggregateId, test).WithSourceId(testCommandId);
                    yield return(testCommand);
                }

                var poisonCommand = new PoisonTestAggregateCommand(testAggregateId);

                yield return(poisonCommand);
            }

            var aggregateId = TestAggregateId.New;
            var commandId   = SourceId.New;

            var fixture = new AggregateFixture <TestAggregateId, TestAggregate,
                                                ITestAggregateState, TestAggregateState>(this);

            fixture.For(aggregateId)
            .GivenNothing()
            .When(GetCommands(aggregateId, commandId).ToArray())
            .ThenExpectDomainEvent <TestCreatedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectDomainEvent <TestAddedEvent>()
            .ThenExpectState(
                x => x.TestCollection.Count == 5);
        }
Beispiel #27
0
        public void UpdateMemoTest()
        {
            //Создаем заготовку для теста
            var fixture = new AggregateFixture <MemoId, MemoAggregate, IMemoState, MemoState>(this);

            var id = MemoId.New;

            //Параметры документа
            var docId      = Guid.NewGuid().ToString();
            var docNumber  = new DocumentNumber("100");
            var docAddress = new Address("12700", "Россия",
                                         "Москва", "проспект Мира", "1");

            //BDD тест (сценарий)
            fixture.For(id)

            //Допустим (предусловия)
            .GivenNothing()

            //Когда (тестируемые действия)
            .When(new UpdateMemo(id,
                                 new MemoDocument(docId, docNumber, docAddress)))

            //Тогда (проверка результатов)
            .ThenExpectResult(e => e.IsSuccess)
            .ThenExpectDomainEvent <MemoUpdated>(e
                                                 => e.AggregateEvent.Id == id &&
                                                 e.AggregateEvent.Document != null &&
                                                 e.AggregateEvent.Document.Id == docId &&
                                                 e.AggregateEvent.Document.Number == docNumber &&
                                                 e.AggregateEvent.Document.CustomerAddress == docAddress)
            .ThenExpectState(s
                             => s.Status == MemoStatus.Undefined &&
                             s.Document != null &&
                             s.Document.Id == docId &&
                             s.Document.Number == docNumber &&
                             s.Document.CustomerAddress == docAddress);
        }