diff --git a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/implementation/MonthlyAlertRuleImpl.java b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/implementation/MonthlyAlertRuleImpl.java new file mode 100644 index 0000000000..d1f28454f3 --- /dev/null +++ b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/implementation/MonthlyAlertRuleImpl.java @@ -0,0 +1,17 @@ +package org.smartregister.chw.core.implementation; + +import android.content.Context; + +import org.smartregister.chw.core.rule.MonthlyAlertRule; + +public class MonthlyAlertRuleImpl extends MonthlyAlertRule { + + public MonthlyAlertRuleImpl(Context context, long lastVisitDateLong, long dateCreatedLong) { + super(context, lastVisitDateLong, dateCreatedLong); + } + + @Override + public String getRuleKey() { + return null; + } +} diff --git a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/FpAlertRuleTest.java b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/FpAlertRuleTest.java index 55aca3adf0..7db64cce5d 100644 --- a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/FpAlertRuleTest.java +++ b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/FpAlertRuleTest.java @@ -98,4 +98,26 @@ public void buttonStatusNotDueIfDateBeforeDueAndExpiry() { } -} + @Test + public void canGetDueDate() { + dueDate = LocalDate.now().plusDays(10).toDateTime(LocalTime.MIDNIGHT); + ReflectionHelpers.setField(fpAlertRule, "dueDate", dueDate); + Assert.assertEquals(dueDate.toDate(), fpAlertRule.getDueDate()); + } + + @Test + public void canGetOverdueDate() { + overDueDate = LocalDate.now().minusDays(1).toDateTime(LocalTime.MIDNIGHT); + ReflectionHelpers.setField(fpAlertRule, "overDueDate", overDueDate); + Assert.assertEquals(overDueDate.toDate(), fpAlertRule.getOverDueDate()); + } + + @Test + public void canGetExpiryDate() { + expiryDate = LocalDate.now().plusDays(10).toDateTime(LocalTime.MIDNIGHT); + ReflectionHelpers.setField(fpAlertRule, "expiryDate", expiryDate); + Assert.assertEquals(expiryDate.toDate(), fpAlertRule.getExpiryDate()); + } + + +} \ No newline at end of file diff --git a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/MonthlyAlertRuleTest.java b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/MonthlyAlertRuleTest.java index 42a1aa106c..7343911805 100644 --- a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/MonthlyAlertRuleTest.java +++ b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/rule/MonthlyAlertRuleTest.java @@ -3,33 +3,45 @@ import android.content.Context; import org.joda.time.DateTime; +import org.joda.time.LocalDate; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.MockitoAnnotations; +import org.powermock.reflect.Whitebox; import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.chw.core.BaseRobolectricTest; +import org.smartregister.chw.core.implementation.MonthlyAlertRuleImpl; import java.util.Date; + /** * Created by Qazi Abubakar */ -public class MonthlyAlertRuleTest { - private final Context context = RuntimeEnvironment.application; - private final long lastVisitDate = new DateTime().minusDays(7).toDate().getTime(); - private final long dateCreated = new DateTime().minusDays(30).toDate().getTime(); - private final MonthlyAlertRule monthlyAlertRule = new MonthlyAlertRule(context, lastVisitDate, dateCreated) { - @Override - public String getRuleKey() { - return "familyKitAlertRule"; - } - }; + +public class MonthlyAlertRuleTest extends BaseRobolectricTest { + + private MonthlyAlertRule monthlyAlertRule; @Before public void setUp() { - MockitoAnnotations.initMocks(this); + Context context = RuntimeEnvironment.application; + monthlyAlertRule = new MonthlyAlertRuleImpl(context, new Date().getTime(), new Date().getTime()); } + @Test + public void lastDueDateIs1stIfLastVisitEarlierThanCreated() throws Exception { + Date firstDayOfThisMonth = LocalDate.now().withDayOfMonth(1).toDate(); + LocalDate dateCreated = LocalDate.fromDateFields(firstDayOfThisMonth).plusWeeks(2); + LocalDate lastVisitDate = LocalDate.fromDateFields(firstDayOfThisMonth).plusWeeks(1); + + ReflectionHelpers.setField(monthlyAlertRule, "dateCreated", dateCreated); + ReflectionHelpers.setField(monthlyAlertRule, "lastVisitDate", lastVisitDate); + + Assert.assertEquals(firstDayOfThisMonth, Whitebox.invokeMethod(monthlyAlertRule, "getLastDueDate")); + } + @Test public void testGetLastDayOfMonth(){ DateTime first = new DateTime(new Date()).withDayOfMonth(1);