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());
}
use of org.mockito.stubbing.Answer in project hadoop by apache.
the class TestProportionalCapacityPreemptionPolicy method mockLeafQueue.
@SuppressWarnings("rawtypes")
LeafQueue mockLeafQueue(ParentQueue p, Resource tot, int i, Resource[] abs, Resource[] used, Resource[] pending, Resource[] reserved, int[] apps, Resource[] gran) {
LeafQueue lq = mock(LeafQueue.class);
ResourceCalculator rc = mCS.getResourceCalculator();
List<ApplicationAttemptId> appAttemptIdList = new ArrayList<ApplicationAttemptId>();
when(lq.getTotalPendingResourcesConsideringUserLimit(isA(Resource.class), isA(String.class), eq(false))).thenReturn(pending[i]);
when(lq.getTotalPendingResourcesConsideringUserLimit(isA(Resource.class), isA(String.class), eq(true))).thenReturn(Resources.componentwiseMax(Resources.subtract(pending[i], reserved[i] == null ? Resources.none() : reserved[i]), Resources.none()));
// need to set pending resource in resource usage as well
ResourceUsage ru = new ResourceUsage();
ru.setPending(pending[i]);
ru.setUsed(used[i]);
ru.setReserved(reserved[i]);
when(lq.getQueueResourceUsage()).thenReturn(ru);
// consider moving where CapacityScheduler::comparator accessible
final NavigableSet<FiCaSchedulerApp> qApps = new TreeSet<FiCaSchedulerApp>(new Comparator<FiCaSchedulerApp>() {
@Override
public int compare(FiCaSchedulerApp a1, FiCaSchedulerApp a2) {
return a1.getApplicationAttemptId().compareTo(a2.getApplicationAttemptId());
}
});
// applications are added in global L->R order in queues
if (apps[i] != 0) {
Resource aUsed = Resources.divideAndCeil(rc, used[i], apps[i]);
Resource aPending = Resources.divideAndCeil(rc, pending[i], apps[i]);
Resource aReserve = Resources.divideAndCeil(rc, reserved[i], apps[i]);
for (int a = 0; a < apps[i]; ++a) {
FiCaSchedulerApp mockFiCaApp = mockApp(i, appAlloc, aUsed, aPending, aReserve, gran[i]);
qApps.add(mockFiCaApp);
++appAlloc;
appAttemptIdList.add(mockFiCaApp.getApplicationAttemptId());
}
when(mCS.getAppsInQueue("queue" + (char) ('A' + i - 1))).thenReturn(appAttemptIdList);
}
when(lq.getApplications()).thenReturn(qApps);
@SuppressWarnings("unchecked") OrderingPolicy<FiCaSchedulerApp> so = mock(OrderingPolicy.class);
when(so.getPreemptionIterator()).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
return qApps.descendingIterator();
}
});
when(lq.getOrderingPolicy()).thenReturn(so);
if (setAMResourcePercent != 0.0f) {
when(lq.getMaxAMResourcePerQueuePercent()).thenReturn(setAMResourcePercent);
}
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
when(lq.getReadLock()).thenReturn(lock.readLock());
when(lq.getPriority()).thenReturn(Priority.newInstance(0));
p.getChildQueues().add(lq);
return lq;
}
use of org.mockito.stubbing.Answer in project zeppelin by apache.
the class CorsFilterTest method InvalidCorsFilterTest.
@Test
@SuppressWarnings("rawtypes")
public void InvalidCorsFilterTest() throws IOException, ServletException {
CorsFilter filter = new CorsFilter();
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
FilterChain mockedFilterChain = mock(FilterChain.class);
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getHeader("Origin")).thenReturn("http://evillocalhost:8080");
when(mockRequest.getMethod()).thenReturn("Empty");
when(mockRequest.getServerName()).thenReturn("evillocalhost");
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
headers[count] = invocationOnMock.getArguments()[1].toString();
count++;
return null;
}
}).when(mockResponse).addHeader(anyString(), anyString());
filter.doFilter(mockRequest, mockResponse, mockedFilterChain);
Assert.assertTrue(headers[0].equals(""));
}
use of org.mockito.stubbing.Answer in project zookeeper by apache.
the class LearnerHandlerTest method setUp.
@Before
public void setUp() throws Exception {
// Intercept when startForwarding is called
leader = mock(Leader.class);
when(leader.startForwarding(Matchers.any(LearnerHandler.class), Matchers.anyLong())).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
currentZxid = (Long) invocation.getArguments()[1];
return 0;
}
});
sock = mock(Socket.class);
db = new MockZKDatabase(null);
learnerHandler = new MockLearnerHandler(sock, leader);
}
use of org.mockito.stubbing.Answer in project hbase by apache.
the class TestTableInputFormat method createIOEScannerTable.
/**
* Create a table that IOE's on first scanner next call
*
* @throws IOException
*/
static Table createIOEScannerTable(byte[] name, final int failCnt) throws IOException {
// build up a mock scanner stuff to fail the first time
Answer<ResultScanner> a = new Answer<ResultScanner>() {
int cnt = 0;
@Override
public ResultScanner answer(InvocationOnMock invocation) throws Throwable {
// first invocation return the busted mock scanner
if (cnt++ < failCnt) {
// create mock ResultScanner that always fails.
Scan scan = mock(Scan.class);
// avoid npe
doReturn("bogus".getBytes()).when(scan).getStartRow();
ResultScanner scanner = mock(ResultScanner.class);
// simulate TimeoutException / IOException
doThrow(new IOException("Injected exception")).when(scanner).next();
return scanner;
}
// otherwise return the real scanner.
return (ResultScanner) invocation.callRealMethod();
}
};
Table htable = spy(createTable(name));
doAnswer(a).when(htable).getScanner((Scan) anyObject());
return htable;
}
Aggregations