Merge changes from topic "dashboard-api"

* changes:
  DashboardIT: Test that replacing the default dashboard works
  DashboardIT: Handle case where default dashboard ref already exists
  DashboardIT: Move setup of permission into @Before annotated method
  DashboardApi: Add method to set a dashboard as the default dashboard
This commit is contained in:
David Pursehouse
2017-10-01 14:44:16 +00:00
committed by Gerrit Code Review
4 changed files with 64 additions and 3 deletions

View File

@@ -23,16 +23,23 @@ import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.server.project.DashboardsCollection;
import java.util.List;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;
@NoHttpd
public class DashboardIT extends AbstractDaemonTest {
@Before
public void setup() throws Exception {
allow("refs/meta/dashboards/*", Permission.CREATE, REGISTERED_USERS);
}
@Test
public void defaultDashboardDoesNotExist() throws Exception {
exception.expect(ResourceNotFoundException.class);
@@ -58,6 +65,31 @@ public class DashboardIT extends AbstractDaemonTest {
assertThat(dashboards()).hasSize(1);
}
@Test
public void setDefaultDashboard() throws Exception {
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
assertThat(info.isDefault).isNull();
gApi.projects().name(project.get()).dashboard(info.id).setDefault();
assertThat(gApi.projects().name(project.get()).dashboard(info.id).get().isDefault).isTrue();
assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(info.id);
}
@Test
public void replaceDefaultDashboard() throws Exception {
DashboardInfo d1 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test1");
DashboardInfo d2 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test2");
assertThat(d1.isDefault).isNull();
assertThat(d2.isDefault).isNull();
gApi.projects().name(project.get()).dashboard(d1.id).setDefault();
assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isTrue();
assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isNull();
assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d1.id);
gApi.projects().name(project.get()).dashboard(d2.id).setDefault();
assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d2.id);
assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isNull();
assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isTrue();
}
@Test
public void cannotGetDashboardWithInheritedForNonDefault() throws Exception {
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
@@ -73,8 +105,14 @@ public class DashboardIT extends AbstractDaemonTest {
private DashboardInfo createDashboard(String ref, String path) throws Exception {
DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path);
String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref);
allow("refs/meta/dashboards/*", Permission.CREATE, REGISTERED_USERS);
try {
gApi.projects().name(project.get()).branch(canonicalRef).create(new BranchInput());
} catch (ResourceConflictException e) {
// The branch already exists if this method has already been called once.
if (!e.getMessage().contains("already exists")) {
throw e;
}
}
try (Repository r = repoManager.openRepository(project)) {
TestRepository<Repository>.CommitBuilder cb =
new TestRepository<>(r).branch(canonicalRef).commit();

View File

@@ -23,6 +23,8 @@ public interface DashboardApi {
DashboardInfo get(boolean inherited) throws RestApiException;
void setDefault() throws RestApiException;
/**
* A default implementation which allows source compatibility when adding new methods to the
* interface.
@@ -37,5 +39,10 @@ public interface DashboardApi {
public DashboardInfo get(boolean inherited) throws RestApiException {
throw new NotImplementedException();
}
@Override
public void setDefault() throws RestApiException {
throw new NotImplementedException();
}
}
}

View File

@@ -18,6 +18,7 @@ import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
import com.google.gerrit.extensions.api.projects.DashboardApi;
import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.common.SetDashboardInput;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
@@ -26,6 +27,7 @@ import com.google.gerrit.server.project.DashboardResource;
import com.google.gerrit.server.project.DashboardsCollection;
import com.google.gerrit.server.project.GetDashboard;
import com.google.gerrit.server.project.ProjectResource;
import com.google.gerrit.server.project.SetDashboard;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
@@ -39,6 +41,7 @@ public class DashboardApiImpl implements DashboardApi {
private final DashboardsCollection dashboards;
private final Provider<GetDashboard> get;
private final SetDashboard set;
private final ProjectResource project;
private final String id;
@@ -46,10 +49,12 @@ public class DashboardApiImpl implements DashboardApi {
DashboardApiImpl(
DashboardsCollection dashboards,
Provider<GetDashboard> get,
SetDashboard set,
@Assisted ProjectResource project,
@Assisted String id) {
this.dashboards = dashboards;
this.get = get;
this.set = set;
this.project = project;
this.id = id;
}
@@ -68,6 +73,17 @@ public class DashboardApiImpl implements DashboardApi {
}
}
@Override
public void setDefault() throws RestApiException {
SetDashboardInput input = new SetDashboardInput();
input.id = id;
try {
set.apply(DashboardResource.projectDefault(project.getControl()), input);
} catch (Exception e) {
throw asRestApiException("Cannot set default dashboard", e);
}
}
private DashboardResource resource()
throws ResourceNotFoundException, IOException, ConfigInvalidException,
PermissionBackendException {

View File

@@ -27,7 +27,7 @@ import com.google.inject.Singleton;
import java.io.IOException;
@Singleton
class SetDashboard implements RestModifyView<DashboardResource, SetDashboardInput> {
public class SetDashboard implements RestModifyView<DashboardResource, SetDashboardInput> {
private final Provider<SetDefaultDashboard> defaultSetter;
@Inject