public void GetOneMandrillEventsAndThrowsExceptionFailsOnBothTries()
        {
            var leadRepository = new Mock <LocalRepository <Contact> >()
            {
                CallBase = true
            };
            var xDataProvider     = new Mock <IExternalDataProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            var mandrill_event =
                "[{\"event\":\"send\",\"msg\":{\"ts\":1365109999,\"subject\":\"This an example webhook message\",\"email\":\"[email protected]\"," +
                "\"sender\":\"[email protected]\",\"tags\":[\"webhook-example\"],\"opens\":[],\"clicks\":[],\"state\":\"sent\",\"metadata\":{\"user_id\":111}," +
                "\"_id\":\"1\",\"_version\":\"exampleaaaaaaaaaaaaaaa1\"},\"_id\":\"1\",\"ts\":1385020180}]";

            leadRepository.Setup(er => er.SaveChanges()).Throws(new Exception());

            var clientRepository = new LocalRepository <Client>();
            var client           = ModelHelper.TestClient1AllDataNoReferences;

            client.EmailAddress = "*****@*****.**";
            clientRepository.Add(client);
            var serverTime     = new Mock <IServerTime>();
            var contactService = new Mock <IContactService>();

            var emailController = new WebhookController(leadRepository.Object, clientRepository, contactService.Object, serverTime.Object, xDataProvider.Object, contactAutoRating.Object);
            var result          = emailController.ManrillWebhook(mandrill_event);

            Assert.AreEqual(0, leadRepository.Object.All().Count());
            contactAutoRating.Verify(ar => ar.SetAutoRating(It.IsAny <Contact>()), Times.Exactly(2));

            contactService.Verify(cs => cs.NotifyClientsForNewContactWithEmail(It.IsAny <int>()), Times.Never);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithPhoneNotification(It.IsAny <int>()), Times.Never);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithSmsNotification(It.IsAny <int>()), Times.Never);
        }
        public void GetOneMandrillEventsAndThrowsExceptionAndRetriesSuccessfully()
        {
            var leadRepository = new Mock <LocalRepository <Contact> >()
            {
                CallBase = true
            };
            var xDataProvider     = new Mock <IExternalDataProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();

            var mandrill_event =
                "[{\"event\":\"send\",\"msg\":{\"ts\":1365109999,\"subject\":\"This an example webhook message\",\"email\":\"[email protected]\"," +
                "\"sender\":\"[email protected]\",\"tags\":[\"webhook-example\"],\"opens\":[],\"clicks\":[],\"state\":\"sent\",\"metadata\":{\"user_id\":111}," +
                "\"_id\":\"1\",\"_version\":\"exampleaaaaaaaaaaaaaaa1\"},\"_id\":\"1\",\"ts\":1385020180}]";


            var    numCalls = 0;
            Action throwExceptionOnFirstCall = () =>
            {
                if (numCalls == 0)
                {
                    numCalls++;
                    throw new Exception();
                }
                else
                {
                    numCalls++;
                }
            };

            leadRepository.Setup(er => er.SaveChanges())
            .Callback(throwExceptionOnFirstCall);

            var clientRepository = new LocalRepository <Client>();
            var client           = ModelHelper.TestClient1AllDataNoReferences;

            client.EmailAddress = "*****@*****.**";
            clientRepository.Add(client);

            var contactService = new Mock <IContactService>();
            var serverTime     = new Mock <IServerTime>();

            serverTime.Setup(st => st.RequestStarted).Returns(new DateTime(2013, 11, 21, 8, 50, 0)); //2013-11-21 08:49:40 on email date

            var emailController = new WebhookController(leadRepository.Object, clientRepository, contactService.Object, serverTime.Object, xDataProvider.Object, contactAutoRating.Object);
            var result          = emailController.ManrillWebhook(mandrill_event);

            Assert.AreEqual(1, leadRepository.Object.All().Count());
            string propertyValue = leadRepository.Object.Where(l => l.Property.Any(lp => lp.Type == "MandrillId" && lp.Value == "1")).Single().Property.First(lp => lp.Type == "Subject").Value;

            Assert.AreEqual("This an example webhook message", propertyValue);
            contactAutoRating.Verify(ar => ar.SetAutoRating(It.IsAny <Contact>()), Times.Exactly(2));

            contactService.Verify(cs => cs.NotifyClientsForNewContactWithEmail(It.IsAny <int>()), Times.Once);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithPhoneNotification(It.IsAny <int>()), Times.Once);
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithSmsNotification(It.IsAny <int>()), Times.Once());
        }
        public void MultiMandrilleventsWithSameIdIsNotDuplicatedInRepository()
        {
            var leadRepository    = new LocalRepository <Contact>();
            var xDataProvider     = new Mock <IExternalDataProvider>();
            var contactAutoRating = new Mock <IContactAutoRating>();
            var mandrill_event    =
                "[{\"event\":\"send\",\"msg\":{\"ts\":1365109999,\"subject\":\"This an example webhook message\",\"email\":\"[email protected]\"," +
                "\"sender\":\"[email protected]\",\"tags\":[\"webhook-example\"],\"opens\":[],\"clicks\":[],\"state\":\"sent\",\"metadata\":{\"user_id\":111}," +
                "\"_id\":\"1\",\"_version\":\"exampleaaaaaaaaaaaaaaa1\"},\"_id\":\"1\",\"ts\":1385020180}," +

                "{\"event\":\"send\",\"msg\":{\"ts\":1365109999,\"subject\":\"This an example webhook message\",\"email\":\"[email protected]\"," +
                "\"sender\":\"[email protected]\",\"tags\":[\"webhook-example\"],\"opens\":[],\"clicks\":[],\"state\":\"sent\",\"metadata\":{\"user_id\":111}," +
                "\"_id\":\"1\",\"_version\":\"exampleaaaaaaaaaaaaaaa1\"},\"_id\":\"1\",\"ts\":1385020180}," +

                "{\"event\":\"send\",\"msg\":{\"ts\":1365109999,\"subject\":\"This an example webhook message2\",\"email\":\"[email protected]\"," +
                "\"sender\":\"[email protected]\",\"tags\":[\"webhook-example\"],\"opens\":[],\"clicks\":[],\"state\":\"sent\",\"metadata\":{\"user_id\":222}," +
                "\"_id\":\"2\",\"_version\":\"exampleaaaaaaaaaaaaaaa2\"},\"_id\":\"2\",\"ts\":1385020180}]";

            var clientRepository = new LocalRepository <Client>();
            var client           = ModelHelper.TestClient1AllDataNoReferences;

            client.EmailAddress = "*****@*****.**";
            clientRepository.Add(client);

            var client2 = ModelHelper.TestClient1AllDataNoReferences;

            client2.EmailAddress = "*****@*****.**";
            client2.Id           = 2;
            clientRepository.Add(client2);

            var serverTime = new Mock <IServerTime>();

            serverTime.Setup(st => st.RequestStarted).Returns(new DateTime(2013, 11, 21, 8, 50, 0)); //2013-11-21 08:49:40 on email date

            var contactService = new Mock <IContactService>();

            var emailController = new WebhookController(leadRepository, clientRepository, contactService.Object, serverTime.Object, xDataProvider.Object, contactAutoRating.Object);
            var result          = emailController.ManrillWebhook(mandrill_event);

            Assert.AreEqual(2, leadRepository.All().Count());

            Assert.AreEqual("This an example webhook message", leadRepository.Where(l => l.Property.Any(lp => lp.Type == "MandrillId" && lp.Value == "1")).First().Property.First(lp => lp.Type == "Subject").Value);
            Assert.AreEqual("This an example webhook message2", leadRepository.Where(l => l.Property.Any(lp => lp.Type == "MandrillId" && lp.Value == "2")).First().Property.First(lp => lp.Type == "Subject").Value);
            contactAutoRating.Verify(ar => ar.SetAutoRating(It.IsAny <Contact>()), Times.Exactly(3));

            contactService.Verify(cs => cs.NotifyClientsForNewContactWithEmail(It.IsAny <int>()), Times.Exactly(2));
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithPhoneNotification(It.IsAny <int>()), Times.Exactly(2));
            contactService.Verify(cs => cs.NotifyClientsForNewContactWithSmsNotification(It.IsAny <int>()), Times.Exactly(2));
        }