5
5
import net .sqlcipher .database .SQLiteDatabase ;
6
6
7
7
import org .json .JSONException ;
8
+ import org .junit .After ;
8
9
import org .junit .Before ;
9
10
import org .junit .Test ;
10
11
import org .mockito .ArgumentCaptor ;
13
14
import org .mockito .Mockito ;
14
15
import org .mockito .MockitoAnnotations ;
15
16
import org .powermock .reflect .Whitebox ;
17
+ import org .robolectric .util .ReflectionHelpers ;
16
18
import org .smartregister .BaseRobolectricUnitTest ;
19
+ import org .smartregister .Context ;
17
20
import org .smartregister .CoreLibrary ;
21
+ import org .smartregister .DristhiConfiguration ;
18
22
import org .smartregister .SyncConfiguration ;
19
23
import org .smartregister .domain .Location ;
20
24
import org .smartregister .domain .LocationTag ;
21
25
import org .smartregister .domain .Response ;
22
26
import org .smartregister .domain .ResponseStatus ;
27
+ import org .smartregister .domain .jsonmapping .util .LocationTree ;
23
28
import org .smartregister .exception .NoHttpResponseException ;
24
29
import org .smartregister .repository .AllSharedPreferences ;
25
30
import org .smartregister .repository .BaseRepository ;
30
35
import org .smartregister .service .HTTPAgent ;
31
36
import org .smartregister .view .activity .DrishtiApplication ;
32
37
38
+ import java .text .MessageFormat ;
33
39
import java .util .ArrayList ;
34
40
import java .util .Collections ;
35
41
import java .util .List ;
36
42
37
43
import static org .junit .Assert .assertEquals ;
38
44
import static org .junit .Assert .assertFalse ;
39
45
import static org .junit .Assert .assertNotNull ;
46
+ import static org .junit .Assert .assertNull ;
40
47
import static org .junit .Assert .assertTrue ;
41
48
import static org .mockito .Mockito .verify ;
42
49
import static org .mockito .Mockito .when ;
@@ -78,6 +85,15 @@ public class LocationServiceHelperTest extends BaseRobolectricUnitTest {
78
85
79
86
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\" }" ;
80
87
88
+ @ Mock
89
+ private CoreLibrary coreLibrary ;
90
+
91
+ @ Mock
92
+ private Context context ;
93
+
94
+ @ Mock
95
+ private DristhiConfiguration configuration ;
96
+
81
97
@ Before
82
98
public void setUp () {
83
99
MockitoAnnotations .initMocks (this );
@@ -274,4 +290,70 @@ public void testFetchAllLocations() {
274
290
String requestString = stringArgumentCaptor .getAllValues ().get (1 );
275
291
assertEquals ("{\" is_jurisdiction\" :true,\" serverVersion\" :0}" , requestString );
276
292
}
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
+ }
277
359
}
0 commit comments