Skip to content

Commit

Permalink
Merge pull request #177 from opensrp/fix-negative-offset-bug
Browse files Browse the repository at this point in the history
Fix negative offset not respected for calculations
  • Loading branch information
ndegwamartin authored Jun 29, 2021
2 parents 2c099f8 + effc622 commit 9eed514
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 84 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.0.11-SNAPSHOT
VERSION_NAME=4.0.0-SNAPSHOT
VERSION_CODE=2
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Immunization
Expand Down
2 changes: 1 addition & 1 deletion opensrp-immunization/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,23 @@ 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;
vaccineCondition = null;
this.window = window;
}

public VaccineTrigger(String offset, String window, VaccineRepo.Vaccine prerequisite) {
reference = Reference.PREREQUISITE;
this.offset = offset;
this.prerequisite = prerequisite;
this.window = window;
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;
this.window = window;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,65 +1102,47 @@ public static List<String> getVaccineFiles(Context context) throws IOException {

public static void processConfigCalendarOffset(Calendar calendar, 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;
}

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);
String[] offsetTokens = offset.split(",");

for (int i = 0; i < offsetTokens.length; i++) {

Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]);
if (m2.find()) {
int curValue = getOperator(offsetTokens[i]) * 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;
}
String[] offsetTokens = offset.split(",");

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);
}
for (int i = 0; i < offsetTokens.length; i++) {

Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]);
if (m2.find()) {
int curValue = getOperator(offsetTokens[i]) * 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;
Expand All @@ -1169,36 +1151,40 @@ public static DateTime processConfigDateTimeOffset(DateTime afterOffset, String

public static int processOffsetValueInDays(String offset) {
if (offset == null) return -1;
Matcher m1 = getPrefixSymbolMatcher(offset);
int days = 0;
String[] offsetTokens = offset.split(",");

for (int i = 0; i < offsetTokens.length; i++) {

Matcher m2 = getSuffixSymbolMatcher(offsetTokens[i]);
if (m2.find()) {
int curValue = getOperator(offsetTokens[i]) * 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;

}

private static int getOperator(String offsetToken) {
int operator = 1;
Matcher m1 = getPrefixSymbolMatcher(offsetToken);
if (m1.find()) {
String operatorString = m1.group(1);
String valueString = m1.group(2);

int 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;
}
}
}
}
return days;

return operator;
}

private static Matcher getSuffixSymbolMatcher(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
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;
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.service.AlertService;
import org.smartregister.util.AppProperties;
import org.smartregister.util.JsonFormUtils;

import java.util.Calendar;
Expand All @@ -34,13 +39,23 @@ 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\"}";

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockImmunizationLibrary(immunizationLibrary, context, vaccineRepository, alertService, appProperties);
}

@Test
Expand Down Expand Up @@ -77,8 +92,54 @@ 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());

}

@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);

}
}
Loading

0 comments on commit 9eed514

Please sign in to comment.