Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add env parsing in gradle and use properties sync config #1320

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 28 additions & 2 deletions opensrp-reveal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<? extends BaseLoginActivity> getAuthenticationActivity() {
return LoginActivity.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -36,14 +40,20 @@ public class RevealSyncConfigurationTest extends BaseUnitTest {
@Mock
private LocationRepository locationRepository;

@Mock
private EnvironmentManager environmentManager;

@Mock
private Environment mockEnvironment;

@Mock
private AllSharedPreferences allSharedPreferences;

private RevealSyncConfiguration syncConfiguration;

@Before
public void setUp() {
syncConfiguration = new RevealSyncConfiguration(locationRepository, allSharedPreferences);
syncConfiguration = new RevealSyncConfiguration(environmentManager, locationRepository, allSharedPreferences);
}

@Test
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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;

import androidx.appcompat.app.AppCompatActivity;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -54,6 +63,9 @@ public class DrawerMenuViewTest extends BaseUnitTest {
@Mock
private TextView operationalAreaTextView;

@Mock
private TextView p2pSyncTextView;

@Mock
private BaseDrawerContract.Presenter presenter;

Expand Down Expand Up @@ -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);
Expand Down