package com.cronutils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;

public class Issue143Test {
    private CronParser parser;
    private ZonedDateTime currentDateTime;

    @Before
    public void setUp() throws Exception {
        // Make sure that current date is before Dec-31
        currentDateTime = ZonedDateTime.of(LocalDateTime.of(2016, 12, 20, 12, 00),
                ZoneId.systemDefault());

        parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
    }

    @Test
    public void testCase1() {
        ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 31 12 ? *"));
        ZonedDateTime actual = et.lastExecution(currentDateTime).get();

        ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2015, 12, 31, 12, 00),
                ZoneId.systemDefault());
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testCase2() {
        ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 ? 12 SAT#5 *"));
        ZonedDateTime actual = et.lastExecution(currentDateTime).get();

        ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2012, 12, 29, 12, 00),
                ZoneId.systemDefault());
        Assert.assertEquals(expected, actual);
    }

    //    @Test
    public void testCase3() {
        ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 31 1/1 ? *"));
        ZonedDateTime actual = et.lastExecution(currentDateTime).get();

        ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2015, 12, 31, 12, 00),
                ZoneId.systemDefault());
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testCase4() {
        ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 ? 1/1 SAT#5 *"));
        ZonedDateTime actual = et.lastExecution(currentDateTime).get();

        ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2016, 10, 29, 12, 00),
                ZoneId.systemDefault());
        Assert.assertEquals(expected, actual);
    }

}

@@ -655,25 +655,24 @@
 12, 20, 12, 
-0
 0),%0A        
@@ -988,37 +988,88 @@
 urrentDateTime).
-get()
+isPresent()?et.lastExecution(currentDateTime).get():null
 ;%0A%0A        Zoned
@@ -1132,33 +1132,32 @@
 15, 12, 31, 12, 
-0
 0),%0A            
@@ -1423,37 +1423,88 @@
 urrentDateTime).
-get()
+isPresent()?et.lastExecution(currentDateTime).get():null
 ;%0A%0A        Zoned
@@ -1567,33 +1567,32 @@
 12, 12, 29, 12, 
-0
 0),%0A            
@@ -1680,25 +1680,31 @@
 %0A%0A    //
-    
 @Test
+ TODO #289
 %0A    pub
@@ -1868,37 +1868,88 @@
 urrentDateTime).
-get()
+isPresent()?et.lastExecution(currentDateTime).get():null
 ;%0A%0A        Zoned
@@ -2016,25 +2016,24 @@
 12, 31, 12, 
-0
 0),%0A        
@@ -2312,21 +2312,72 @@
 teTime).
-get()
+isPresent()?et.lastExecution(currentDateTime).get():null
 ;%0A%0A     
@@ -2456,17 +2456,16 @@
 29, 12, 
-0
 0),%0A    

add .isPresent() check on Optional<> statements. Comment test for issue #289