diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java index 1b79eb973da3..b0e3582e8606 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java @@ -42,12 +42,12 @@ public Project(ResourceManager resourceManager, ProjectInfo projectInfo) { /** * Constructs a Project object that contains project information loaded from the server. * - * @return Project object containing the project's metadata + * @return Project object containing the project's metadata or {@code null} if not found * @throws ResourceManagerException upon failure */ public static Project load(ResourceManager resourceManager, String projectId) { ProjectInfo projectInfo = resourceManager.get(projectId); - return new Project(resourceManager, projectInfo); + return projectInfo != null ? new Project(resourceManager, projectInfo) : null; } /** @@ -67,7 +67,7 @@ public ResourceManager resourceManager() { /** * Returns a Project object with updated project information. * - * @return Project object containing the project's updated metadata + * @return Project object containing the project's updated metadata or {@code null} if not found * @throws ResourceManagerException upon failure */ public Project reload() { diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java index 65bb37dbccf9..5d765d20c313 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java @@ -21,6 +21,8 @@ import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import com.google.common.collect.ImmutableMap; @@ -78,6 +80,24 @@ public void testReload() { assertEquals(newInfo, newProject.info()); } + @Test + public void testLoadNull() { + expect(resourceManager.get(PROJECT_INFO.projectId())).andReturn(null); + replay(resourceManager); + assertNull(Project.load(resourceManager, PROJECT_INFO.projectId())); + } + + @Test + public void testReloadDeletedProject() { + expect(resourceManager.get(PROJECT_INFO.projectId())).andReturn(PROJECT_INFO); + expect(resourceManager.get(PROJECT_INFO.projectId())).andReturn(null); + replay(resourceManager); + Project loadedProject = Project.load(resourceManager, PROJECT_INFO.projectId()); + assertNotNull(loadedProject); + Project reloadedProject = loadedProject.reload(); + assertNull(reloadedProject); + } + @Test public void testInfo() { replay(resourceManager);