Skip to content

Commit b19574c

Browse files
committed
Unit tests: feat fetch family fron server
1 parent 40bfdc8 commit b19574c

2 files changed

Lines changed: 178 additions & 1 deletion

File tree

app/src/main/java/org/openimis/imispolicies/ClientAndroidInterface.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4725,6 +4725,10 @@ public void BackToDefaultRarPassword() {
47254725
}
47264726
}
47274727

4728+
protected Family newFetchFamilyExecute(String insuranceNumber) throws Exception {
4729+
return new FetchFamily().execute(insuranceNumber);
4730+
}
4731+
47284732
@JavascriptInterface
47294733
@SuppressWarnings("unused")
47304734
public int ModifyFamily(final String insuranceNumber) {
@@ -4736,7 +4740,7 @@ public int ModifyFamily(final String insuranceNumber) {
47364740
return 0;
47374741
} else {
47384742
try {
4739-
Family family = new FetchFamily().execute(insuranceNumber);
4743+
Family family = newFetchFamilyExecute(insuranceNumber);
47404744
InsertFamilyDataFromOnline(family);
47414745
InsertInsureeDataFromOnline(family.getMembers());
47424746
InsertPolicyDataFromOnline(family.getPolicies());
@@ -4764,13 +4768,15 @@ private void InsertFamilyDataFromOnline(@NonNull Family family) throws JSONExcep
47644768

47654769
if (family.getSms() != null) {
47664770
try {
4771+
System.out.println("Family SMS: " + family.getSms().isApproval() + ", " + family.getSms().getLanguage());
47674772
addOrUpdateFamilySms(family.getId(),
47684773
family.getSms().isApproval(),
47694774
family.getSms().getLanguage()
47704775
);
47714776
} catch (Exception e) {
47724777
e.printStackTrace();
47734778
Log.w("ModifyFamily", "No familySMS data in family payload");
4779+
System.out.println("problem in try block, handling in catch block");
47744780
}
47754781
}
47764782
}

app/src/test/java/org/openimis/imispolicies/ClientAndroidInterfaceTest.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import static org.mockito.Mockito.*;
66

77
import org.openimis.imispolicies.tools.StorageManager;
8+
import org.openimis.imispolicies.domain.entity.Family;
9+
import org.openimis.imispolicies.domain.entity.Policy;
810
import org.robolectric.RobolectricTestRunner;
11+
import org.openimis.imispolicies.network.exception.HttpException;
912

1013
import android.app.Activity;
1114
import android.content.res.Resources;
@@ -27,10 +30,14 @@
2730
import org.robolectric.shadows.ShadowLooper;
2831

2932
import android.app.ProgressDialog;
33+
import android.app.AlertDialog;
3034
import android.view.WindowManager;
3135

3236
import java.util.HashMap;
3337
import java.util.ArrayList;
38+
import java.net.HttpURLConnection;
39+
import java.util.List;
40+
import java.util.Date;
3441

3542

3643
@RunWith(RobolectricTestRunner.class)
@@ -42,6 +49,7 @@ public class ClientAndroidInterfaceTest {
4249
@Mock Resources resources;
4350
@Mock StorageManager storageManager;
4451
@Mock ProgressDialog progressDialog;
52+
@Mock AlertDialog alertDialog;
4553

4654
ClientAndroidInterface client;
4755

@@ -438,4 +446,167 @@ private HashMap<String, String> buildInsureeMap() {
438446

439447
return map;
440448
}
449+
450+
@Test
451+
public void testModifyFamily_ShouldImportFamilyInsureesAndPolicies() throws Exception {
452+
453+
ClientAndroidInterface spyClient = spy(client);
454+
455+
String chfId = "CHF123456";
456+
457+
when(sqlHandler.getCount(
458+
eq("tblInsuree"),
459+
eq("Trim(CHFID) = ?"),
460+
eq(new String[]{chfId})
461+
)).thenReturn(0);
462+
463+
// Mock Family
464+
Family mockFamily = mock(Family.class);
465+
when(mockFamily.getUuid()).thenReturn("uuid-family");
466+
when(mockFamily.getId()).thenReturn(1);
467+
when(mockFamily.getLocationId()).thenReturn(1);
468+
when(mockFamily.isPoor()).thenReturn(false);
469+
when(mockFamily.isOffline()).thenReturn(false);
470+
when(mockFamily.getType()).thenReturn("N");
471+
when(mockFamily.getAddress()).thenReturn("ADDR");
472+
when(mockFamily.getEthnicity()).thenReturn("ETH");
473+
when(mockFamily.getConfirmationNumber()).thenReturn("CONF");
474+
when(mockFamily.getConfirmationType()).thenReturn("TYPE");
475+
when(mockFamily.getSms()).thenReturn(new Family.SMS(false, "EN"));
476+
477+
Family.Member head = mock(Family.Member.class);
478+
when(head.getId()).thenReturn(1);
479+
when(head.getUuid()).thenReturn("uuid-insuree");
480+
when(mockFamily.getHead()).thenReturn(head);
481+
482+
// Membres
483+
List<Family.Member> members = new ArrayList<>();
484+
Family.Member member = mock(Family.Member.class);
485+
when(member.getChfId()).thenReturn(chfId);
486+
when(member.getId()).thenReturn(1);
487+
when(member.getUuid()).thenReturn("uuid-member");
488+
when(member.getFamilyId()).thenReturn(1);
489+
when(member.getFamilyUuid()).thenReturn("uuid-family");
490+
when(member.getIdentificationNumber()).thenReturn("12345");
491+
when(member.getLastName()).thenReturn("Doe");
492+
when(member.getOtherNames()).thenReturn("John");
493+
when(member.getDateOfBirth()).thenReturn(new java.util.Date());
494+
when(member.getGender()).thenReturn("M");
495+
when(member.getMarital()).thenReturn("M");
496+
when(member.isHead()).thenReturn(true);
497+
when(member.getPhone()).thenReturn("690000000");
498+
when(member.getPhotoPath()).thenReturn("/storage/emulated/0/DCIM/test.jpg");
499+
when(member.isCardIssued()).thenReturn(true);
500+
when(member.isOffline()).thenReturn(false);
501+
when(member.getRelationship()).thenReturn(1);
502+
when(member.getProfession()).thenReturn(1);
503+
when(member.getEducation()).thenReturn(1);
504+
when(member.getEmail()).thenReturn("email");
505+
when(member.getTypeOfId()).thenReturn("type");
506+
when(member.getHealthFacilityId()).thenReturn(1);
507+
when(member.getCurrentAddress()).thenReturn("addr");
508+
when(member.getGeolocation()).thenReturn("geo");
509+
when(member.getCurrentVillage()).thenReturn(1);
510+
members.add(member);
511+
when(mockFamily.getMembers()).thenReturn(members);
512+
513+
// Polices
514+
List<Family.Policy> policies = new ArrayList<>();
515+
Family.Policy policy = mock(Family.Policy.class);
516+
when(policy.getId()).thenReturn(1);
517+
when(policy.getFamilyId()).thenReturn(1);
518+
when(policy.getStatus()).thenReturn("ACTIVE");
519+
when(policy.getValue()).thenReturn(100.0);
520+
when(policy.getProductId()).thenReturn(1);
521+
when(policy.getOfficerId()).thenReturn(1);
522+
when(policy.getStartDate()).thenReturn(new java.util.Date());
523+
when(policy.getEffectiveDate()).thenReturn(new java.util.Date());
524+
when(policy.getExpiryDate()).thenReturn(new java.util.Date());
525+
when(policy.getEnrollDate()).thenReturn(new java.util.Date());
526+
when(policy.isOffline()).thenReturn(false);
527+
policies.add(policy);
528+
when(mockFamily.getPolicies()).thenReturn(policies);
529+
530+
when(sqlHandler.getResult(anyString(), any())).thenReturn(new JSONArray());
531+
532+
doNothing().when(spyClient).ShowDialog(anyString());
533+
534+
// Mock FetchFamily
535+
doReturn(mockFamily)
536+
.when(spyClient)
537+
.newFetchFamilyExecute(chfId);
538+
539+
int result = spyClient.ModifyFamily(chfId);
540+
541+
assertEquals(1, result);
542+
543+
verify(sqlHandler).insertData(eq("tblFamilies"), any(), any(JSONArray.class), anyString());
544+
verify(sqlHandler).insertData(eq("tblInsuree"), any(), any(JSONArray.class), anyString());
545+
verify(sqlHandler).insertData(eq("tblPolicy"), any(), any(JSONArray.class), anyString());
546+
}
547+
548+
@Test
549+
public void testModifyFamily_ShouldNotImportIfInsureeExists() {
550+
551+
ClientAndroidInterface spyClient = spy(client);
552+
553+
when(sqlHandler.getCount(
554+
eq("tblInsuree"),
555+
eq("Trim(CHFID) = ?"),
556+
any(String[].class)
557+
)).thenReturn(1);
558+
559+
doNothing().when(spyClient).ShowDialog(anyString());
560+
561+
int result = spyClient.ModifyFamily("CHF_EXIST");
562+
563+
assertEquals(0, result);
564+
verify(spyClient).ShowDialog(anyString());
565+
}
566+
567+
@Test
568+
public void testModifyFamily_ShouldHandleFamilyNotFound() throws Exception {
569+
570+
ClientAndroidInterface spyClient = spy(client);
571+
572+
when(sqlHandler.getCount(anyString(), anyString(), any()))
573+
.thenReturn(0);
574+
575+
576+
doThrow(new HttpException(
577+
HttpURLConnection.HTTP_NOT_FOUND,
578+
"Not Found",
579+
null,
580+
null
581+
)).when(spyClient).newFetchFamilyExecute(anyString());
582+
583+
doNothing().when(spyClient).ShowDialog(anyString());
584+
585+
int result = spyClient.ModifyFamily("CHF_UNKNOWN");
586+
587+
assertEquals(0, result);
588+
verify(spyClient).ShowDialog(anyString());
589+
}
590+
591+
@Test
592+
public void testModifyFamily_ShouldHandleServerError() throws Exception {
593+
594+
ClientAndroidInterface spyClient = spy(client);
595+
596+
when(sqlHandler.getCount(anyString(), anyString(), any()))
597+
.thenReturn(0);
598+
599+
doThrow(new RuntimeException("Server down"))
600+
.when(spyClient)
601+
.newFetchFamilyExecute(anyString());
602+
603+
doNothing().when(spyClient).ShowDialog(anyString());
604+
605+
int result = spyClient.ModifyFamily("CHF_ERR");
606+
607+
assertEquals(0, result);
608+
verify(spyClient).ShowDialog(anyString());
609+
}
610+
611+
441612
}

0 commit comments

Comments
 (0)