Skip to content

Commit

Permalink
Merge pull request #663 from OpenSRP/modify-location-service
Browse files Browse the repository at this point in the history
Add overload method to fetch LocationServiceHelper
  • Loading branch information
vincent-karuri authored Oct 14, 2020
2 parents 8ff27b5 + b0af7d9 commit 1f8c2b9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 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=2.2.5-SNAPSHOT
VERSION_NAME=2.2.6-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ public class SyncAllLocationsServiceJob extends BaseJob {

public static final String TAG = "SyncAllLocationsServiceJob";

private Class<? extends SyncAllLocationsIntentService> serviceClass;

public SyncAllLocationsServiceJob() {
this(SyncAllLocationsIntentService.class);
}

public SyncAllLocationsServiceJob(Class<? extends SyncAllLocationsIntentService> serviceClass) {
this.serviceClass = serviceClass;
}

@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
Intent intent = new Intent(getApplicationContext(), SyncAllLocationsIntentService.class);
Intent intent = new Intent(getApplicationContext(), serviceClass);
getApplicationContext().startService(intent);
return params != null && params.getExtras().getBoolean(AllConstants.INTENT_KEY.TO_RESCHEDULE, false) ? Result.RESCHEDULE : Result.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.smartregister.sync.helper;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.google.gson.Gson;
Expand All @@ -21,13 +22,15 @@
import org.smartregister.domain.Response;
import org.smartregister.domain.SyncEntity;
import org.smartregister.domain.SyncProgress;
import org.smartregister.domain.jsonmapping.util.LocationTree;
import org.smartregister.exception.NoHttpResponseException;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.BaseRepository;
import org.smartregister.repository.LocationRepository;
import org.smartregister.repository.LocationTagRepository;
import org.smartregister.repository.StructureRepository;
import org.smartregister.service.HTTPAgent;
import org.smartregister.util.AssetHandler;
import org.smartregister.util.PropertiesConverter;
import org.smartregister.util.Utils;

Expand Down Expand Up @@ -56,6 +59,7 @@ public class LocationServiceHelper extends BaseHelper {
public static final String CREATE_STRUCTURE_URL = "/rest/location/add";
public static final String COMMON_LOCATIONS_SERVICE_URL = "/location/by-level-and-tags";
public static final String OPENMRS_LOCATION_BY_TEAM_IDS = "/location/by-team-ids";
public static final String LOCATION_HIERARCHY_URL = "/rest/location/hierarchy/";
public static final String STRUCTURES_LAST_SYNC_DATE = "STRUCTURES_LAST_SYNC_DATE";
public static final String LOCATION_LAST_SYNC_DATE = "LOCATION_LAST_SYNC_DATE";
private static final String LOCATIONS_NOT_PROCESSED = "Locations with Ids not processed: ";
Expand Down Expand Up @@ -441,5 +445,19 @@ public void fetchAllLocations() {
Timber.e(e, "EXCEPTION %s", e.toString());
}
}

@Nullable
public LocationTree getLocationHierarchy(String locationId) {
LocationTree locationTree = null;
Response<String> resp = getHttpAgent().fetch(MessageFormat.format("{0}{1}{2}",
getFormattedBaseUrl(), LOCATION_HIERARCHY_URL, locationId));

if (resp.isFailure()) {
Timber.e("Location hierarchy sync failed");
return locationTree;
}

return AssetHandler.jsonStringToJava(resp.payload(), LocationTree.class);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.sqlcipher.database.SQLiteDatabase;

import org.json.JSONException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -13,13 +14,17 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.reflect.Whitebox;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.DristhiConfiguration;
import org.smartregister.SyncConfiguration;
import org.smartregister.domain.Location;
import org.smartregister.domain.LocationTag;
import org.smartregister.domain.Response;
import org.smartregister.domain.ResponseStatus;
import org.smartregister.domain.jsonmapping.util.LocationTree;
import org.smartregister.exception.NoHttpResponseException;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.BaseRepository;
Expand All @@ -30,13 +35,15 @@
import org.smartregister.service.HTTPAgent;
import org.smartregister.view.activity.DrishtiApplication;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -78,6 +85,15 @@ public class LocationServiceHelperTest extends BaseRobolectricUnitTest {

private String structureJSon = "{\"geometry\":{\"coordinates\":[28.351322951711495,-15.419607299156059],\"type\":\"Point\"},\"id\":\"3c35325e-4a34-4730-b67d-c824d6e783ba\",\"properties\":{\"effectiveStartDate\":\"2019-06-11T1129\",\"geographicLevel\":0,\"parentId\":\"3951\",\"status\":\"Pending Review\",\"type\":\"Mosquito Collection Point\",\"uid\":\"f8e27dee-81d7-4a5e-997c-b9d670a676f7\",\"version\":0},\"serverVersion\":1560245526899,\"syncStatus\":\"Unsynced\",\"type\":\"Feature\"}";

@Mock
private CoreLibrary coreLibrary;

@Mock
private Context context;

@Mock
private DristhiConfiguration configuration;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Expand Down Expand Up @@ -274,4 +290,70 @@ public void testFetchAllLocations() {
String requestString = stringArgumentCaptor.getAllValues().get(1);
assertEquals("{\"is_jurisdiction\":true,\"serverVersion\":0}", requestString);
}

@Test
public void testGetFormattedBaseUrlStripsEndingSlashDelimiter() {
String url = "https://base.url/";

ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
Mockito.doReturn(context).when(coreLibrary).context();
Mockito.doReturn(configuration).when(context).configuration();
Mockito.doReturn(url).when(configuration).dristhiBaseURL();

LocationServiceHelper spyLocationServiceHelper = Mockito.spy(locationServiceHelper);
String baseUrl = spyLocationServiceHelper.getFormattedBaseUrl();
assertNotNull(baseUrl);
assertEquals(url.substring(0, url.length() - 1), baseUrl);
}

@Test
public void testGetLocationHierarchyReturnsNullWhenRequestFails() {
String parentLocation = "1c7ba751-35e8-4b46-9e53-3cb8fd193697";

Mockito.doReturn(new Response<>(ResponseStatus.failure, ""))
.when(httpAgent).fetch(stringArgumentCaptor.capture());

LocationTree tree = locationServiceHelper.getLocationHierarchy(parentLocation);

String expectedUrl = MessageFormat.format("{0}{1}{2}", locationServiceHelper.getFormattedBaseUrl(), LocationServiceHelper.LOCATION_HIERARCHY_URL, parentLocation);
assertEquals(expectedUrl, stringArgumentCaptor.getValue());
assertNull(tree);
}

@Test
public void testGetLocationHierarchyReturnsEmptyTreeForInvalidParentLocationId() {
String parentLocation = "1c7ba751-35e8-4b46-9e53-3cb8fd193697";

Mockito.doReturn(new Response<>(ResponseStatus.success, "{}"))
.when(httpAgent).fetch(stringArgumentCaptor.capture());

LocationTree tree = locationServiceHelper.getLocationHierarchy(parentLocation);

String expectedUrl = MessageFormat.format("{0}{1}{2}", locationServiceHelper.getFormattedBaseUrl(), LocationServiceHelper.LOCATION_HIERARCHY_URL, parentLocation);
assertEquals(expectedUrl, stringArgumentCaptor.getValue());
assertNotNull(tree);
assertEquals(0, tree.getLocationsHierarchy().size());
}

@Test
public void testGetLocationHierarchyReturnsTreeForValidParentLocationId() {
String parentLocation = "1c7ba751-35e8-4b46-9e53-3cb8fd193697";
String hierarchy = "{\"locationsHierarchy\":{\"map\":{\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\":{\"id\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"label\":\"Indonesia Division 1\",\"node\":{\"locationId\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"name\":\"Indonesia Division 1\",\"attributes\":{\"geographicLevel\":0},\"voided\":false},\"children\":{\"2c7ba751-35e8-4b46-9e53-3cb8fd193697\":{\"id\":\"2c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"label\":\"Indonesia Location 1\",\"node\":{\"locationId\":\"2c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"name\":\"Indonesia Location 1\",\"parentLocation\":{\"locationId\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"voided\":false},\"attributes\":{\"geographicLevel\":0},\"voided\":false},\"parent\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\"},\"3c7ba751-35e8-4b46-9e53-3cb8fd193697\":{\"id\":\"3c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"label\":\"Indonesia Location 2\",\"node\":{\"locationId\":\"3c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"name\":\"Indonesia Location 2\",\"parentLocation\":{\"locationId\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"voided\":false},\"attributes\":{\"geographicLevel\":0},\"voided\":false},\"parent\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\"},\"4c7ba751-35e8-4b46-9e53-3cb8fd193697\":{\"id\":\"4c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"label\":\"Indonesia Location 3\",\"node\":{\"locationId\":\"4c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"name\":\"Indonesia Location 3\",\"parentLocation\":{\"locationId\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"voided\":false},\"attributes\":{\"geographicLevel\":0},\"voided\":false},\"parent\":\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\"}}}},\"parentChildren\":{\"1c7ba751-35e8-4b46-9e53-3cb8fd193697\":[\"2c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"3c7ba751-35e8-4b46-9e53-3cb8fd193697\",\"4c7ba751-35e8-4b46-9e53-3cb8fd193697\"]}}}";

Mockito.doReturn(new Response<>(ResponseStatus.success, hierarchy))
.when(httpAgent).fetch(stringArgumentCaptor.capture());

LocationTree tree = locationServiceHelper.getLocationHierarchy(parentLocation);

String expectedUrl = MessageFormat.format("{0}{1}{2}", locationServiceHelper.getFormattedBaseUrl(), LocationServiceHelper.LOCATION_HIERARCHY_URL, parentLocation);
assertEquals(expectedUrl, stringArgumentCaptor.getValue());
assertNotNull(tree);
assertTrue(tree.hasLocation(parentLocation));
assertEquals(3, tree.getLocationsHierarchy().get(parentLocation).getChildren().size());
}

@After
public void tearDown() {
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
}
}

0 comments on commit 1f8c2b9

Please sign in to comment.