From 16b451d0d4733549105c9ec9ad7c90009719ce73 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 4 Jan 2016 09:18:30 -0800 Subject: [PATCH 1/2] Check for null in Project.load --- .../com/google/gcloud/resourcemanager/Project.java | 6 +++--- .../google/gcloud/resourcemanager/ProjectTest.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) 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..f137c0370d04 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,17 @@ public void testReload() { assertEquals(newInfo, newProject.info()); } + @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); From 2e152a341a9ca34fc3ef61b54d8c56f92d99ea7e Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 4 Jan 2016 10:48:42 -0800 Subject: [PATCH 2/2] add load null test --- .../com/google/gcloud/resourcemanager/ProjectTest.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 f137c0370d04..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 @@ -80,6 +80,13 @@ 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);