diff --git a/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java b/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java index 29abc04b0..7b6ab959d 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java +++ b/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java @@ -36,11 +36,11 @@ protected ArrayList doInBackground(Void... params) { String motherBaseEntityId = Utils.getValue(childDetails.getColumnmaps(), Constants.KEY.RELATIONAL_ID, false); if (!TextUtils.isEmpty(motherBaseEntityId) && !TextUtils.isEmpty(baseEntityId)) { + String whereClause = constructWhereClause(motherBaseEntityId); List> childList = ChildLibrary.getInstance() .eventClientRepository() .rawQuery(ChildLibrary.getInstance().getRepository().getReadableDatabase(), - Utils.metadata().getRegisterQueryProvider().mainRegisterQuery() - + " WHERE " + Utils.metadata().getRegisterQueryProvider().getChildDetailsTable() + ".relational_id IN ('" + motherBaseEntityId + "')"); + Utils.metadata().getRegisterQueryProvider().mainRegisterQuery() + whereClause); List children = new ArrayList<>(); @@ -64,6 +64,13 @@ protected ArrayList doInBackground(Void... params) { return null; } + private String constructWhereClause(String motherBaseEntityId) { + return " WHERE " + Utils.metadata().getRegisterQueryProvider().getChildDetailsTable() + ".relational_id IN ('" + motherBaseEntityId + "') AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".date_removed IS NULL AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".dod IS NULL AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".is_closed = 0"; + } + @Override protected void onPostExecute(ArrayList baseEntityIds) { super.onPostExecute(baseEntityIds); diff --git a/opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java b/opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java new file mode 100644 index 000000000..79918602c --- /dev/null +++ b/opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java @@ -0,0 +1,47 @@ +package org.smartregister.child.task; + +import static org.junit.Assert.assertEquals; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.child.BaseUnitTest; +import org.smartregister.child.contract.IGetSiblings; +import org.smartregister.child.util.Constants; +import org.smartregister.child.util.DBConstants; +import org.smartregister.commonregistry.CommonPersonObjectClient; + +import java.util.HashMap; + +public class GetSiblingsTaskTest extends BaseUnitTest { + + private GetSiblingsTask getSiblingsTask; + + @Mock + private IGetSiblings getSiblings; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + HashMap details = new HashMap<>(); + details.put(DBConstants.KEY.ZEIR_ID, "123123"); + CommonPersonObjectClient commonPersonObjectClient = new CommonPersonObjectClient("baseEntityId", details, Constants.KEY.CHILD); + getSiblingsTask = new GetSiblingsTask(commonPersonObjectClient, getSiblings); + } + + @Test + public void testConstructorNotNull() { + Assert.assertNotNull(getSiblingsTask); + } + + @Test + public void testConstructWhereClauseFiltersDeadChild() { + String whereClause = ReflectionHelpers.callInstanceMethod(getSiblingsTask, "constructWhereClause", ReflectionHelpers.ClassParameter.from(String.class, "123")); + assertEquals(" WHERE ec_child_details.relational_id IN ('123') AND ec_client.date_removed IS NULL AND ec_client.dod IS NULL AND ec_client.is_closed = 0", whereClause); + } + +}