Search in sources :

Example 6 with NetworkStateHelper

use of com.microsoft.appcenter.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);
}
Also used : HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) Test(org.junit.Test)

Example 7 with NetworkStateHelper

use of com.microsoft.appcenter.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);
    verify(networkStateHelper).addListener(any(NetworkStateHelper.Listener.class));
    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();
    verify(networkStateHelper).removeListener(any(NetworkStateHelper.Listener.class));
    /* Reopen. */
    decorator.reopen();
    verify(httpClient).reopen();
    verify(networkStateHelper, times(2)).addListener(any(NetworkStateHelper.Listener.class));
}
Also used : HashMap(java.util.HashMap) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) Test(org.junit.Test)

Example 8 with NetworkStateHelper

use of com.microsoft.appcenter.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();
}
Also used : SocketException(java.net.SocketException) HashMap(java.util.HashMap) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) Test(org.junit.Test)

Example 9 with NetworkStateHelper

use of com.microsoft.appcenter.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();
}
Also used : HashMap(java.util.HashMap) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) Test(org.junit.Test)

Aggregations

NetworkStateHelper (com.microsoft.appcenter.utils.NetworkStateHelper)9 HashMap (java.util.HashMap)8 Test (org.junit.Test)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 Answer (org.mockito.stubbing.Answer)3 DefaultHttpClient (com.microsoft.appcenter.http.DefaultHttpClient)2 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 DEFAULT_API_URL (com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_API_URL)1 DEFAULT_INSTALL_URL (com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_INSTALL_URL)1 HttpClient (com.microsoft.appcenter.http.HttpClient)1 HttpClientRetryer (com.microsoft.appcenter.http.HttpClientRetryer)1 HttpException (com.microsoft.appcenter.http.HttpException)1 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1