Merge changes from topic "dashboard-api"

* changes:
  DashboardIT: Factor out getting the project API to a method
  ProjectApi: Add method to remove default dashboard
  ProjectApi: Add method to set the default dashboard
This commit is contained in:
David Pursehouse
2017-10-01 16:16:54 +00:00
committed by Gerrit Code Review
4 changed files with 81 additions and 20 deletions

View File

@@ -22,9 +22,11 @@ import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.common.data.Permission; import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.projects.BranchInput; import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.DashboardInfo; import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.api.projects.ProjectApi;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.project.DashboardsCollection; import com.google.gerrit.server.project.DashboardsCollection;
import java.util.List; import java.util.List;
import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.junit.TestRepository;
@@ -43,20 +45,20 @@ public class DashboardIT extends AbstractDaemonTest {
@Test @Test
public void defaultDashboardDoesNotExist() throws Exception { public void defaultDashboardDoesNotExist() throws Exception {
exception.expect(ResourceNotFoundException.class); exception.expect(ResourceNotFoundException.class);
gApi.projects().name(project.get()).defaultDashboard().get(); project().defaultDashboard().get();
} }
@Test @Test
public void dashboardDoesNotExist() throws Exception { public void dashboardDoesNotExist() throws Exception {
exception.expect(ResourceNotFoundException.class); exception.expect(ResourceNotFoundException.class);
gApi.projects().name(project.get()).dashboard("my:dashboard").get(); project().dashboard("my:dashboard").get();
} }
@Test @Test
public void getDashboard() throws Exception { public void getDashboard() throws Exception {
assertThat(dashboards()).isEmpty(); assertThat(dashboards()).isEmpty();
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test"); DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
DashboardInfo result = gApi.projects().name(project.get()).dashboard(info.id).get(); DashboardInfo result = project().dashboard(info.id).get();
assertThat(result.id).isEqualTo(info.id); assertThat(result.id).isEqualTo(info.id);
assertThat(result.path).isEqualTo(info.path); assertThat(result.path).isEqualTo(info.path);
assertThat(result.ref).isEqualTo(info.ref); assertThat(result.ref).isEqualTo(info.ref);
@@ -69,9 +71,24 @@ public class DashboardIT extends AbstractDaemonTest {
public void setDefaultDashboard() throws Exception { public void setDefaultDashboard() throws Exception {
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test"); DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
assertThat(info.isDefault).isNull(); assertThat(info.isDefault).isNull();
gApi.projects().name(project.get()).dashboard(info.id).setDefault(); project().dashboard(info.id).setDefault();
assertThat(gApi.projects().name(project.get()).dashboard(info.id).get().isDefault).isTrue(); assertThat(project().dashboard(info.id).get().isDefault).isTrue();
assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(info.id); assertThat(project().defaultDashboard().get().id).isEqualTo(info.id);
}
@Test
public void setDefaultDashboardByProject() throws Exception {
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
assertThat(info.isDefault).isNull();
project().defaultDashboard(info.id);
assertThat(project().dashboard(info.id).get().isDefault).isTrue();
assertThat(project().defaultDashboard().get().id).isEqualTo(info.id);
project().removeDefaultDashboard();
assertThat(project().dashboard(info.id).get().isDefault).isNull();
exception.expect(ResourceNotFoundException.class);
project().defaultDashboard().get();
} }
@Test @Test
@@ -80,14 +97,14 @@ public class DashboardIT extends AbstractDaemonTest {
DashboardInfo d2 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test2"); DashboardInfo d2 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test2");
assertThat(d1.isDefault).isNull(); assertThat(d1.isDefault).isNull();
assertThat(d2.isDefault).isNull(); assertThat(d2.isDefault).isNull();
gApi.projects().name(project.get()).dashboard(d1.id).setDefault(); project().dashboard(d1.id).setDefault();
assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isTrue(); assertThat(project().dashboard(d1.id).get().isDefault).isTrue();
assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isNull(); assertThat(project().dashboard(d2.id).get().isDefault).isNull();
assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d1.id); assertThat(project().defaultDashboard().get().id).isEqualTo(d1.id);
gApi.projects().name(project.get()).dashboard(d2.id).setDefault(); project().dashboard(d2.id).setDefault();
assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d2.id); assertThat(project().defaultDashboard().get().id).isEqualTo(d2.id);
assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isNull(); assertThat(project().dashboard(d1.id).get().isDefault).isNull();
assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isTrue(); assertThat(project().dashboard(d2.id).get().isDefault).isTrue();
} }
@Test @Test
@@ -95,18 +112,22 @@ public class DashboardIT extends AbstractDaemonTest {
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test"); DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
exception.expect(BadRequestException.class); exception.expect(BadRequestException.class);
exception.expectMessage("inherited flag can only be used with default"); exception.expectMessage("inherited flag can only be used with default");
gApi.projects().name(project.get()).dashboard(info.id).get(true); project().dashboard(info.id).get(true);
} }
private List<DashboardInfo> dashboards() throws Exception { private List<DashboardInfo> dashboards() throws Exception {
return gApi.projects().name(project.get()).dashboards().get(); return project().dashboards().get();
}
private ProjectApi project() throws RestApiException {
return gApi.projects().name(project.get());
} }
private DashboardInfo createDashboard(String ref, String path) throws Exception { private DashboardInfo createDashboard(String ref, String path) throws Exception {
DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path); DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path);
String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref); String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref);
try { try {
gApi.projects().name(project.get()).branch(canonicalRef).create(new BranchInput()); project().branch(canonicalRef).create(new BranchInput());
} catch (ResourceConflictException e) { } catch (ResourceConflictException e) {
// The branch already exists if this method has already been called once. // The branch already exists if this method has already been called once.
if (!e.getMessage().contains("already exists")) { if (!e.getMessage().contains("already exists")) {
@@ -124,7 +145,7 @@ public class DashboardIT extends AbstractDaemonTest {
+ "query = is:open"; + "query = is:open";
cb.add(info.path, content); cb.add(info.path, content);
RevCommit c = cb.create(); RevCommit c = cb.create();
gApi.projects().name(project.get()).commit(c.name()); project().commit(c.name());
} }
return info; return info;
} }

View File

@@ -154,6 +154,16 @@ public interface ProjectApi {
*/ */
DashboardApi defaultDashboard() throws RestApiException; DashboardApi defaultDashboard() throws RestApiException;
/**
* Set the project's default dashboard.
*
* @param name the dashboard to set as default.
*/
void defaultDashboard(String name) throws RestApiException;
/** Remove the project's default dashboard. */
void removeDefaultDashboard() throws RestApiException;
abstract class ListDashboardsRequest { abstract class ListDashboardsRequest {
public abstract List<DashboardInfo> get() throws RestApiException; public abstract List<DashboardInfo> get() throws RestApiException;
} }
@@ -284,5 +294,15 @@ public interface ProjectApi {
public ListDashboardsRequest dashboards() throws RestApiException { public ListDashboardsRequest dashboards() throws RestApiException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override
public void defaultDashboard(String name) throws RestApiException {
throw new NotImplementedException();
}
@Override
public void removeDefaultDashboard() throws RestApiException {
throw new NotImplementedException();
}
} }
} }

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.api.projects;
import static com.google.gerrit.server.api.ApiUtil.asRestApiException; import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.api.projects.DashboardApi; import com.google.gerrit.extensions.api.projects.DashboardApi;
import com.google.gerrit.extensions.api.projects.DashboardInfo; import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.common.SetDashboardInput; import com.google.gerrit.extensions.common.SetDashboardInput;
@@ -51,7 +52,7 @@ public class DashboardApiImpl implements DashboardApi {
Provider<GetDashboard> get, Provider<GetDashboard> get,
SetDashboard set, SetDashboard set,
@Assisted ProjectResource project, @Assisted ProjectResource project,
@Assisted String id) { @Assisted @Nullable String id) {
this.dashboards = dashboards; this.dashboards = dashboards;
this.get = get; this.get = get;
this.set = set; this.set = set;
@@ -80,7 +81,8 @@ public class DashboardApiImpl implements DashboardApi {
try { try {
set.apply(DashboardResource.projectDefault(project.getControl()), input); set.apply(DashboardResource.projectDefault(project.getControl()), input);
} catch (Exception e) { } catch (Exception e) {
throw asRestApiException("Cannot set default dashboard", e); String msg = String.format("Cannot %s default dashboard", id != null ? "set" : "remove");
throw asRestApiException(msg, e);
} }
} }

View File

@@ -485,6 +485,24 @@ public class ProjectApiImpl implements ProjectApi {
return dashboard(DEFAULT_DASHBOARD_NAME); return dashboard(DEFAULT_DASHBOARD_NAME);
} }
@Override
public void defaultDashboard(String name) throws RestApiException {
try {
dashboardApi.create(checkExists(), name).setDefault();
} catch (Exception e) {
throw asRestApiException("Cannot set default dashboard", e);
}
}
@Override
public void removeDefaultDashboard() throws RestApiException {
try {
dashboardApi.create(checkExists(), null).setDefault();
} catch (Exception e) {
throw asRestApiException("Cannot remove default dashboard", e);
}
}
@Override @Override
public ListDashboardsRequest dashboards() throws RestApiException { public ListDashboardsRequest dashboards() throws RestApiException {
return new ListDashboardsRequest() { return new ListDashboardsRequest() {