use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method setUrls.
@Test
public void setUrls() throws Exception {
/* Setup mock. */
Distribute.setInstallUrl("http://mock");
Distribute.setApiUrl("https://mock2");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
UUID requestId = UUID.randomUUID();
when(UUIDUtils.randomUUID()).thenReturn(requestId);
when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn(requestId.toString());
/* Start and resume: open browser. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
Distribute.getInstance().onActivityResumed(mActivity);
verifyStatic();
String url = "http://mock";
url += String.format(UPDATE_SETUP_PATH_FORMAT, "a");
url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
url += "&" + PARAMETER_REQUEST_ID + "=" + requestId.toString();
url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
/* Store token. */
Distribute.getInstance().storeUpdateToken("some token", requestId.toString());
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith("https://mock2");
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.
the class BrowserUtilsTest method onlySystemBrowserNoDefaultAsNull.
@Test
public void onlySystemBrowserNoDefaultAsNull() throws Exception {
/* Mock no browser. */
Activity activity = mock(Activity.class);
doThrow(new ActivityNotFoundException()).when(activity).startActivity(argThat(CHROME_MATCHER));
PackageManager packageManager = mock(PackageManager.class);
when(activity.getPackageManager()).thenReturn(packageManager);
when(packageManager.resolveActivity(any(Intent.class), eq(PackageManager.MATCH_DEFAULT_ONLY))).thenReturn(null);
{
ActivityInfo activityInfo = new ActivityInfo();
activityInfo.packageName = "system";
activityInfo.name = "browser";
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = activityInfo;
when(packageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(resolveInfo));
}
/* Open Chrome then abort. */
BrowserUtils.openBrowser(TEST_URL, activity);
InOrder order = inOrder(activity);
order.verify(activity).startActivity(argThat(CHROME_MATCHER));
order.verify(activity).startActivity(argThat(new ArgumentMatcher<Intent>() {
@Override
public boolean matches(Object o) {
Intent intent = (Intent) o;
return Intent.ACTION_VIEW.equals(intent.getAction()) && Uri.parse(TEST_URL).equals(intent.getData()) && intent.getComponent().getClassName().equals("browser");
}
}));
order.verifyNoMoreInteractions();
}
use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionSuccess.
@Test
public void disabledWhileHandlingIngestionSuccess() throws Exception {
/* Set up mocking. */
final Semaphore beforeCallSemaphore = new Semaphore(0);
final Semaphore afterCallSemaphore = new Semaphore(0);
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.countLogs(anyString())).thenReturn(1);
when(mockPersistence.getLogs(anyString(), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(mockPersistence.getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
@Override
public Object answer(final InvocationOnMock invocation) throws Throwable {
new Thread() {
@Override
public void run() {
beforeCallSemaphore.acquireUninterruptibly();
((ServiceCallback) invocation.getArguments()[3]).onCallSucceeded("");
afterCallSemaphore.release();
}
}.start();
return mock(ServiceCall.class);
}
});
/* Simulate enable module then disable. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
Channel.GroupListener listener = mock(Channel.GroupListener.class);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, listener);
channel.setEnabled(false);
channel.setEnabled(true);
/* Release call to mock ingestion. */
beforeCallSemaphore.release();
/* Wait for callback ingestion. */
afterCallSemaphore.acquireUninterruptibly();
/* Verify handling success was ignored. */
verify(listener, never()).onSuccess(any(Log.class));
verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {
@Override
public boolean matches(Object argument) {
return argument instanceof CancellationException;
}
}));
}
use of org.mockito.ArgumentMatcher in project pulsar by yahoo.
the class PulsarAdminToolTest method persistentTopics.
@Test
void persistentTopics() throws Exception {
PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
PersistentTopics mockTopics = mock(PersistentTopics.class);
when(admin.persistentTopics()).thenReturn(mockTopics);
CmdPersistentTopics topics = new CmdPersistentTopics(admin);
topics.run(split("delete persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).delete("persistent://myprop/clust/ns1/ds1");
topics.run(split("list myprop/clust/ns1"));
verify(mockTopics).getList("myprop/clust/ns1");
topics.run(split("subscriptions persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getSubscriptions("persistent://myprop/clust/ns1/ds1");
topics.run(split("unsubscribe persistent://myprop/clust/ns1/ds1 -s sub1"));
verify(mockTopics).deleteSubscription("persistent://myprop/clust/ns1/ds1", "sub1");
topics.run(split("stats persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getStats("persistent://myprop/clust/ns1/ds1");
topics.run(split("stats-internal persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getInternalStats("persistent://myprop/clust/ns1/ds1");
topics.run(split("partitioned-stats persistent://myprop/clust/ns1/ds1 --per-partition"));
verify(mockTopics).getPartitionedStats("persistent://myprop/clust/ns1/ds1", true);
topics.run(split("skip-all persistent://myprop/clust/ns1/ds1 -s sub1"));
verify(mockTopics).skipAllMessages("persistent://myprop/clust/ns1/ds1", "sub1");
topics.run(split("skip persistent://myprop/clust/ns1/ds1 -s sub1 -n 100"));
verify(mockTopics).skipMessages("persistent://myprop/clust/ns1/ds1", "sub1", 100);
topics.run(split("expire-messages persistent://myprop/clust/ns1/ds1 -s sub1 -t 100"));
verify(mockTopics).expireMessages("persistent://myprop/clust/ns1/ds1", "sub1", 100);
topics.run(split("expire-messages-all-subscriptions persistent://myprop/clust/ns1/ds1 -t 100"));
verify(mockTopics).expireMessagesForAllSubscriptions("persistent://myprop/clust/ns1/ds1", 100);
topics.run(split("create-partitioned-topic persistent://myprop/clust/ns1/ds1 --partitions 32"));
verify(mockTopics).createPartitionedTopic("persistent://myprop/clust/ns1/ds1", 32);
topics.run(split("get-partitioned-topic-metadata persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getPartitionedTopicMetadata("persistent://myprop/clust/ns1/ds1");
topics.run(split("delete-partitioned-topic persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).deletePartitionedTopic("persistent://myprop/clust/ns1/ds1");
topics.run(split("peek-messages persistent://myprop/clust/ns1/ds1 -s sub1 -n 3"));
verify(mockTopics).peekMessages("persistent://myprop/clust/ns1/ds1", "sub1", 3);
// range of +/- 1 second of the expected timestamp
class TimestampMatcher extends ArgumentMatcher<Long> {
@Override
public boolean matches(Object argument) {
long timestamp = (Long) argument;
long expectedTimestamp = System.currentTimeMillis() - (1 * 60 * 1000);
if (timestamp < (expectedTimestamp + 1000) && timestamp > (expectedTimestamp - 1000)) {
return true;
}
return false;
}
}
topics.run(split("reset-cursor persistent://myprop/clust/ns1/ds1 -s sub1 -t 1m"));
verify(mockTopics).resetCursor(Matchers.eq("persistent://myprop/clust/ns1/ds1"), Matchers.eq("sub1"), Matchers.longThat(new TimestampMatcher()));
}
use of org.mockito.ArgumentMatcher in project powermock by powermock.
the class PowerMockMatchersBinder method bindMatchers.
public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, final Invocation invocation) {
List<LocalizedMatcher> lastMatchers = argumentMatcherStorage.pullLocalizedMatchers();
validateMatchers(invocation, lastMatchers);
// In Mockito 2.0 LocalizedMatcher no more extend ArgumentMatcher, so new list should be created.
final List<ArgumentMatcher> argumentMatchers = extractArgumentMatchers(lastMatchers);
final InvocationMatcher invocationWithMatchers = new InvocationMatcher(invocation, argumentMatchers) {
@Override
public String toString() {
return invocation.toString();
}
};
return invocationWithMatchers;
}
Aggregations