diff --git a/README.md b/README.md index 207880cbc7..746de406de 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,15 @@ These instructions will get you a copy of the project up and running on your loc ### Steps to set up [OpenSRP android client app build](https://smartregister.atlassian.net/wiki/spaces/Documentation/pages/6619236/OpenSRP+App+Build) +### Setting up local.properties +Add at least one environment in your properties file using the below format + +> oauth.client.{env}.id="{client_id_value}" +> +> oauth.client.{env}.secret="{client_secret_value}" +> +> oauth.client.{env}.url="{environment_url}" + ### Running the tests [Android client unit tests](https://smartregister.atlassian.net/wiki/spaces/Documentation/pages/65570428/OpenSRP+Client) diff --git a/opensrp-reveal/build.gradle b/opensrp-reveal/build.gradle index ee3b7ebecc..2e6f8aed82 100644 --- a/opensrp-reveal/build.gradle +++ b/opensrp-reveal/build.gradle @@ -71,7 +71,7 @@ android { minSdkVersion 18 targetSdkVersion 28 versionCode 34 - versionName "5.3.26" + versionName "5.4.0" multiDexEnabled true buildConfigField "long", "MAX_SERVER_TIME_DIFFERENCE", "1800000l" buildConfigField "boolean", "TIME_CHECK", "false" @@ -122,13 +122,31 @@ android { project.logger.error("oauth.client.secret variable is not set in your local.properties") } - + if (properties != null) { + def envsArray = "[ " + properties.toSorted().findAll { + return it.key.toString().contains("oauth.client") && it.key.toString().tokenize(".").size() > 3 + }.groupBy { + return it.key.toString().contains("secret") + }[true].each { + def env = it.key.toString().tokenize(".")[2] + envsArray += """${getJavaMap([env : "$env", + 'client.id' : properties["oauth.client." + env + ".id"], + 'client.secret' : properties["oauth.client." + env + ".secret"], + url: properties["oauth.client." + env + ".url"] + ])},""" + } + envsArray = envsArray.substring(0, envsArray.length() - 1) + envsArray += "]" + buildConfigField "String", "ENV_ARRAY", "\"${envsArray}\"" + } } else { println("local.properties does not exist") buildConfigField "String", "MAPBOX_SDK_ACCESS_TOKEN", "\"sample_key\"" buildConfigField "String", "DG_CONNECT_ID", "\"sample_key\"" buildConfigField "String", "OAUTH_CLIENT_ID", "\"sample_client_id\"" buildConfigField "String", "OAUTH_CLIENT_SECRET", "\"sample_client_secret\"" + buildConfigField "String", "ENV_ARRAY", '"[]"' } } dexOptions { @@ -201,6 +219,13 @@ android { } +private static String getJavaMap(apiUrlMap) { + def hashMap = "{ " + apiUrlMap.each { k, v -> hashMap += "\\\"${k}\\\":" + "\\\"${v.toString().replace("\"", '')}\\\"" + "," } + hashMap = hashMap.substring(0, hashMap.length() - 1) + return hashMap + "}" +} + tasks.withType(Test) { jacoco.includeNoLocationClasses = true maxHeapSize = "4g" @@ -224,6 +249,7 @@ dependencies { } implementation('org.smartregister:opensrp-client-core:4.3.3-SNAPSHOT@aar') { + transitive = true exclude group: 'com.google.guava', module: 'guava' exclude group: 'com.android.support', module: 'appcompat-v7' diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/application/RevealApplication.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/application/RevealApplication.java index aef89fb714..9ca056ecb6 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/application/RevealApplication.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/application/RevealApplication.java @@ -19,6 +19,7 @@ import org.json.JSONObject; import org.smartregister.Context; import org.smartregister.CoreLibrary; +import org.smartregister.EnvironmentManager; import org.smartregister.P2POptions; import org.smartregister.commonregistry.CommonFtsObject; import org.smartregister.configurableviews.ConfigurableViewsLibrary; @@ -126,7 +127,7 @@ public void onCreate() { // Initialize Modules Fabric.with(this, new Crashlytics.Builder().core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build()); P2POptions p2POptions = new P2POptions(true); - CoreLibrary.init(context, new RevealSyncConfiguration(), BuildConfig.BUILD_TIMESTAMP, p2POptions); + CoreLibrary.init(context, new RevealSyncConfiguration(new EnvironmentManager(BuildConfig.ENV_ARRAY)), BuildConfig.BUILD_TIMESTAMP, p2POptions); forceRemoteLoginForInConsistentUsername(); if (BuildConfig.BUILD_COUNTRY == Country.NAMIBIA) { CoreLibrary.getInstance().setEcClientFieldsFile(Constants.ECClientConfig.NAMIBIA_EC_CLIENT_FIELDS); diff --git a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealSyncConfiguration.java b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealSyncConfiguration.java index a28d54e39e..657bc8de8f 100644 --- a/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealSyncConfiguration.java +++ b/opensrp-reveal/src/main/java/org/smartregister/reveal/util/RevealSyncConfiguration.java @@ -3,7 +3,8 @@ import android.text.TextUtils; import android.util.Pair; -import org.smartregister.SyncConfiguration; +import org.smartregister.EnvironmentManager; +import org.smartregister.PropertiesSyncConfiguration; import org.smartregister.SyncFilter; import org.smartregister.repository.AllSharedPreferences; import org.smartregister.repository.LocationRepository; @@ -18,16 +19,18 @@ /** * Created by samuelgithengi on 11/29/18. */ -public class RevealSyncConfiguration extends SyncConfiguration { +public class RevealSyncConfiguration extends PropertiesSyncConfiguration { private AllSharedPreferences sharedPreferences; private LocationRepository locationRepository; - public RevealSyncConfiguration() { + public RevealSyncConfiguration(EnvironmentManager environmentManager) { + super(environmentManager); } - public RevealSyncConfiguration(LocationRepository locationRepository, AllSharedPreferences sharedPreferences) { + public RevealSyncConfiguration(EnvironmentManager environmentManager, LocationRepository locationRepository, AllSharedPreferences sharedPreferences) { + this(environmentManager); this.locationRepository = locationRepository; this.sharedPreferences = sharedPreferences; } @@ -129,16 +132,6 @@ public boolean clearDataOnNewTeamLogin() { return true; } - @Override - public String getOauthClientId() { - return BuildConfig.OAUTH_CLIENT_ID; - } - - @Override - public String getOauthClientSecret() { - return BuildConfig.OAUTH_CLIENT_SECRET; - } - @Override public Class getAuthenticationActivity() { return LoginActivity.class; diff --git a/opensrp-reveal/src/test/java/org/smartregister/reveal/TestRevealApplication.java b/opensrp-reveal/src/test/java/org/smartregister/reveal/TestRevealApplication.java index 7eab4d9da8..778562df20 100644 --- a/opensrp-reveal/src/test/java/org/smartregister/reveal/TestRevealApplication.java +++ b/opensrp-reveal/src/test/java/org/smartregister/reveal/TestRevealApplication.java @@ -7,6 +7,7 @@ import org.smartregister.Context; import org.smartregister.CoreLibrary; +import org.smartregister.EnvironmentManager; import org.smartregister.configurableviews.ConfigurableViewsLibrary; import org.smartregister.family.FamilyLibrary; import org.smartregister.receiver.ValidateAssignmentReceiver; @@ -31,7 +32,7 @@ public void onCreate() { mInstance = this; context = Context.getInstance(); context.updateApplicationContext(getApplicationContext()); - CoreLibrary.init(context, new RevealSyncConfiguration()); + CoreLibrary.init(context, new RevealSyncConfiguration(new EnvironmentManager(BuildConfig.ENV_ARRAY))); ConfigurableViewsLibrary.init(context); FamilyLibrary.init(context, getMetadata(), BuildConfig.VERSION_CODE, BuildConfig.DATABASE_VERSION); diff --git a/opensrp-reveal/src/test/java/org/smartregister/reveal/util/RevealSyncConfigurationTest.java b/opensrp-reveal/src/test/java/org/smartregister/reveal/util/RevealSyncConfigurationTest.java index a259af5e24..2820541bb1 100644 --- a/opensrp-reveal/src/test/java/org/smartregister/reveal/util/RevealSyncConfigurationTest.java +++ b/opensrp-reveal/src/test/java/org/smartregister/reveal/util/RevealSyncConfigurationTest.java @@ -9,7 +9,9 @@ import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.powermock.reflect.Whitebox; +import org.smartregister.EnvironmentManager; import org.smartregister.SyncFilter; +import org.smartregister.domain.Environment; import org.smartregister.repository.AllSharedPreferences; import org.smartregister.repository.LocationRepository; import org.smartregister.reveal.BaseUnitTest; @@ -23,6 +25,8 @@ import static org.junit.Assert.assertFalse; 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.Mockito.when; /** @@ -36,6 +40,12 @@ public class RevealSyncConfigurationTest extends BaseUnitTest { @Mock private LocationRepository locationRepository; + @Mock + private EnvironmentManager environmentManager; + + @Mock + private Environment mockEnvironment; + @Mock private AllSharedPreferences allSharedPreferences; @@ -43,7 +53,7 @@ public class RevealSyncConfigurationTest extends BaseUnitTest { @Before public void setUp() { - syncConfiguration = new RevealSyncConfiguration(locationRepository, allSharedPreferences); + syncConfiguration = new RevealSyncConfiguration(environmentManager, locationRepository, allSharedPreferences); } @Test @@ -76,6 +86,13 @@ public void getLocationSyncFilterValue() { Whitebox.setInternalState(BuildConfig.class, BuildConfig.BUILD_COUNTRY, buildCountry); } + @Test + public void testGetSettingsSyncFilterValue() { + when(allSharedPreferences.fetchRegisteredANM()).thenReturn("122132"); + when(allSharedPreferences.fetchDefaultTeamId(anyString())).thenReturn("122132"); + assertEquals("122132", syncConfiguration.getSettingsSyncFilterValue()); + } + @Test public void getUniqueIdSource() { assertEquals(BuildConfig.OPENMRS_UNIQUE_ID_SOURCE, syncConfiguration.getUniqueIdSource()); @@ -143,11 +160,15 @@ public void testClearDataOnNewTeamLogin() { @Test public void testGetOauthClientId() { + when(environmentManager.getEnvironment(any())).thenReturn(mockEnvironment); + when(mockEnvironment.getId()).thenReturn(BuildConfig.OAUTH_CLIENT_ID); assertEquals(BuildConfig.OAUTH_CLIENT_ID, syncConfiguration.getOauthClientId()); } @Test public void testGetOauthClientSecret() { + when(environmentManager.getEnvironment(any())).thenReturn(mockEnvironment); + when(mockEnvironment.getSecret()).thenReturn(BuildConfig.OAUTH_CLIENT_SECRET); assertEquals(BuildConfig.OAUTH_CLIENT_SECRET, syncConfiguration.getOauthClientSecret()); } diff --git a/opensrp-reveal/src/test/java/org/smartregister/reveal/view/DrawerMenuViewTest.java b/opensrp-reveal/src/test/java/org/smartregister/reveal/view/DrawerMenuViewTest.java index 7373a91bb5..8da7a725f5 100644 --- a/opensrp-reveal/src/test/java/org/smartregister/reveal/view/DrawerMenuViewTest.java +++ b/opensrp-reveal/src/test/java/org/smartregister/reveal/view/DrawerMenuViewTest.java @@ -1,7 +1,10 @@ package org.smartregister.reveal.view; import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.view.View; +import android.view.ViewTreeObserver; import android.widget.ProgressBar; import android.widget.TextView; @@ -9,6 +12,8 @@ import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; +import com.google.android.material.navigation.NavigationView; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -21,11 +26,15 @@ import org.robolectric.Robolectric; import org.robolectric.RuntimeEnvironment; import org.smartregister.reveal.BaseUnitTest; +import org.smartregister.reveal.BuildConfig; import org.smartregister.reveal.R; import org.smartregister.reveal.contract.BaseDrawerContract; +import org.smartregister.reveal.util.Country; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -54,6 +63,9 @@ public class DrawerMenuViewTest extends BaseUnitTest { @Mock private TextView operationalAreaTextView; + @Mock + private TextView p2pSyncTextView; + @Mock private BaseDrawerContract.Presenter presenter; @@ -111,6 +123,43 @@ public void testSetOperationalArea() { assertEquals("Akros_1", stringArgumentCaptor.getValue()); } + + @Test + public void testInitializeDrawerLayoutForZambiaAndSenegal() throws PackageManager.NameNotFoundException { + drawerMenuView = spy(drawerMenuView); + doReturn(mockActivity).when(activity).getActivity(); + NavigationView navView = mock(NavigationView.class); + View headView = mock(View.class); + DrawerLayout drawerLayout = mock(DrawerLayout.class); + PackageManager packageManager = mock(PackageManager.class); + when(packageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mock(PackageInfo.class)); + when(mockActivity.findViewById(R.id.drawer_layout)).thenReturn(drawerLayout); + when(mockActivity.findViewById(R.id.nav_view)).thenReturn(navView); + when(mockActivity.getPackageManager()).thenReturn(packageManager); + when(mockActivity.getPackageName()).thenReturn(""); + when(navView.getHeaderView(0)).thenReturn(headView); + when(headView.getViewTreeObserver()).thenReturn(mock(ViewTreeObserver.class)); + when(headView.findViewById(R.id.btn_navMenu_p2pSyncBtn)).thenReturn(p2pSyncTextView); + when(headView.findViewById(R.id.btn_navMenu_offline_maps)).thenReturn(planTextView); + when(headView.findViewById(R.id.sync_button)).thenReturn(planTextView); + when(headView.findViewById(R.id.logout_button)).thenReturn(planTextView); + when(headView.findViewById(R.id.plan_selector)).thenReturn(planTextView); + when(headView.findViewById(R.id.operational_area_selector)).thenReturn(planTextView); + when(headView.findViewById(R.id.application_updated)).thenReturn(planTextView); + when(headView.findViewById(R.id.application_version)).thenReturn(planTextView); + when(headView.findViewById(R.id.btn_navMenu_summaryForms)).thenReturn(planTextView); + when(headView.findViewById(R.id.operator_label)).thenReturn(planTextView); + when(headView.findViewById(R.id.facility_label)).thenReturn(planTextView); + when(headView.findViewById(R.id.district_label)).thenReturn(planTextView); + when(headView.findViewById(R.id.btn_navMenu_filled_forms)).thenReturn(planTextView); + + Country buildCountry = BuildConfig.BUILD_COUNTRY; + Whitebox.setInternalState(BuildConfig.class, BuildConfig.BUILD_COUNTRY, Country.ZAMBIA); + drawerMenuView.initializeDrawerLayout(); + verify(p2pSyncTextView).setVisibility(View.VISIBLE); + Whitebox.setInternalState(BuildConfig.class, BuildConfig.BUILD_COUNTRY, buildCountry); + } + @Test public void testGetOperationalArea() { drawerMenuView = spy(drawerMenuView);