RestApiModule: Support binding a RestView for resource creation
Instead of handling resource creation by implementing AcceptsCreate in the RestCollection, add a new RestCreateView that can be bound via RestApiModule. This improves code readability since by reading the Module class we can now directly see which REST collections support resource creation. In addition we no longer need factories to create the REST view that creates the resource. This allows us at Google to bind a different REST view for the resource creation internally while we still use the upstream REST collection for parsing and listing. Without this change we would need to subclass the upstream RestCollection and override the create method which is error-prone. This change adds REST tests for creating a resource on a root collection (com.google.gerrit.acceptance.rest.account.CreateAccountIT#createAccountRestApi) and for creating a resource on a child collection (com.google.gerrit.acceptance.rest.account.CreateBranchIT#createBranchRestApi), however it doesn't test resource creation via REST for all possible resource types. There are more things that we likely also want to replace by bindings (AcceptsPost, AcceptsDelete, listing members). This should be done by follow-up changes. Change-Id: I5cd61f77aad2a59a02333b5f68b86bda6c353879 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// Copyright (C) 2018 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.acceptance.rest.account;
|
||||
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.RestResponse;
|
||||
import com.google.gerrit.extensions.api.accounts.AccountInput;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CreateAccountIT extends AbstractDaemonTest {
|
||||
@Test
|
||||
public void createAccountRestApi() throws Exception {
|
||||
AccountInput input = new AccountInput();
|
||||
input.username = "foo";
|
||||
assertThat(accountCache.getByUsername(input.username)).isEmpty();
|
||||
RestResponse r = adminRestSession.put("/accounts/" + input.username, input);
|
||||
r.assertCreated();
|
||||
assertThat(accountCache.getByUsername(input.username)).isPresent();
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import com.google.gerrit.extensions.api.changes.ReviewInput;
|
||||
import com.google.gerrit.extensions.common.ChangeInput;
|
||||
import com.google.gerrit.extensions.common.GroupInfo;
|
||||
import com.google.gerrit.extensions.common.SuggestedReviewerInfo;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.extensions.restapi.TopLevelResource;
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
@@ -42,7 +43,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SuggestReviewersIT extends AbstractDaemonTest {
|
||||
@Inject private CreateGroup.Factory createGroupFactory;
|
||||
@Inject private CreateGroup createGroup;
|
||||
|
||||
private InternalGroup group1;
|
||||
private InternalGroup group2;
|
||||
@@ -490,7 +491,8 @@ public class SuggestReviewersIT extends AbstractDaemonTest {
|
||||
}
|
||||
|
||||
private InternalGroup newGroup(String name) throws Exception {
|
||||
GroupInfo group = createGroupFactory.create(name(name)).apply(TopLevelResource.INSTANCE, null);
|
||||
GroupInfo group =
|
||||
createGroup.apply(TopLevelResource.INSTANCE, IdString.fromDecoded(name(name)), null);
|
||||
return group(new AccountGroup.UUID(group.id));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
package com.google.gerrit.acceptance.rest.project;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static com.google.gerrit.reviewdb.client.RefNames.REFS_HEADS;
|
||||
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
|
||||
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
|
||||
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.GerritConfig;
|
||||
import com.google.gerrit.acceptance.NoHttpd;
|
||||
import com.google.gerrit.acceptance.RestResponse;
|
||||
import com.google.gerrit.common.data.Permission;
|
||||
import com.google.gerrit.extensions.api.projects.BranchApi;
|
||||
import com.google.gerrit.extensions.api.projects.BranchInfo;
|
||||
@@ -35,7 +37,6 @@ import com.google.gerrit.reviewdb.client.RefNames;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@NoHttpd
|
||||
public class CreateBranchIT extends AbstractDaemonTest {
|
||||
private Branch.NameKey testBranch;
|
||||
|
||||
@@ -44,6 +45,19 @@ public class CreateBranchIT extends AbstractDaemonTest {
|
||||
testBranch = new Branch.NameKey(project, "test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createBranchRestApi() throws Exception {
|
||||
BranchInput input = new BranchInput();
|
||||
input.ref = "foo";
|
||||
assertThat(gApi.projects().name(project.get()).branches().get().stream().map(i -> i.ref))
|
||||
.doesNotContain(REFS_HEADS + input.ref);
|
||||
RestResponse r =
|
||||
adminRestSession.put("/projects/" + project.get() + "/branches/" + input.ref, input);
|
||||
r.assertCreated();
|
||||
assertThat(gApi.projects().name(project.get()).branches().get().stream().map(i -> i.ref))
|
||||
.contains(REFS_HEADS + input.ref);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createBranch_Forbidden() throws Exception {
|
||||
setApiUser(user);
|
||||
|
||||
Reference in New Issue
Block a user