Skip to content

Commit 1f8c2b9

Browse files
Merge pull request #663 from OpenSRP/modify-location-service
Add overload method to fetch LocationServiceHelper
2 parents 8ff27b5 + b0af7d9 commit 1f8c2b9

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION_NAME=2.2.5-SNAPSHOT
1+
VERSION_NAME=2.2.6-SNAPSHOT
22
VERSION_CODE=1
33
GROUP=org.smartregister
44
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application

opensrp-app/src/main/java/org/smartregister/job/SyncAllLocationsServiceJob.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ public class SyncAllLocationsServiceJob extends BaseJob {
1010

1111
public static final String TAG = "SyncAllLocationsServiceJob";
1212

13+
private Class<? extends SyncAllLocationsIntentService> serviceClass;
14+
15+
public SyncAllLocationsServiceJob() {
16+
this(SyncAllLocationsIntentService.class);
17+
}
18+
19+
public SyncAllLocationsServiceJob(Class<? extends SyncAllLocationsIntentService> serviceClass) {
20+
this.serviceClass = serviceClass;
21+
}
22+
1323
@NonNull
1424
@Override
1525
protected Result onRunJob(@NonNull Params params) {
16-
Intent intent = new Intent(getApplicationContext(), SyncAllLocationsIntentService.class);
26+
Intent intent = new Intent(getApplicationContext(), serviceClass);
1727
getApplicationContext().startService(intent);
1828
return params != null && params.getExtras().getBoolean(AllConstants.INTENT_KEY.TO_RESCHEDULE, false) ? Result.RESCHEDULE : Result.SUCCESS;
1929
}

opensrp-app/src/main/java/org/smartregister/sync/helper/LocationServiceHelper.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.smartregister.sync.helper;
22

33
import android.content.Context;
4+
import android.support.annotation.Nullable;
45
import android.text.TextUtils;
56

67
import com.google.gson.Gson;
@@ -21,13 +22,15 @@
2122
import org.smartregister.domain.Response;
2223
import org.smartregister.domain.SyncEntity;
2324
import org.smartregister.domain.SyncProgress;
25+
import org.smartregister.domain.jsonmapping.util.LocationTree;
2426
import org.smartregister.exception.NoHttpResponseException;
2527
import org.smartregister.repository.AllSharedPreferences;
2628
import org.smartregister.repository.BaseRepository;
2729
import org.smartregister.repository.LocationRepository;
2830
import org.smartregister.repository.LocationTagRepository;
2931
import org.smartregister.repository.StructureRepository;
3032
import org.smartregister.service.HTTPAgent;
33+
import org.smartregister.util.AssetHandler;
3134
import org.smartregister.util.PropertiesConverter;
3235
import org.smartregister.util.Utils;
3336

@@ -56,6 +59,7 @@ public class LocationServiceHelper extends BaseHelper {
5659
public static final String CREATE_STRUCTURE_URL = "/rest/location/add";
5760
public static final String COMMON_LOCATIONS_SERVICE_URL = "/location/by-level-and-tags";
5861
public static final String OPENMRS_LOCATION_BY_TEAM_IDS = "/location/by-team-ids";
62+
public static final String LOCATION_HIERARCHY_URL = "/rest/location/hierarchy/";
5963
public static final String STRUCTURES_LAST_SYNC_DATE = "STRUCTURES_LAST_SYNC_DATE";
6064
public static final String LOCATION_LAST_SYNC_DATE = "LOCATION_LAST_SYNC_DATE";
6165
private static final String LOCATIONS_NOT_PROCESSED = "Locations with Ids not processed: ";
@@ -441,5 +445,19 @@ public void fetchAllLocations() {
441445
Timber.e(e, "EXCEPTION %s", e.toString());
442446
}
443447
}
448+
449+
@Nullable
450+
public LocationTree getLocationHierarchy(String locationId) {
451+
LocationTree locationTree = null;
452+
Response<String> resp = getHttpAgent().fetch(MessageFormat.format("{0}{1}{2}",
453+
getFormattedBaseUrl(), LOCATION_HIERARCHY_URL, locationId));
454+
455+
if (resp.isFailure()) {
456+
Timber.e("Location hierarchy sync failed");
457+
return locationTree;
458+
}
459+
460+
return AssetHandler.jsonStringToJava(resp.payload(), LocationTree.class);
461+
}
444462
}
445463

opensrp-app/src/test/java/org/smartregister/sync/helper/LocationServiceHelperTest.java

+82
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.sqlcipher.database.SQLiteDatabase;
66

77
import org.json.JSONException;
8+
import org.junit.After;
89
import org.junit.Before;
910
import org.junit.Test;
1011
import org.mockito.ArgumentCaptor;
@@ -13,13 +14,17 @@
1314
import org.mockito.Mockito;
1415
import org.mockito.MockitoAnnotations;
1516
import org.powermock.reflect.Whitebox;
17+
import org.robolectric.util.ReflectionHelpers;
1618
import org.smartregister.BaseRobolectricUnitTest;
19+
import org.smartregister.Context;
1720
import org.smartregister.CoreLibrary;
21+
import org.smartregister.DristhiConfiguration;
1822
import org.smartregister.SyncConfiguration;
1923
import org.smartregister.domain.Location;
2024
import org.smartregister.domain.LocationTag;
2125
import org.smartregister.domain.Response;
2226
import org.smartregister.domain.ResponseStatus;
27+
import org.smartregister.domain.jsonmapping.util.LocationTree;
2328
import org.smartregister.exception.NoHttpResponseException;
2429
import org.smartregister.repository.AllSharedPreferences;
2530
import org.smartregister.repository.BaseRepository;
@@ -30,13 +35,15 @@
3035
import org.smartregister.service.HTTPAgent;
3136
import org.smartregister.view.activity.DrishtiApplication;
3237

