AccountApi: Add methods to get and set DiffPreferencesInfo

Change-Id: I139d2c784052d92d3f0c84044a5a361e1bbf061d
This commit is contained in:
David Ostrovsky
2016-03-20 09:11:23 +01:00
parent d0cbb00e73
commit 273debb90d
4 changed files with 57 additions and 25 deletions

View File

@@ -12,34 +12,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.rest.account;
package com.google.gerrit.acceptance.api.accounts;
import static com.google.common.truth.Truth.assertThat;
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.Whitespace;
import com.google.gerrit.extensions.client.Theme;
import org.junit.Test;
@NoHttpd
public class DiffPreferencesIT extends AbstractDaemonTest {
@Test
public void getDiffPreferencesOfNonExistingAccount_NotFound()
throws Exception {
adminSession.get("/accounts/non-existing/preferences.diff")
.assertNotFound();
}
@Test
public void getDiffPreferences() throws Exception {
RestResponse r = adminSession.get("/accounts/" + admin.email
+ "/preferences.diff");
r.assertOK();
DiffPreferencesInfo d = DiffPreferencesInfo.defaults();
DiffPreferencesInfo o =
newGson().fromJson(r.getReader(), DiffPreferencesInfo.class);
DiffPreferencesInfo o = gApi.accounts()
.id(admin.getId().toString())
.getDiffPreferences();
assertThat(o.context).isEqualTo(d.context);
assertThat(o.tabSize).isEqualTo(d.tabSize);
@@ -93,11 +85,9 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
i.hideEmptyPane ^= true;
i.matchBrackets ^= true;
RestResponse r = adminSession.put("/accounts/" + admin.email
+ "/preferences.diff", i);
r.assertOK();
DiffPreferencesInfo o = newGson().fromJson(r.getReader(),
DiffPreferencesInfo.class);
DiffPreferencesInfo o = gApi.accounts()
.id(admin.getId().toString())
.setDiffPreferences(i);
assertThat(o.context).isEqualTo(i.context);
assertThat(o.tabSize).isEqualTo(i.tabSize);
@@ -125,10 +115,9 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
// Partially fill input record
i = new DiffPreferencesInfo();
i.tabSize = 42;
r = adminSession.put("/accounts/" + admin.email
+ "/preferences.diff", i);
DiffPreferencesInfo a = newGson().fromJson(r.getReader(),
DiffPreferencesInfo.class);
DiffPreferencesInfo a = gApi.accounts()
.id(admin.getId().toString())
.setDiffPreferences(i);
assertThat(a.context).isEqualTo(o.context);
assertThat(a.tabSize).isEqualTo(42);

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.extensions.api.accounts;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.client.EditPreferencesInfo;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.extensions.common.AccountInfo;
@@ -33,6 +34,10 @@ public interface AccountApi {
GeneralPreferencesInfo setPreferences(GeneralPreferencesInfo in)
throws RestApiException;
DiffPreferencesInfo getDiffPreferences() throws RestApiException;
DiffPreferencesInfo setDiffPreferences(DiffPreferencesInfo in)
throws RestApiException;
EditPreferencesInfo getEditPreferences() throws RestApiException;
EditPreferencesInfo setEditPreferences(EditPreferencesInfo in)
throws RestApiException;
@@ -72,6 +77,17 @@ public interface AccountApi {
throw new NotImplementedException();
}
@Override
public DiffPreferencesInfo getDiffPreferences() throws RestApiException {
throw new NotImplementedException();
}
@Override
public DiffPreferencesInfo setDiffPreferences(DiffPreferencesInfo in)
throws RestApiException {
throw new NotImplementedException();
}
@Override
public EditPreferencesInfo getEditPreferences() throws RestApiException {
throw new NotImplementedException();

View File

@@ -28,7 +28,6 @@ import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.UserConfigSections;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@@ -60,7 +59,7 @@ public class SetDiffPreferences implements
@Override
public DiffPreferencesInfo apply(AccountResource rsrc, DiffPreferencesInfo in)
throws AuthException, BadRequestException, ConfigInvalidException,
RepositoryNotFoundException, IOException, OrmException {
RepositoryNotFoundException, IOException {
if (self.get() != rsrc.getUser()
&& !self.get().getCapabilities().canModifyAccount()) {
throw new AuthException("requires Modify Account capability");

View File

@@ -18,6 +18,7 @@ import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.extensions.api.accounts.AccountApi;
import com.google.gerrit.extensions.api.accounts.EmailInput;
import com.google.gerrit.extensions.api.accounts.GpgKeyApi;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.client.EditPreferencesInfo;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.extensions.common.AccountInfo;
@@ -30,8 +31,10 @@ import com.google.gerrit.server.account.AccountLoader;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.CreateEmail;
import com.google.gerrit.server.account.GetAvatar;
import com.google.gerrit.server.account.GetDiffPreferences;
import com.google.gerrit.server.account.GetEditPreferences;
import com.google.gerrit.server.account.GetPreferences;
import com.google.gerrit.server.account.SetDiffPreferences;
import com.google.gerrit.server.account.SetEditPreferences;
import com.google.gerrit.server.account.SetPreferences;
import com.google.gerrit.server.account.StarredChanges;
@@ -59,6 +62,8 @@ public class AccountApiImpl implements AccountApi {
private final Provider<GetAvatar> getAvatar;
private final GetPreferences getPreferences;
private final SetPreferences setPreferences;
private final GetDiffPreferences getDiffPreferences;
private final SetDiffPreferences setDiffPreferences;
private final GetEditPreferences getEditPreferences;
private final SetEditPreferences setEditPreferences;
private final StarredChanges.Create starredChangesCreate;
@@ -72,6 +77,8 @@ public class AccountApiImpl implements AccountApi {
Provider<GetAvatar> getAvatar,
GetPreferences getPreferences,
SetPreferences setPreferences,
GetDiffPreferences getDiffPreferences,
SetDiffPreferences setDiffPreferences,
GetEditPreferences getEditPreferences,
SetEditPreferences setEditPreferences,
StarredChanges.Create starredChangesCreate,
@@ -85,6 +92,8 @@ public class AccountApiImpl implements AccountApi {
this.getAvatar = getAvatar;
this.getPreferences = getPreferences;
this.setPreferences = setPreferences;
this.getDiffPreferences = getDiffPreferences;
this.setDiffPreferences = setDiffPreferences;
this.getEditPreferences = getEditPreferences;
this.setEditPreferences = setEditPreferences;
this.starredChangesCreate = starredChangesCreate;
@@ -128,6 +137,25 @@ public class AccountApiImpl implements AccountApi {
}
}
@Override
public DiffPreferencesInfo getDiffPreferences() throws RestApiException {
try {
return getDiffPreferences.apply(account);
} catch (IOException | ConfigInvalidException e) {
throw new RestApiException("Cannot query diff preferences", e);
}
}
@Override
public DiffPreferencesInfo setDiffPreferences(DiffPreferencesInfo in)
throws RestApiException {
try {
return setDiffPreferences.apply(account, in);
} catch (IOException | ConfigInvalidException e) {
throw new RestApiException("Cannot set diff preferences", e);
}
}
@Override
public EditPreferencesInfo getEditPreferences() throws RestApiException {
try {