package com.cronutils;

import com.cronutils.model.Cron;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import org.junit.Test;
import org.threeten.bp.ZonedDateTime;

import org.junit.Before;
import static org.junit.Assert.assertEquals;

public class Issue228Test {
	private CronDefinition cronDefinition;
	@Before
	public void setUp(){
		cronDefinition = CronDefinitionBuilder.defineCron()
        	.withMinutes().and()
        	.withHours().and()
        	.withDayOfMonth().supportsL().and()
        	.withMonth().and()
        	.withDayOfWeek().withValidRange(0,7).withMondayDoWValue(1).and()
        	.enforceStrictRanges()
        	.matchDayOfWeekAndDayOfMonth()
        	.instance();
	}
    /**
     * Issue #228: dayOfWeek just isn't honored in the cron next execution evaluation and needs to be
     */
    @Test
    public void testFirstMondayOfTheMonthNextExecution() {
        CronParser parser = new CronParser(cronDefinition);

        // This is 9am on a day between the 1st and 7th which is a Monday (in this case it should be Oct 2
        Cron myCron = parser.parse("0 9 1-7 * 1");
        ZonedDateTime time = ZonedDateTime.parse("2017-09-29T14:46:01.166-07:00");
        assertEquals(ZonedDateTime.parse("2017-10-02T09:00-07:00"), ExecutionTime.forCron(myCron).nextExecution(time).get());
    }

    @Test
    public void testEveryWeekdayFirstWeekOfMonthNextExecution() {
        CronParser parser = new CronParser(cronDefinition);

        // This is 9am on Mon-Fri day between the 1st and 7th (in this case it should be Oct 2)
        Cron myCron = parser.parse("0 9 1-7 * 1-5");
        ZonedDateTime time = ZonedDateTime.parse("2017-09-29T14:46:01.166-07:00");
        assertEquals(ZonedDateTime.parse("2017-10-02T09:00-07:00"), ExecutionTime.forCron(myCron).nextExecution(time).get());
    }

    @Test
    public void testEveryWeekendFirstWeekOfMonthNextExecution() {
        CronParser parser = new CronParser(cronDefinition);

        // This is 9am on Sat and Sun day between the 1st and 7th (in this case it should be Oct 1)
        Cron myCron = parser.parse("0 9 1-7 * 6-7");
        ZonedDateTime time = ZonedDateTime.parse("2017-09-29T14:46:01.166-07:00");
        assertEquals(ZonedDateTime.parse("2017-10-01T09:00-07:00"), ExecutionTime.forCron(myCron).nextExecution(time).get());
    }

    @Test
    public void testEveryWeekdaySecondWeekOfMonthNextExecution() {
        CronParser parser = new CronParser(cronDefinition);

        // This is 9am on Mon-Fri day between the 8th and 14th (in this case it should be Oct 9 Mon)
        Cron myCron = parser.parse("0 9 8-14 * 1-5");
        ZonedDateTime time = ZonedDateTime.parse("2017-09-29T14:46:01.166-07:00");
        assertEquals(ZonedDateTime.parse("2017-10-09T09:00-07:00"), ExecutionTime.forCron(myCron).nextExecution(time).get());
    }

    @Test
    public void testEveryWeekendForthWeekOfMonthNextExecution() {
        CronParser parser = new CronParser(cronDefinition);

        // This is 9am on Sat and Sun day between the 22nd and 28th (in this case it should be Oct 22)
        Cron myCron = parser.parse("0 9 22-28 * 6-7");
        ZonedDateTime time = ZonedDateTime.parse("2017-09-29T14:46:01.166-07:00");
        assertEquals(ZonedDateTime.parse("2017-10-22T09:00-07:00"), ExecutionTime.forCron(myCron).nextExecution(time).get());
    }
}

@@ -54,45 +54,8 @@
 on;%0A
-import com.cronutils.model.CronType;%0A
 impo
@@ -417,22 +417,46 @@
 t %7B%0A
-%09private CronD
+    /**%0A     * This is the UNIX cron d
 efin
@@ -465,57 +465,113 @@
 ion 
-cronDefinition;%0A%09@Before%0A%09public void setUp()%7B%0A%09%09
+with a single modification to match both Day Of Week and Day Of Month%0A     */%0A    private CronDefinition 
 cron
@@ -622,25 +622,24 @@
 n()%0A        
-%09
 .withMinutes
@@ -651,25 +651,24 @@
 d()%0A        
-%09
 .withHours()
@@ -674,33 +674,32 @@
 ).and()%0A        
-%09
 .withDayOfMonth(
@@ -704,20 +704,8 @@
 h().
-supportsL().
 and(
@@ -710,25 +710,24 @@
 d()%0A        
-%09
 .withMonth()
@@ -741,17 +741,16 @@
         
-%09
 .withDay
@@ -775,16 +775,17 @@
 Range(0,
+ 
 7).withM
@@ -801,16 +801,37 @@
 alue(1).
+withIntMapping(7, 0).
 and()%0A  
@@ -836,17 +836,16 @@
         
-%09
 .enforce
@@ -867,17 +867,16 @@
         
-%09
 .matchDa
@@ -901,151 +901,101 @@
 th()
-%0A        %09.instance();%0A%09%7D%0A    /**%0A     * Issue #228: dayOfWeek just isn't honored in the cron next execution evaluation and needs to be%0A     */
+ // the regular UNIX cron definition permits matching either DoW or DoM%0A        .instance();%0A
 %0A   

Solve merge conflict.