Ejemplo n.º 1
0
        public void If_The_Ola_Is_Breached_Then_Any_Placed_Sales_Call_Is_Cancelled()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                fixture.Deliver(new CustomerAccountCreated {
                    CustomerName = customerName
                });
                bus.HasSentLocal <ScheduleASalesCall>(
                    with: x => x.CustomerName == customerName,
                    because: "a sales call is scheduled after the account is created");

                fixture.Deliver(new VerifyCustomerOnboardingOla {
                    CustomerName = customerName
                });

                fixture.ShouldHaveCompleted();
                bus.HasSentLocal <CancellSalesCall>(
                    with: x => x.CustomerName == customerName,
                    because: "any scheduled sales call is cancelled after an OLA breach");
            }
        }
Ejemplo n.º 2
0
        public void CanSimulateConflict_MultipleMessages_SingleConflict()
        {
            using (var fixture = SagaFixture.For(() => new ConflictSagaHandler()))
            {
                fixture.DumpLogsOnDispose();

                fixture.Deliver(new SomeMessage("some-id", "HEJ MED DIG"));
                fixture.Deliver(new SomeMessage("some-id", "MIN VEN"));

                fixture.PrepareConflict <ConflictSagaHandlerData>(d => d.CorrelationId == "some-id");

                fixture.Deliver(new SomeMessage("some-id", "IGEN"));

                var sagaData = fixture.Data
                               .OfType <ConflictSagaHandlerData>()
                               .FirstOrDefault(d => d.CorrelationId == "some-id")
                               ?? throw new AssertionException($"Could not find saga data with correlation ID 'some-id'");

                string GetReceivedStrings() =>
                $@"Got these strings:

{string.Join(Environment.NewLine, sagaData.ReceivedStrings)}
";

                Assert.That(sagaData.ReceivedStrings.Count, Is.EqualTo(3), GetReceivedStrings);
                Assert.That(sagaData.ReceivedStrings.Contains("HEJ MED DIG"), Is.True, GetReceivedStrings);
                Assert.That(sagaData.ReceivedStrings.Contains("MIN VEN"), Is.True, GetReceivedStrings);
                Assert.That(sagaData.ReceivedStrings.Contains("IGEN"), Is.True, GetReceivedStrings);
            }
        }
Ejemplo n.º 3
0
        public void Other_Systems_Are_Notified_After_A_Succcessful_Onboarding()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                fixture.Deliver(new CustomerAccountCreated {
                    CustomerName = customerName
                });
                fixture.Deliver(new WelcomePackSentToCustomer {
                    CustomerName = customerName
                });
                fixture.ShouldHaveCompleted();
                bus.HasPublished <CustomerOnboarded>(
                    with: x => x.CustomerName == customerName,
                    because: "the saga should publishes an event notifying of the customer onboarding")
                .HasNotSentLocal <CustomerOnboardingOlaBreached>(
                    because: "the onboarding was successful");

                fixture.ShouldHaveCompleted();

                OnboardCustomerSagaData SagaDataShould(Predicate <OnboardCustomerSagaData> have, string because = null)
                {
                    return(fixture.SagaData <OnboardCustomerSaga, OnboardCustomerSagaData>().Should(have, because));
                }
            }
        }
Ejemplo n.º 4
0
        public void A_Welcome_Pack_Is_Sent_To_The_Customer_After_Their_Account_Has_Been_Created()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                fixture.Deliver(new CustomerAccountCreated {
                    CustomerName = customerName
                });

                fixture.SagaDataShould(
                    have: x => x.AccountCreated,
                    because: "it should remember the account has been created")
                .SagaDataIsNotComplete();
                bus.HasSent <SendWelcomePackToCustomer>(
                    with: x => x.CustomerName == customerName,
                    because: "it should initiate welcome pack creation"
                    );

                fixture.ShouldNotHaveCompleted();
            }
        }
Ejemplo n.º 5
0
        public void Onboarding_Causes_An_Account_To_Be_Created_For_The_Customer()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                fixture.SagaDataShould(
                    have: x => x.CustomerName == customerName,
                    because: "store the customer name")
                .SagaDataIsNotComplete();
                bus.HasSentLocal <CreateCustomerAccount>(
                    with: x => x.CustomerName == customerName,
                    because: "it should initiate account creation"
                    );


                fixture.Deliver(new CustomerAccountCreated {
                    CustomerName = customerName
                });
                fixture.SagaDataShould(
                    have: x => x.AccountCreated,
                    because: "it should remember the account has been created")
                .SagaDataIsNotComplete();

                fixture.ShouldNotHaveCompleted();
            }
        }
Ejemplo n.º 6
0
        public void The_Sales_Team_Will_Set_Up_A_Call_With_The_Customer_After_Their_Account_Has_Been_Created()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                fixture.Deliver(new CustomerAccountCreated {
                    CustomerName = customerName
                });
                fixture.SagaDataShould(
                    have: x => x.AccountCreated,
                    because: "it should remember the account has been created")
                .SagaDataIsNotComplete();

                bus.HasSentLocal <ScheduleASalesCall>(
                    with: x => x.CustomerName == customerName,
                    because: "a sales call is scheduled after the account is created");

                fixture.ShouldNotHaveCompleted();
            }
        }
