Skip to content

Commit 83be681

Browse files
committed
testing the creation of a new insuree
1 parent c5d0813 commit 83be681

3 files changed

Lines changed: 135 additions & 9 deletions

File tree

app/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ dependencies {
233233
implementation 'androidx.appcompat:appcompat:1.6.1'
234234
implementation 'com.google.android.material:material:1.10.0'
235235
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
236-
//noinspection GradleDependency
237236
implementation 'com.squareup.picasso:picasso:2.8'
238237
implementation 'commons-io:commons-io:2.11.0'
239238
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
@@ -244,5 +243,10 @@ dependencies {
244243
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
245244
exclude group: 'com.android.support', module: 'support-annotations'
246245
})
246+
247+
// Tests unitaires
247248
testImplementation 'junit:junit:4.13.2'
249+
testImplementation 'org.mockito:mockito-core:5.5.0'
250+
testImplementation 'org.robolectric:robolectric:4.11.1'
248251
}
252+

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ public class ClientAndroidInterface {
136136
@NonNull
137137
private final Activity activity;
138138
@NonNull
139-
private final SQLHandler sqlHandler;
139+
protected final SQLHandler sqlHandler;
140140
@NonNull
141141
private final HashMap<String, String> controls = new HashMap<>();
142142
@NonNull
143143
private final ArrayList<String> myList = new ArrayList<>();
144144
@NonNull
145145
private final ArrayList<String> enrolMessages = new ArrayList<>();
146146
@NonNull
147-
private final Global global;
147+
protected final Global global;
148148
@NonNull
149149
private final StorageManager storageManager;
150150
@NonNull
@@ -167,6 +167,14 @@ public class ClientAndroidInterface {
167167
.build();
168168
}
169169

170+
public ClientAndroidInterface(Activity activity, SQLHandler sqlHandler, Global global, Picasso picasso, StorageManager storageManager) {
171+
this.activity = activity;
172+
this.sqlHandler = sqlHandler;
173+
this.global = global;
174+
this.storageManager = storageManager;
175+
this.picassoInstance = picasso;
176+
}
177+
170178
@JavascriptInterface
171179
@SuppressWarnings("unused")
172180
public void SetUrl(String Url) {
@@ -666,7 +674,7 @@ public String getHF(int DistrictId, String HFLevel) {
666674
return HFs.toString();
667675
}
668676

669-
private HashMap<String, String> jsonToTable(String jsonString) {
677+
protected HashMap<String, String> jsonToTable(String jsonString) {
670678
HashMap<String, String> data = new HashMap<>();
671679
try {
672680
JSONArray array = new JSONArray(jsonString);
@@ -849,7 +857,7 @@ public void addOrUpdateFamilySms(int familyId, Boolean approve, String language)
849857
}
850858
}
851859

852-
private int isValidInsureeData(HashMap<String, String> data) {
860+
protected int isValidInsureeData(HashMap<String, String> data) {
853861
int Result;
854862

855863
String InsuranceNumber = data.get("txtInsuranceNumber");
@@ -1044,7 +1052,7 @@ else if (ExceedThreshold == 0)
10441052
return rtInsureeId;
10451053
}
10461054

1047-
private String copyImageFromGalleryToApplication(String selectedPath, String InsuranceNumber) {
1055+
protected String copyImageFromGalleryToApplication(String selectedPath, String InsuranceNumber) {
10481056
String result = "";
10491057

10501058
try {
@@ -5021,7 +5029,7 @@ public int getFamilyStat(int FamilyId) {
50215029
return status;
50225030
}
50235031

5024-
private int getFamilyStatus(int FamilyId) throws JSONException {
5032+
protected int getFamilyStatus(int FamilyId) throws JSONException {
50255033
if (FamilyId < 0) return 0;
50265034
@Language("SQL")
50275035
String Query = "SELECT isOffline FROM tblFamilies WHERE FamilyId = " + FamilyId;
@@ -5034,7 +5042,7 @@ private int getFamilyStatus(int FamilyId) throws JSONException {
50345042
else return 0;
50355043
}
50365044

5037-
private int getInsureeStatus(int InsureeId) throws JSONException {//herman
5045+
protected int getInsureeStatus(int InsureeId) throws JSONException {//herman
50385046
if (InsureeId == 0) return 1;
50395047
@Language("SQL")
50405048
String Query = "SELECT isOffline FROM tblInsuree WHERE InsureeId = " + InsureeId;
@@ -5252,7 +5260,7 @@ private int getNextAvailablePolicyId() {
52525260
return getMaxIdFromTable("PolicyId", "tblPolicy");
52535261
}
52545262

5255-
private int getNextAvailableInsureeId() {
5263+
protected int getNextAvailableInsureeId() {
52565264
return getMaxIdFromTable("InsureeId", "tblInsuree");
52575265
}
52585266

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.openimis.imispolicies;
2+
3+
import static org.junit.Assert.*;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.anyInt;
6+
import static org.mockito.ArgumentMatchers.anyString;
7+
import static org.mockito.Mockito.*;
8+
import com.squareup.picasso.Picasso;
9+
import org.openimis.imispolicies.tools.StorageManager;
10+
import org.robolectric.RobolectricTestRunner;
11+
12+
import android.app.Activity;
13+
import android.content.res.Resources;
14+
15+
import org.json.JSONArray;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
import org.mockito.Mock;
20+
import org.mockito.MockitoAnnotations;
21+
import org.mockito.junit.MockitoJUnitRunner;
22+
23+
import java.util.HashMap;
24+
25+
@RunWith(RobolectricTestRunner.class)
26+
public class ClientAndroidInterfaceTest {
27+
28+
@Mock
29+
SQLHandler sqlHandler;
30+
31+
@Mock
32+
Global global;
33+
34+
@Mock
35+
Activity activity;
36+
37+
@Mock
38+
Resources resources;
39+
40+
@Mock
41+
StorageManager storageManager;
42+
43+
ClientAndroidInterface client;
44+
45+
@Before
46+
public void setup() {
47+
MockitoAnnotations.openMocks(this);
48+
49+
when(activity.getResources()).thenReturn(resources);
50+
when(resources.getString(anyInt())).thenReturn("mockString");
51+
52+
client = new ClientAndroidInterface(activity, sqlHandler, global, null, storageManager);
53+
54+
when(global.isNetworkAvailable()).thenReturn(true);
55+
}
56+
57+
@Test
58+
public void testCreateNewInsuree_ShouldInsertInDatabase() throws Exception {
59+
60+
String insureeJson = "{"
61+
+ "\"txtInsuranceNumber\":\"12345\","
62+
+ "\"hfInsureeId\":\"0\","
63+
+ "\"hfisHead\":\"1\","
64+
+ "\"txtLastName\":\"Doe\","
65+
+ "\"txtOtherNames\":\"John\","
66+
+ "\"txtBirthDate\":\"1991-01-01\","
67+
+ "\"ddlGender\":\"M\","
68+
+ "\"txtPhoneNumber\":\"690000000\""
69+
+ "\"hfNewPhotoPath\":\"\""
70+
+ "\"hfImagePath\":\"/storage/emulated/0/DCIM/test.jpg\""
71+
+ "}";
72+
73+
ClientAndroidInterface spyClient = spy(client);
74+
75+
HashMap<String, String> mockData = new HashMap<>();
76+
mockData.put("txtInsuranceNumber", "12345");
77+
mockData.put("hfInsureeId", "0");
78+
mockData.put("hfisHead", "1");
79+
mockData.put("txtLastName", "Doe");
80+
mockData.put("txtOtherNames", "John");
81+
mockData.put("txtBirthDate", "1991-01-01");
82+
mockData.put("ddlGender", "M");
83+
mockData.put("txtPhoneNumber", "690000000");
84+
mockData.put("hfNewPhotoPath", "");
85+
mockData.put("hfImagePath", "/storage/emulated/0/DCIM/test.jpg");
86+
87+
when(sqlHandler.getResult(anyString(), any(String[].class))).thenReturn(new JSONArray());
88+
doNothing().when(spyClient).SaveInsureePolicy(anyInt(), anyInt(), anyBoolean(), anyInt());
89+
doNothing().when(sqlHandler).insertData(anyString(), any());
90+
doNothing().when(sqlHandler).updateData(anyString(), any(), anyString(), any(String[].class));
91+
doReturn(mockData).when(spyClient).jsonToTable(anyString());
92+
doReturn(0).when(spyClient).isValidInsureeData(mockData);
93+
doReturn(999).when(spyClient).getNextAvailableInsureeId();
94+
doReturn(0).when(spyClient).getFamilyStatus(anyInt());
95+
doReturn(0).when(spyClient).getInsureeStatus(anyInt());
96+
doNothing().when(spyClient).ShowDialog(anyString());
97+
doNothing().when(spyClient).ShowDialogYesNo(anyInt(), anyInt(), anyInt());
98+
doReturn("").when(spyClient).copyImageFromGalleryToApplication(anyString(), anyString());
99+
100+
doNothing().when(sqlHandler).insertData(anyString(), any());
101+
102+
int result = spyClient.SaveInsuree(
103+
insureeJson,
104+
1, // FamilyId
105+
1, // isHead
106+
0, // ExceedThreshold
107+
0 // PolicyId
108+
);
109+
110+
assertEquals(-999, result);
111+
112+
verify(sqlHandler, times(1)).insertData(eq("tblInsuree"), any());
113+
}
114+
}

0 commit comments

Comments
 (0)