Ejemplo n.º 1
0
        public void JobCanContainMultipleSteps()
        {
            var step1 = FakeStep <string, string> .Create("step1");

            var step2 = FakeStep <string, string> .Create("step2");

            step1.MockReader.Setup(r => r.Read(0, 1)).Returns(new[] { "STEP1: item read" });
            step1.MockProcessor.Setup(p => p.Process(It.IsAny <string>())).Returns("STEP1: item processed");

            step2.MockReader.Setup(r => r.Read(0, 1)).Returns(new[] { "STEP2: item read" });
            step2.MockProcessor.Setup(p => p.Process(It.IsAny <string>())).Returns("STEP2: item processed");

            var jobRepo = new Mock <IJobRepository>();

            jobRepo.Setup(j => j.GetStartIndex(It.IsAny <string>())).Returns(new StepContext());

            new Job(jobRepo.Object)
            .AddStep(step1)
            .AddStep(step2)
            .Start();

            jobRepo.Verify(j => j.GetStartIndex("step1"), Times.Once);
            jobRepo.Verify(j => j.GetStartIndex("step2"), Times.Once);
            VerifyMocks(step1, "STEP1: item read", "STEP1: item processed");
            VerifyMocks(step2, "STEP2: item read", "STEP2: item processed");
        }
Ejemplo n.º 2
0
        public void IfSkipLimitIsNotSetThenThrowExceptionOnFirstError()
        {
            var step = FakeStep <string, string> .Create("step1");

            step.MockReader.Setup(r => r.Read(It.IsAny <long>(), It.IsAny <int>())).Throws <FlatFileParseException>();

            Assert.Throws <FlatFileParseException>(() => step.Process(new StepContext(), _jobRepo.Object));
            _jobRepo.Verify(r => r.GetExceptionCount(It.IsAny <SkipContext>()), Times.Never());
        }
Ejemplo n.º 3
0
        public void StepsMustHaveUniqueNames()
        {
            var step1 = FakeStep <string, string> .Create("step1");

            var step2 = FakeStep <string, string> .Create("step1");

            var jobRepo = new Mock <IJobRepository>();

            Assert.Throws <InvalidStepNameException>(() => new Job(jobRepo.Object).AddStep(step1).AddStep(step2));
        }
Ejemplo n.º 4
0
        public void StepIndexNotIncrementedWhenExceptionThrownAndSkipLimitReached()
        {
            var step = FakeStep <string, string> .Create("step1");

            step.SkipLimit(1).SkippableExceptions(typeof(Exception));

            _jobRepo.Setup(r => r.GetExceptionCount(It.Is <SkipContext>(ctx => ctx.StepIndex == 2))).Returns(1);

            step.MockReader.Setup(r => r.Read(It.IsAny <long>(), It.IsAny <int>())).Returns(new[] { "line1" });
            step.MockProcessor.Setup(p => p.Process(It.IsAny <string>())).Throws <Exception>();

            Assert.Throws <Exception>(() => step.Process(new StepContext(), _jobRepo.Object));
        }
Ejemplo n.º 5
0
        public void WriterShouldBeCalledWithTheSpecifiedChunkSize(int chunkSize, int itemCount)
        {
            var step1 = FakeStep <string, string> .Create("step1");

            step1.WithChunkSize(chunkSize);

            step1.MockReader.Setup(r => r.Read(0, chunkSize)).Returns(Enumerable.Range(0, chunkSize).Select(s => "item read"));
            step1.MockProcessor.Setup(p => p.Process(It.IsAny <string>())).Returns("processed");

            step1.Process(new StepContext(), _jobRepo.Object);

            step1.MockWriter.Verify(w => w.Write(It.Is <IEnumerable <string> >(items => items.Count() == itemCount)));
        }
Ejemplo n.º 6
0
        public void WhenSkipLimitIsReachedThrowException()
        {
            // Using an in-memory job repository in order to increment exception count
            var inMemoryJobRepo = new InMemoryJobRepository();

            var step = FakeStep <string, string> .Create("step1");

            step.WithChunkSize(10);
            step.SkipLimit(5)
            .SkippableExceptions(typeof(Exception));

            step.MockReader.Setup(r => r.Read(It.IsAny <long>(), It.IsAny <int>())).Throws <FlatFileParseException>();

            Assert.Throws <FlatFileParseException>(() => step.Process(new StepContext(), inMemoryJobRepo));
        }
Ejemplo n.º 7
0
        public void SkippableExceptionsAreSkipped()
        {
            var step = FakeStep <string, string> .Create("step1");

            step.SkipLimit(1)
            .SkippableExceptions(typeof(FlatFileParseException), typeof(Exception));

            step.MockReader.Setup(r => r.Read(0, 1)).Throws <FlatFileParseException>();

            var stepContext = new StepContext {
                StepName = "step1"
            };

            step.Process(stepContext, _jobRepo.Object);

            _jobRepo.Verify(j => j.GetExceptionCount(It.Is <SkipContext>(ctx => ctx.StepName == "step1")));
            _jobRepo.Verify(j => j.SaveExceptionInfo(It.IsAny <SkipContext>(), It.IsAny <int>()));
        }