Add extension API to get/set default diff preferences

Change-Id: Ia2c4e9516b16e79a18c7e4d84911ef548eb1d1c5
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2016-06-03 13:52:06 +02:00
parent 3a663b6caa
commit 41655df2b5
3 changed files with 63 additions and 17 deletions

View File

@ -18,18 +18,20 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.config.ConfigUtil.skipField;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import org.junit.Test;
import java.lang.reflect.Field;
@NoHttpd
public class DiffPreferencesIT extends AbstractDaemonTest {
@Test
public void getDiffPreferences() throws Exception {
DiffPreferencesInfo result = get();
DiffPreferencesInfo result =
gApi.config().server().getDefaultDiffPreferences();
assertPrefsEqual(result, DiffPreferencesInfo.defaults());
}
@ -38,28 +40,16 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
int newLineLength = DiffPreferencesInfo.defaults().lineLength + 10;
DiffPreferencesInfo update = new DiffPreferencesInfo();
update.lineLength = newLineLength;
DiffPreferencesInfo result = put(update);
DiffPreferencesInfo result =
gApi.config().server().setDefaultDiffPreferences(update);
assertThat(result.lineLength).named("lineLength").isEqualTo(newLineLength);
result = get();
result = gApi.config().server().getDefaultDiffPreferences();
DiffPreferencesInfo expected = DiffPreferencesInfo.defaults();
expected.lineLength = newLineLength;
assertPrefsEqual(result, expected);
}
private DiffPreferencesInfo get() throws Exception {
RestResponse r = adminRestSession.get("/config/server/preferences.diff");
r.assertOK();
return newGson().fromJson(r.getReader(), DiffPreferencesInfo.class);
}
private DiffPreferencesInfo put(DiffPreferencesInfo input) throws Exception {
RestResponse r = adminRestSession.put(
"/config/server/preferences.diff", input);
r.assertOK();
return newGson().fromJson(r.getReader(), DiffPreferencesInfo.class);
}
private void assertPrefsEqual(DiffPreferencesInfo actual,
DiffPreferencesInfo expected) throws Exception {
for (Field field : actual.getClass().getDeclaredFields()) {

View File

@ -14,6 +14,7 @@
package com.google.gerrit.extensions.api.config;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.RestApiException;
@ -23,6 +24,10 @@ public interface Server {
*/
String getVersion() throws RestApiException;
DiffPreferencesInfo getDefaultDiffPreferences() throws RestApiException;
DiffPreferencesInfo setDefaultDiffPreferences(DiffPreferencesInfo in)
throws RestApiException;
/**
* A default implementation which allows source compatibility
* when adding new methods to the interface.
@ -32,5 +37,17 @@ public interface Server {
public String getVersion() throws RestApiException {
throw new NotImplementedException();
}
@Override
public DiffPreferencesInfo getDefaultDiffPreferences()
throws RestApiException {
throw new NotImplementedException();
}
@Override
public DiffPreferencesInfo setDefaultDiffPreferences(DiffPreferencesInfo in)
throws RestApiException {
throw new NotImplementedException();
}
}
}

View File

@ -16,13 +16,52 @@ package com.google.gerrit.server.api.config;
import com.google.gerrit.common.Version;
import com.google.gerrit.extensions.api.config.Server;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.config.ConfigResource;
import com.google.gerrit.server.config.GetDiffPreferences;
import com.google.gerrit.server.config.SetDiffPreferences;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException;
import java.io.IOException;
@Singleton
public class ServerImpl implements Server {
private final GetDiffPreferences getDiffPreferences;
private final SetDiffPreferences setDiffPreferences;
@Inject
ServerImpl(GetDiffPreferences getDiffPreferences,
SetDiffPreferences setDiffPreferences) {
this.getDiffPreferences = getDiffPreferences;
this.setDiffPreferences = setDiffPreferences;
}
@Override
public String getVersion() throws RestApiException {
return Version.getVersion();
}
@Override
public DiffPreferencesInfo getDefaultDiffPreferences()
throws RestApiException {
try {
return getDiffPreferences.apply(new ConfigResource());
} catch (IOException | ConfigInvalidException e) {
throw new RestApiException("Cannot get default diff preferences", e);
}
}
@Override
public DiffPreferencesInfo setDefaultDiffPreferences(DiffPreferencesInfo in)
throws RestApiException {
try {
return setDiffPreferences.apply(new ConfigResource(), in);
} catch (IOException | ConfigInvalidException e) {
throw new RestApiException("Cannot set default diff preferences", e);
}
}
}