Throw REST error for duplicate in create project API

The create project API threw a NullPointerException because it had a
ProjectResource instead of a String name. Check for that condition, and throw a
proper ResourceConflictException instead.

Change-Id: I8286bdaab1f62c4d8f736cd806bf73a2fe793daa
This commit is contained in:
Dan Willemsen 2014-04-03 23:00:00 -07:00
parent 6b9159abe7
commit 7d09d00d00
2 changed files with 18 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.ProjectInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException;
import org.eclipse.jgit.api.errors.GitAPIException;
@ -50,6 +51,18 @@ public class ProjectIT extends AbstractDaemonTest {
.create(in);
}
@Test(expected = ResourceConflictException.class)
public void createProjectDuplicate() throws RestApiException {
ProjectInput in = new ProjectInput();
in.name = "baz";
gApi.projects()
.name("baz")
.create(in);
gApi.projects()
.name("baz")
.create(in);
}
@Test
public void createBranch() throws GitAPIException,
IOException, RestApiException {

View File

@ -96,6 +96,9 @@ public class ProjectApiImpl implements ProjectApi {
@Override
public ProjectApi create(ProjectInput in) throws RestApiException {
try {
if (name == null) {
throw new ResourceConflictException("Project already exists");
}
if (in.name != null && !name.equals(in.name)) {
throw new RestApiException("name must match input.name");
}
@ -103,8 +106,8 @@ public class ProjectApiImpl implements ProjectApi {
.apply(TopLevelResource.INSTANCE, in);
return projectApi.create(projects.parse(name));
} catch (BadRequestException | UnprocessableEntityException
| ResourceConflictException | ResourceNotFoundException
| ProjectCreationFailedException | IOException e) {
| ResourceNotFoundException | ProjectCreationFailedException
| IOException e) {
throw new RestApiException("Cannot create project", e);
}
}