LCOV - code coverage report
Current view: top level - testing - TestChanges.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 53 56 94.6 %
Date: 2022-11-19 15:00:39 Functions: 8 10 80.0 %

          Line data    Source code
       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             : 
      15             : package com.google.gerrit.testing;
      16             : 
      17             : import static com.google.common.base.MoreObjects.firstNonNull;
      18             : 
      19             : import com.google.common.collect.Ordering;
      20             : import com.google.gerrit.entities.Account;
      21             : import com.google.gerrit.entities.BranchNameKey;
      22             : import com.google.gerrit.entities.Change;
      23             : import com.google.gerrit.entities.PatchSet;
      24             : import com.google.gerrit.entities.PatchSetInfo;
      25             : import com.google.gerrit.entities.Project;
      26             : import com.google.gerrit.extensions.config.FactoryModule;
      27             : import com.google.gerrit.server.CurrentUser;
      28             : import com.google.gerrit.server.git.GitRepositoryManager;
      29             : import com.google.gerrit.server.notedb.AbstractChangeNotes;
      30             : import com.google.gerrit.server.notedb.ChangeNotes;
      31             : import com.google.gerrit.server.notedb.ChangeUpdate;
      32             : import com.google.gerrit.server.util.time.TimeUtil;
      33             : import com.google.inject.Injector;
      34             : import java.time.ZoneId;
      35             : import java.util.concurrent.atomic.AtomicInteger;
      36             : import org.eclipse.jgit.junit.TestRepository;
      37             : import org.eclipse.jgit.lib.ObjectId;
      38             : import org.eclipse.jgit.lib.PersonIdent;
      39             : import org.eclipse.jgit.lib.Ref;
      40             : import org.eclipse.jgit.lib.Repository;
      41             : 
      42             : /**
      43             :  * Utility functions to create and manipulate Change, ChangeUpdate, and ChangeControl objects for
      44             :  * testing.
      45             :  */
      46           0 : public class TestChanges {
      47           4 :   private static final AtomicInteger nextChangeId = new AtomicInteger(1);
      48             : 
      49             :   public static Change newChange(Project.NameKey project, Account.Id userId) {
      50           3 :     return newChange(project, userId, nextChangeId.getAndIncrement());
      51             :   }
      52             : 
      53             :   public static Change newChange(Project.NameKey project, Account.Id userId, int id) {
      54           4 :     Change.Id changeId = Change.id(id);
      55           4 :     Change c =
      56             :         new Change(
      57           4 :             Change.key("Iabcd1234abcd1234abcd1234abcd1234abcd1234"),
      58             :             changeId,
      59             :             userId,
      60           4 :             BranchNameKey.create(project, "master"),
      61           4 :             TimeUtil.now());
      62           4 :     incrementPatchSet(c);
      63           4 :     return c;
      64             :   }
      65             : 
      66             :   public static PatchSet newPatchSet(PatchSet.Id id, ObjectId revision, Account.Id userId) {
      67           0 :     return newPatchSet(id, revision.name(), userId);
      68             :   }
      69             : 
      70             :   public static PatchSet newPatchSet(PatchSet.Id id, String revision, Account.Id userId) {
      71           2 :     return PatchSet.builder()
      72           2 :         .id(id)
      73           2 :         .commitId(ObjectId.fromString(revision))
      74           2 :         .uploader(userId)
      75           2 :         .createdOn(TimeUtil.now())
      76           2 :         .build();
      77             :   }
      78             : 
      79             :   public static ChangeUpdate newUpdate(
      80             :       Injector injector, Change c, CurrentUser user, boolean shouldExist) throws Exception {
      81           1 :     injector =
      82           1 :         injector.createChildInjector(
      83           1 :             new FactoryModule() {
      84             :               @Override
      85             :               public void configure() {
      86           1 :                 bind(CurrentUser.class).toInstance(user);
      87           1 :               }
      88             :             });
      89           1 :     ChangeUpdate update =
      90             :         injector
      91           1 :             .getInstance(ChangeUpdate.Factory.class)
      92           1 :             .create(
      93             :                 new ChangeNotes(
      94           1 :                         injector.getInstance(AbstractChangeNotes.Args.class), c, shouldExist, null)
      95           1 :                     .load(),
      96             :                 user,
      97           1 :                 TimeUtil.now(),
      98           1 :                 Ordering.natural());
      99             : 
     100           1 :     ChangeNotes notes = update.getNotes();
     101           1 :     boolean hasPatchSets = notes.getPatchSets() != null && !notes.getPatchSets().isEmpty();
     102           1 :     if (hasPatchSets) {
     103           1 :       return update;
     104             :     }
     105             : 
     106             :     // Change doesn't exist yet. NoteDb requires that there be a commit for the
     107             :     // first patch set, so create one.
     108           1 :     GitRepositoryManager repoManager = injector.getInstance(GitRepositoryManager.class);
     109           1 :     try (Repository repo = repoManager.openRepository(c.getProject());
     110           1 :         TestRepository<Repository> tr = new TestRepository<>(repo)) {
     111           1 :       PersonIdent ident =
     112           1 :           user.asIdentifiedUser().newCommitterIdent(update.getWhen(), ZoneId.systemDefault());
     113           1 :       TestRepository<Repository>.CommitBuilder cb =
     114           1 :           tr.commit()
     115           1 :               .author(ident)
     116           1 :               .committer(ident)
     117           1 :               .message(firstNonNull(c.getSubject(), "Test change"));
     118           1 :       Ref parent = repo.exactRef(c.getDest().branch());
     119           1 :       if (parent != null) {
     120           0 :         cb.parent(tr.getRevWalk().parseCommit(parent.getObjectId()));
     121             :       }
     122           1 :       update.setBranch(c.getDest().branch());
     123           1 :       update.setChangeId(c.getKey().get());
     124           1 :       update.setCommit(tr.getRevWalk(), cb.create());
     125           1 :       return update;
     126             :     }
     127             :   }
     128             : 
     129             :   public static void incrementPatchSet(Change change) {
     130           4 :     PatchSet.Id curr = change.currentPatchSetId();
     131           4 :     PatchSetInfo ps =
     132           4 :         new PatchSetInfo(PatchSet.id(change.getId(), curr != null ? curr.get() + 1 : 1));
     133           4 :     ps.setSubject("Change subject");
     134           4 :     change.setCurrentPatchSet(ps);
     135           4 :   }
     136             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750