38+
import java.text.MessageFormat;
3339
import java.util.ArrayList;
3440
import java.util.Collections;
3541
import java.util.List;
3642

3743
import static org.junit.Assert.assertEquals;
3844
import static org.junit.Assert.assertFalse;
3945
import static org.junit.Assert.assertNotNull;
46+
import static org.junit.Assert.assertNull;
4047
import static org.junit.Assert.assertTrue;
4148
import static org.mockito.Mockito.verify;
4249
import static org.mockito.Mockito.when;
@@ -78,6 +85,15 @@ public class LocationServiceHelperTest extends BaseRobolectricUnitTest {
7885

7986
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\"}";
8087

88+
@Mock
89+
private CoreLibrary coreLibrary;
90+
91+
@Mock
92+
private Context context;
93+
94+
@Mock
95+
private DristhiConfiguration configuration;
96+
8197
@Before
8298
public void setUp() {
8399
MockitoAnnotations.initMocks(this);
@@ -274,4 +290,70 @@ public void testFetchAllLocations() {
274290
String requestString = stringArgumentCaptor.getAllValues().get(1);
275291
assertEquals("{\"is_jurisdiction\":true,\"serverVersion\":0}", requestString);
276292
}
293+
294+
@Test
295+
public void testGetFormattedBaseUrlStripsEndingSlashDelimiter() {
296+
String url = "https://base.url/";
297+
298+
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
299+
Mockito.doReturn(context).when(coreLibrary).context();
300+
Mockito.doReturn(configuration).when(context).configuration();
301+
Mockito.doReturn(url).when(configuration).dristhiBaseURL();
302+
303+
LocationServiceHelper spyLocationServiceHelper = Mockito.spy(locationServiceHelper);
304+
String baseUrl = spyLocationServiceHelper.getFormattedBaseUrl();
305+
assertNotNull(baseUrl);
306+
assertEquals(url.substring(0, url.length() - 1), baseUrl);
307+
}
308+
309+
@Test
310+
public void testGetLocationHierarchyReturnsNullWhenRequestFails() {
311+
String parentLocation = "1c7ba751-35e8-4b46-9e53-3cb8fd193697";
312+
313+
Mockito.doReturn(new Response<>(ResponseStatus.failure, ""))
314+
.when(httpAgent).fetch(stringArgumentCaptor.capture());
315+
316+
LocationTree tree = locationServiceHelper.getLocationHierarchy(parentLocation);
317+
318+
String expectedUrl = MessageFormat.format("{0}{1}{2}", locationServiceHelper.getFormattedBaseUrl(), LocationServiceHelper.LOCATION_HIERARCHY_URL, parentLocation);
319+
assertEquals(expectedUrl, stringArgumentCaptor.getValue());
320+
assertNull(tree);
321+
}
322+
323+
@Test
324+
public void testGetLocationHierarchyReturnsEmptyTreeForInvalidParentLocationId() {
325+
String parentLocation = "1c7ba751-35e8-4b46-9e53-3cb8fd193697";
326+
327+
Mockito.doReturn(new Response<>(ResponseStatus.success, "{}"))
328+
.when(httpAgent).fetch(stringArgumentCaptor.capture());
329+
330+
LocationTree tree = locationServiceHelper.getLocationHierarchy(parentLocation);
331+
332+
String expectedUrl = MessageFormat.format("{0}{1}{2}", locationServiceHelper.getFormattedBaseUrl(), LocationServiceHelper.LOCATION_HIERARCHY_URL, parentLocation);
333+
assertEquals(expectedUrl, stringArgumentCaptor.getValue());
334+
assertNotNull(tree);
335+
assertEquals(0, tree.getLocationsHierarchy().size());
336+
}
337+
338+
@Test
339+
public void testGetLocationHierarchyReturnsTreeForValidParentLocationId() {
340+
String parentLocation = "1c7ba751-35e8-4b46-9e53-3cb8fd193697";
341+
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\"]}}}";
342+
343+
Mockito.doReturn(new Response<>(ResponseStatus.success, hierarchy))
344+
.when(httpAgent).fetch(stringArgumentCaptor.capture());
345+
346+
LocationTree tree = locationServiceHelper.getLocationHierarchy(parentLocation);
347+
348+
String expectedUrl = MessageFormat.format("{0}{1}{2}", locationServiceHelper.getFormattedBaseUrl(), LocationServiceHelper.LOCATION_HIERARCHY_URL, parentLocation);
349+
assertEquals(expectedUrl, stringArgumentCaptor.getValue());
350+
assertNotNull(tree);
351+
assertTrue(tree.hasLocation(parentLocation));
352+
assertEquals(3, tree.getLocationsHierarchy().get(parentLocation).getChildren().size());
353+
}
354+
355+
@After
356+
public void tearDown() {
357+
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
358+
}
277359
}

0 commit comments

Comments
 (0)