Search in sources :

Example 91 with ArgumentMatcher

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()));
}
Also used : PulsarAdmin(com.yahoo.pulsar.client.admin.PulsarAdmin) ArgumentMatcher(org.mockito.ArgumentMatcher) PersistentTopics(com.yahoo.pulsar.client.admin.PersistentTopics) Test(org.testng.annotations.Test)

Example 92 with ArgumentMatcher

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;
}
Also used : LocalizedMatcher(org.mockito.internal.matchers.LocalizedMatcher) ArgumentMatcher(org.mockito.ArgumentMatcher) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher)

Example 93 with ArgumentMatcher

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;
        }
    }));
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) Log(com.microsoft.azure.mobile.ingestion.models.Log) Semaphore(java.util.concurrent.Semaphore) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) CancellationException(com.microsoft.azure.mobile.CancellationException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatcher(org.mockito.ArgumentMatcher) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Example 94 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method queuePendingCrashesAlwaysSend.

@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()));
    verify(mockChannel, times(errorAttachmentLogList.size())).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 95 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method trackException.

@Test
public void trackException() {
    /* Track exception test. */
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    Crashes.trackException(EXCEPTION);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof ManagedErrorLog && EXCEPTION.getMessage().equals(((ManagedErrorLog) item).getException().getMessage());
        }
    }), eq(crashes.getGroupName()));
    ManagedErrorLog mockLog = mock(ManagedErrorLog.class);
    when(mockLog.getFatal()).thenReturn(false);
    CrashesListener mockListener = mock(CrashesListener.class);
    crashes.setInstanceListener(mockListener);
    /* Crashes callback test for trackException. */
    crashes.getChannelListener().onBeforeSending(mockLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(mockLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(mockLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
    ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
    crashes.getChannelListener().onBeforeSending(attachmentLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(attachmentLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ArgumentMatcher (org.mockito.ArgumentMatcher)142 Test (org.junit.Test)116 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 Context (android.content.Context)26 Matchers.anyString (org.mockito.Matchers.anyString)26 HashMap (java.util.HashMap)25 Appender (ch.qos.logback.core.Appender)23 Logger (org.slf4j.Logger)23 ArrayList (java.util.ArrayList)19 UUID (java.util.UUID)19 Intent (android.content.Intent)18 File (java.io.File)15 ResolveInfo (android.content.pm.ResolveInfo)14 PackageManager (android.content.pm.PackageManager)13 Channel (com.microsoft.azure.mobile.channel.Channel)13 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 IOException (java.io.IOException)12 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)11 Activity (android.app.Activity)10