use of com.microsoft.azure.mobile.utils.NetworkStateHelper in project mobile-center-sdk-android by Microsoft.
the class HttpClientNetworkStateHandlerTest method cancelRunningCall.
@Test
public void cancelRunningCall() throws InterruptedException, IOException {
/* Configure mock wrapped API. */
String url = "http://mock/call";
Map<String, String> headers = new HashMap<>();
final HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class);
final ServiceCallback callback = mock(ServiceCallback.class);
final ServiceCall call = mock(ServiceCall.class);
HttpClient httpClient = mock(HttpClient.class);
final AtomicReference<Thread> threadRef = new AtomicReference<>();
doAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(final InvocationOnMock invocationOnMock) throws Throwable {
Thread thread = new Thread() {
@Override
public void run() {
try {
sleep(200);
((ServiceCallback) invocationOnMock.getArguments()[4]).onCallSucceeded("mockPayload");
} catch (InterruptedException ignored) {
}
}
};
thread.start();
threadRef.set(thread);
return call;
}
}).when(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
threadRef.get().interrupt();
return null;
}
}).when(call).cancel();
/* Simulate network down then becomes up. */
NetworkStateHelper networkStateHelper = mock(NetworkStateHelper.class);
when(networkStateHelper.isNetworkConnected()).thenReturn(true);
/* Test call. */
HttpClientNetworkStateHandler decorator = new HttpClientNetworkStateHandler(httpClient, networkStateHelper);
ServiceCall decoratorCall = decorator.callAsync(url, METHOD_GET, headers, callTemplate, callback);
/* Wait some time. */
Thread.sleep(100);
/* Cancel. */
decoratorCall.cancel();
/* Verify that the call was attempted then canceled. */
verify(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
verify(call).cancel();
verifyNoMoreInteractions(callback);
/* Close. */
decorator.close();
verify(httpClient).close();
}
use of com.microsoft.azure.mobile.utils.NetworkStateHelper in project mobile-center-sdk-android by Microsoft.
the class HttpClientNetworkStateHandlerTest method failure.
@Test
public void failure() throws IOException {
/* Configure mock wrapped API. */
String url = "http://mock/call";
Map<String, String> headers = new HashMap<>();
final HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class);
final ServiceCallback callback = mock(ServiceCallback.class);
final ServiceCall call = mock(ServiceCall.class);
HttpClient httpClient = mock(HttpClient.class);
doAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocationOnMock) throws Throwable {
ServiceCallback serviceCallback = (ServiceCallback) invocationOnMock.getArguments()[4];
serviceCallback.onCallFailed(new HttpException(503));
serviceCallback.onCallFailed(new SocketException());
return call;
}
}).when(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
/* Simulate network is initially up. */
NetworkStateHelper networkStateHelper = mock(NetworkStateHelper.class);
when(networkStateHelper.isNetworkConnected()).thenReturn(true);
/* Test call. */
HttpClient decorator = new HttpClientNetworkStateHandler(httpClient, networkStateHelper);
decorator.callAsync(url, METHOD_GET, headers, callTemplate, callback);
verify(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
verify(callback).onCallFailed(new HttpException(503));
verifyNoMoreInteractions(callback);
/* Close. */
decorator.close();
verify(httpClient).close();
}
use of com.microsoft.azure.mobile.utils.NetworkStateHelper in project mobile-center-sdk-android by Microsoft.
the class Distribute method getLatestReleaseDetails.
/**
* Get latest release details from server.
*
* @param updateToken token to secure API call.
*/
@VisibleForTesting
synchronized void getLatestReleaseDetails(@NonNull String updateToken) {
MobileCenterLog.debug(LOG_TAG, "Get latest release details...");
HttpClientRetryer retryer = new HttpClientRetryer(new DefaultHttpClient());
NetworkStateHelper networkStateHelper = NetworkStateHelper.getSharedInstance(mContext);
HttpClient httpClient = new HttpClientNetworkStateHandler(retryer, networkStateHelper);
String url = mApiUrl + String.format(GET_LATEST_RELEASE_PATH_FORMAT, mAppSecret, computeReleaseHash(mPackageInfo));
Map<String, String> headers = new HashMap<>();
headers.put(HEADER_API_TOKEN, updateToken);
final Object releaseCallId = mCheckReleaseCallId = new Object();
mCheckReleaseApiCall = httpClient.callAsync(url, METHOD_GET, headers, new HttpClient.CallTemplate() {
@Override
public String buildRequestBody() throws JSONException {
/* Only GET is used by Distribute service. This method is never getting called. */
return null;
}
@Override
public void onBeforeCalling(URL url, Map<String, String> headers) {
if (MobileCenterLog.getLogLevel() <= VERBOSE) {
/* Log url. */
String urlString = url.toString().replaceAll(mAppSecret, HttpUtils.hideSecret(mAppSecret));
MobileCenterLog.verbose(LOG_TAG, "Calling " + urlString + "...");
/* Log headers. */
Map<String, String> logHeaders = new HashMap<>(headers);
String apiToken = logHeaders.get(HEADER_API_TOKEN);
if (apiToken != null) {
logHeaders.put(HEADER_API_TOKEN, HttpUtils.hideSecret(apiToken));
}
MobileCenterLog.verbose(LOG_TAG, "Headers: " + logHeaders);
}
}
}, new ServiceCallback() {
@Override
public void onCallSucceeded(final String payload) {
/* onPostExecute is not always called on UI thread due to an old Android bug. */
HandlerUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
handleApiCallSuccess(releaseCallId, payload, ReleaseDetails.parse(payload));
} catch (JSONException e) {
onCallFailed(e);
}
}
});
}
@Override
public void onCallFailed(Exception e) {
handleApiCallFailure(releaseCallId, e);
}
});
}
Aggregations