Skip to content

Commit

Permalink
Merge pull request #698 from OpenSRP/move-checkPlatformMigrations-down
Browse files Browse the repository at this point in the history
call checkPlatformMigrations on the last init method
  • Loading branch information
bennsimon authored Nov 24, 2020
2 parents 3a1554f + 9593623 commit 99a954e
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.3.0-SNAPSHOT
VERSION_NAME=3.3.1-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
15 changes: 5 additions & 10 deletions opensrp-app/src/main/java/org/smartregister/CoreLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import android.accounts.AccountManager;
import android.accounts.OnAccountsUpdateListener;
import android.content.Intent;
import androidx.annotation.NonNull;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.text.TextUtils;

import org.apache.commons.lang3.StringUtils;
import org.smartregister.account.AccountAuthenticatorXml;
Expand Down Expand Up @@ -57,19 +57,14 @@ public static void init(Context context) {
}

public static void init(Context context, SyncConfiguration syncConfiguration) {
if (instance == null) {
instance = new CoreLibrary(context, syncConfiguration, null);
}
init(context, syncConfiguration, BuildConfig.BUILD_TIMESTAMP);
}

public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp) {
if (instance == null) {
instance = new CoreLibrary(context, syncConfiguration, null);
buildTimeStamp = buildTimestamp;
}
init(context, syncConfiguration, buildTimestamp, null);
}

