public void Inspect_WhenGivenABounceMailEventAndTheAnalyzerSayingItsBlockingIpBounceAndTheGroupIsDefault_ShouldDoNothing()
        {
            SetSettings();

            DroneActions.StoreCollection(new[]
                                             {
                                                 AddCreativePackage("$default$"),
                                             });

            var task1 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "$default$";
                                                                    },
                                                                x => x.WithIntervalInHours(1).RepeatForever()
                );

            DroneActions.StartScheduledTask(task1);
            Jobs.Drone().WaitForJobToStart(task1);

            FireEvent<DeliveryRealTimeDecision, AggregatedMailBounced>(x =>
                                                                           {
                                                                               x.MailEvents = new List<MailBounced>
                                                                                                  {
                                                                                                      new MailBounced
                                                                                                          {
                                                                                                              DomainGroup = "$default$",
                                                                                                              Recipient = "*****@*****.**",
                                                                                                              Message = "message meaning its a bad bounce",
                                                                                                              Classification = new MailClassfication { Type = Classification.TempBlock }
                                                                                                          }
                                                                                                  };
                                                                           });

            Jobs.Drone().AssertJobIsCurrentlyRunnnig<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "$default$");
        }
        public void Execute_WhenPackageWasAlreadySentThreeTimes_ShouldSetThePackageAndProcessedAndNotSendIt()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });

            var creativePackages = new[]
                                       {
                                           CreatePackage("*****@*****.**", "gmail"),
                                       };

            creativePackages[0].RetryCount = 3;

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            DroneActions.WaitForChangeOnStoredObject<CreativePackage>(x => x.Processed);
            Email.AssertEmailNotSentTo(new[] { "*****@*****.**" });
        }
        public void AssertTaskIsNotRunning(SendCreativePackagesWithIntervalTask task)
        {
            var schedulerFactory = new StdSchedulerFactory();
            var schedulers = schedulerFactory.AllSchedulers;

            foreach (var job in schedulers.Select(scheduler => JobExists(task, scheduler)).Where(job => job != null))
            {
                NUnit.Framework.Assert.Fail("The job still exist {0}", job.Key);
            }
        }
        public void Execute_WhenSendingTaskIsRunningWithGroupsTogetherWithOtherTasks_ShouldStopOnlyTheGroupGiven()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);
            DroneActions.EditSettings<EmailingSettings>(x =>
            {
                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                x.MailingDomain = "example.com";

            });

            DroneActions.StoreCollection(new[]
                                             {
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("hotmail"),
                                                 AddCreativePackage("hotmail")
                                             });

            var task1 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInHours(1)
                );

            var task2 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "hotmail";
                                                                    },
                                                                x => x.WithIntervalInHours(1)
                );

            var anotherTask = new FetchIntervalRulesTask();

            DroneActions.StartScheduledTask(task1);
            DroneActions.StartScheduledTask(task2);
            DroneActions.StartScheduledTask(anotherTask);

            Jobs.Drone().WaitForJobToStart(task1);
            Jobs.Drone().WaitForJobToStart(task2);
            Jobs.Drone().WaitForJobToStart(anotherTask);

            DroneActions.ExecuteCommand<PauseSpecificSendingJobsCommand>(x => x.Group = "gmail");

            Jobs.Drone().AssertJobIsCurrentlyRunnnig<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "hotmail");
            Jobs.Drone().AssertJobWasPaused<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "gmail");
        }
        public void Inspect_WhenGivenABounceMailEventAndTheAnalyzerSayingItsBlockingIpBounce_ShouldPersistTheNonSendingPolicy()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = "dev/null";
                                                                x.MailingDomain = "example.com";

                                                            });

            DroneActions.StoreCollection(new[]
                                             {
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("hotmail"),
                                                 AddCreativePackage("hotmail")
                                             });

            var task1 = new SendCreativePackagesWithIntervalTask(x =>
                                                                     {
                                                                         x.Group = "gmail";
                                                                     },
                                                                 x => x.WithIntervalInHours(1).RepeatForever()
                );

            var task2 = new SendCreativePackagesWithIntervalTask(x =>
                                                                     {
                                                                         x.Group = "hotmail";
                                                                     },
                                                                 x => x.WithIntervalInHours(1).RepeatForever()
                );

            DroneActions.StartScheduledTask(task1);
            DroneActions.StartScheduledTask(task2);

            Jobs.Drone().WaitForJobToStart(task1);
            Jobs.Drone().WaitForJobToStart(task2);

            FireEvent<DeliveryRealTimeDecision, AggregatedMailBounced>(x =>
                {
                    x.MailEvents = new List<MailBounced>
                        {
                            new MailBounced
                                {
                                    DomainGroup = "gmail",
                                    Recipient = "*****@*****.**",
                                    Message = "message meaning its a bad bounce",
                                    Classification = new MailClassfication {Type = Classification.TempBlock, TimeSpan = TimeSpan.FromHours(2)}
                                }
                        };
                });

            DroneActions.WaitForDocumentToExist<GroupsAndIndividualDomainsSendingPolicies>();

            var result = DroneActions.FindSingle<GroupsAndIndividualDomainsSendingPolicies>();

            result.GroupSendingPolicies.Should().ContainKey("gmail");
        }
        public void Inspect_WhenRulesAreNotMatchingForBouncedMail_ShouldDoNothing()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
            {
                x.WritingEmailsToDiskPath = "dev/null";
                x.MailingDomain = "example.com";

            });

            DroneActions.StoreCollection(new[]
                                             {
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("hotmail"),
                                                 AddCreativePackage("hotmail")
                                             });

            var task1 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInHours(1).RepeatForever()
                );

            var task2 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "hotmail";
                                                                    },
                                                                x => x.WithIntervalInHours(1).RepeatForever()
                );

            DroneActions.StartScheduledTask(task1);
            DroneActions.StartScheduledTask(task2);

            Jobs.Drone().WaitForJobToStart(task1);
            Jobs.Drone().WaitForJobToStart(task2);

            FireEvent<DeliveryRealTimeDecision, AggregatedMailBounced>(x =>
                                                                           {
                                                                               x.MailEvents = new List<MailBounced>
                                                                                                  {
                                                                                                      new MailBounced
                                                                                                          {
                                                                                                              DomainGroup = "gmail",
                                                                                                              Recipient = "*****@*****.**",
                                                                                                              Message = "message meaning its a good bounce",
                                                                                                              Classification = new MailClassfication { Type = Classification.NotClassified}

                                                                                                          }
                                                                                                  };
                                                                           });

            Jobs.Drone().AssertJobIsCurrentlyRunnnig<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "hotmail");
            Jobs.Drone().AssertJobIsCurrentlyRunnnig<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "gmail");
        }
        public void Inspect_WhenGivenABounceMailEventAndTheAnalyzerSayingItsBlockingIpBounce_ShouldStopSendingForTheGivenGroup()
        {
            SetSettings();

            DroneActions.StoreCollection(new[]
                                             {
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("gmail"),
                                                 AddCreativePackage("hotmail"),
                                                 AddCreativePackage("hotmail")
                                             });

            var task1 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInHours(1).RepeatForever()
                );

            var task2 = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "hotmail";
                                                                    },
                                                                x => x.WithIntervalInHours(1).RepeatForever()
                );

            DroneActions.StartScheduledTask(task1);
            DroneActions.StartScheduledTask(task2);

            Jobs.Drone().WaitForJobToStart(task1);
            Jobs.Drone().WaitForJobToStart(task2);

            FireEvent<DeliveryRealTimeDecision, AggregatedMailBounced>(x =>
                {
                    x.MailEvents = new List<MailBounced>
                        {
                            new MailBounced
                                {
                                    DomainGroup = "gmail",
                                    Recipient = "*****@*****.**",
                                    Message = "message meaning its a bad bounce",
                                    Classification = new MailClassfication {Type = Classification.TempBlock, TimeSpan = TimeSpan.FromHours(2)}
                                }
                        };
                });

            Jobs.Drone().AssertJobIsCurrentlyRunnnig<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "hotmail");
            Jobs.Drone().AssertJobWasPaused<SendCreativePackagesWithIntervalTask.Data>(x => x.Group == "gmail");
        }
        public void Execute_WhenPckagesArePresent_ShouldSetThemAsProcessed()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });

            var creativePackages = new[]
                                       {
                                           CreatePackage("*****@*****.**", "gmail"),
                                           CreatePackage("*****@*****.**", "gmail"),
                                           CreatePackage("*****@*****.**", "gmail")
                                       };

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            var recipients = creativePackages.Select(x => x.To).ToList();
            Email.AssertEmailsSentWithInterval(recipients, 5, 30);

            var packages = DroneActions.FindAll<CreativePackage>();
            packages.Should().OnlyContain(x => x.Processed);
        }
        public void Execute_WhenThereIsATaskThatWasRecentlyTouched_ShouldSendItLast()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });
            var creativePackages = new[]
                {
                    CreatePackage("*****@*****.**", "gmail", DateTime.UtcNow.AddMinutes(-10)),
                    CreatePackage("*****@*****.**", "gmail", DateTime.UtcNow.AddMinutes(-30)),
                    CreatePackage("*****@*****.**", "gmail", DateTime.UtcNow.AddMinutes(-20)),
                };

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            var recipients = new[] { "*****@*****.**", "*****@*****.**", "*****@*****.**" };
            Email.AssertEmailsSentInOrder(recipients, 30);
        }
        public void Execute_WhenThereAreProcessedAndNonProcessed_ShouldSendOnlyTheUnprocessedPackages()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });

            var creativePackages = new[]
                                       {
                                           CreatePackage("*****@*****.**", "gmail"),
                                           CreatePackage("*****@*****.**", "gmail"),
                                           CreatePackage("*****@*****.**", "gmail")
                                       };

            creativePackages[2].Processed = true;

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            var recipients = creativePackages.Select(x => x.To).ToList();
            Email.AssertEmailsSentWithInterval(recipients.Take(2).ToList(), 5, 30);
            Email.AssertEmailNotSentTo(new[] { recipients[2] }, 30);
        }
        public void Execute_WhenTheGroupIsDefaultAndSomePackagesAreNotDeliverable_ShouldNotSendThatPackages()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });

            var creativePackages = new[]
                                       {
                                           CreatePackage("*****@*****.**", "$default$"),
                                           CreatePackage("*****@*****.**", "$default$")
                                       };

            creativePackages[1].Processed = true;

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "$default$";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            var recipients = creativePackages.Select(x => x.To).ToList();

            Email.AssertEmailsSentTo(new[] { recipients[0] });
            Email.AssertEmailNotSentTo(new[] { recipients[1] });
        }
        public void Execute_WhenTaskHasAnIntervalAndNoPackagesWereFound_ShouldStopExecutingTheTaskAndRemoveIt()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });

            var creativePackages = new[]
                                       {
                                           CreatePackage("*****@*****.**", "gmail"),
                                           CreatePackage("*****@*****.**", "gmail"),
                                           CreatePackage("*****@*****.**", "gmail")
                                       };

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            var recipients = creativePackages.Select(x => x.To).ToList();
            Email.AssertEmailsSentWithInterval(recipients, 5, 30);

            Thread.Sleep(6000);

            Tasks.AssertTaskIsNotRunning(task);
        }
        public void Execute_WhenSending_ShouldIncreaseTheRetryCountByOne()
        {
            DroneActions.EditSettings<DroneSettings>(x => x.StoreHostname = DefaultHostUrl);

            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";

                                                            });

            var creativePackages = new[]
                                       {
                                           CreatePackage("*****@*****.**", "gmail"),
                                       };

            DroneActions.StoreCollection(creativePackages);

            var task = new SendCreativePackagesWithIntervalTask(x =>
                                                                    {
                                                                        x.Group = "gmail";
                                                                    },
                                                                x => x.WithIntervalInSeconds(5).RepeatForever()
                );

            DroneActions.StartScheduledTask(task);

            DroneActions.WaitForChangeOnStoredObject<CreativePackage>(x => x.RetryCount == 1);
        }