55import static org .mockito .Mockito .*;
66
77import org .openimis .imispolicies .tools .StorageManager ;
8+ import org .openimis .imispolicies .domain .entity .Family ;
9+ import org .openimis .imispolicies .domain .entity .Policy ;
810import org .robolectric .RobolectricTestRunner ;
11+ import org .openimis .imispolicies .network .exception .HttpException ;
912
1013import android .app .Activity ;
1114import android .content .res .Resources ;
2730import org .robolectric .shadows .ShadowLooper ;
2831
2932import android .app .ProgressDialog ;
33+ import android .app .AlertDialog ;
3034import android .view .WindowManager ;
3135
3236import java .util .HashMap ;
3337import 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