Ejemplo n.º 7
0
    public void CanRetrieveSagaData()
    {
        using var fixture = SagaFixture.For <MySaga>();

        fixture.Deliver(new TestMessage("hej"));

        var current = fixture.Data.OfType <MySagaState>().ToList();

        Assert.That(current.Count, Is.EqualTo(1));
        Assert.That(current[0].Text, Is.EqualTo("hej"));
    }
    public void EmitsCreatedEvent()
    {
        using var fixture = SagaFixture.For(sagaHandlerFactory: () => new MySaga(), sagaSerializerFactory: () => new MicrosoftSagaSerializer());

        var gotEvent = false;

        fixture.Created += _ => gotEvent = true;

        fixture.Deliver(new TestMessage("hej"));

        Assert.That(gotEvent, Is.True);
    }
Ejemplo n.º 9
0
        public void EmitsCouldNotCorrelateEvent()
        {
            using (var fixture = SagaFixture.For <MySaga>())
            {
                var gotEvent = false;
                fixture.CouldNotCorrelate += () => gotEvent = true;

                fixture.Deliver(new TestMessage("hej"));

                Assert.That(gotEvent, Is.True);
            }
        }
Ejemplo n.º 10
0
        public void EmitsCreatedEvent()
        {
            using (var fixture = SagaFixture.For <MySaga>())
            {
                var gotEvent = false;
                fixture.Created += d => gotEvent = true;

                fixture.Deliver(new TestMessage("hej"));

                Assert.That(gotEvent, Is.True);
            }
        }
Ejemplo n.º 11
0
        public void IdIsSetUponNullId()
        {
            using (var fixture = SagaFixture.For <MySaga>())
            {
                // Arrange

                // Act
                fixture.Add(new MySagaState());

                // Asert
                Assert.That(fixture.Data.Count(), Is.EqualTo(1));
                Assert.That(fixture.Data.Single().Id, Is.Not.Null);
            }
        }
Ejemplo n.º 12
0
    public void EmitsUpdatedEvent()
    {
        using var fixture = SagaFixture.For <MySaga>();

        fixture.Deliver(new TestMessage("hej"));

        var gotEvent = false;

        fixture.Updated += _ => gotEvent = true;

        fixture.Deliver(new TestMessage("hej"));

        Assert.That(gotEvent, Is.True);
    }
Ejemplo n.º 13
0
    public void Reproduce()
    {
        using var fixture = SagaFixture.For(() => new MySaga());

        fixture.DumpLogsOnDispose();

        fixture.Deliver(new MyMessage());

        Task.Delay(TimeSpan.FromSeconds(1)).Wait();

        var logs = fixture.LogEvents.ToList();

        Assert.That(logs.Count(l => l.Level == LogLevel.Warn), Is.EqualTo(1));
    }
Ejemplo n.º 14
0
        public void SagaDataWithNonEmptyIdIsAddedToSagaFixtureData()
        {
            using (var fixture = SagaFixture.For <MySaga>())
            {
                // Arrange

                // Act
                fixture.Add(new MySagaState {
                    Id = Guid.NewGuid()
                });

                // Assert
                Assert.That(fixture.Data.Count(), Is.EqualTo(1));
            }
        }
Ejemplo n.º 15
0
    public void CanSetUpFakeSagaData()
    {
        using var fixture = SagaFixture.For <MySaga>();

        fixture.Add(new MySagaState {
            Text = "I know you!"
        });
        fixture.AddRange(new[] { new MySagaState {
                                     Text = "I know you too!"
                                 } });

        Assert.That(fixture.Data.Count(), Is.EqualTo(2));
        Assert.That(fixture.Data.OfType <MySagaState>().Count(d => d.Text == "I know you!"), Is.EqualTo(1));
        Assert.That(fixture.Data.OfType <MySagaState>().Count(d => d.Text == "I know you too!"), Is.EqualTo(1));
    }
Ejemplo n.º 16
0
    public void CanDispatchFailedMessage()
    {
        var failedMessageWasReceived = new ManualResetEvent(false);

        Using(failedMessageWasReceived);

        using var fixture = SagaFixture.For(() => new MySaga(failedMessageWasReceived), secondLevelRetriesEnabled: true);

        fixture.DumpLogsOnDispose();
        fixture.Add(new MySagaState {
            Text = "known-string"
        });
        fixture.DeliverFailed(new TestMessage("known-string"), new DriveNotFoundException("B:"));
        failedMessageWasReceived.WaitOrDie(TimeSpan.FromSeconds(3));
    }
