From 58a9178f4903c9a57664613a5be1ef6dc4464a03 Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Mon, 28 Jun 2021 21:59:50 +0300 Subject: [PATCH 1/4] Fix negative offset not respected for calculations --- gradle.properties | 2 +- .../immunization/domain/VaccineTrigger.java | 7 +- .../immunization/util/VaccinatorUtils.java | 132 +++++++++--------- .../domain/VaccineTriggerTest.java | 27 +++- .../util/VaccinatorUtilsTest.java | 95 +++++++++++++ 5 files changed, 191 insertions(+), 72 deletions(-) create mode 100644 opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java diff --git a/gradle.properties b/gradle.properties index 6cc2a24b..d7849652 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.0.11-SNAPSHOT +VERSION_NAME=3.0.12-SNAPSHOT VERSION_CODE=2 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Immunization diff --git a/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java b/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java index 3056357b..38de81b6 100644 --- a/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java +++ b/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java @@ -20,7 +20,7 @@ public class VaccineTrigger { private final VaccineRepo.Vaccine prerequisite; private final VaccineCondition vaccineCondition; - public VaccineTrigger(String offset, String window, Reference reference) { + private VaccineTrigger(String offset, String window, Reference reference) { this.reference = reference; this.offset = offset; prerequisite = null; @@ -28,7 +28,7 @@ public VaccineTrigger(String offset, String window, Reference reference) { this.window = window; } - public VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite) { + private VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite) { reference = Reference.PREREQUISITE; this.offset = offset; this.prerequisite = prerequisite; @@ -36,7 +36,7 @@ public VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequi vaccineCondition = null; } - public VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite, VaccineCondition vaccineCondition) { + private VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite, VaccineCondition vaccineCondition) { reference = Reference.PREREQUISITE; this.offset = offset; this.prerequisite = prerequisite; @@ -44,6 +44,7 @@ public VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequi this.vaccineCondition = vaccineCondition; } + //This init method should be the only entry point public static VaccineTrigger init(String vaccineCategory, Due data) { if (data != null) { //Vaccine Relaxation Logic diff --git a/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java b/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java index 98a507c7..6e976875 100644 --- a/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java +++ b/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java @@ -1101,66 +1101,64 @@ public static List getVaccineFiles(Context context) throws IOException { } public static void processConfigCalendarOffset(Calendar calendar, String offset) { + int operator = 1; + String[] offsetTokens = offset.split(","); - Matcher m1 = getPrefixSymbolMatcher(offset); - if (m1.find()) { - String operatorString = m1.group(1); - String valueString = m1.group(2); + for (int i = 0; i < offsetTokens.length; i++) { + Matcher m1 = getPrefixSymbolMatcher(offsetTokens[i]); + if (m1.find()) { + String operatorString = m1.group(1); - int operator = 1; - if ("-".equals(operatorString)) { - operator = -1; + if ("-".equals(operatorString)) { + operator = -1; + } } - String[] values = valueString.split(","); - for (int i = 0; i < values.length; i++) { - Matcher m2 = getSuffixSymbolMatcher(values[i]); - - if (m2.find()) { - int curValue = operator * Integer.parseInt(m2.group(1)); - String fieldString = m2.group(2); - int field = Calendar.DATE; - if ("d".equals(fieldString)) { - field = Calendar.DATE; - } else if ("m".equals(fieldString)) { - field = Calendar.MONTH; - } else if ("y".equals(fieldString)) { - field = Calendar.YEAR; - } - - calendar.add(field, curValue); + Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]); + if (m2.find()) { + int curValue = operator * Integer.parseInt(m2.group(1)); + String fieldString = m2.group(2); + int field = Calendar.DATE; + if ("d".equals(fieldString)) { + field = Calendar.DATE; + } else if ("m".equals(fieldString)) { + field = Calendar.MONTH; + } else if ("y".equals(fieldString)) { + field = Calendar.YEAR; } + + calendar.add(field, curValue); } } } public static DateTime processConfigDateTimeOffset(DateTime afterOffset, String offset) { - Matcher m1 = getPrefixSymbolMatcher(offset); - if (m1.find()) { - String operatorString = m1.group(1); - String valueString = m1.group(2); - - int operator = 1; - if ("-".equals(operatorString)) { - operator = -1; + int operator = 1; + String[] offsetTokens = offset.split(","); + + for (int i = 0; i < offsetTokens.length; i++) { + Matcher m1 = getPrefixSymbolMatcher(offsetTokens[i]); + if (m1.find()) { + String operatorString = m1.group(1); + + if ("-".equals(operatorString)) { + operator = -1; + } } - String[] values = valueString.split(","); - for (int i = 0; i < values.length; i++) { - Matcher m2 = getSuffixSymbolMatcher(values[i]); - - if (m2.find()) { - int curValue = operator * Integer.parseInt(m2.group(1)); - String fieldString = m2.group(2); - if ("d".endsWith(fieldString)) { - afterOffset = afterOffset.plusDays(curValue); - } else if ("m".equals(fieldString)) { - afterOffset = afterOffset.plusMonths(curValue); - } else if ("y".equals(fieldString)) { - afterOffset = afterOffset.plusYears(curValue); - } + Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]); + if (m2.find()) { + int curValue = operator * Integer.parseInt(m2.group(1)); + String fieldString = m2.group(2); + if ("d".endsWith(fieldString)) { + afterOffset = afterOffset.plusDays(curValue); + } else if ("m".equals(fieldString)) { + afterOffset = afterOffset.plusMonths(curValue); + } else if ("y".equals(fieldString)) { + afterOffset = afterOffset.plusYears(curValue); } + } } return afterOffset; @@ -1169,34 +1167,34 @@ public static DateTime processConfigDateTimeOffset(DateTime afterOffset, String public static int processOffsetValueInDays(String offset) { if (offset == null) return -1; - Matcher m1 = getPrefixSymbolMatcher(offset); + int operator = 1; int days = 0; - if (m1.find()) { - String operatorString = m1.group(1); - String valueString = m1.group(2); + String[] offsetTokens = offset.split(","); + + for (int i = 0; i < offsetTokens.length; i++) { + Matcher m1 = getPrefixSymbolMatcher(offsetTokens[i]); + if (m1.find()) { + String operatorString = m1.group(1); - int operator = 1; - if ("-".equals(operatorString)) { - operator = -1; + if ("-".equals(operatorString)) { + operator = -1; + } } - String[] values = valueString.split(","); - for (int i = 0; i < values.length; i++) { - Matcher m2 = getSuffixSymbolMatcher(values[i]); - - if (m2.find()) { - int curValue = operator * Integer.parseInt(m2.group(1)); - String fieldString = m2.group(2); - if ("d".endsWith(fieldString)) { - days += curValue; - } else if ("m".equals(fieldString)) { - days += (int) Math.round(30.44 * curValue); - } else if ("y".equals(fieldString)) { - days += 366 * curValue; - } + Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]); + if (m2.find()) { + int curValue = operator * Integer.parseInt(m2.group(1)); + String fieldString = m2.group(2); + if ("d".endsWith(fieldString)) { + days += curValue; + } else if ("m".equals(fieldString)) { + days += (int) Math.round(30.44 * curValue); + } else if ("y".equals(fieldString)) { + days += 366 * curValue; } } } + return days; } diff --git a/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java b/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java index 483aca42..a60dcc87 100644 --- a/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java +++ b/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java @@ -10,11 +10,17 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.rule.PowerMockRule; +import org.smartregister.Context; +import org.smartregister.commonregistry.CommonFtsObject; import org.smartregister.immunization.BaseUnitTest; import org.smartregister.immunization.ImmunizationLibrary; import org.smartregister.immunization.db.VaccineRepo; import org.smartregister.immunization.domain.jsonmapping.Due; +import org.smartregister.immunization.repository.VaccineRepository; import org.smartregister.immunization.util.IMConstants; +import org.smartregister.repository.Repository; +import org.smartregister.service.AlertService; +import org.smartregister.util.AppProperties; import org.smartregister.util.JsonFormUtils; import java.util.Calendar; @@ -34,6 +40,15 @@ public class VaccineTriggerTest extends BaseUnitTest { @Mock private ImmunizationLibrary immunizationLibrary; + @Mock + private VaccineRepository vaccineRepository; + @Mock + private Context context; + @Mock + private AlertService alertService; + @Mock + private AppProperties appProperties; + public static final String CHILD = "child"; public static final String stringdata1 = "{ \"reference\": \"dob\", \"offset\": \"+0d\"}"; public static final String stringdata2 = "{\"reference\": \"prerequisite\", \"prerequisite\": \"OPV 1\", \"offset\": \"+28d\"}"; @@ -41,6 +56,7 @@ public class VaccineTriggerTest extends BaseUnitTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); + mockImmunizationLibrary(immunizationLibrary, context, vaccineRepository, alertService, appProperties); } @Test @@ -77,8 +93,17 @@ public void assertGetWindowTestReturnsCurrentWindow() { String notNull = vaccineTrigger.getWindow(); Mockito.verify(vaccineTrigger, Mockito.times(1)).getWindow(); Assert.assertNull(notNull); - vaccineTrigger = new VaccineTrigger("", "win", VaccineRepo.Vaccine.opv0); + + Due due = new Due(); + due.reference = "prerequisite"; + due.prerequisite = "OPV 0"; + due.offset = ""; + due.window = "win"; + + vaccineTrigger = VaccineTrigger.init(CHILD, due); Assert.assertNotNull(vaccineTrigger.getWindow()); + Assert.assertEquals("win",vaccineTrigger.getWindow()); + } } diff --git a/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java b/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java new file mode 100644 index 00000000..798417e4 --- /dev/null +++ b/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java @@ -0,0 +1,95 @@ +package org.smartregister.immunization.util; + +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 java.util.Calendar; + +/** + * Created by ndegwamartin on 28/06/2021. + */ +public class VaccinatorUtilsTest { + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testProcessConfigCalendarOffsetReturnsCorrectCalendarInstanceForAddedMonthAndDays() { + LocalDate date = LocalDate.parse("2020-03-28"); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date.toDate()); + VaccinatorUtils.processConfigCalendarOffset(calendar, "+3m,+7d"); + + Assert.assertEquals(Calendar.JULY, calendar.get(Calendar.MONTH)); + Assert.assertEquals(5, calendar.get(Calendar.DAY_OF_MONTH)); + + } + + @Test + public void testProcessConfigCalendarOffsetReturnsCorrectCalendarInstanceForAddedMonthAndSubtractedDays() { + LocalDate date = LocalDate.parse("2020-04-28"); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date.toDate()); + VaccinatorUtils.processConfigCalendarOffset(calendar, "+2m,-7d"); + + Assert.assertEquals(Calendar.JUNE, calendar.get(Calendar.MONTH)); + Assert.assertEquals(21, calendar.get(Calendar.DAY_OF_MONTH)); + + } + + @Test + public void testProcessConfigCalendarOffsetReturnsCorrectDateTimeInstanceForAddedMonthAndDays() { + DateTime dateTime = new DateTime(2020, 3, 28, 0, 0, 0, 0); + DateTime resultDateTime = VaccinatorUtils.processConfigDateTimeOffset(dateTime, "+3m,+7d"); + + Assert.assertEquals(7, resultDateTime.getMonthOfYear()); + Assert.assertEquals(5, resultDateTime.getDayOfMonth()); + + } + + @Test + public void testProcessConfigCalendarOffsetReturnsCorrectDateTimeInstanceForAddedMonthAndSubtractedDays() { + + DateTime dateTime = new DateTime(2020, 4, 28, 12, 0, 0, 0); + DateTime resultDateTime = VaccinatorUtils.processConfigDateTimeOffset(dateTime, "+2m,-7d"); + + Assert.assertEquals(6, resultDateTime.getMonthOfYear()); + Assert.assertEquals(21, resultDateTime.getDayOfMonth()); + + } + + @Test + public void testProcessOffsetValueInDaysForAddedMonthAndDays() { + int days = VaccinatorUtils.processOffsetValueInDays("+3m,+7d"); + Assert.assertEquals(98, days); + + } + + @Test + public void testProcessOffsetValueInDaysForAddedMonthAndSubtractedDays() { + + int days = VaccinatorUtils.processOffsetValueInDays("+2m,-7d"); + Assert.assertEquals(54, days); + + } + + @Test + public void testProcessOffsetValueInDaysForAddedYearsMonthAndDays() { + int days = VaccinatorUtils.processOffsetValueInDays("+2y, +3m,+7d"); + Assert.assertEquals(830, days); + + } + + @Test + public void testProcessOffsetValueInDaysForSubtractedYearsMonthAndDays() { + int days = VaccinatorUtils.processOffsetValueInDays("-2y, -3m,-7d"); + Assert.assertEquals(-830, days); + + } +} \ No newline at end of file From b05167b08d352be54d17d81117baece1535fc886 Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 29 Jun 2021 08:21:20 +0300 Subject: [PATCH 2/4] Refactor and clean up --- gradle.properties | 2 +- opensrp-immunization/build.gradle | 2 +- .../immunization/domain/VaccineTrigger.java | 8 ---- .../immunization/util/VaccinatorUtils.java | 48 +++++++------------ .../domain/VaccineTriggerTest.java | 4 +- 5 files changed, 21 insertions(+), 43 deletions(-) diff --git a/gradle.properties b/gradle.properties index d7849652..fcec1918 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.0.12-SNAPSHOT +VERSION_NAME=4.0.0-SNAPSHOT VERSION_CODE=2 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Immunization diff --git a/opensrp-immunization/build.gradle b/opensrp-immunization/build.gradle index 3b67830a..8bc31f7a 100644 --- a/opensrp-immunization/build.gradle +++ b/opensrp-immunization/build.gradle @@ -120,7 +120,7 @@ dependencies { exclude group: 'com.ibm.fhir', module: 'fhir-path' } - implementation('org.smartregister:opensrp-client-native-form:1.14.8.1-SNAPSHOT@aar') { + implementation('org.smartregister:opensrp-client-native-form:1.14.8.2-SNAPSHOT@aar') { transitive = true exclude group: 'com.android.support', module: 'recyclerview-v7' exclude group: 'com.google.code.gson', module: 'gson' diff --git a/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java b/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java index 38de81b6..27bc13b3 100644 --- a/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java +++ b/opensrp-immunization/src/main/java/org/smartregister/immunization/domain/VaccineTrigger.java @@ -28,14 +28,6 @@ private VaccineTrigger(String offset, String window, Reference reference) { this.window = window; } - private VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite) { - reference = Reference.PREREQUISITE; - this.offset = offset; - this.prerequisite = prerequisite; - this.window = window; - vaccineCondition = null; - } - private VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite, VaccineCondition vaccineCondition) { reference = Reference.PREREQUISITE; this.offset = offset; diff --git a/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java b/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java index 6e976875..f8214fec 100644 --- a/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java +++ b/opensrp-immunization/src/main/java/org/smartregister/immunization/util/VaccinatorUtils.java @@ -1101,22 +1101,14 @@ public static List getVaccineFiles(Context context) throws IOException { } public static void processConfigCalendarOffset(Calendar calendar, String offset) { - int operator = 1; + String[] offsetTokens = offset.split(","); for (int i = 0; i < offsetTokens.length; i++) { - Matcher m1 = getPrefixSymbolMatcher(offsetTokens[i]); - if (m1.find()) { - String operatorString = m1.group(1); - - if ("-".equals(operatorString)) { - operator = -1; - } - } Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]); if (m2.find()) { - int curValue = operator * Integer.parseInt(m2.group(1)); + int curValue = getOperator(offsetTokens[i]) * Integer.parseInt(m2.group(1)); String fieldString = m2.group(2); int field = Calendar.DATE; if ("d".equals(fieldString)) { @@ -1134,22 +1126,14 @@ public static void processConfigCalendarOffset(Calendar calendar, String offset) } public static DateTime processConfigDateTimeOffset(DateTime afterOffset, String offset) { - int operator = 1; + String[] offsetTokens = offset.split(","); for (int i = 0; i < offsetTokens.length; i++) { - Matcher m1 = getPrefixSymbolMatcher(offsetTokens[i]); - if (m1.find()) { - String operatorString = m1.group(1); - - if ("-".equals(operatorString)) { - operator = -1; - } - } Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]); if (m2.find()) { - int curValue = operator * Integer.parseInt(m2.group(1)); + int curValue = getOperator(offsetTokens[i]) * Integer.parseInt(m2.group(1)); String fieldString = m2.group(2); if ("d".endsWith(fieldString)) { afterOffset = afterOffset.plusDays(curValue); @@ -1167,23 +1151,14 @@ public static DateTime processConfigDateTimeOffset(DateTime afterOffset, String public static int processOffsetValueInDays(String offset) { if (offset == null) return -1; - int operator = 1; int days = 0; String[] offsetTokens = offset.split(","); for (int i = 0; i < offsetTokens.length; i++) { - Matcher m1 = getPrefixSymbolMatcher(offsetTokens[i]); - if (m1.find()) { - String operatorString = m1.group(1); - - if ("-".equals(operatorString)) { - operator = -1; - } - } Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]); if (m2.find()) { - int curValue = operator * Integer.parseInt(m2.group(1)); + int curValue = getOperator(offsetTokens[i]) * Integer.parseInt(m2.group(1)); String fieldString = m2.group(2); if ("d".endsWith(fieldString)) { days += curValue; @@ -1199,6 +1174,19 @@ public static int processOffsetValueInDays(String offset) { } + private static int getOperator(String offsetToken) { + int operator = 1; + Matcher m1 = getPrefixSymbolMatcher(offsetToken); + if (m1.find()) { + String operatorString = m1.group(1); + + if ("-".equals(operatorString)) { + operator = -1; + } + } + return operator; + } + private static Matcher getSuffixSymbolMatcher(String value) { Pattern p2 = Pattern.compile("(\\d+)([dwmy]{1})"); return p2.matcher(value); diff --git a/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java b/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java index a60dcc87..342d786b 100644 --- a/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java +++ b/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java @@ -11,14 +11,12 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.rule.PowerMockRule; import org.smartregister.Context; -import org.smartregister.commonregistry.CommonFtsObject; import org.smartregister.immunization.BaseUnitTest; import org.smartregister.immunization.ImmunizationLibrary; import org.smartregister.immunization.db.VaccineRepo; import org.smartregister.immunization.domain.jsonmapping.Due; import org.smartregister.immunization.repository.VaccineRepository; import org.smartregister.immunization.util.IMConstants; -import org.smartregister.repository.Repository; import org.smartregister.service.AlertService; import org.smartregister.util.AppProperties; import org.smartregister.util.JsonFormUtils; @@ -102,7 +100,7 @@ public void assertGetWindowTestReturnsCurrentWindow() { vaccineTrigger = VaccineTrigger.init(CHILD, due); Assert.assertNotNull(vaccineTrigger.getWindow()); - Assert.assertEquals("win",vaccineTrigger.getWindow()); + Assert.assertEquals("win", vaccineTrigger.getWindow()); } From a09d80e81abf08f3d2146dac8e3e73d15d38f09b Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 29 Jun 2021 13:03:59 +0300 Subject: [PATCH 3/4] Add unit test on Vaccinator Utils --- .../util/VaccinatorUtilsTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java b/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java index 798417e4..538c193c 100644 --- a/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java +++ b/opensrp-immunization/src/test/java/org/smartregister/immunization/util/VaccinatorUtilsTest.java @@ -1,24 +1,57 @@ package org.smartregister.immunization.util; +import android.content.Context; +import android.content.res.AssetManager; + 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.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import java.io.IOException; import java.util.Calendar; +import java.util.List; /** * Created by ndegwamartin on 28/06/2021. */ public class VaccinatorUtilsTest { + @Mock + private Context context; + + @Mock + private AssetManager assetManager; + @Before public void setUp() { MockitoAnnotations.initMocks(this); } + @Test + public void testGetVaccineFilesReturnsListOfFilesFromVaccinesFolder() throws IOException { + + Mockito.doReturn(assetManager).when(context).getAssets(); + Mockito.doReturn(new String[]{VaccinatorUtils.vaccines_file, VaccinatorUtils.special_vaccines_file, VaccinatorUtils.mother_vaccines_file}).when(assetManager).list(VaccinatorUtils.vaccines_folder); + + List resultList = VaccinatorUtils.getVaccineFiles(context); + Assert.assertNotNull(resultList); + Assert.assertEquals(VaccinatorUtils.vaccines_file, resultList.get(0)); + Assert.assertEquals(VaccinatorUtils.special_vaccines_file, resultList.get(1)); + Assert.assertEquals(VaccinatorUtils.mother_vaccines_file, resultList.get(2)); + + ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); + Mockito.verify(assetManager).list(stringArgumentCaptor.capture()); + String folderName = stringArgumentCaptor.getValue(); + Assert.assertEquals(VaccinatorUtils.vaccines_folder, folderName); + + } + @Test public void testProcessConfigCalendarOffsetReturnsCorrectCalendarInstanceForAddedMonthAndDays() { LocalDate date = LocalDate.parse("2020-03-28"); From effc622a3130ac869cb1099ae26f051e4a881834 Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 29 Jun 2021 14:50:23 +0300 Subject: [PATCH 4/4] Add VaccineTrigger Unit tests --- .../domain/VaccineTriggerTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java b/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java index 342d786b..f8cee6c6 100644 --- a/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java +++ b/opensrp-immunization/src/test/java/org/smartregister/immunization/domain/VaccineTriggerTest.java @@ -10,6 +10,7 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.rule.PowerMockRule; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.immunization.BaseUnitTest; import org.smartregister.immunization.ImmunizationLibrary; @@ -104,4 +105,41 @@ public void assertGetWindowTestReturnsCurrentWindow() { } + @Test + public void testInitCreatesValidVaccineTriggerForExpiryConstructorParamWithLMP() { + + Due expiry = new Due(); + expiry.reference = "LMP"; + expiry.offset = "+1y, +3m, -1d"; + + VaccineTrigger vaccineTrigger = VaccineTrigger.init(expiry); + Assert.assertNotNull(vaccineTrigger); + + Assert.assertEquals("LMP", ReflectionHelpers.getField(vaccineTrigger, "reference").toString()); + Assert.assertEquals("+1y, +3m, -1d", ReflectionHelpers.getField(vaccineTrigger, "offset")); + + } + + @Test + public void testInitCreatesValidVaccineTriggerForExpiryConstructorParamWithDOB() { + + Due expiry = new Due(); + expiry.reference = "DOB"; + expiry.offset = "+2m,+2d"; + + VaccineTrigger vaccineTrigger = VaccineTrigger.init(expiry); + Assert.assertNotNull(vaccineTrigger); + + Assert.assertEquals("DOB", ReflectionHelpers.getField(vaccineTrigger, "reference").toString()); + Assert.assertEquals("+2m,+2d", ReflectionHelpers.getField(vaccineTrigger, "offset")); + + } + + @Test + public void testInitReturnsNullForNullExpiryConstructorParam() { + Due nullDueParam = null; + VaccineTrigger vaccineTrigger = VaccineTrigger.init(nullDueParam); + Assert.assertNull(vaccineTrigger); + + } }