Skip to content

Commit 5cc556e

Browse files
return the actual error coming back from nn when fetching an offline route fails (#1660)
1 parent 2fe22ae commit 5cc556e

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/OfflineRoute.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.support.annotation.NonNull;
55
import android.support.annotation.Nullable;
66

7-
import com.google.gson.Gson;
87
import com.mapbox.api.directions.v5.models.DirectionsResponse;
98
import com.mapbox.api.directions.v5.models.DirectionsRoute;
109
import com.mapbox.core.exceptions.ServicesException;
@@ -14,7 +13,6 @@
1413
import java.util.List;
1514

1615
import okhttp3.HttpUrl;
17-
import timber.log.Timber;
1816

1917
/**
2018
* The {@link OfflineRoute} class wraps the {@link NavigationRoute} class with parameters which
@@ -73,10 +71,10 @@ public String buildUrl() {
7371
@Nullable
7472
DirectionsRoute retrieveOfflineRoute(@NonNull RouterResult response) {
7573
boolean success = response.getSuccess();
76-
String jsonResponse = response.getJson();
77-
if (checkOfflineRoute(success, jsonResponse)) {
74+
if (!checkOfflineRoute(success)) {
7875
return null;
7976
}
77+
String jsonResponse = response.getJson();
8078
return obtainRouteFor(jsonResponse);
8179
}
8280

@@ -155,11 +153,8 @@ private String buildOfflineUrl(String onlineUrl) {
155153
return offlineUrlBuilder.build().toString();
156154
}
157155

158-
private boolean checkOfflineRoute(boolean isSuccess, String json) {
159-
if (!isSuccess) {
160-
Gson gson = new Gson();
161-
OfflineRouteError error = gson.fromJson(json, OfflineRouteError.class);
162-
Timber.e("Error occurred fetching offline route: %s - Code: %d", error.getError(), error.getErrorCode());
156+
private boolean checkOfflineRoute(boolean isSuccess) {
157+
if (isSuccess) {
163158
return true;
164159
}
165160
return false;

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/OfflineRouteRetrievalTask.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package com.mapbox.services.android.navigation.v5.navigation;
22

33
import android.os.AsyncTask;
4+
import android.support.annotation.NonNull;
45

6+
import com.google.gson.Gson;
57
import com.mapbox.api.directions.v5.models.DirectionsRoute;
68
import com.mapbox.navigator.Navigator;
79
import com.mapbox.navigator.RouterResult;
810

11+
import timber.log.Timber;
12+
913
class OfflineRouteRetrievalTask extends AsyncTask<OfflineRoute, Void, DirectionsRoute> {
1014
private static final int FIRST_ROUTE = 0;
1115
private final Navigator navigator;
1216
private final OnOfflineRouteFoundCallback callback;
17+
private RouterResult routerResult;
1318

1419
OfflineRouteRetrievalTask(Navigator navigator, OnOfflineRouteFoundCallback callback) {
1520
this.navigator = navigator;
1621
this.callback = callback;
1722
}
1823

24+
// For testing only
25+
OfflineRouteRetrievalTask(Navigator navigator, OnOfflineRouteFoundCallback callback, RouterResult routerResult) {
26+
this(navigator, callback);
27+
this.routerResult = routerResult;
28+
}
29+
1930
@Override
2031
protected DirectionsRoute doInBackground(OfflineRoute... offlineRoutes) {
21-
RouterResult routerResult;
2232
String url = offlineRoutes[FIRST_ROUTE].buildUrl();
2333

2434
synchronized (navigator) {
@@ -33,8 +43,20 @@ protected void onPostExecute(DirectionsRoute offlineRoute) {
3343
if (offlineRoute != null) {
3444
callback.onRouteFound(offlineRoute);
3545
} else {
36-
OfflineError error = new OfflineError("Offline route was not found");
46+
String errorMessage = generateErrorMessage();
47+
OfflineError error = new OfflineError(errorMessage);
3748
callback.onError(error);
3849
}
3950
}
51+
52+
@NonNull
53+
private String generateErrorMessage() {
54+
String jsonResponse = routerResult.getJson();
55+
Gson gson = new Gson();
56+
OfflineRouteError routeError = gson.fromJson(jsonResponse, OfflineRouteError.class);
57+
String errorMessage = String.format("Error occurred fetching offline route: %s - Code: %d", routeError.getError(),
58+
routeError.getErrorCode());
59+
Timber.e(errorMessage);
60+
return errorMessage;
61+
}
4062
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.mapbox.services.android.navigation.v5.navigation;
2+
3+
import com.mapbox.api.directions.v5.models.DirectionsRoute;
4+
import com.mapbox.navigator.Navigator;
5+
import com.mapbox.navigator.RouterResult;
6+
7+
import org.junit.Test;
8+
import org.mockito.ArgumentCaptor;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.mockito.ArgumentMatchers.any;
12+
import static org.mockito.ArgumentMatchers.eq;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.verify;
15+
import static org.mockito.Mockito.when;
16+
17+
public class OfflineRouteRetrievalTaskTest {
18+
19+
@Test
20+
public void checksOnErrorIsCalledIfRouteIsNotFetched() {
21+
Navigator mockedNavigator = mock(Navigator.class);
22+
OnOfflineRouteFoundCallback mockedCallback = mock(OnOfflineRouteFoundCallback.class);
23+
RouterResult mockedResult = mock(RouterResult.class);
24+
when(mockedResult.getJson()).thenReturn("{\"status\": \"Bad Request\", \"status_code\": 400, \"error\": \"No " +
25+
"suitable edges near location\", \"error_code\": 171}");
26+
OfflineRouteRetrievalTask theOfflineRouteRetrievalTask = new OfflineRouteRetrievalTask(mockedNavigator,
27+
mockedCallback, mockedResult);
28+
DirectionsRoute nullRoute = null;
29+
30+
theOfflineRouteRetrievalTask.onPostExecute(nullRoute);
31+
32+
verify(mockedCallback).onError(any(OfflineError.class));
33+
}
34+
35+
@Test
36+
public void checksErrorMessageIsWellFormedIfRouteIsNotFetched() {
37+
Navigator mockedNavigator = mock(Navigator.class);
38+
OnOfflineRouteFoundCallback mockedCallback = mock(OnOfflineRouteFoundCallback.class);
39+
RouterResult mockedResult = mock(RouterResult.class);
40+
when(mockedResult.getJson()).thenReturn("{\"status\": \"Bad Request\", \"status_code\": 400, \"error\": \"No " +
41+
"suitable edges near location\", \"error_code\": 171}");
42+
OfflineRouteRetrievalTask theOfflineRouteRetrievalTask = new OfflineRouteRetrievalTask(mockedNavigator,
43+
mockedCallback, mockedResult);
44+
DirectionsRoute nullRoute = null;
45+
ArgumentCaptor<OfflineError> offlineError = ArgumentCaptor.forClass(OfflineError.class);
46+
47+
theOfflineRouteRetrievalTask.onPostExecute(nullRoute);
48+
49+
verify(mockedCallback).onError(offlineError.capture());
50+
assertEquals("Error occurred fetching offline route: No suitable edges near location - Code: 171",
51+
offlineError.getValue().getMessage());
52+
}
53+
54+
@Test
55+
public void checksOnRouteFoundIsCalledIfRouteIsFetched() {
56+
Navigator mockedNavigator = mock(Navigator.class);
57+
OnOfflineRouteFoundCallback mockedCallback = mock(OnOfflineRouteFoundCallback.class);
58+
OfflineRouteRetrievalTask theOfflineRouteRetrievalTask = new OfflineRouteRetrievalTask(mockedNavigator,
59+
mockedCallback);
60+
DirectionsRoute aRoute = mock(DirectionsRoute.class);
61+
62+
theOfflineRouteRetrievalTask.onPostExecute(aRoute);
63+
64+
verify(mockedCallback).onRouteFound(eq(aRoute));
65+
}
66+
}

0 commit comments

Comments
 (0)