public void SendSingleSmsNow_SendingThenFail()
        {
            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 smsFailed = new SmsFailed(sid, "c", "m");
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsSending);
            smsService.Expect(s => s.CheckStatus(smsSending.Sid)).Return(smsFailed);
            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>()
                .WhenSagaTimesOut()
                .AssertSagaCompletionIs(true);
        }
        public void SendOneMessageNow_SmsSendingSetsPriceAndId()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var timeoutCalculator = MockRepository.GenerateMock<ITimeoutCalculator>();
            var smsSending = new SmsSending("id", 0.06m);
            var data = new SmsActionerData();

            smsService
                .Expect(s => s.Send(sendOneMessageNow))
                .Return(smsSending);
            var timeoutRequested = new TimeSpan();
            timeoutCalculator.Expect(t => t.RequiredTimeout(data.NumberOfTimeoutRequests)).Return(timeoutRequested);

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a =>
                    {
                        a.SmsService = smsService;
                        a.Data = data;
                        a.TimeoutCalculator = timeoutCalculator;
                    })
                .WhenReceivesMessageFrom("somewhere")
                    .ExpectTimeoutToBeSetIn<SmsPendingTimeout>((timeoutMessage, timespan) => timespan == timeoutRequested)
                .When(a => a.Handle(sendOneMessageNow));

            Assert.That(data.SmsRequestId, Is.EqualTo(smsSending.Sid));
            Assert.That(data.Price, Is.EqualTo(smsSending.Price));
            Assert.That(data.OriginalMessage, Is.EqualTo(sendOneMessageNow));
            Assert.That(data.NumberOfTimeoutRequests, Is.EqualTo(1));
            smsService.VerifyAllExpectations();
            timeoutCalculator.VerifyAllExpectations();
        }
        public void ValidNullCountryConfigSendsMessageReturnsToDetails()
        {
            var bus = MockRepository.GenerateMock<IBus>();
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var docStore = MockRepository.GenerateMock<IDocumentStore>();
            var docSession = MockRepository.GenerateMock<IDocumentSession>();

            ravenDocStore.Expect(r => r.GetStore()).Return(docStore);
            docStore.Expect(d => d.OpenSession("Configuration")).Return(docSession);
            docSession.Expect(d => d.Load<CountryCodeReplacement>("CountryCodeConfig")).Return(null);

            var sentMessage = new SendOneMessageNow();
            bus.Expect(b => b.Send(Arg<SendOneMessageNow>.Is.NotNull))
                .WhenCalled(a => sentMessage = ((SendOneMessageNow)((object[])a.Arguments[0])[0]));

            var controller = new SendNowController { ControllerContext = new ControllerContext(), Bus = bus, RavenDocStore = ravenDocStore };
            var sendNowModel = new SendNowModel { MessageBody = "asdflj", Number = "number" , ConfirmationEmail = "sdakflj" };
            var result = (RedirectToRouteResult)controller.Create(sendNowModel);

            Assert.That(result.RouteValues["action"], Is.EqualTo("Details"));
            Assert.That(sentMessage.SmsData.Mobile, Is.EqualTo(sendNowModel.Number));
            Assert.That(sentMessage.SmsData.Message, Is.EqualTo(sendNowModel.MessageBody));
            Assert.That(sentMessage.ConfirmationEmailAddress, Is.EqualTo(sendNowModel.ConfirmationEmail));
            bus.VerifyAllExpectations();
        }
 public ActionResult Create(SendNowModel model)
 {
     var isValid = TryValidateModel(model);
     if(isValid)
     {
         model.MessageId = Guid.NewGuid();
         if (model.MessageBody.Length > 160)
             model.MessageBody = model.MessageBody.Substring(0, 160);
         CountryCodeReplacement countryCodeReplacement;
         using (var session = RavenDocStore.GetStore().OpenSession("Configuration"))
         {
             countryCodeReplacement = session.Load<CountryCodeReplacement>("CountryCodeConfig");
         }
         var cleanInternationalNumber = countryCodeReplacement != null ? countryCodeReplacement.CleanAndInternationaliseNumber(model.Number) : model.Number.Trim();
         var sendOneMessageNow = new SendOneMessageNow
         {
             CorrelationId = model.MessageId,
             SmsData = new SmsData(cleanInternationalNumber, model.MessageBody),
             ConfirmationEmailAddress = model.ConfirmationEmail,
             SmsMetaData = new SmsMetaData
             {
                 Tags = string.IsNullOrEmpty(model.Tags) ? null : model.Tags.Split(',').ToList().Select(t => t.Trim()).ToList(),
                 Topic = model.Topic
             }
         };
         Bus.Send(sendOneMessageNow);
         return RedirectToAction("Details", "SendNow", new { requestId = model.MessageId.ToString()});
     }
     return View("Create", model);
 }
        public void SmsUsesTwilio()
        {
            _docSession.Expect(d => d.Load<SmsProviderConfiguration>("SmsProviderConfiguration")).Return(_twilioProvider);

            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper, RavenDocStore = _ravenDocStore };

            twilioWrapper.Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message));
            smsService.Send(messageToSend);
            twilioWrapper.VerifyAllExpectations();
        }
        public void SmsServiceAuthenticationFailed()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var smsTechWrapper = MockRepository.GenerateMock<ISmsTechWrapper>();
            var smsService = new SmsService { SmsTechWrapper = smsTechWrapper };

            var smsMessageSending = new SendSmsResponse { Cost = (float)0.06, MessageId = 123456, Error = new Error { Code = "AUTH_FAILED", Description = "we don't know who you are!" } };
            smsTechWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            Assert.That(() => smsService.Send(messageToSend), Throws.Exception.TypeOf<SmsTechAuthenticationFailed>().With.Message.EqualTo(smsMessageSending.Error.Description));
        }
