use of com.google.api.services.cloudresourcemanager.model.ListProjectsResponse in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectLoader method loadUserProjects.
private List<Project> loadUserProjects(CredentialedUser user) throws IOException {
CloudResourceManager cloudResourceManagerClient = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential());
final List<Project> result = new ArrayList<>();
ListProjectsResponse response = cloudResourceManagerClient.projects().list().setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
if (response != null && response.getProjects() != null) {
// Create a sorted set to sort the projects list by project name.
Set<Project> allProjects = new TreeSet<>(Comparator.comparing(project -> project.getName().toLowerCase()));
allProjects.addAll(response.getProjects());
while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
response = cloudResourceManagerClient.projects().list().setPageToken(response.getNextPageToken()).setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
allProjects.addAll(response.getProjects());
}
allProjects.stream().filter((project) -> !PROJECT_DELETE_REQUESTED.equals(project.getLifecycleState())).filter((project) -> !Strings.isNullOrEmpty(project.getProjectId())).forEach(result::add);
}
return result;
}
use of com.google.api.services.cloudresourcemanager.model.ListProjectsResponse in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectLoaderTest method mockListProjectsResponse.
/**
* 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);
}
}
use of com.google.api.services.cloudresourcemanager.model.ListProjectsResponse in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectLoaderTest method mockTwoPageProjectsRespose.
/**
* 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;
}
Aggregations