Inheritance: SmsStatus
        public void SendSingleSmsNow_QueuedReturnedIsInvalid_HasNoPrice()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var smsQueued = new SmsQueued("sid");
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsQueued);

            Test.Initialize();
            Assert.That(() =>
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a => a.SmsService = smsService)
                    .ExpectPublish<MessageSent>()
                .When(a => a.Handle(sendOneMessageNow)),
                Throws.Exception.With.Message.Contains("SmsQueued type is invalid - followup is required to get delivery status")
            );
        }
        public void SendOneMessageNow_SmsQueued_InvalidThrowsException()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var smsQueued = new SmsQueued("sid");

            smsService
                .Expect(s => s.Send(sendOneMessageNow))
                .Return(smsQueued);

            var smsActioner = new SmsActioner.SmsActioner
                {
                    SmsService = smsService
                };

            Assert.That(() => smsActioner.Handle(sendOneMessageNow), Throws.Exception.With.Message.Contains("SmsQueued type is invalid - followup is required to get delivery status"));
        }
        public void SendSingleSmsNowQueuedThenSuccess()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();

            var smsQueued = new SmsQueued("12");
            var smsSent = new SmsSent(new SmsConfirmationData("r", DateTime.Now, .44m));
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsQueued);
            smsService.Expect(s => s.CheckStatus(smsQueued.Sid)).Return(smsSent);

            Test.Initialize();
            Test.Saga<SmsActioner>()
                .WithExternalDependencies(a => a.SmsService = smsService)
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == TimeSpan.FromSeconds(10))
                .When(a => a.Handle(sendOneMessageNow))
                    .ExpectPublish<MessageSent>()
                .WhenSagaTimesOut()
                .AssertSagaCompletionIs(true);
        }
        public void SendSingleSmsNowQueuedThenFail()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();

            const string sid = "12";
            var smsQueued = new SmsQueued(sid);
            var smsFailed = new SmsFailed(sid, "c", "m", "m", "s");
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsQueued);
            smsService.Expect(s => s.CheckStatus(smsQueued.Sid)).Return(smsFailed);

            Test.Initialize();
            Test.Saga<SmsActioner>()
                .WithExternalDependencies(a => a.SmsService = smsService)
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == TimeSpan.FromSeconds(10))
                .When(a => a.Handle(sendOneMessageNow))
                    .ExpectNotPublish<MessageSent>()
                .WhenSagaTimesOut()
                .AssertSagaCompletionIs(true);
        }
        public void SendSingleSmsNowQueuedTwiceThenSuccess()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();

            const string sid = "12";
            var smsQueued = new SmsQueued(sid);
            var smsSuccess = new SmsSent(new SmsConfirmationData("r", DateTime.Now, 3.3m));
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsQueued);
            smsService.Expect(s => s.CheckStatus(smsQueued.Sid)).Repeat.Once().Return(smsQueued);
            smsService.Expect(s => s.CheckStatus(smsQueued.Sid)).Return(smsSuccess);

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a => a.SmsService = smsService)
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == TimeSpan.FromSeconds(10))
                .When(a => a.Handle(sendOneMessageNow))
                    .ExpectNotPublish<MessageSent>()
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == TimeSpan.FromSeconds(10))
                .WhenSagaTimesOut()
                    .ExpectPublish<MessageSent>()
                    .ExpectReplyToOriginator<MessageSuccessfullyDelivered>()
                    .ExpectSendLocal<MessageSuccessfullyDelivered>()
                .WhenSagaTimesOut();
        }
        public void SendSingleSmsNow_SendingThenQueuedThenSuccess()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var timeoutCalculator = MockRepository.GenerateMock<ITimeoutCalculator>();

            const string sid = "12";
            var smsSending = new SmsSending(sid, 0.06m);
            var smsQueued = new SmsQueued(sid);
            var smsSuccess = new SmsSent("r", DateTime.Now);
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsSending);
            smsService.Expect(s => s.CheckStatus(smsQueued.Sid)).Repeat.Once().Return(smsQueued);
            smsService.Expect(s => s.CheckStatus(smsQueued.Sid)).Return(smsSuccess);
            var timeoutTimespan = new TimeSpan();
            timeoutCalculator.Expect(t => t.RequiredTimeout(Arg<int>.Is.Anything)).Return(timeoutTimespan);

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a =>
                {
                    a.SmsService = smsService;
                    a.TimeoutCalculator = timeoutCalculator;
                })
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == timeoutTimespan)
                .When(a => a.Handle(sendOneMessageNow))
                    .ExpectNotPublish<MessageSent>()
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == timeoutTimespan)
                .WhenSagaTimesOut()
                    .ExpectPublish<MessageSent>()
                .WhenSagaTimesOut();
        }
        public void TimeoutHandle_CheckStatus_SmsQueued_RequestsTimeout()
        {
            var timeout = new SmsPendingTimeout();
            var sendOneMessageNow = new SendOneMessageNow();
            var data = new SmsActionerData
            {
                Id = Guid.NewGuid(),
                OriginalMessage = sendOneMessageNow,
                Price = 0.06m,
                SmsRequestId = "123",
                NumberOfTimeoutRequests = 1
            };

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var timeoutCalculator = MockRepository.GenerateMock<ITimeoutCalculator>();
            var smsQueued = new SmsQueued(data.SmsRequestId);

            smsService
                .Expect(s => s.CheckStatus(data.SmsRequestId))
                .Return(smsQueued);

            var timeoutTimespan = new TimeSpan();
            timeoutCalculator.Expect(t => t.RequiredTimeout(data.NumberOfTimeoutRequests)).Return(timeoutTimespan);

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a =>
                    {
                        a.SmsService = smsService;
                        a.Data = data;
                        a.TimeoutCalculator = timeoutCalculator;
                    })
                .WhenReceivesMessageFrom("somewhere")
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == timeoutTimespan)
                .When(a => a.Timeout(timeout))
                .AssertSagaCompletionIs(false);

            Assert.That(data.NumberOfTimeoutRequests, Is.EqualTo(2));
            smsService.VerifyAllExpectations();
            timeoutCalculator.VerifyAllExpectations();
        }