Beispiel #7
0
 /// <summary>
 /// This method will never get success / fail of message delivery - just that it is valid, and how much it will cost
 /// </summary>
 /// <param name="messageToSend"></param>
 /// <returns></returns>
 public SmsStatus Send(SendOneMessageNow messageToSend)
 {
     var createdSmsMessage = SmsTechWrapper.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message);
     if (createdSmsMessage.Error.Code.Equals("AUTH_FAILED"))
         throw new SmsTechAuthenticationFailed(createdSmsMessage.Error.Description);
     if (createdSmsMessage.Error.Code.Equals("LEDGER_ERROR"))
         throw new AccountOutOfMoneyException("Could not send message - account is currently out of money");
     if (createdSmsMessage.Error.Code.Equals("RECIPIENTS_ERROR"))
         return new SmsFailed(createdSmsMessage.MessageId.ToString(), createdSmsMessage.Error.Code, createdSmsMessage.Error.Description);
     if (createdSmsMessage.Error.Code.Equals("SUCCESS"))
         return new SmsSending(createdSmsMessage.MessageId.ToString(), Convert.ToDecimal(createdSmsMessage.Cost));
     throw new ArgumentException("Error code expected");
 }
        public void SmsServiceOutOfMoney()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var smsTechWrapper = MockRepository.GenerateMock<ISmsTechWrapper>();
            var smsService = new SmsService { SmsTechWrapper = smsTechWrapper };

            var smsMessageSending = new SendSmsResponse { Cost = (float)0.06, MessageId = 123456, Error = new Error { Code = "LEDGER_ERROR", Description = "can't send a message to those dudes!" } };
            const string accountIsCurrentlyOutOfMoney = "Could not send message - account is currently out of money";
            smsTechWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            Assert.That(() => smsService.Send(messageToSend), Throws.Exception.TypeOf<AccountOutOfMoneyException>().With.Message.EqualTo(accountIsCurrentlyOutOfMoney));
        }
        public void SendSingleSmsNow_Failure()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var smsSent = new SmsFailed("sid", "code", "message");
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsSent);

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a => a.SmsService = smsService)
                    .ExpectPublish<MessageFailedSending>()
                .When(a => a.Handle(sendOneMessageNow))
                .AssertSagaCompletionIs(true);
        }
        public void SmsServiceSending()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper };

            var smsMessageSending = new SMSMessage { Status = "sending", Sid = "sidReceipt" };
            twilioWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof (SmsSending)));
            Assert.That(response.Sid, Is.EqualTo(smsMessageSending.Sid));
            twilioWrapper.VerifyAllExpectations();
        }
