Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
//noinspection GradleDependency
implementation 'com.squareup.picasso:picasso:2.8'
implementation 'commons-io:commons-io:2.11.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
Expand All @@ -244,5 +243,10 @@ dependencies {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})

// Tests unitaires
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:5.5.0'
testImplementation 'org.robolectric:robolectric:4.11.1'
}

Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ public class ClientAndroidInterface {
@NonNull
private final Activity activity;
@NonNull
private final SQLHandler sqlHandler;
protected final SQLHandler sqlHandler;
@NonNull
private final HashMap<String, String> controls = new HashMap<>();
@NonNull
private final ArrayList<String> myList = new ArrayList<>();
@NonNull
private final ArrayList<String> enrolMessages = new ArrayList<>();
@NonNull
private final Global global;
protected final Global global;
@NonNull
private final StorageManager storageManager;
@NonNull
Expand All @@ -167,6 +167,14 @@ public class ClientAndroidInterface {
.build();
}

public ClientAndroidInterface(Activity activity, SQLHandler sqlHandler, Global global, Picasso picasso, StorageManager storageManager) {
this.activity = activity;
this.sqlHandler = sqlHandler;
this.global = global;
this.storageManager = storageManager;
this.picassoInstance = picasso;
}

@JavascriptInterface
@SuppressWarnings("unused")
public void SetUrl(String Url) {
Expand Down Expand Up @@ -666,7 +674,7 @@ public String getHF(int DistrictId, String HFLevel) {
return HFs.toString();
}

private HashMap<String, String> jsonToTable(String jsonString) {
protected HashMap<String, String> jsonToTable(String jsonString) {
HashMap<String, String> data = new HashMap<>();
try {
JSONArray array = new JSONArray(jsonString);
Expand Down Expand Up @@ -849,7 +857,7 @@ public void addOrUpdateFamilySms(int familyId, Boolean approve, String language)
}
}

private int isValidInsureeData(HashMap<String, String> data) {
protected int isValidInsureeData(HashMap<String, String> data) {
int Result;

String InsuranceNumber = data.get("txtInsuranceNumber");
Expand Down Expand Up @@ -1044,7 +1052,7 @@ else if (ExceedThreshold == 0)
return rtInsureeId;
}

private String copyImageFromGalleryToApplication(String selectedPath, String InsuranceNumber) {
protected String copyImageFromGalleryToApplication(String selectedPath, String InsuranceNumber) {
String result = "";

try {
Expand Down Expand Up @@ -5021,7 +5029,7 @@ public int getFamilyStat(int FamilyId) {
return status;
}

private int getFamilyStatus(int FamilyId) throws JSONException {
protected int getFamilyStatus(int FamilyId) throws JSONException {
if (FamilyId < 0) return 0;
@Language("SQL")
String Query = "SELECT isOffline FROM tblFamilies WHERE FamilyId = " + FamilyId;
Expand All @@ -5034,7 +5042,7 @@ private int getFamilyStatus(int FamilyId) throws JSONException {
else return 0;
}

private int getInsureeStatus(int InsureeId) throws JSONException {//herman
protected int getInsureeStatus(int InsureeId) throws JSONException {//herman
if (InsureeId == 0) return 1;
@Language("SQL")
String Query = "SELECT isOffline FROM tblInsuree WHERE InsureeId = " + InsureeId;
Expand Down Expand Up @@ -5252,7 +5260,7 @@ private int getNextAvailablePolicyId() {
return getMaxIdFromTable("PolicyId", "tblPolicy");
}

private int getNextAvailableInsureeId() {
protected int getNextAvailableInsureeId() {
return getMaxIdFromTable("InsureeId", "tblInsuree");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public Login(

@WorkerThread
public void execute(@NonNull String username, @NonNull String password) throws Exception {
if (Global.getGlobal().getOfficerCode() == null) {
Global.getGlobal().setOfficerCode(username);
}
String officerCode = Global.getGlobal().getOfficerCode();
if (officerCode == null) {
throw new IllegalStateException("OfficerCode should not be null on login");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.openimis.imispolicies;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import com.squareup.picasso.Picasso;
import org.openimis.imispolicies.tools.StorageManager;
import org.robolectric.RobolectricTestRunner;

import android.app.Activity;
import android.content.res.Resources;

import org.json.JSONArray;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.HashMap;

@RunWith(RobolectricTestRunner.class)
public class ClientAndroidInterfaceTest {

@Mock
SQLHandler sqlHandler;

@Mock
Global global;

@Mock
Activity activity;

@Mock
Resources resources;

@Mock
StorageManager storageManager;

ClientAndroidInterface client;

@Before
public void setup() {
MockitoAnnotations.openMocks(this);

when(activity.getResources()).thenReturn(resources);
when(resources.getString(anyInt())).thenReturn("mockString");

client = new ClientAndroidInterface(activity, sqlHandler, global, null, storageManager);

when(global.isNetworkAvailable()).thenReturn(true);
}

@Test
public void testCreateNewInsuree_ShouldInsertInDatabase() throws Exception {

String insureeJson = "{"
+ "\"txtInsuranceNumber\":\"12345\","
+ "\"hfInsureeId\":\"0\","
+ "\"hfisHead\":\"1\","
+ "\"txtLastName\":\"Doe\","
+ "\"txtOtherNames\":\"John\","
+ "\"txtBirthDate\":\"1991-01-01\","
+ "\"ddlGender\":\"M\","
+ "\"txtPhoneNumber\":\"690000000\""
+ "\"hfNewPhotoPath\":\"\""
+ "\"hfImagePath\":\"/storage/emulated/0/DCIM/test.jpg\""
+ "}";

ClientAndroidInterface spyClient = spy(client);

HashMap<String, String> mockData = new HashMap<>();
mockData.put("txtInsuranceNumber", "12345");
mockData.put("hfInsureeId", "0");
mockData.put("hfisHead", "1");
mockData.put("txtLastName", "Doe");
mockData.put("txtOtherNames", "John");
mockData.put("txtBirthDate", "1991-01-01");
mockData.put("ddlGender", "M");
mockData.put("txtPhoneNumber", "690000000");
mockData.put("hfNewPhotoPath", "");
mockData.put("hfImagePath", "/storage/emulated/0/DCIM/test.jpg");

when(sqlHandler.getResult(anyString(), any(String[].class))).thenReturn(new JSONArray());
doNothing().when(spyClient).SaveInsureePolicy(anyInt(), anyInt(), anyBoolean(), anyInt());
doNothing().when(sqlHandler).insertData(anyString(), any());
doNothing().when(sqlHandler).updateData(anyString(), any(), anyString(), any(String[].class));
doReturn(mockData).when(spyClient).jsonToTable(anyString());
doReturn(0).when(spyClient).isValidInsureeData(mockData);
doReturn(999).when(spyClient).getNextAvailableInsureeId();
doReturn(0).when(spyClient).getFamilyStatus(anyInt());
doReturn(0).when(spyClient).getInsureeStatus(anyInt());
doNothing().when(spyClient).ShowDialog(anyString());
doNothing().when(spyClient).ShowDialogYesNo(anyInt(), anyInt(), anyInt());
doReturn("").when(spyClient).copyImageFromGalleryToApplication(anyString(), anyString());

doNothing().when(sqlHandler).insertData(anyString(), any());

int result = spyClient.SaveInsuree(
insureeJson,
1, // FamilyId
1, // isHead
0, // ExceedThreshold
0 // PolicyId
);

assertEquals(-999, result);

verify(sqlHandler, times(1)).insertData(eq("tblInsuree"), any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
assertEquals(5, 2 + 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a quoi sert ceci?

}
}