PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/javatests/com/google/gerrit/server/change/LabelNormalizerTest.java

https://gitlab.com/chenfengxu/gerrit
Java | 230 lines | 191 code | 25 blank | 14 comment | 6 complexity | ea866f973a5adc052d5c80e99d5aa7f8 MD5 | raw file
  1. // Copyright (C) 2014 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package com.google.gerrit.server.change;
  15. import static com.google.gerrit.common.data.Permission.forLabel;
  16. import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
  17. import static com.google.gerrit.server.project.testing.Util.allow;
  18. import static com.google.gerrit.server.project.testing.Util.category;
  19. import static com.google.gerrit.server.project.testing.Util.value;
  20. import static org.junit.Assert.assertEquals;
  21. import com.google.common.collect.ImmutableList;
  22. import com.google.gerrit.common.TimeUtil;
  23. import com.google.gerrit.common.data.AccessSection;
  24. import com.google.gerrit.common.data.LabelType;
  25. import com.google.gerrit.lifecycle.LifecycleManager;
  26. import com.google.gerrit.reviewdb.client.Account;
  27. import com.google.gerrit.reviewdb.client.Branch;
  28. import com.google.gerrit.reviewdb.client.Change;
  29. import com.google.gerrit.reviewdb.client.LabelId;
  30. import com.google.gerrit.reviewdb.client.PatchSet;
  31. import com.google.gerrit.reviewdb.client.PatchSetApproval;
  32. import com.google.gerrit.reviewdb.client.PatchSetInfo;
  33. import com.google.gerrit.reviewdb.server.ReviewDb;
  34. import com.google.gerrit.server.CurrentUser;
  35. import com.google.gerrit.server.IdentifiedUser;
  36. import com.google.gerrit.server.account.AccountManager;
  37. import com.google.gerrit.server.account.AuthRequest;
  38. import com.google.gerrit.server.change.LabelNormalizer.Result;
  39. import com.google.gerrit.server.config.AllProjectsName;
  40. import com.google.gerrit.server.git.GitRepositoryManager;
  41. import com.google.gerrit.server.git.meta.MetaDataUpdate;
  42. import com.google.gerrit.server.notedb.ChangeNotes;
  43. import com.google.gerrit.server.project.ProjectCache;
  44. import com.google.gerrit.server.project.ProjectConfig;
  45. import com.google.gerrit.server.schema.SchemaCreator;
  46. import com.google.gerrit.server.util.RequestContext;
  47. import com.google.gerrit.server.util.ThreadLocalRequestContext;
  48. import com.google.gerrit.testing.InMemoryDatabase;
  49. import com.google.gerrit.testing.InMemoryModule;
  50. import com.google.inject.Guice;
  51. import com.google.inject.Inject;
  52. import com.google.inject.Injector;
  53. import com.google.inject.Provider;
  54. import com.google.inject.util.Providers;
  55. import java.util.List;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.junit.After;
  58. import org.junit.Before;
  59. import org.junit.Test;
  60. /** Unit tests for {@link LabelNormalizer}. */
  61. public class LabelNormalizerTest {
  62. @Inject private AccountManager accountManager;
  63. @Inject private AllProjectsName allProjects;
  64. @Inject private GitRepositoryManager repoManager;
  65. @Inject private IdentifiedUser.GenericFactory userFactory;
  66. @Inject private InMemoryDatabase schemaFactory;
  67. @Inject private LabelNormalizer norm;
  68. @Inject private MetaDataUpdate.User metaDataUpdateFactory;
  69. @Inject private ProjectCache projectCache;
  70. @Inject private SchemaCreator schemaCreator;
  71. @Inject protected ThreadLocalRequestContext requestContext;
  72. @Inject private ChangeNotes.Factory changeNotesFactory;
  73. private LifecycleManager lifecycle;
  74. private ReviewDb db;
  75. private Account.Id userId;
  76. private IdentifiedUser user;
  77. private Change change;
  78. private ChangeNotes notes;
  79. @Before
  80. public void setUpInjector() throws Exception {
  81. Injector injector = Guice.createInjector(new InMemoryModule());
  82. injector.injectMembers(this);
  83. lifecycle = new LifecycleManager();
  84. lifecycle.add(injector);
  85. lifecycle.start();
  86. db = schemaFactory.open();
  87. schemaCreator.create(db);
  88. userId = accountManager.authenticate(AuthRequest.forUser("user")).getAccountId();
  89. user = userFactory.create(userId);
  90. requestContext.setContext(
  91. new RequestContext() {
  92. @Override
  93. public CurrentUser getUser() {
  94. return user;
  95. }
  96. @Override
  97. public Provider<ReviewDb> getReviewDbProvider() {
  98. return Providers.of(db);
  99. }
  100. });
  101. configureProject();
  102. setUpChange();
  103. }
  104. private void configureProject() throws Exception {
  105. ProjectConfig pc = loadAllProjects();
  106. for (AccessSection sec : pc.getAccessSections()) {
  107. for (String label : pc.getLabelSections().keySet()) {
  108. sec.removePermission(forLabel(label));
  109. }
  110. }
  111. LabelType lt =
  112. category("Verified", value(1, "Verified"), value(0, "No score"), value(-1, "Fails"));
  113. pc.getLabelSections().put(lt.getName(), lt);
  114. save(pc);
  115. }
  116. private void setUpChange() throws Exception {
  117. change =
  118. new Change(
  119. new Change.Key("Iabcd1234abcd1234abcd1234abcd1234abcd1234"),
  120. new Change.Id(1),
  121. userId,
  122. new Branch.NameKey(allProjects, "refs/heads/master"),
  123. TimeUtil.nowTs());
  124. PatchSetInfo ps = new PatchSetInfo(new PatchSet.Id(change.getId(), 1));
  125. ps.setSubject("Test change");
  126. change.setCurrentPatchSet(ps);
  127. db.changes().insert(ImmutableList.of(change));
  128. notes = changeNotesFactory.createChecked(db, change);
  129. }
  130. @After
  131. public void tearDown() {
  132. if (lifecycle != null) {
  133. lifecycle.stop();
  134. }
  135. requestContext.setContext(null);
  136. if (db != null) {
  137. db.close();
  138. }
  139. InMemoryDatabase.drop(schemaFactory);
  140. }
  141. @Test
  142. public void noNormalizeByPermission() throws Exception {
  143. ProjectConfig pc = loadAllProjects();
  144. allow(pc, forLabel("Code-Review"), -1, 1, REGISTERED_USERS, "refs/heads/*");
  145. allow(pc, forLabel("Verified"), -1, 1, REGISTERED_USERS, "refs/heads/*");
  146. save(pc);
  147. PatchSetApproval cr = psa(userId, "Code-Review", 2);
  148. PatchSetApproval v = psa(userId, "Verified", 1);
  149. assertEquals(Result.create(list(cr, v), list(), list()), norm.normalize(notes, list(cr, v)));
  150. }
  151. @Test
  152. public void normalizeByType() throws Exception {
  153. ProjectConfig pc = loadAllProjects();
  154. allow(pc, forLabel("Code-Review"), -5, 5, REGISTERED_USERS, "refs/heads/*");
  155. allow(pc, forLabel("Verified"), -5, 5, REGISTERED_USERS, "refs/heads/*");
  156. save(pc);
  157. PatchSetApproval cr = psa(userId, "Code-Review", 5);
  158. PatchSetApproval v = psa(userId, "Verified", 5);
  159. assertEquals(
  160. Result.create(list(), list(copy(cr, 2), copy(v, 1)), list()),
  161. norm.normalize(notes, list(cr, v)));
  162. }
  163. @Test
  164. public void emptyPermissionRangeKeepsResult() throws Exception {
  165. PatchSetApproval cr = psa(userId, "Code-Review", 1);
  166. PatchSetApproval v = psa(userId, "Verified", 1);
  167. assertEquals(Result.create(list(cr, v), list(), list()), norm.normalize(notes, list(cr, v)));
  168. }
  169. @Test
  170. public void explicitZeroVoteOnNonEmptyRangeIsPresent() throws Exception {
  171. ProjectConfig pc = loadAllProjects();
  172. allow(pc, forLabel("Code-Review"), -1, 1, REGISTERED_USERS, "refs/heads/*");
  173. save(pc);
  174. PatchSetApproval cr = psa(userId, "Code-Review", 0);
  175. PatchSetApproval v = psa(userId, "Verified", 0);
  176. assertEquals(Result.create(list(cr, v), list(), list()), norm.normalize(notes, list(cr, v)));
  177. }
  178. private ProjectConfig loadAllProjects() throws Exception {
  179. try (Repository repo = repoManager.openRepository(allProjects)) {
  180. ProjectConfig pc = new ProjectConfig(allProjects);
  181. pc.load(repo);
  182. return pc;
  183. }
  184. }
  185. private void save(ProjectConfig pc) throws Exception {
  186. try (MetaDataUpdate md = metaDataUpdateFactory.create(pc.getProject().getNameKey(), user)) {
  187. pc.commit(md);
  188. projectCache.evict(pc.getProject().getNameKey());
  189. }
  190. }
  191. private PatchSetApproval psa(Account.Id accountId, String label, int value) {
  192. return new PatchSetApproval(
  193. new PatchSetApproval.Key(change.currentPatchSetId(), accountId, new LabelId(label)),
  194. (short) value,
  195. TimeUtil.nowTs());
  196. }
  197. private PatchSetApproval copy(PatchSetApproval src, int newValue) {
  198. PatchSetApproval result = new PatchSetApproval(src.getKey().getParentKey(), src);
  199. result.setValue((short) newValue);
  200. return result;
  201. }
  202. private static List<PatchSetApproval> list(PatchSetApproval... psas) {
  203. return ImmutableList.<PatchSetApproval>copyOf(psas);
  204. }
  205. }