/** * Mocks the {@link ListProjectsResponse} returned by the {@link #mockCloudResourceManager}. * * @param projects the list of {@link Project Projects} to return as part of the {@link * ListProjectsResponse} */ private void mockListProjectsResponse(List<Project> projects) { when(mockCloudResourceManager.projects()).thenReturn(mockProjects); try { when(mockProjects.list()).thenReturn(mockList); when(mockList.setPageSize(any())).thenReturn(mockList); when(mockList.setPageToken(any())).thenReturn(mockList); ListProjectsResponse response = new ListProjectsResponse(); response.setProjects(projects); response.setNextPageToken(""); when(mockList.execute()).thenReturn(response); } catch (IOException e) { // Should not happen when setting up these mocks. throw new AssertionError(e); } }
/** * Mocks {@link ListProjectsResponse} to contain two pages, one test project per each, using the * specified token. * * @param token token to identify second page. * @return Argument captor to check which tokens are requested. */ private ArgumentCaptor<String> mockTwoPageProjectsRespose(String token) throws IOException { mockListProjectsResponse(null); // emulate page with size 1, one token returned, then completed. ListProjectsResponse firstResponse = new ListProjectsResponse(); firstResponse.setProjects(Collections.singletonList(testProject1)); firstResponse.setNextPageToken(token); ListProjectsResponse secondResponse = new ListProjectsResponse(); secondResponse.setProjects(Collections.singletonList(testProject2)); secondResponse.setNextPageToken(""); ArgumentCaptor<String> pageTokenCaptor = ArgumentCaptor.forClass(String.class); when(mockList.setPageToken(pageTokenCaptor.capture())).thenReturn(mockList); when(mockList.execute()).thenReturn(firstResponse, secondResponse); return pageTokenCaptor; } }