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

Null pointer fix #423

Merged
merged 2 commits into from
Nov 21, 2019
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Mapbox welcomes participation and contributions from everyone.

## Mapbox Android Telemetry

## v4.6.1
- Fix crash `setBaseUrl()` on null `TelemetryClient` [#423](https://github.com/mapbox/mapbox-events-android/pull/423)

## v4.6.0
- Add public api to change base url for telemetry endpoint [#420](https://github.com/mapbox/mapbox-events-android/pull/420)
- Telemetry metrics [#397](https://github.com/mapbox/mapbox-events-android/pull/397)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,12 @@ public Thread newThread(Runnable runnable) {
}

@SuppressWarnings("WeakerAccess")
public void setBaseUrl(String eventsHost) {
if (isValidUrl(eventsHost)) {
public synchronized boolean setBaseUrl(String eventsHost) {
if (isValidUrl(eventsHost) && checkNetworkAndParameters()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to check for network here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TelemetryClient is created inside that check. It's a pipeline used by MapboxTelemetry class to init TelemetryClient

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkNetworkAndParameters() checks for both network connection and init as required, so if you need the network check then this is fine, if the intent is to check and init TelemetryClient then you could checkRequiredParameters(..)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we discussed it internally and decided to check network connection as well.

telemetryClient.setBaseUrl(eventsHost);
return true;
}
return false;
}

private static boolean isValidUrl(String eventsHost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,66 +403,80 @@ public void checkFlushIsCalled() {
}

@Test
public void checksSetBaseUrlWithValidHost() throws Exception {
public void checksSetBaseUrlWithValidHostAndWithConnection() throws Exception {
Context mockedContext = obtainNetworkConnectedMockedContext();
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST);
Callback mockedHttpCallback = mock(Callback.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedContext, mockedTelemetryClient,
mockedHttpCallback);
assertTrue(theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST));
verify(mockedTelemetryClient, times(1)).setBaseUrl(eq(DEFAULT_STAGING_EVENTS_HOST));
}

@Test
public void checksSetBaseUrlWithValidHostAndWithoutConnection() throws Exception {
Context mockedContext = obtainNetworkNotConnectedMockedContext();
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
Callback mockedHttpCallback = mock(Callback.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedContext, mockedTelemetryClient,
mockedHttpCallback);
assertFalse(theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST));
verify(mockedTelemetryClient, never()).setBaseUrl(eq(DEFAULT_STAGING_EVENTS_HOST));
}

@Test
public void checksSetBaseUrlWithNullHost() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl(null);
assertFalse(theMapboxTelemetry.setBaseUrl(null));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

@Test
public void checksSetBaseUrlWithEmptyHost() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl("");
assertFalse(theMapboxTelemetry.setBaseUrl(""));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

@Test
public void checksSetBaseUrlWithInvalidHostOne() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl("[email protected]");
assertFalse(theMapboxTelemetry.setBaseUrl("[email protected]"));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

@Test
public void checksSetBaseUrlWithInvalidHostTwo() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl("new host.com");
assertFalse(theMapboxTelemetry.setBaseUrl("new host.com"));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

@Test
public void checksSetBaseUrlWithInvalidHostThree() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl("host..com");
assertFalse(theMapboxTelemetry.setBaseUrl("host..com"));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

@Test
public void checksSetBaseUrlWithInvalidHostFour() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl("host.c");
assertFalse(theMapboxTelemetry.setBaseUrl("host.c"));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

@Test
public void checksSetBaseUrlWithInvalidHostFive() throws Exception {
TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class);
MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient);
theMapboxTelemetry.setBaseUrl("host.com.");
assertFalse(theMapboxTelemetry.setBaseUrl("host.com."));
verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class));
}

Expand Down