RenamePreconditions: check if project state is not null

Avoid NullPointerException when performing ssh rename-project on a non
existing project. The cause of null pointer is ProjectCache.get() method
which returns null if no such project exists.

Change-Id: Ifea14a48bf61e14306bb09bfbe387d7a8f2b5325
diff --git a/src/main/java/com/googlesource/gerrit/plugins/renameproject/conditions/RenamePreconditions.java b/src/main/java/com/googlesource/gerrit/plugins/renameproject/conditions/RenamePreconditions.java
index 9efa51d..e92a4c1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/renameproject/conditions/RenamePreconditions.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/renameproject/conditions/RenamePreconditions.java
@@ -75,6 +75,9 @@
 
   public void assertCanRename(ProjectResource oldProjectRsrc, Project.NameKey newProjectKey)
       throws CannotRenameProjectException {
+    if (oldProjectRsrc.getProjectState() == null) {
+      throw new CannotRenameProjectException("Project does not exist");
+    }
     Project.NameKey oldProjectKey = oldProjectRsrc.getNameKey();
     assertNewRepoNotExists(newProjectKey);
     assertIsNotDefaultProject(oldProjectKey);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/renameproject/RenameIT.java b/src/test/java/com/googlesource/gerrit/plugins/renameproject/RenameIT.java
index c0ae8f0..6e23206 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/renameproject/RenameIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/renameproject/RenameIT.java
@@ -42,6 +42,7 @@
 
   private static final String PLUGIN_NAME = "rename-project";
   private static final String NEW_PROJECT_NAME = "newProject";
+  private static final String NON_EXISTING_NAME = "nonExistingProject";
   private static final String CACHE_NAME = "changeid_project";
 
   @Inject
@@ -94,6 +95,16 @@
 
   @Test
   @UseLocalDisk
+  public void testRenameNonExistingProjectFail() throws Exception {
+    createChange();
+    adminSshSession.exec(PLUGIN_NAME + " " + NON_EXISTING_NAME + " " + project.get());
+
+    assertThat(adminSshSession.getError()).contains("Project does not exist");
+    adminSshSession.assertFailure();
+  }
+
+  @Test
+  @UseLocalDisk
   public void testRenameSubscribedFail() throws Exception {
     NameKey superProject = createProject("super-project");
     TestRepository<?> superRepo = cloneProject(superProject);