package be.raildelays.batch.tasklet;

import be.raildelays.batch.decider.MaxMonthsDecider;
import be.raildelays.domain.Sens;
import be.raildelays.domain.xls.ExcelRow;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.item.support.IteratorItemReader;
import org.springframework.batch.test.MetaDataInstanceFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;

@RunWith(BlockJUnit4ClassRunner.class)
public class MaxMonthsDeciderTest {

    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("dd/MM/yyyy");
    // The S.U.T.
    private MaxMonthsDecider decider;
    private JobExecution jobExecution;
    private StepExecution stepExecution;

    @Before
    public void setUp() throws ParseException {
        jobExecution = MetaDataInstanceFactory.createJobExecution();
        stepExecution = MetaDataInstanceFactory.createStepExecution();
        decider = new MaxMonthsDecider();
        decider.setMaxNumberOfMonth(6);
    }

    @Test
    public void testCompleted() throws ParseException {
        decider.setReader(new IteratorItemReader<>(Arrays.asList(
                new ExcelRow
                        .Builder(FORMAT.parse("01/01/2000"), Sens.ARRIVAL)
                        .build(false),
                new ExcelRow
                        .Builder(FORMAT.parse("31/01/2000"), Sens.ARRIVAL)
                        .build(false)
        )));

        FlowExecutionStatus status = decider.decide(jobExecution, stepExecution);

        Assert.assertEquals(FlowExecutionStatus.COMPLETED, status);
    }


    @Test
    public void testCompletedWithMaxMonths() throws ParseException {
        decider.setReader(new IteratorItemReader<>(Arrays.asList(
                new ExcelRow
                        .Builder(FORMAT.parse("01/01/2000"), Sens.ARRIVAL)
                        .build(false),
                new ExcelRow
                        .Builder(FORMAT.parse("01/07/2000"), Sens.ARRIVAL)
                        .build(false)
        )));

        FlowExecutionStatus status = decider.decide(jobExecution, stepExecution);

        Assert.assertEquals(MaxMonthsDecider.COMPLETED_WITH_MAX_MONTHS, status);
    }
}

@@ -702,19 +702,173 @@
 ava.
-util.Arrays
+time.LocalDate;%0Aimport java.time.ZoneId;%0Aimport java.time.temporal.ChronoUnit;%0Aimport java.time.temporal.TemporalUnit;%0Aimport java.util.Arrays;%0Aimport java.util.Date
 ;%0A%0A@
@@ -1164,16 +1164,74 @@
 ecution;
+%0A    private static final LocalDate NOW = LocalDate.now();
 %0A%0A    @B
@@ -1653,32 +1653,40 @@
                 
+        
 new ExcelRow%0A   
@@ -1685,67 +1685,253 @@
 lRow
-%0A                        .Builder(FORMAT.parse(%2201/01/2000%22
+.Builder(Date.from(NOW%0A                                .minus(1, ChronoUnit.MONTHS)%0A                                .atStartOfDay(ZoneId.systemDefault())%0A                                .toInstant() // 1 month before Now%0A                        
 ), S
@@ -1935,32 +1935,40 @@
 , Sens.ARRIVAL)%0A
+        
                 
@@ -1998,32 +1998,40 @@
                 
+        
 new ExcelRow%0A   
@@ -2030,67 +2030,251 @@
 lRow
-%0A                        .Builder(FORMAT.parse(%2231/01/2000%22
+.Builder(Date.from(NOW%0A                                .plus(1, ChronoUnit.MONTHS)%0A                                .atStartOfDay(ZoneId.systemDefault())%0A                                .toInstant() // 1 month after Now%0A                        
 ), S
@@ -2302,32 +2302,40 @@
                 
+        
 .build(false)%0A  
@@ -2332,34 +2332,68 @@
 (false)%0A        
+ 
-))
+       )%0A                )%0A        
 );%0A%0A        Flow
@@ -2692,32 +2692,40 @@
                 
+        
 new ExcelRow%0A   
@@ -2724,67 +2724,253 @@
 lRow
-%0A                        .Builder(FORMAT.parse(%2201/01/2000%22
+.Builder(Date.from(NOW%0A                                .minus(6, ChronoUnit.MONTHS)%0A                                .atStartOfDay(ZoneId.systemDefault())%0A                                .toInstant() // 6 month before Now%0A                        
 ), S
@@ -2998,32 +2998,40 @@
                 
+       
+ 
 .build(false),%0A 
@@ -3037,32 +3037,40 @@
                 
+        
 new ExcelRow%0A   
@@ -3069,67 +3069,251 @@
 lRow
-%0A                        .Builder(FORMAT.parse(%2201/07/2000%22
+.Builder(Date.from(NOW%0A                                .plus(2, ChronoUnit.MONTHS)%0A                                .atStartOfDay(ZoneId.systemDefault())%0A                                .toInstant() // 2 month after Now%0A                        
 ), S
@@ -3341,32 +3341,40 @@
                 
+        
 .build(false)%0A  
@@ -3379,18 +3379,52 @@
         
+  
-))
+      )%0A                )%0A        
 );%0A%0A    

Use today as the threshold date