Beispiel #11
0
 public SmsStatus Send(SendOneMessageNow messageToSend)
 {
     using (var session = RavenDocStore.GetStore().OpenSession(RavenDocStore.ConfigurationDatabaseName()))
     {
         var smsProvider = session.Load<SmsProviderConfiguration>("SmsProviderConfiguration");
         if (smsProvider == null)
             throw new Exception("No SMS provider selected");
         switch (smsProvider.SmsProvider)
         {
             case SmsProvider.Nexmo:
                 return NexmoWrapper.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message);
             case SmsProvider.Twilio:
                 return TwilioWrapper.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message);
         }
         throw new Exception("SMS Provder delivery not implemented for " + smsProvider.SmsProvider.ToString());
     }
 }
        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 ValidSendsMessageReturnsToDetails()
        {
            var bus = MockRepository.GenerateMock<IBus>();

            var sentMessage = new SendOneMessageNow();
            bus.Expect(b => b.Send(Arg<SendOneMessageNow>.Is.NotNull))
                .WhenCalled(a => sentMessage = ((SendOneMessageNow)((object[])a.Arguments[0])[0]));

            var controller = new SendNowController { ControllerContext = new ControllerContext(), Bus = bus };
            var sendNowModel = new SendNowModel { MessageBody = "asdflj", Number = "number" , ConfirmationEmail = "sdakflj" };
            var result = (RedirectToRouteResult)controller.Create(sendNowModel);

            Assert.That(result.RouteValues["action"], Is.EqualTo("Details"));
            Assert.That(sentMessage.SmsData.Mobile, Is.EqualTo(sendNowModel.Number));
            Assert.That(sentMessage.SmsData.Message, Is.EqualTo(sendNowModel.MessageBody));
            Assert.That(sentMessage.ConfirmationEmailAddress, Is.EqualTo(sendNowModel.ConfirmationEmail));
            bus.VerifyAllExpectations();
        }
        public void SmsServiceSending()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var smsTechWrapper = MockRepository.GenerateMock<ISmsTechWrapper>();
            var smsService = new SmsService { SmsTechWrapper = smsTechWrapper };

            var smsMessageSending = new SendSmsResponse {Cost = (float) 0.06, MessageId = 123456, Error = new Error { Code = "SUCCESS"} };
            smsTechWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof (SmsSending)));
            Assert.That(response.Sid, Is.EqualTo(smsMessageSending.MessageId.ToString()));
            var smsSending = response as SmsSending;
            Assert.That(smsSending.Price, Is.EqualTo(smsMessageSending.Cost));
            smsTechWrapper.VerifyAllExpectations();
        }
        public void SmsServiceInvalidNumber()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var smsTechWrapper = MockRepository.GenerateMock<ISmsTechWrapper>();
            var smsService = new SmsService { SmsTechWrapper = smsTechWrapper };

            var smsMessageSending = new SendSmsResponse { Cost = (float)0.06, MessageId = 123456, Error = new Error { Code = "RECIPIENTS_ERROR", Description = "can't send a message to those dudes!"} };
            smsTechWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof(SmsFailed)));
            Assert.That(response.Sid, Is.EqualTo(smsMessageSending.MessageId.ToString()));
            var smsFailed = response as SmsFailed;
            Assert.That(smsFailed.Code, Is.EqualTo(smsMessageSending.Error.Code));
            Assert.That(smsFailed.Message, Is.EqualTo(smsMessageSending.Error.Description));
            smsTechWrapper.VerifyAllExpectations();
        }
        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 SmsServiceSendingFails()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper };

            var smsMessageSending = new SMSMessage { Status = "failed", Sid = "sidReceipt", RestException = new RestException {Code = "code", Message = "message", MoreInfo = "moreInfo", Status = "status"}};
            twilioWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof(SmsFailed)));
            Assert.That(response.Sid, Is.EqualTo(smsMessageSending.Sid));
            var smsFailed = response as SmsFailed;
            Assert.That(smsFailed.Status, Is.EqualTo(smsMessageSending.RestException.Status));
            Assert.That(smsFailed.Code, Is.EqualTo(smsMessageSending.RestException.Code));
            Assert.That(smsFailed.Message, Is.EqualTo(smsMessageSending.RestException.Message));
            Assert.That(smsFailed.MoreInfo, Is.EqualTo(smsMessageSending.RestException.MoreInfo));
            twilioWrapper.VerifyAllExpectations();
        }
        public void SendOneMessageNow_SmsFailed()
        {
            var sendOneMessageNow = new SendOneMessageNow
                {
                    ConfirmationEmailAddress = "*****@*****.**",
                    CorrelationId = Guid.NewGuid(),
                    SmsData = new SmsData("mobile", "message"),
                    SmsMetaData = new SmsMetaData { Tags = new List<string> { "tag1", "tag2" }, Topic = "topic" }
                };

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var smsFailed = new SmsFailed("sid", "faile", "why why why");

            smsService
                .Expect(s => s.Send(sendOneMessageNow))
                .Return(smsFailed);
            var data = new SmsActionerData();

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a =>
                    {
                        a.SmsService = smsService;
                        a.Data = data;
                    })
                .WhenReceivesMessageFrom("somewhere")
                    .ExpectPublish<MessageFailedSending>(message =>
                        message.ConfirmationEmailAddress == sendOneMessageNow.ConfirmationEmailAddress &&
                        message.CorrelationId == sendOneMessageNow.CorrelationId &&
                        message.SmsData == sendOneMessageNow.SmsData &&
                        message.SmsMetaData == sendOneMessageNow.SmsMetaData &&
                        message.SmsFailed == smsFailed
                    )
                .When(a => a.Handle(sendOneMessageNow))
                .AssertSagaCompletionIs(true);

            smsService.VerifyAllExpectations();
        }
 public ActionResult Create(SendNowModel model)
 {
     var isValid = TryValidateModel(model);
     if(isValid)
     {
         model.MessageId = Guid.NewGuid();
         if (model.MessageBody.Length > 160)
             model.MessageBody = model.MessageBody.Substring(0, 160);
         var sendOneMessageNow = new SendOneMessageNow
         {
             CorrelationId = model.MessageId,
             SmsData = new SmsData(model.Number, model.MessageBody),
             ConfirmationEmailAddress = model.ConfirmationEmail,
             SmsMetaData = new SmsMetaData
             {
                 Tags = string.IsNullOrEmpty(model.Tags) ? null : model.Tags.Split(',').ToList().Select(t => t.Trim()).ToList(),
                 Topic = model.Topic
             }
         };
         Bus.Send(sendOneMessageNow);
         return RedirectToAction("Details", "SendNow", new { requestId = model.MessageId.ToString()});
     }
     return View("Create", model);
 }
        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();
        }
        public void SmsServiceSuccess()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message")};
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper };

            var smsMessage = new SMSMessage { Status = "sent", Sid = "sidReceipt", DateSent = DateTime.Now, Price = 3 };
            twilioWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessage);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof(SmsSent)));
            Assert.That(response.Sid, Is.EqualTo(smsMessage.Sid));
            var smsSent = response as SmsSent;
            Assert.That(smsSent.SmsConfirmationData.Receipt, Is.EqualTo(smsMessage.Sid));
            Assert.That(smsSent.SmsConfirmationData.SentAtUtc, Is.EqualTo(smsMessage.DateSent));
            Assert.That(smsSent.SmsConfirmationData.Price, Is.EqualTo(smsMessage.Price));
            twilioWrapper.VerifyAllExpectations();
        }
        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 SendSingleSmsNowSuccess()
        {
            var sendOneMessageNow = new SendOneMessageNow();

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var smsSent = new SmsSent(new SmsConfirmationData("r", DateTime.Now, 0.44m));
            smsService.Expect(s => s.Send(sendOneMessageNow)).Return(smsSent);

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a => a.SmsService = smsService)
                    .ExpectPublish<MessageSent>()
                    .ExpectReplyToOriginator<MessageSuccessfullyDelivered>()
                    .ExpectSendLocal<MessageSuccessfullyDelivered>()
                .When(a => a.Handle(sendOneMessageNow))
                .AssertSagaCompletionIs(true);
        }
        public void TimeoutPromptsMessageSending_Data()
        {
            var bus = MockRepository.GenerateMock<IBus>();

            var sendOneMessageNow = new SendOneMessageNow();
            bus.Expect(b => b.Send(Arg<SendOneMessageNow>.Is.NotNull))
                .WhenCalled(i => sendOneMessageNow = (SendOneMessageNow)((object[])(i.Arguments[0]))[0]);

            var dataId = Guid.NewGuid();
            var originalMessage = new ScheduleSmsForSendingLater { SmsData = new SmsData("3443", "message"), SmsMetaData = new SmsMetaData { Tags = new List<string> { "a", "b" }, Topic = "topic" } };
            var data = new ScheduledSmsData { Id = dataId, OriginalMessage = originalMessage};

            var scheduleSms = new ScheduleSms { Bus = bus, Data = data };
            var timeoutMessage = new ScheduleSmsTimeout();
            scheduleSms.Timeout(timeoutMessage);

            Assert.That(sendOneMessageNow.SmsData, Is.EqualTo(data.OriginalMessage.SmsData));
            Assert.That(sendOneMessageNow.SmsMetaData, Is.EqualTo(data.OriginalMessage.SmsMetaData));
            Assert.That(sendOneMessageNow.CorrelationId, Is.EqualTo(data.Id));

            bus.VerifyAllExpectations();
        }
 public SmsStatus Send(SendOneMessageNow messageToSend)
 {
     var createdSmsMessage = TwilioWrapper.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message);
     return ProcessSms(createdSmsMessage);
 }
        public void SmsServiceSending_WithoutERRORCode_ThrowsException()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var smsTechWrapper = MockRepository.GenerateMock<ISmsTechWrapper>();
            var smsService = new SmsService { SmsTechWrapper = smsTechWrapper };

            var smsMessageSending = new SendSmsResponse {Cost = (float) 0.06, MessageId = 123456, Error = new Error { Code = string.Empty } };
            smsTechWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            Assert.That(() => smsService.Send(messageToSend), Throws.ArgumentException.With.Message.Contains("Error code expected"));

            smsTechWrapper.VerifyAllExpectations();
        }
        public void TimeoutHandle_CheckStatus_SmsSent()
        {
            var timeout = new SmsPendingTimeout();
            var sendOneMessageNow = new SendOneMessageNow
                {
                    ConfirmationEmailAddress = "*****@*****.**",
                    CorrelationId = Guid.NewGuid(),
                    SmsData = new SmsData("mobile", "message"),
                    SmsMetaData = new SmsMetaData { Tags = new List<string> { "tag1", "tag2" }, Topic = "topic" }
                };
            var data = new SmsActionerData
            {
                Id = Guid.NewGuid(),
                OriginalMessage = sendOneMessageNow,
                Price = 0.06m,
                SmsRequestId = "123"
            };

            var smsService = MockRepository.GenerateMock<ISmsService>();
            var smsSent = new SmsSent("doesn't matter", DateTime.Now);

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

            Test.Initialize();
            Test.Saga<SmsActioner.SmsActioner>()
                .WithExternalDependencies(a =>
                    {
                        a.SmsService = smsService;
                        a.Data = data;
                    })
                .WhenReceivesMessageFrom("somewhere")
                    .ExpectPublish<MessageSent>(message =>
                        message.ConfirmationEmailAddress == sendOneMessageNow.ConfirmationEmailAddress &&
                        message.CorrelationId == sendOneMessageNow.CorrelationId &&
                        message.SmsData == sendOneMessageNow.SmsData &&
                        message.SmsMetaData == sendOneMessageNow.SmsMetaData &&
                        message.ConfirmationData.Price == data.Price &&
                        message.ConfirmationData.Receipt == data.SmsRequestId &&
                        message.ConfirmationData.SentAtUtc == smsSent.SentAtUtc
                    )
                .When(a => a.Timeout(timeout))
                .AssertSagaCompletionIs(true);

            smsService.VerifyAllExpectations();
        }