Search in sources :

Example 21 with Answer

use of org.mockito.stubbing.Answer in project che by eclipse.

the class GithubImporterPagePresenterTest method onLoadRepoClickedWhenGetUserReposIsSuccessful.

@Test
public void onLoadRepoClickedWhenGetUserReposIsSuccessful() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onSuccessRequest(jsArrayMixed);
            return null;
        }
    }).when(presenter).doRequest(any(Promise.class), any(Promise.class), any(Promise.class));
    when(view.getAccountName()).thenReturn("AccountName");
    presenter.onLoadRepoClicked();
    verify(gitHubClientService).getRepositoriesList();
    verify(gitHubClientService).getUserInfo();
    verify(gitHubClientService).getOrganizations();
    verify(view).setLoaderVisibility(eq(true));
    verify(view).setInputsEnableState(eq(false));
    verify(view).setLoaderVisibility(eq(false));
    verify(view).setInputsEnableState(eq(true));
    verify(view).setAccountNames(Matchers.<Set>anyObject());
    verify(view, times(2)).showGithubPanel();
    verify(view).setRepositories(Matchers.<List<ProjectData>>anyObject());
    verify(view).reset();
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Promise(org.eclipse.che.api.promises.client.Promise) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ProjectData(org.eclipse.che.plugin.github.ide.load.ProjectData) Test(org.junit.Test)

Example 22 with Answer

use of org.mockito.stubbing.Answer in project che by eclipse.

the class GithubImporterPagePresenterTest method onLoadRepoClickedWhenGetUserReposIsFailed.

@Test
public void onLoadRepoClickedWhenGetUserReposIsFailed() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onFailRequest(promiseError);
            return null;
        }
    }).when(presenter).doRequest(any(Promise.class), any(Promise.class), any(Promise.class));
    presenter.onLoadRepoClicked();
    verify(gitHubClientService).getRepositoriesList();
    verify(gitHubClientService).getUserInfo();
    verify(gitHubClientService).getOrganizations();
    verify(view).setLoaderVisibility(eq(true));
    verify(view).setInputsEnableState(eq(false));
    verify(view).setLoaderVisibility(eq(false));
    verify(view).setInputsEnableState(eq(true));
    verify(view, never()).setAccountNames((Set<String>) anyObject());
    verify(view, never()).showGithubPanel();
    verify(view, never()).setRepositories(Matchers.<List<ProjectData>>anyObject());
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Promise(org.eclipse.che.api.promises.client.Promise) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) ProjectData(org.eclipse.che.plugin.github.ide.load.ProjectData) Test(org.junit.Test)

Example 23 with Answer

use of org.mockito.stubbing.Answer in project che by eclipse.

the class GithubImporterPagePresenterTest method onLoadRepoClickedWhenAuthorizeIsSuccessful.

@Test
public void onLoadRepoClickedWhenAuthorizeIsSuccessful() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onFailRequest(promiseError);
            return null;
        }
    }).doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onSuccessRequest(jsArrayMixed);
            return null;
        }
    }).when(presenter).doRequest(any(Promise.class), any(Promise.class), any(Promise.class));
    final Throwable exception = mock(UnauthorizedException.class);
    String userId = "userId";
    CurrentUser user = mock(CurrentUser.class);
    ProfileDto profile = mock(ProfileDto.class);
    doReturn(exception).when(promiseError).getCause();
    when(appContext.getCurrentUser()).thenReturn(user);
    when(user.getProfile()).thenReturn(profile);
    when(profile.getUserId()).thenReturn(userId);
    presenter.onLoadRepoClicked();
    verify(gitHubClientService).getRepositoriesList();
    verify(gitHubClientService).getUserInfo();
    verify(gitHubClientService).getOrganizations();
    verify(gitHubAuthenticator).authenticate(anyString(), asyncCallbackCaptor.capture());
    AsyncCallback<OAuthStatus> asyncCallback = asyncCallbackCaptor.getValue();
    asyncCallback.onSuccess(null);
    verify(view, times(3)).setLoaderVisibility(eq(true));
    verify(view, times(3)).setInputsEnableState(eq(false));
    verify(view, times(3)).setInputsEnableState(eq(true));
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Promise(org.eclipse.che.api.promises.client.Promise) CurrentUser(org.eclipse.che.ide.api.app.CurrentUser) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) Test(org.junit.Test)

Example 24 with Answer

use of org.mockito.stubbing.Answer in project che by eclipse.

the class GithubImporterPagePresenterTest method onLoadRepoClickedWhenAuthorizeIsFailed.

