use of com.microsoft.azure.mobile.utils.NetworkStateHelper in project mobile-center-sdk-android by Microsoft.
the class HttpClientNetworkStateHandlerTest method networkDownCancelBeforeUp.
@Test
public void networkDownCancelBeforeUp() 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) invocationOnMock.getArguments()[4]).onCallSucceeded("");
return call;
}
}).when(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
/* Simulate network down then becomes up. */
NetworkStateHelper networkStateHelper = mock(NetworkStateHelper.class);
when(networkStateHelper.isNetworkConnected()).thenReturn(false).thenReturn(true);
/* Test call and cancel right away. */
HttpClientNetworkStateHandler decorator = new HttpClientNetworkStateHandler(httpClient, networkStateHelper);
decorator.callAsync(url, METHOD_GET, headers, callTemplate, callback).cancel();
/* Network now up, verify no interaction with anything. */
decorator.onNetworkStateUpdated(true);
verifyNoMoreInteractions(httpClient);
verifyNoMoreInteractions(call);
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 success.
@Test
public void success() 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.onCallSucceeded("mockPayload");
serviceCallback.onCallSucceeded("duplicateCallbackPayloadToIgnore");
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).onCallSucceeded("mockPayload");
verify(callback, never()).onCallSucceeded("duplicateCallbackPayloadToIgnore");
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 cancelRunningCallByClosing.
@Test
public void cancelRunningCallByClosing() 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 ignore) {
}
}
};
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(httpClient).close();
/* Simulate network down then becomes up. */
NetworkStateHelper networkStateHelper = mock(NetworkStateHelper.class);
when(networkStateHelper.isNetworkConnected()).thenReturn(true);
/* Test call. */
HttpClientNetworkStateHandler decorator = new HttpClientNetworkStateHandler(httpClient, networkStateHelper);
decorator.callAsync(url, METHOD_GET, headers, callTemplate, callback);
/* Wait some time. */
Thread.sleep(100);
/* Cancel by closing. */
decorator.close();
/* Verify that the call was attempted then canceled. */
verify(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
verify(httpClient).close();
verify(call).cancel();
verifyNoMoreInteractions(callback);
}
use of com.microsoft.azure.mobile.utils.NetworkStateHelper in project mobile-center-sdk-android by Microsoft.
the class HttpClientNetworkStateHandlerTest method networkDownBecomesUp.
@Test
public void networkDownBecomesUp() 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) invocationOnMock.getArguments()[4]).onCallSucceeded("");
return call;
}
}).when(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
/* Simulate network down then becomes up. */
NetworkStateHelper networkStateHelper = mock(NetworkStateHelper.class);
when(networkStateHelper.isNetworkConnected()).thenReturn(false).thenReturn(true);
/* Test call. */
HttpClientNetworkStateHandler decorator = new HttpClientNetworkStateHandler(httpClient, networkStateHelper);
decorator.callAsync(url, METHOD_GET, headers, callTemplate, callback);
/* Network is down: no call to target API must be done. */
verify(httpClient, times(0)).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
verify(callback, times(0)).onCallSucceeded("");
/* Network now up: call must be done and succeed. */
decorator.onNetworkStateUpdated(true);
verify(httpClient).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
verify(callback).onCallSucceeded("");
/* 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 networkLossDuringCall.
@Test
public void networkLossDuringCall() 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 ignore) {
}
}
};
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 up then down then up again. */
NetworkStateHelper networkStateHelper = mock(NetworkStateHelper.class);
when(networkStateHelper.isNetworkConnected()).thenReturn(true).thenReturn(false).thenReturn(true);
/* Test call. */
HttpClientNetworkStateHandler decorator = new HttpClientNetworkStateHandler(httpClient, networkStateHelper);
decorator.callAsync(url, METHOD_GET, headers, callTemplate, callback);
/* Wait some time. */
Thread.sleep(100);
/* Lose network. */
decorator.onNetworkStateUpdated(false);
/* 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);
/* Then up again. */
decorator.onNetworkStateUpdated(true);
verify(httpClient, times(2)).callAsync(eq(url), eq(METHOD_GET), eq(headers), eq(callTemplate), any(ServiceCallback.class));
Thread.sleep(300);
verify(callback).onCallSucceeded("mockPayload");
verifyNoMoreInteractions(callback);
/* Close. */
decorator.close();
verify(httpClient).close();
}
Aggregations