public void Execute_JobClassProvided_ShouldScheduleNewJob()
        {
            const string groupName = "Group";
            const string jobName   = "Job";

            var stub = new SchedulerHostStub(new[] { typeof(System.Object) });

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                Group       = groupName,
                Job         = jobName,
                JobClass    = typeof(System.Object).ToString(),
                Name        = "Trigger",
                TriggerType = "Simple"
            });

            result.AssertSuccessfull();

            GroupStub   group   = stub.GetSingleGroup();
            JobStub     job     = group.GetSingleJob();
            TriggerStub trigger = job.GetSingleTrigger();

            Assert.That(group.Name, Is.EqualTo(groupName));
            Assert.That(job.Name, Is.EqualTo(jobName));
            Assert.That(job.JobType, Is.EqualTo(typeof(System.Object)));
            Assert.That(trigger.Name, Is.EqualTo("Trigger"));
        }
        public void Execute_UnknownInputType_ShouldReturnValidationIssue()
        {
            var stub = new SchedulerHostStub();

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                TriggerType = "Cron",
                JobDataMap  = new[]
                {
                    new JobDataItem
                    {
                        Key           = "CustomKey",
                        Value         = "CustomCode",
                        InputTypeCode = "custom"
                    },
                }
            });

            stub.AssertEmpty();

            result.AssertSuccessfull();

            Assert.That(result.ValidationErrors, Is.Not.Null);
            Assert.That(result.ValidationErrors["CustomKey"], Is.EqualTo("Unknown input type: custom"));
        }
        public void Execute_ValidJobDataConversionIssue_ShouldReturnValidationIssue()
        {
            var converter = Substitute.For <IInputTypeConverter>();

            converter.Convert("CustomCode").Throws(new Exception("Custom conversion issue"));

            var stub = new SchedulerHostStub(new Type[0]);

            var command = new AddTriggerCommand(() => stub.Value, new[] { new RegisteredInputType(new InputType("custom"), converter) });

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                TriggerType = "Cron",
                JobDataMap  = new[]
                {
                    new JobDataItem
                    {
                        Key           = "CustomKey",
                        Value         = "CustomCode",
                        InputTypeCode = "custom"
                    },
                }
            });

            stub.AssertEmpty();

            result.AssertSuccessfull();

            Assert.That(result.ValidationErrors, Is.Not.Null);
            Assert.That(result.ValidationErrors["CustomKey"], Is.EqualTo("Custom conversion issue"));
        }
        public void Execute_ValidJobDataMapWithConversion_ShouldPassJobDataMap()
        {
            var customValue = new { };

            var converter = Substitute.For <IInputTypeConverter>();

            converter.Convert("CustomCode").Returns(customValue);

            AddTriggerOutput result = AssertTriggerJobDataMap(
                new AddTriggerInput
            {
                Job         = "Default",
                TriggerType = "Cron",
                JobDataMap  = new[]
                {
                    new JobDataItem
                    {
                        Key           = "CustomKey",
                        Value         = "CustomCode",
                        InputTypeCode = "custom"
                    },
                }
            },
                jobDataMap =>
            {
                Assert.That(jobDataMap, Is.Not.Null);
                Assert.That(jobDataMap["CustomKey"], Is.EqualTo(customValue));
            },
                new RegisteredInputType(new InputType("custom"), converter));

            AssertNoValidationIssues(result);
        }
        public void Execute_NoJobClass_ShouldAddTriggerToExistingJob()
        {
            const string groupName = "Group";
            const string jobName   = "Job";

            var stub = new SchedulerHostStub();

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                Group       = groupName,
                Job         = jobName,
                Name        = "Trigger",
                TriggerType = "Simple"
            });

            result.AssertSuccessfull();

            GroupStub   group   = stub.GetSingleGroup();
            JobStub     job     = group.GetSingleJob();
            TriggerStub trigger = job.GetSingleTrigger();

            Assert.That(group.Name, Is.EqualTo(groupName));
            Assert.That(job.Name, Is.EqualTo(jobName));
            Assert.That(job.JobType, Is.Null);
            Assert.That(trigger.Name, Is.EqualTo("Trigger"));
        }
        public void Execute_ValidJobDataMapWithoutConversion_ShouldPassJobDataMap()
        {
            AddTriggerOutput result = AssertTriggerJobDataMap(
                new AddTriggerInput
            {
                Job         = "Default",
                TriggerType = "Cron",
                JobDataMap  = new[]
                {
                    new JobDataItem
                    {
                        Key           = "StringKey",
                        Value         = "StringValue",
                        InputTypeCode = "string"
                    },
                }
            },
                jobDataMap =>
            {
                Assert.That(jobDataMap, Is.Not.Null);
                Assert.That(jobDataMap["StringKey"], Is.EqualTo("StringValue"));
            },
                new RegisteredInputType(new InputType("string"), null));

            AssertNoValidationIssues(result);
        }
        public void Execute_NoJobClassOrNameProvided_ShouldFail()
        {
            var stub = new SchedulerHostStub(new[] { typeof(System.Object) });

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                Group       = "Group",
                TriggerType = "Simple"
            });

            Assert.That(result.Success, Is.False);
            Assert.That(stub.SchedulerCommander.Groups.Count, Is.EqualTo(0));
        }
        public void Execute_JobClassIsNotAllowed_ShouldFail()
        {
            var stub = new SchedulerHostStub();

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                JobClass    = typeof(System.Object).ToString(),
                Name        = "Trigger",
                TriggerType = "Simple"
            });

            Assert.That(result.Success, Is.False, "Success");
        }
        private AddTriggerOutput AssertTriggerJobDataMap(
            AddTriggerInput input,
            Action <IDictionary <string, object> > assertAction,
            params RegisteredInputType[] inputTypes)
        {
            var stub = new SchedulerHostStub();

            var command = new AddTriggerCommand(() => stub.Value, inputTypes);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(input);

            result.AssertSuccessfull();
            assertAction(stub.GetSingleGroup().GetSingleJob().GetSingleTrigger().TriggerJobData);

            return(result);
        }
        private AddTriggerOutput AssertTriggerType(AddTriggerInput input, Action <TriggerType> assertAction)
        {
            var stub = new SchedulerHostStub();

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(input);

            result.AssertSuccessfull();

            TriggerStub trigger = stub.GetSingleGroup().GetSingleJob().GetSingleTrigger();

            assertAction(trigger.Trigger);

            return(result);
        }
        public void Execute_UnknownTriggerType_ShouldReturnError()
        {
            var stub = new SchedulerHostStub();

            var command = new AddTriggerCommand(() => stub.Value, new RegisteredInputType[0]);

            AddTriggerOutput result = (AddTriggerOutput)command.Execute(new AddTriggerInput
            {
                TriggerType = "Unsupported"
            });

            Assert.That(result.Success, Is.False, "Success");
            Assert.That(result.ErrorMessage, Is.Not.Null);

            stub.AssertEmpty();
        }
 private void AssertNoValidationIssues(AddTriggerOutput result)
 {
     Assert.That(result.ValidationErrors, Is.Null.Or.Empty);
 }