public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp, @NonNull P2POptions options) {
public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp, @Nullable P2POptions options) {
if (instance == null) {
instance = new CoreLibrary(context, syncConfiguration, options);
buildTimeStamp = buildTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public static void hideKeyboard(Context context, View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
} catch (Exception e) {
logError("Error encountered while hiding keyboard " + e);
Timber.e(e, "Error encountered while hiding keyboard");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
Expand Down Expand Up @@ -65,10 +66,13 @@ public class ClientProcessorTest extends BaseUnitTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
CoreLibrary.init(context, new TestSyncConfiguration());
when(context.detailsRepository()).thenReturn(detailsRepository);

when(coreLibrary.context()).thenReturn(context);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
doReturn(new TestSyncConfiguration()).when(coreLibrary).getSyncConfiguration();
doReturn(context).when(coreLibrary).context();
doReturn("ec_client_fields.json").when(coreLibrary).getEcClientFieldsFile();

when(context.detailsRepository()).thenReturn(detailsRepository);
when(context.commonrepository(Mockito.anyString())).thenReturn(cr);
when(cr.executeInsertStatement(Mockito.any(ContentValues.class), Mockito.anyString())).thenReturn(1l);
context.updateApplicationContext(RuntimeEnvironment.application);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.HurlStack;
Expand All @@ -22,11 +21,13 @@
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.util.mock.OpenSRPImageLoaderTestActivity;
import org.smartregister.view.activity.DrishtiApplication;

import static org.mockito.Mockito.doReturn;

/**
* Created by kaderchowdhury on 14/11/17.
Expand All @@ -39,12 +40,13 @@ public class OpenSRPImageLoaderTest extends BaseUnitTest {
public PowerMockRule rule = new PowerMockRule();

private OpenSRPImageLoaderTestActivity activity;

@Mock
private Context context;

@Mock
Resources res;
@Mock
private DrishtiApplication drishtiApplication;
private CoreLibrary coreLibrary;

private ActivityController<OpenSRPImageLoaderTestActivity> controller;

@Before
Expand All @@ -53,7 +55,8 @@ public void setUp() throws Exception {
Intent intent = new Intent(RuntimeEnvironment.application, OpenSRPImageLoaderTestActivity.class);
controller = Robolectric.buildActivity(OpenSRPImageLoaderTestActivity.class, intent);
activity = controller.get();
CoreLibrary.init(context);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
doReturn(context).when(coreLibrary).context();
controller.setup();
}

Expand Down Expand Up @@ -81,5 +84,4 @@ public void assertServiceConstructorInitializationNotNull() throws Exception {
OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(Mockito.mock(Service.class), -1);
Assert.assertNotNull(openSRPImageLoader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import android.content.pm.PackageManager;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseUnitTest;
import org.smartregister.CoreLibrary;
import org.smartregister.domain.Setting;
import org.smartregister.repository.AllSettings;
Expand All @@ -28,9 +30,8 @@
* Created by Vincent Karuri on 10/03/2020
*/

@RunWith(PowerMockRunner.class)
@PrepareForTest({Utils.class})
public class SyncUtilsTest {
@PrepareForTest(Utils.class)
public class SyncUtilsTest extends BaseUnitTest {

@Mock
private Context context;
Expand All @@ -43,10 +44,17 @@ public class SyncUtilsTest {

private SyncUtils syncUtils;

@Rule
public PowerMockRule rule = new PowerMockRule();

@Mock
private CoreLibrary coreLibrary;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
CoreLibrary.init(opensrpContext);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
doReturn(opensrpContext).when(coreLibrary).context();
syncUtils = new SyncUtils(context);
doReturn(settingsRepository).when(opensrpContext).allSettings();
}
Expand Down
43 changes: 43 additions & 0 deletions opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.smartregister.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TableRow;

import androidx.test.core.app.ApplicationProvider;
Expand Down Expand Up @@ -31,6 +34,7 @@
import org.smartregister.domain.jsonmapping.util.TeamMember;
import org.smartregister.receiver.SyncStatusBroadcastReceiver;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.service.UserService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -46,6 +50,16 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.smartregister.TestUtils.getContext;
import static org.smartregister.util.Utils.getDefaultLocale;

Expand Down Expand Up @@ -449,5 +463,34 @@ public void testGetAppIdShouldReturnAppId() {
public void testGetAppVersionShouldReturnAppVersion() {
assertNull(Utils.getAppVersion(RuntimeEnvironment.application));
}

@Test
public void testLogoutUserShouldInvokeRequiredMethods() {
org.smartregister.Context opensrpContext = spy(CoreLibrary.getInstance().context());
Context context = spy(opensrpContext.applicationContext());
doReturn(context).when(opensrpContext).applicationContext();
UserService mockUserService = mock(UserService.class);
doReturn(mockUserService).when(opensrpContext).userService();
Utils.logoutUser(opensrpContext, "logged out");
verify(mockUserService, times(1)).forceRemoteLogin(anyString());
verify(mockUserService, times(1)).logoutSession();
verify(context, times(1)).startActivity(any(Intent.class));
}

@Test
public void testHideKeyboardShouldInvokeRequireMethods() {
Activity activity = mock(Activity.class);

View view = mock(View.class);
doReturn(view).when(activity).getCurrentFocus();

InputMethodManager keyboard = mock(InputMethodManager.class);

doReturn(keyboard).when(activity).getSystemService(Context.INPUT_METHOD_SERVICE);

Utils.hideKeyboard(activity);

verify(keyboard, only()).hideSoftInputFromWindow(isNull(), eq(0));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.os.Build;

import com.evernote.android.job.ShadowJobManager;

import net.sqlcipher.database.SQLiteDatabase;

import org.junit.Assert;
Expand All @@ -16,11 +18,11 @@
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.TestSyncConfiguration;
import org.smartregister.customshadows.FontTextViewShadow;
import org.smartregister.repository.Repository;
import org.smartregister.shadows.ShadowAppDatabase;
import org.smartregister.shadows.ShadowDrawableResourcesImpl;
import com.evernote.android.job.ShadowJobManager;
import org.smartregister.shadows.ShadowSQLiteDatabase;
import org.smartregister.util.CredentialsHelper;

Expand Down Expand Up @@ -115,7 +117,7 @@ public void onCreate() {

context = Context.getInstance();
context.updateApplicationContext(getApplicationContext());
CoreLibrary.init(context, null, 1588062490000l);
CoreLibrary.init(context, new TestSyncConfiguration(), 1588062490000l);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.smartregister.BaseUnitTest;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.R;

import java.util.Arrays;

import static org.mockito.Mockito.doReturn;

@RunWith(PowerMockRunner.class)
@PrepareForTest(CoreLibrary.class)
public class FPClientTest {
public class FPClientTest {

@Mock
private Context context;
Expand All @@ -30,19 +32,18 @@ public class FPClientTest {
CoreLibrary coreLibrary;

@Before
public void setUp() {
public void setUp() {
MockitoAnnotations.initMocks(this);
CoreLibrary.init(context);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
doReturn(context).when(coreLibrary).context();

PowerMockito.mockStatic(CoreLibrary.class);
PowerMockito.when(CoreLibrary.getInstance()).thenReturn(coreLibrary);
Mockito.doReturn(context).when(coreLibrary).context();

fpClient = new FPClient("entity id 1", "woman name", "husband name", "village name", "ec no 1");
}

@Test
public void shouldReturnTheReferralFollowUpAlert() {
public void shouldReturnTheReferralFollowUpAlert() {
fpClient.withAlerts(Arrays.asList(new AlertDTO("FP Referral Followup", "normal", "2013-02-02")
, new AlertDTO("OCP Refill", "urgent", "2013-02-02")
, new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02")
Expand All @@ -69,7 +70,7 @@ public void shouldReturnTheReferralFollowUpAlert() {
}

@Test
public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExist() {
public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExist() {
fpClient.withAlerts(Arrays.asList(new AlertDTO("OCP Refill", "urgent", "2013-02-02")
, new AlertDTO("FP Followup", "normal", "2013-02-02")
, new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02")))
Expand All @@ -89,7 +90,7 @@ public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExi
}

@Test
public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAlertExitsAndReferralDataAndFPFollowUpIsNotSpecified() {
public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAlertExitsAndReferralDataAndFPFollowUpIsNotSpecified() {
fpClient.withAlerts(Arrays.asList(new AlertDTO("OCP Refill", "urgent", "2013-02-02")
, new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02")))
.withFPMethod("female_sterilization");
Expand All @@ -108,7 +109,7 @@ public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAler
}

@Test
public void shouldOnlySetFemaleSterilizationFollowUpDataWhenFPMethodIsAlsoFemaleSterilization() {
public void shouldOnlySetFemaleSterilizationFollowUpDataWhenFPMethodIsAlsoFemaleSterilization() {
fpClient.withAlerts(Arrays.asList(new AlertDTO("Male Sterilization Followup", "urgent", "2013-02-02")
, new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02")))
.withFPMethod("female_sterilization");
Expand Down Expand Up @@ -145,7 +146,7 @@ public void shouldSetCondomRefillDataOnlyIfFPMethodIsAlsoCondom() {
}

@Test
public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndNotAnyOfSterilizationMethods() {
public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndNotAnyOfSterilizationMethods() {
fpClient.withAlerts(Arrays.asList(new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02")
, new AlertDTO("Condom Refill", "urgent", "2013-02-02")))
.withFPMethod("condom");
Expand Down
Loading

0 comments on commit 99a954e

Please sign in to comment.