Ejemplo n.º 17
0
        public void The_Systems_Track_The_Ola_For_Customer_Onboarding()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                bus.HasDeferredLocal <VerifyCustomerOnboardingOla>(
                    with: x => x.CustomerName == customerName,
                    because: "it should start tracking the OLA");
            }
        }
    public async Task YeahItWorks()
    {
        var receivedEvents = new ConcurrentQueue <string>();

        using var fixture = SagaFixture.For(() => new RandomSaga(receivedEvents));

        fixture.Deliver(new RandomMessage("hej"));
        fixture.HandlerExceptions.ThrowIfNotEmpty();

        await Task.Delay(millisecondsDelay : 17);

        fixture.Deliver(new RandomMessage("hej igen"));

        Assert.That(receivedEvents.Count, Is.EqualTo(2));
        Assert.That(receivedEvents, Is.EqualTo(new[] { "hej", "hej igen" }));
    }
Ejemplo n.º 19
0
        public void CanGetCaughtException()
        {
            using (var fixture = SagaFixture.For <MySaga>())
            {
                fixture.Deliver(new FailingTestMessage("whoohoo"));

                var handlerExceptions = fixture.HandlerExceptions.ToList();

                Console.WriteLine(string.Join(Environment.NewLine + Environment.NewLine, handlerExceptions));

                Assert.That(handlerExceptions.Count, Is.EqualTo(1));

                var exception = handlerExceptions.Single();

                Assert.That(exception.Exception, Is.TypeOf <RebusApplicationException>());
                Assert.That(exception.Exception.ToString(), Contains.Substring("oh no something bad happened"));
            }
        }
Ejemplo n.º 20
0
        public void If_The_Ola_Is_Breached_Then_And_No_Sales_Call_Was_Placed_Then_No_Cancellation_Takes_Place()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                bus.HasNotSentLocal <ScheduleASalesCall>(
                    because: "the account creation is has not been confirmed");

                fixture.Deliver(new VerifyCustomerOnboardingOla {
                    CustomerName = customerName
                });

                fixture.ShouldHaveCompleted();
                bus.HasNotSentLocal <CancellSalesCall>(
                    because: "the  sales call is only cancelled after an OLA breach if it has already been requested");
            }
        }
Ejemplo n.º 21
0
        public void If_The_Ola_Is_Breached_Then_Other_Systems_Will_Not_Be_Notified_About_The_Onboarding()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                bus.HasDeferredLocal <VerifyCustomerOnboardingOla>(
                    with: x => x.CustomerName == customerName,
                    because: "it should start tracking the OLA");

                fixture.Deliver(new VerifyCustomerOnboardingOla {
                    CustomerName = customerName
                });

                fixture.ShouldHaveCompleted();
                bus.HasNotPublished <CustomerOnboarded>(because: "the saga was not successful and there should be no event notifying of a customer onboarding");
            }
        }
Ejemplo n.º 22
0
        public void TestSaga()
        {
            var id      = MemoId.New;
            var fixture = new SagaFixture <AutoConfimMemoSaga, AutoConfirmSagaState>(this);

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

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

            //Когда (тестируемые действия)
            .When <MemoId, SigningStarted>(new SigningStarted(id, "TestUser"))

            //Тогда (проверка результатов)
            .ThenExpect <MemoId, ConfirmSigningMemo>(command => command.Id == id)

            //Когда (тестируемые действия)
            .AndWhen <MemoId, MemoSigned>(new MemoSigned(id))

            //Тогда (проверка результатов)
            .ThenExpectState(state => state.UserId == "TestUser");
        }
Ejemplo n.º 23
0
        public void If_The_Ola_Is_Breached_Then_The_Service_Desk_Takes_Over_The_Process()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                bus.HasDeferredLocal <VerifyCustomerOnboardingOla>(
                    with: x => x.CustomerName == customerName,
                    because: "it should start tracking the OLA");

                fixture.Deliver(new VerifyCustomerOnboardingOla {
                    CustomerName = customerName
                });

                fixture.ShouldHaveCompleted();
                bus.HasSentLocal <CustomerOnboardingOlaBreached>(
                    with: x => x.CustomerName == customerName,
                    because: "the service desk should be notified of the failed onboarding");
            }
        }
Ejemplo n.º 24
0
        public void A_Welcome_Pack_Is_Not_Sent_To_The_Customer_After_Another_Account_Has_Been_Created()
        {
            var customerName = RandomName;
            var bus          = new FakeBus();

            using (var fixture = SagaFixture.For(() => new OnboardCustomerSaga(bus)))
            {
                fixture.Deliver(new OnboardCustomer {
                    CustomerName = customerName
                });
                fixture.Deliver(new CustomerAccountCreated {
                    CustomerName = RandomName
                });

                fixture.SagaDataShould(
                    have: x => !x.AccountCreated,
                    because: "it should not have reacted to the other customer's account being created");
                bus.HasNotSent <SendWelcomePackToCustomer>(
                    because: "it should initiate welcome pack creation because of another customer's account being created"
                    );

                fixture.ShouldNotHaveCompleted();
            }
        }