@Test
public void onLoadRepoClickedWhenAuthorizeIsFailed() throws Exception {
    String userId = "userId";
    CurrentUser user = mock(CurrentUser.class);
    ProfileDto profile = mock(ProfileDto.class);
    when(appContext.getCurrentUser()).thenReturn(user);
    when(user.getProfile()).thenReturn(profile);
    when(profile.getUserId()).thenReturn(userId);
    final Throwable exception = mock(UnauthorizedException.class);
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            presenter.onFailRequest(promiseError);
            return null;
        }
    }).when(presenter).doRequest(any(Promise.class), any(Promise.class), any(Promise.class));
    doReturn(exception).when(promiseError).getCause();
    presenter.onLoadRepoClicked();
    verify(gitHubClientService).getRepositoriesList();
    verify(gitHubClientService).getUserInfo();
    verify(gitHubClientService).getOrganizations();
    verify(gitHubAuthenticator).authenticate(anyString(), asyncCallbackCaptor.capture());
    AsyncCallback<OAuthStatus> asyncCallback = asyncCallbackCaptor.getValue();
    asyncCallback.onFailure(exception);
    verify(view, times(2)).setLoaderVisibility(eq(true));
    verify(view, times(2)).setInputsEnableState(eq(false));
    verify(view, times(2)).setInputsEnableState(eq(true));
    verify(view, never()).setAccountNames((Set<String>) anyObject());
    verify(view, never()).showGithubPanel();
    verify(view, never()).setRepositories(Matchers.<List<ProjectData>>anyObject());
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Promise(org.eclipse.che.api.promises.client.Promise) CurrentUser(org.eclipse.che.ide.api.app.CurrentUser) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) OAuthStatus(org.eclipse.che.security.oauth.OAuthStatus) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) ProjectData(org.eclipse.che.plugin.github.ide.load.ProjectData) Test(org.junit.Test)

Example 25 with Answer

use of org.mockito.stubbing.Answer in project opentsdb by OpenTSDB.

the class TestBatchedDataPoints method before.

@SuppressWarnings("unchecked")
@Before
public void before() throws Exception {
    config = new Config(false);
    tsdb = new TSDB(client, config);
    // replace the "real" field objects with mocks
    Field met = tsdb.getClass().getDeclaredField("metrics");
    met.setAccessible(true);
    met.set(tsdb, metrics);
    Field tagk = tsdb.getClass().getDeclaredField("tag_names");
    tagk.setAccessible(true);
    tagk.set(tsdb, tag_names);
    Field tagv = tsdb.getClass().getDeclaredField("tag_values");
    tagv.setAccessible(true);
    tagv.set(tsdb, tag_values);
    when(metrics.width()).thenReturn((short) 3);
    when(tag_names.width()).thenReturn((short) 3);
    when(tag_values.width()).thenReturn((short) 3);
    when(metrics.getId("foo")).thenReturn(new byte[] { 0, 0, 1 });
    when(metrics.getNameAsync(new byte[] { 0, 0, 1 })).thenReturn(Deferred.fromResult("foo"));
    PowerMockito.mockStatic(IncomingDataPoints.class);
    final byte[] row = new byte[] { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 };
    PowerMockito.doAnswer(new Answer<byte[]>() {

        public byte[] answer(final InvocationOnMock unused) throws Exception {
            return row;
        }
    }).when(IncomingDataPoints.class, "rowKeyTemplate", (TSDB) any(), anyString(), (Map<String, String>) any());
    Map<String, String> tags = new HashMap<String, String>();
    tags.put("host", "web01");
    bdp = new BatchedDataPoints(tsdb, "foo", tags);
}
Also used : Field(java.lang.reflect.Field) Answer(org.mockito.stubbing.Answer) HashMap(java.util.HashMap) Config(net.opentsdb.utils.Config) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) Before(org.junit.Before)

Aggregations

Answer (org.mockito.stubbing.Answer)262 InvocationOnMock (org.mockito.invocation.InvocationOnMock)247 Test (org.junit.Test)148 Mockito.doAnswer (org.mockito.Mockito.doAnswer)99 Before (org.junit.Before)36 Matchers.anyString (org.mockito.Matchers.anyString)32 HashMap (java.util.HashMap)31 ArrayList (java.util.ArrayList)30 IOException (java.io.IOException)20 HashSet (java.util.HashSet)16 File (java.io.File)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 Map (java.util.Map)13 List (java.util.List)12 Test (org.testng.annotations.Test)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Configuration (org.apache.hadoop.conf.Configuration)11 RequestFinishedListener (com.android.volley.RequestQueue.RequestFinishedListener)9 MockRequest (com.android.volley.mock.MockRequest)9