private void VerifyMocks(FakeStep <string, string> step, string readString, string processedString) { step.MockReader.Verify(reader => reader.Read(0, 1), Times.Once); step.MockProcessor.Verify(p => p.Process(readString)); step.MockWriter.Verify(writer => writer.Write( It.Is <IEnumerable <string> >(items => items.Count() == 1 && items.Contains(processedString)))); }
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"); }
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()); }
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)); }
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)); }
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))); }
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)); }
public static FakeStep <T, U> Create(string name) { var reader = new Mock <IReader <T> >(); var writer = new Mock <IWriter <U> >(); var processor = new Mock <IProcessor <T, U> >(); var fakeStep = new FakeStep <T, U>(name, reader, processor, writer); fakeStep.SetReader(reader.Object); fakeStep.SetProcessor(processor.Object); fakeStep.SetWriter(writer.Object); // Set chunk size to one for tests fakeStep.WithChunkSize(1); return(fakeStep); }
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>())); }