Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-vd committed Feb 23, 2023
1 parent aff9f4d commit 3e21331
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ public static boolean childBelongsToCurrentFacility(@NotNull Map<String, String>
public static boolean isChildTemporaryOOC(@NotNull Map<String, String> childDetails) {
return !childBelongsToCurrentFacility(childDetails) && (ChildLibrary.getInstance()
.getProperties().isTrue(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT)
&& Boolean.valueOf(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false)));
&& Boolean.parseBoolean(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false)));
}

public static String getLocationIdFromChildTempOOCEvent(String baseEntityId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.smartregister.child.util;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;

import android.content.res.Resources;
import android.graphics.Color;
import android.widget.EditText;
Expand Down Expand Up @@ -30,13 +34,15 @@
import org.smartregister.clientandeventmodel.Event;
import org.smartregister.clientandeventmodel.FormEntityConstants;
import org.smartregister.clientandeventmodel.Obs;
import org.smartregister.commonregistry.CommonPersonObjectClient;
import org.smartregister.domain.UniqueId;
import org.smartregister.domain.db.EventClient;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.domain.Vaccine;
import org.smartregister.immunization.repository.VaccineRepository;
import org.smartregister.location.helper.LocationHelper;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.repository.UniqueIdRepository;
import org.smartregister.sync.ClientProcessorForJava;
import org.smartregister.sync.helper.ECSyncHelper;
Expand All @@ -53,7 +59,7 @@
import java.util.Map;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ChildLibrary.class, Utils.class, LocationHelper.class})
@PrepareForTest({ChildLibrary.class, Utils.class, LocationHelper.class, ChildDbUtils.class})
public class UtilsTest {
@Mock
private ChildLibrary childLibrary;
Expand Down Expand Up @@ -213,7 +219,6 @@ public void testCreateArchiveRecordEventShouldCreateValidEvents() throws Excepti
Assert.assertNotNull(result);
Assert.assertEquals(result.size(), 2);
Mockito.verify(ecSyncHelper, Mockito.times(1)).addEvent(Mockito.eq("231-erer7"), Mockito.any(JSONObject.class));
Mockito.verify(ecSyncHelper, Mockito.times(1)).addEvent(Mockito.eq("232-erer7"), Mockito.any(JSONObject.class));
}

@Test
Expand Down Expand Up @@ -528,4 +533,57 @@ public void testIsChildHasNFCCardShouldReturnTrueIfCardIsBlacklisted() {
boolean isChildHasNFCCard = Utils.isChildHasNFCCard(childDetails);
Assert.assertFalse(isChildHasNFCCard);
}

@Test
public void testChildBelongsToCurrentFacilityShouldReturnTrueWhenChildExistsInDB() {
PowerMockito.mockStatic(ChildDbUtils.class);
Map<String, String> childDetails = new HashMap<>();
childDetails.put("base_entity_id", "case_id");
childDetails.put("is_out_of_catchment", "false");


CommonPersonObjectClient client = new CommonPersonObjectClient("case_id", childDetails, "child_name");
PowerMockito.when(ChildDbUtils.fetchCommonPersonObjectClientByBaseEntityId(eq("case_id"))).thenReturn(client);

Assert.assertTrue(Utils.childBelongsToCurrentFacility(childDetails));
}

@Test
public void testiIsChildTemporaryOOCShouldReturnTrueWhenOOCEvent() {
PowerMockito.mockStatic(ChildLibrary.class);
PowerMockito.when(ChildLibrary.getInstance()).thenReturn(childLibrary);
PowerMockito.doReturn(true).when(appProperties).isTrue(eq(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT));

PowerMockito.mockStatic(ChildDbUtils.class);
Map<String, String> childDetails = new HashMap<>();
childDetails.put("base_entity_id", "case_id");
childDetails.put("is_out_of_catchment", "true");

PowerMockito.when(ChildDbUtils.fetchCommonPersonObjectClientByBaseEntityId(eq("case_id"))).thenReturn(null);

Assert.assertTrue(Utils.isChildTemporaryOOC(childDetails));
}

@Test
public void testGetLocationIdFromChildTempOOCEventRetursCorrectLocation() {
PowerMockito.mockStatic(ChildLibrary.class);
EventClientRepository repository = Mockito.mock(EventClientRepository.class);
PowerMockito.when(ChildLibrary.getInstance()).thenReturn(childLibrary);
PowerMockito.when(childLibrary.eventClientRepository()).thenReturn(repository);

List<EventClient> eventClientList = new ArrayList<>();
org.smartregister.domain.Event event = new org.smartregister.domain.Event();
event.setBaseEntityId("case_id");
org.smartregister.domain.Obs obs = new org.smartregister.domain.Obs();
obs.setFormSubmissionField("From_LocationId");
obs.addToValueList("location_123");
event.addObs(obs);
EventClient eventClient = new EventClient(event);
eventClientList.add(eventClient);

Mockito.doReturn(eventClientList).when(repository).fetchEventClientsCore(anyString(), any());

String locationId = Utils.getLocationIdFromChildTempOOCEvent("case_id");
Assert.assertEquals("location_123", locationId);
}
}

0 comments on commit 3e21331

Please sign in to comment.