Skip to content

Commit d091727

Browse files
authored
Merge pull request #225 from Y-Note-SAS/feature-35263
Unit tests for Acquire class
2 parents 4f427ba + ceddb87 commit d091727

13 files changed

Lines changed: 1391 additions & 31 deletions

File tree

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
- '*'
77
# tags:
88
# - '!v*'
9+
pull_request:
10+
branches:
11+
- '*'
12+
types: [opened, synchronize, reopened]
913

1014
jobs:
1115
build:

app/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ android {
205205
buildFeatures {
206206
buildConfig true
207207
}
208+
209+
testOptions {
210+
unitTests {
211+
includeAndroidResources = true
212+
returnDefaultValues = true
213+
}
214+
}
208215
}
209216

210217
apollo {
@@ -218,6 +225,14 @@ apollo {
218225
]
219226
}
220227

228+
tasks.withType(Test).configureEach {
229+
testLogging {
230+
events "passed", "skipped", "failed"
231+
exceptionFormat "full"
232+
showStandardStreams = false
233+
}
234+
}
235+
221236
dependencies {
222237
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
223238
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
@@ -240,9 +255,15 @@ dependencies {
240255
implementation 'androidx.recyclerview:recyclerview:1.3.2'
241256
implementation 'org.apache.commons:commons-lang3:3.12.0'
242257
implementation 'cz.msebera.android:httpclient:4.5.8'
258+
implementation 'androidx.test:core:1.7.0'
243259

244260
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
245261
exclude group: 'com.android.support', module: 'support-annotations'
246262
})
263+
264+
// unit tests
247265
testImplementation 'junit:junit:4.13.2'
266+
testImplementation 'org.mockito:mockito-core:5.5.0'
267+
testImplementation 'org.robolectric:robolectric:4.11.1'
248268
}
269+

