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:
@@ -18,18 +18,20 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
import static com.google.gerrit.server.config.ConfigUtil.skipField;
|
import static com.google.gerrit.server.config.ConfigUtil.skipField;
|
||||||
|
|
||||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
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 com.google.gerrit.extensions.client.DiffPreferencesInfo;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
@NoHttpd
|
||||||
public class DiffPreferencesIT extends AbstractDaemonTest {
|
public class DiffPreferencesIT extends AbstractDaemonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDiffPreferences() throws Exception {
|
public void getDiffPreferences() throws Exception {
|
||||||
DiffPreferencesInfo result = get();
|
DiffPreferencesInfo result =
|
||||||
|
gApi.config().server().getDefaultDiffPreferences();
|
||||||
assertPrefsEqual(result, DiffPreferencesInfo.defaults());
|
assertPrefsEqual(result, DiffPreferencesInfo.defaults());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,28 +40,16 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
|
|||||||
int newLineLength = DiffPreferencesInfo.defaults().lineLength + 10;
|
int newLineLength = DiffPreferencesInfo.defaults().lineLength + 10;
|
||||||
DiffPreferencesInfo update = new DiffPreferencesInfo();
|
DiffPreferencesInfo update = new DiffPreferencesInfo();
|
||||||
update.lineLength = newLineLength;
|
update.lineLength = newLineLength;
|
||||||
DiffPreferencesInfo result = put(update);
|
DiffPreferencesInfo result =
|
||||||
|
gApi.config().server().setDefaultDiffPreferences(update);
|
||||||
assertThat(result.lineLength).named("lineLength").isEqualTo(newLineLength);
|
assertThat(result.lineLength).named("lineLength").isEqualTo(newLineLength);
|
||||||
|
|
||||||
result = get();
|
result = gApi.config().server().getDefaultDiffPreferences();
|
||||||
DiffPreferencesInfo expected = DiffPreferencesInfo.defaults();
|
DiffPreferencesInfo expected = DiffPreferencesInfo.defaults();
|
||||||
expected.lineLength = newLineLength;
|
expected.lineLength = newLineLength;
|
||||||
assertPrefsEqual(result, expected);
|
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,
|
private void assertPrefsEqual(DiffPreferencesInfo actual,
|
||||||
DiffPreferencesInfo expected) throws Exception {
|
DiffPreferencesInfo expected) throws Exception {
|
||||||
for (Field field : actual.getClass().getDeclaredFields()) {
|
for (Field field : actual.getClass().getDeclaredFields()) {
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
package com.google.gerrit.extensions.api.config;
|
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.NotImplementedException;
|
||||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||||
|
|
||||||
@@ -23,6 +24,10 @@ public interface Server {
|
|||||||
*/
|
*/
|
||||||
String getVersion() throws RestApiException;
|
String getVersion() throws RestApiException;
|
||||||
|
|
||||||
|
DiffPreferencesInfo getDefaultDiffPreferences() throws RestApiException;
|
||||||
|
DiffPreferencesInfo setDefaultDiffPreferences(DiffPreferencesInfo in)
|
||||||
|
throws RestApiException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default implementation which allows source compatibility
|
* A default implementation which allows source compatibility
|
||||||
* when adding new methods to the interface.
|
* when adding new methods to the interface.
|
||||||
@@ -32,5 +37,17 @@ public interface Server {
|
|||||||
public String getVersion() throws RestApiException {
|
public String getVersion() throws RestApiException {
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DiffPreferencesInfo getDefaultDiffPreferences()
|
||||||
|
throws RestApiException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DiffPreferencesInfo setDefaultDiffPreferences(DiffPreferencesInfo in)
|
||||||
|
throws RestApiException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,13 +16,52 @@ package com.google.gerrit.server.api.config;
|
|||||||
|
|
||||||
import com.google.gerrit.common.Version;
|
import com.google.gerrit.common.Version;
|
||||||
import com.google.gerrit.extensions.api.config.Server;
|
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.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 com.google.inject.Singleton;
|
||||||
|
|
||||||
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class ServerImpl implements Server {
|
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
|
@Override
|
||||||
public String getVersion() throws RestApiException {
|
public String getVersion() throws RestApiException {
|
||||||
return Version.getVersion();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user