package org.dhis2.mobile.utils.date.expiryday;

import org.joda.time.DateTimeConstants;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.junit.Test;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

public class BiWeeklyExpiryDateValidatorTest {
    private static final String PATTERN = "yyyy'BiW'ww";

    @Test
    public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceExpiryDays() {
        LocalDate periodDate = new LocalDate();
        periodDate = periodDate.minusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);
        int expiryDays = Days.daysBetween(periodDate.plusDays(13), new LocalDate()).getDays();
        BiWeeklyExpiryDayValidator weeklyExpiryDayValidator = new BiWeeklyExpiryDayValidator(expiryDays,
                periodDate.toString(PATTERN));
        assertFalse(weeklyExpiryDayValidator.canEdit());
    }

    @Test
    public void testCanEditPreviousPeriodEndsSameTodayMinusExpiryDaysPlusOne() {
        LocalDate periodDate = new LocalDate();
        periodDate = periodDate.minusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);
        int expiryDays = Days.daysBetween(periodDate.plusDays(13), new LocalDate()).getDays() + 1;
        BiWeeklyExpiryDayValidator weeklyExpiryDayValidator = new BiWeeklyExpiryDayValidator(expiryDays,
                periodDate.toString(PATTERN));
        assertTrue(weeklyExpiryDayValidator.canEdit());
    }

    @Test
    public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceMinusOneExpiryDays() {
        LocalDate periodDate = new LocalDate();
        periodDate = periodDate.minusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);
        int expiryDays = Days.daysBetween(periodDate.plusDays(13), new LocalDate()).getDays() - 1;
        BiWeeklyExpiryDayValidator weeklyExpiryDayValidator = new BiWeeklyExpiryDayValidator(expiryDays,
                periodDate.toString(PATTERN));
        assertFalse(weeklyExpiryDayValidator.canEdit());
    }
}

@@ -41,16 +41,116 @@
 ryday;%0A%0A
+import static junit.framework.Assert.assertFalse;%0Aimport static junit.framework.Assert.assertTrue;%0A%0A
 import o
@@ -268,108 +268,8 @@
 t;%0A%0A
-import static junit.framework.Assert.assertFalse;%0Aimport static junit.framework.Assert.assertTrue;%0A%0A
 publ
@@ -311,16 +311,64 @@
 rTest %7B%0A
+    //This dhis pattern not work with LocalDate%0A
     priv
@@ -518,238 +518,8 @@
 ) %7B%0A
-        LocalDate periodDate = new LocalDate();%0A        periodDate = periodDate.minusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);%0A        int expiryDays = Days.daysBetween(periodDate.plusDays(13), new LocalDate()).getDays();%0A
     
@@ -599,33 +599,36 @@
 iryDayValidator(
-e
+getE
 xpiryDays,%0A     
@@ -612,32 +612,35 @@
 or(getExpiryDays
+(0)
 ,%0A              
@@ -645,35 +645,26 @@
     
-periodDate.toString(PATTERN
+getPreviousPeriod(
 ));%0A
@@ -830,233 +830,316 @@
     
-LocalDate periodDate = new LocalDate();%0A        periodDate = periodDate.minusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY
+BiWeeklyExpiryDayValidator weeklyExpiryDayValidator = new BiWeeklyExpiryDayValidator(getExpiryDays(1),%0A                getPreviousPeriod());%0A        assertTrue(weeklyExpiryDayValidator.canEdit()
 );%0A    
+%7D%0A%0A
     
-int expiryDays = Days.daysBetween(periodDate.plusDays(13), new LocalDate()).get
+@Test%0A    public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceMinusOneExpiry
 Days() 
-+ 1;
+%7B
 %0A   
@@ -1220,33 +1220,36 @@
 iryDayValidator(
-e
+getE
 xpiryDays,%0A     
@@ -1233,32 +1233,36 @@
 or(getExpiryDays
+(-1)
 ,%0A              
@@ -1267,35 +1267,26 @@
     
-periodDate.toString(PATTERN
+getPreviousPeriod(
 ));%0A
@@ -1291,35 +1291,36 @@
 ;%0A        assert
-Tru
+Fals
 e(weeklyExpiryDa
@@ -1352,273 +1352,534 @@
   %7D%0A
-%0A
     
-@Test%0A    public void testCanNotEditPreviousPeriodEndsSameTodayMinusDifferenceMinusOneExpiryDays() %7B%0A        LocalDate periodDate = new LocalDate();%0A        periodDate = periodDate.minusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);%0A        int expiryDays =
+//get the expiry days from the end of the period of the current period%0A    private int getExpiryDays(int expiryDay)%7B%0A        LocalDate periodDate = new LocalDate();%0A        LocalDate periodInitRange = new LocalDate();%0A        periodInitRange = periodInitRange.withWeekOfWeekyear(1);%0A        do%7B%0A            periodInitRange = periodInitRange.plusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);%0A        %7D while (periodInitRange.isBefore(periodDate));%0A        periodInitRange = periodInitRange.minusWeeks(2);%0A        return
  Day
@@ -1902,23 +1902,29 @@
 riod
-Date.pl
+InitRange.min
 usDays(1
 3), 
@@ -1923,9 +1923,8 @@
 ys(1
-3
 ), n
@@ -1953,219 +1953,557 @@
 s() 
-- 1
++ expiryDay
 ;%0A    
+%7D%0A%0A
     
-BiWeeklyExpiryDayValidator weeklyExpiryDayValidator = new BiWeeklyExpiryDayValidator(expiryDays,%0A                periodDate.toString(PATTERN));%0A        assertFalse(weeklyExpiryDayValidator.canEdit()
+//get the previous period starting from the actual period%0A    private String getPreviousPeriod() %7B%0A        LocalDate periodDate = new LocalDate();%0A        LocalDate periodInitRange = new LocalDate();%0A        periodInitRange = periodInitRange.withWeekOfWeekyear(1);%0A        int count=0;%0A        do%7B%0A            count++;%0A            periodInitRange = periodInitRange.plusWeeks(2).withDayOfWeek(DateTimeConstants.MONDAY);%0A        %7D while (periodInitRange.isBefore(periodDate));%0A        return periodDate.getYear()+%22BiW%22+(count-1
 );%0A 

fix expiry day test