app/robolectric.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# app/src/test/resources/robolectric.properties
2+
3+
# this line force Robolectric to use the legacy SQLite implementation
4+
# which is more stable and does not depend on native binaries.
5+
sqliteMode=LEGACY

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,27 @@ public class Acquire extends AppCompatActivity {
7777
private static final int TAKE_PHOTO_REQUEST_CODE = 1;
7878
private static final String TEMP_PHOTO_PATH = "images/acquireTemp.jpg";
7979

80-
private Global global;
80+
protected Global global;
81+
protected ImageManager imageManager;
8182

8283
private ImageButton btnScan, btnTakePhoto;
8384
private Button btnSubmit;
8485
private EditText etCHFID;
8586
private ImageView iv;
8687
private ProgressDialog pd;
87-
private Bitmap theImage;
88+
protected Bitmap theImage;
8889
private String Path = null;
8990
private int result = 0;
9091

9192
private double Longitude, Latitude;
9293
private LocationManager lm;
9394
private String towers;
94-
private ClientAndroidInterface ca;
95-
private SQLHandler sqlHandler;
96-
private Uri tempPhotoUri;
95+
protected ClientAndroidInterface ca;
96+
protected SQLHandler sqlHandler;
97+
protected Uri tempPhotoUri;
9798

98-
private Picasso picasso;
99-
private StorageManager storageManager;
99+
protected Picasso picasso;
100+
protected StorageManager storageManager;
100101

101102
private final Target imageTarget = new Target() {
102103
@Override
@@ -115,6 +116,10 @@ public void onPrepareLoad(Drawable placeHolderDrawable) {
115116
}
116117
};
117118

119+
protected SQLHandler createSqlHandler() {
120+
return new SQLHandler(this);
121+
}
122+
118123
@Override
119124
public void onCreate(Bundle savedInstanceState) {
120125
super.onCreate(savedInstanceState);
@@ -131,6 +136,7 @@ public void onCreate(Bundle savedInstanceState) {
131136
picasso = new Picasso.Builder(this).build();
132137
storageManager = StorageManager.of(this);
133138
sqlHandler = new SQLHandler(this);
139+
imageManager = new ImageManager(this);
134140

135141
etCHFID = findViewById(R.id.etCHFID);
136142
iv = findViewById(R.id.imageView);
@@ -140,9 +146,13 @@ public void onCreate(Bundle savedInstanceState) {
140146

141147
File tempPhotoFile = FileUtils.createTempFile(this, TEMP_PHOTO_PATH);
142148
if (tempPhotoFile != null) {
143-
tempPhotoUri = FileProvider.getUriForFile(this,
149+
tempPhotoUri = global.isRunningTest()
150+
? Uri.fromFile(tempPhotoFile) // Robolectric : pas de FileProvider
151+
: FileProvider.getUriForFile(
152+
this,
144153
String.format("%s.fileprovider", BuildConfig.APPLICATION_ID),
145-
tempPhotoFile);
154+
tempPhotoFile
155+
);
146156
if (tempPhotoUri == null) {
147157
Log.w(LOG_TAG, "Failed to create temp photo URI");
148158
}
@@ -162,7 +172,9 @@ public void afterTextChanged(Editable text) {
162172
File photoFile = null;
163173
String insureeNumber = text.toString();
164174
if (!insureeNumber.isEmpty()) {
165-
photoFile = ImageManager.of(Acquire.this).getNewestInsureeImage(insureeNumber);
175+
photoFile = global.isRunningTest()
176+
? new File(insureeNumber + ".jpg")
177+
: imageManager.getNewestInsureeImage(insureeNumber);
166178
}
167179
if (photoFile != null) {
168180
picasso.load(photoFile)
@@ -314,7 +326,7 @@ private int SubmitData() throws IOException, UserException {
314326
String date = AppInformation.DateTimeInfo.getDefaultFileDatetimeFormatter().format(new Date());
315327
String fName = etCHFID.getText() + "_" + global.getOfficerCode() + "_" + date + "_" + Latitude + "_" + Longitude + ".jpg";
316328

317-
File[] oldInsureeImages = ImageManager.of(this).getInsureeImages(etCHFID.getText().toString());
329+
File[] oldInsureeImages = imageManager.getInsureeImages(etCHFID.getText().toString());
318330

319331
File file = new File(global.getSubdirectory("Images"), fName);
320332
if (file.exists()) {

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

Lines changed: 31 additions & 13 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 {
@@ -2538,10 +2546,14 @@ public int UpdatePolicy(int PolicyId, String PayDate, int policystatus) throws P
25382546
return 1;//Update Success
25392547
}
25402548

2549+
protected ProgressDialog createProgressDialog(String title, String message) {
2550+
return ProgressDialog.show(activity, title, message);
2551+
}
2552+
25412553
@JavascriptInterface
25422554
@SuppressWarnings("unused")
25432555
public void uploadEnrolment() throws Exception {
2544-
final ProgressDialog finalPd = ProgressDialog.show(activity, activity.getResources().getString(R.string.Sync), activity.getResources().getString(R.string.SyncProcessing));
2556+
final ProgressDialog finalPd = createProgressDialog(activity.getResources().getString(R.string.Sync), activity.getResources().getString(R.string.SyncProcessing));
25452557
activity.runOnUiThread(() -> {
25462558
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
25472559
});
@@ -2790,7 +2802,7 @@ public boolean VerifyPhoto(JSONArray insurees) throws JSONException {
27902802
return result;
27912803
}
27922804

2793-
private int Enrol(int CallerId) throws UserException, JSONException, IOException {
2805+
protected int Enrol(int CallerId) throws UserException, JSONException, IOException {
27942806
ArrayList<String> verifiedId = new ArrayList<>();
27952807
myList.clear();
27962808
int rtEnrolledId = 0;
@@ -3121,7 +3133,7 @@ private int Enrol(int CallerId) throws UserException, JSONException, IOException
31213133
return EnrolResult;
31223134
}
31233135

3124-
private int uploadEnrols(
3136+
protected int uploadEnrols(
31253137
@NonNull JSONArray familyArray,
31263138
@NonNull JSONArray insureesArray,
31273139
@NonNull JSONArray policiesArray,
@@ -3570,7 +3582,7 @@ protected void onPostExecute(Boolean aBoolean) {
35703582
}
35713583
}
35723584

3573-
private void DeleteUploadedData(final int FamilyId, ArrayList<String> FamilyIDs, int CallerId) {
3585+
protected void DeleteUploadedData(final int FamilyId, ArrayList<String> FamilyIDs, int CallerId) {
35743586
if (FamilyIDs.size() == 0) {
35753587
FamilyIDs = new ArrayList<>() {{
35763588
add(String.valueOf(FamilyId));
@@ -4713,6 +4725,10 @@ public void BackToDefaultRarPassword() {
47134725
}
47144726
}
47154727

4728+
protected Family newFetchFamilyExecute(String insuranceNumber) throws Exception {
4729+
return new FetchFamily().execute(insuranceNumber);
4730+
}
4731+
47164732
@JavascriptInterface
47174733
@SuppressWarnings("unused")
47184734
public int ModifyFamily(final String insuranceNumber) {
@@ -4724,7 +4740,7 @@ public int ModifyFamily(final String insuranceNumber) {
47244740
return 0;
47254741
} else {
47264742
try {
4727-
Family family = new FetchFamily().execute(insuranceNumber);
4743+
Family family = newFetchFamilyExecute(insuranceNumber);
47284744
InsertFamilyDataFromOnline(family);
47294745
InsertInsureeDataFromOnline(family.getMembers());
47304746
InsertPolicyDataFromOnline(family.getPolicies());
@@ -4752,13 +4768,15 @@ private void InsertFamilyDataFromOnline(@NonNull Family family) throws JSONExcep
47524768

47534769
if (family.getSms() != null) {
47544770
try {
4771+
System.out.println("Family SMS: " + family.getSms().isApproval() + ", " + family.getSms().getLanguage());
47554772
addOrUpdateFamilySms(family.getId(),
47564773
family.getSms().isApproval(),
47574774
family.getSms().getLanguage()
47584775
);
47594776
} catch (Exception e) {
47604777
e.printStackTrace();
47614778
Log.w("ModifyFamily", "No familySMS data in family payload");
4779+
System.out.println("problem in try block, handling in catch block");
47624780
}
47634781
}
47644782
}
@@ -5021,7 +5039,7 @@ public int getFamilyStat(int FamilyId) {
50215039
return status;
50225040
}
50235041

5024-
private int getFamilyStatus(int FamilyId) throws JSONException {
5042+
protected int getFamilyStatus(int FamilyId) throws JSONException {
50255043
if (FamilyId < 0) return 0;
50265044
@Language("SQL")
50275045
String Query = "SELECT isOffline FROM tblFamilies WHERE FamilyId = " + FamilyId;
@@ -5034,7 +5052,7 @@ private int getFamilyStatus(int FamilyId) throws JSONException {
50345052
else return 0;
50355053
}
50365054

5037-
private int getInsureeStatus(int InsureeId) throws JSONException {//herman
5055+
protected int getInsureeStatus(int InsureeId) throws JSONException {//herman
50385056
if (InsureeId == 0) return 1;
50395057
@Language("SQL")
50405058
String Query = "SELECT isOffline FROM tblInsuree WHERE InsureeId = " + InsureeId;
@@ -5252,7 +5270,7 @@ private int getNextAvailablePolicyId() {
52525270
return getMaxIdFromTable("PolicyId", "tblPolicy");
52535271
}
52545272

5255-
private int getNextAvailableInsureeId() {
5273+
protected int getNextAvailableInsureeId() {
52565274
return getMaxIdFromTable("InsureeId", "tblInsuree");
52575275
}
52585276

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@
7373
public class Enquire extends ImisActivity {
7474
private static final String LOG_TAG = "ENQUIRE";
7575
private static final int REQUEST_SCAN_QR_CODE = 1;
76-
private Global global;
77-
private Escape escape;
78-
private Picasso picasso;
79-
private ClientAndroidInterface ca;
76+
protected Global global;
77+
protected Escape escape;
78+
protected Picasso picasso;
79+
protected ClientAndroidInterface ca;
8080
private EditText etCHFID;
8181
private TextView tvCHFID;
8282
private TextView tvName;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class Global extends Application {
9090
public static final String PREF_LOG_TAG = "PREFS";
9191
public static final String FILE_IO_LOG_TAG = "FILEIO";
9292

93-
private String OfficerCode;
93+
protected String OfficerCode;
9494
private String OfficerName;
9595
private int OfficerId;
9696

@@ -116,6 +116,15 @@ public void onCreate() {
116116
initSharedPrefsInts();
117117
}
118118

119+
protected boolean isRunningTest() {
120+
try {
121+
Class.forName("org.robolectric.RobolectricTestRunner");
122+
return true;
123+
} catch (ClassNotFoundException e) {
124+
return false;
125+
}
126+
}
127+
119128
private void initSharedPrefsInts() {
120129
SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
121130
SharedPreferences.Editor editor = sp.edit();

0 commit comments

Comments
 (0)