use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method checkReleaseFailsParsing.
@Test
public void checkReleaseFailsParsing() throws Exception {
/* Mock we already have token. */
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
return mock(ServiceCall.class);
}
});
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
when(ReleaseDetails.parse(anyString())).thenThrow(new JSONException("mock"));
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify on failure we complete workflow. */
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* After that if we resume app nothing happens. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method disableBeforeCheckReleaseFails.
@Test
public void disableBeforeCheckReleaseFails() throws Exception {
/* Mock we already have token. */
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
final Semaphore beforeSemaphore = new Semaphore(0);
final Semaphore afterSemaphore = new Semaphore(0);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(final InvocationOnMock invocation) throws Throwable {
new Thread() {
@Override
public void run() {
beforeSemaphore.acquireUninterruptibly();
((ServiceCallback) invocation.getArguments()[4]).onCallFailed(new HttpException(403));
afterSemaphore.release();
}
}.start();
return mock(ServiceCall.class);
}
});
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Disable before it fails. */
Distribute.setEnabled(false);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verifyStatic(never());
PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
beforeSemaphore.release();
afterSemaphore.acquireUninterruptibly();
/* Verify complete workflow call ignored. i.e. no more call to delete the state. */
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* After that if we resume app nothing happens. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeCustomizationTest method mockForCustomizationTest.
private ReleaseDetails mockForCustomizationTest(boolean mandatory) throws Exception {
/* Mock http call. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
return mock(ServiceCall.class);
}
});
/* Mock data model. */
mockStatic(ReleaseDetails.class);
ReleaseDetails details = mock(ReleaseDetails.class);
when(details.getId()).thenReturn(1);
when(details.getVersion()).thenReturn(10);
when(details.getShortVersion()).thenReturn("2.3.4");
when(details.isMandatoryUpdate()).thenReturn(mandatory);
when(ReleaseDetails.parse(anyString())).thenReturn(details);
/* Mock update token. */
when(StorageHelper.PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
return details;
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeHttpTest method getCallTemplate.
private HttpClient.CallTemplate getCallTemplate(String appSecret, String apiToken) throws Exception {
/* Configure mock HTTP to get an instance of IngestionCallTemplate. */
Distribute.getInstance().onStarting(mAppCenterHandler);
Distribute.getInstance().onStarted(mContext, appSecret, mock(Channel.class));
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
mockStatic(NetworkStateHelper.class);
when(NetworkStateHelper.getSharedInstance(any(Context.class))).thenReturn(mock(NetworkStateHelper.class));
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
Distribute.getInstance().getLatestReleaseDetails("mockGroup", apiToken);
return callTemplate.get();
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeWarnUnknownSourcesTest method setUpDialog.
@Before
public void setUpDialog() throws Exception {
/* Mock we already have redirection parameters. */
when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
return mock(ServiceCall.class);
}
});
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(releaseDetails.isMandatoryUpdate()).thenReturn(mMandatoryUpdate);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mFirstActivity);
/* Mock second dialog. */
when(mDialogBuilder.create()).thenReturn(mUnknownSourcesDialog);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(mUnknownSourcesDialog.isShowing()).thenReturn(true);
return null;
}
}).when(mUnknownSourcesDialog).show();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(mUnknownSourcesDialog.isShowing()).thenReturn(false);
return null;
}
}).when(mUnknownSourcesDialog).hide();
/* Click on first dialog. */
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
when(mDialog.isShowing()).thenReturn(false);
/* Second should show. */
verify(mUnknownSourcesDialog).show();
}
Aggregations