ChangeRebuilderIT: Use ChangeBundle for comparisons
Add a new test util class NoteDbChecker that rebuilds a given set of changes and then compares their ChangeBundles. Change-Id: I1db6c449ba7077db6e0f74c47779c5648d0c1867
This commit is contained in:
		@@ -14,27 +14,21 @@
 | 
			
		||||
 | 
			
		||||
package com.google.gerrit.acceptance.server.notedb;
 | 
			
		||||
 | 
			
		||||
import static com.google.common.truth.Truth.assertThat;
 | 
			
		||||
import static com.google.common.truth.TruthJUnit.assume;
 | 
			
		||||
import static com.google.gerrit.common.TimeUtil.roundToSecond;
 | 
			
		||||
import static com.google.gerrit.testutil.GerritServerTests.isNoteDbTestEnabled;
 | 
			
		||||
 | 
			
		||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
 | 
			
		||||
import com.google.gerrit.acceptance.PushOneCommit;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.Change;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.PatchSet;
 | 
			
		||||
import com.google.gerrit.server.notedb.ChangeNotes;
 | 
			
		||||
import com.google.gerrit.server.notedb.ChangeRebuilder;
 | 
			
		||||
import com.google.gerrit.testutil.NoteDbChecker;
 | 
			
		||||
import com.google.inject.Inject;
 | 
			
		||||
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
public class ChangeRebuilderIT extends AbstractDaemonTest {
 | 
			
		||||
  @Inject
 | 
			
		||||
  private ChangeRebuilder rebuilder;
 | 
			
		||||
  private NoteDbChecker checker;
 | 
			
		||||
 | 
			
		||||
  @Before
 | 
			
		||||
  public void setUp() {
 | 
			
		||||
@@ -47,11 +41,7 @@ public class ChangeRebuilderIT extends AbstractDaemonTest {
 | 
			
		||||
    PushOneCommit.Result r = createChange();
 | 
			
		||||
    Change.Id id = r.getPatchSetId().getParentKey();
 | 
			
		||||
    gApi.changes().id(id.get()).topic(name("a-topic"));
 | 
			
		||||
    Change old = db.changes().get(id);
 | 
			
		||||
 | 
			
		||||
    rebuild(id);
 | 
			
		||||
 | 
			
		||||
    assertChangeEqual(old, notesFactory.create(db, project, id).getChange());
 | 
			
		||||
    checker.checkChanges(id);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
@@ -59,68 +49,6 @@ public class ChangeRebuilderIT extends AbstractDaemonTest {
 | 
			
		||||
    PushOneCommit.Result r = createChange();
 | 
			
		||||
    Change.Id id = r.getPatchSetId().getParentKey();
 | 
			
		||||
    r = amendChange(r.getChangeId());
 | 
			
		||||
 | 
			
		||||
    PatchSet ps1 = db.patchSets().get(new PatchSet.Id(id, 1));
 | 
			
		||||
    PatchSet ps2 = db.patchSets().get(new PatchSet.Id(id, 2));
 | 
			
		||||
 | 
			
		||||
    rebuild(id);
 | 
			
		||||
 | 
			
		||||
    ChangeNotes notes = notesFactory.create(db, project, id);
 | 
			
		||||
    Map<PatchSet.Id, PatchSet> patchSets = notes.getPatchSets();
 | 
			
		||||
    assertThat(patchSets.keySet()).containsExactly(ps1.getId(), ps2.getId())
 | 
			
		||||
        .inOrder();
 | 
			
		||||
 | 
			
		||||
    assertPatchSetEqual(ps1, patchSets.get(ps1.getId()));
 | 
			
		||||
    assertPatchSetEqual(ps2, patchSets.get(ps2.getId()));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void rebuild(Change.Id... changeIds) throws Exception {
 | 
			
		||||
    notesMigration.setWriteChanges(true);
 | 
			
		||||
    for (Change.Id id : changeIds) {
 | 
			
		||||
      rebuilder.rebuild(db, id);
 | 
			
		||||
    }
 | 
			
		||||
    notesMigration.setReadChanges(true);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static void assertChangeEqual(Change expectedReviewDb,
 | 
			
		||||
      Change actualNoteDb) {
 | 
			
		||||
    assertThat(actualNoteDb.getId()).isEqualTo(expectedReviewDb.getId());
 | 
			
		||||
    assertThat(actualNoteDb.getKey()).isEqualTo(expectedReviewDb.getKey());
 | 
			
		||||
 | 
			
		||||
    // TODO(dborowitz): actualNoteDb's timestamps should come from notedb, currently
 | 
			
		||||
    // they're read from reviewdb.
 | 
			
		||||
    assertThat(roundToSecond(actualNoteDb.getCreatedOn()))
 | 
			
		||||
        .isEqualTo(roundToSecond(expectedReviewDb.getCreatedOn()));
 | 
			
		||||
    assertThat(roundToSecond(actualNoteDb.getLastUpdatedOn()))
 | 
			
		||||
        .isEqualTo(roundToSecond(expectedReviewDb.getLastUpdatedOn()));
 | 
			
		||||
    assertThat(actualNoteDb.getOwner()).isEqualTo(expectedReviewDb.getOwner());
 | 
			
		||||
    assertThat(actualNoteDb.getDest()).isEqualTo(expectedReviewDb.getDest());
 | 
			
		||||
    assertThat(actualNoteDb.getStatus())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getStatus());
 | 
			
		||||
    assertThat(actualNoteDb.currentPatchSetId())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.currentPatchSetId());
 | 
			
		||||
    assertThat(actualNoteDb.getSubject())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getSubject());
 | 
			
		||||
    assertThat(actualNoteDb.getTopic()).isEqualTo(expectedReviewDb.getTopic());
 | 
			
		||||
    assertThat(actualNoteDb.getOriginalSubject())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getOriginalSubject());
 | 
			
		||||
    assertThat(actualNoteDb.getSubmissionId())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getSubmissionId());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static void assertPatchSetEqual(PatchSet expectedReviewDb,
 | 
			
		||||
      PatchSet actualNoteDb) {
 | 
			
		||||
    assertThat(actualNoteDb.getId()).isEqualTo(expectedReviewDb.getId());
 | 
			
		||||
    assertThat(actualNoteDb.getRevision())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getRevision());
 | 
			
		||||
    assertThat(actualNoteDb.getUploader())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getUploader());
 | 
			
		||||
    assertThat(actualNoteDb.getCreatedOn())
 | 
			
		||||
        .isEqualTo(roundToSecond(expectedReviewDb.getCreatedOn()));
 | 
			
		||||
    assertThat(actualNoteDb.isDraft()).isEqualTo(expectedReviewDb.isDraft());
 | 
			
		||||
    assertThat(actualNoteDb.getGroups())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getGroups());
 | 
			
		||||
    assertThat(actualNoteDb.getPushCertificate())
 | 
			
		||||
        .isEqualTo(expectedReviewDb.getPushCertificate());
 | 
			
		||||
    checker.checkChanges(id);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -245,6 +245,10 @@ public class ChangeBundle {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Change getChange() {
 | 
			
		||||
    return change;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public ImmutableList<String> differencesFrom(ChangeBundle o) {
 | 
			
		||||
    List<String> diffs = new ArrayList<>();
 | 
			
		||||
    diffChanges(diffs, this, o);
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,100 @@
 | 
			
		||||
// Copyright (C) 2016 The Android Open Source Project
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
// http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
package com.google.gerrit.testutil;
 | 
			
		||||
 | 
			
		||||
import com.google.common.base.Function;
 | 
			
		||||
import com.google.common.base.Joiner;
 | 
			
		||||
import com.google.common.collect.Ordering;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.Change;
 | 
			
		||||
import com.google.gerrit.reviewdb.server.ReviewDb;
 | 
			
		||||
import com.google.gerrit.server.PatchLineCommentsUtil;
 | 
			
		||||
import com.google.gerrit.server.notedb.ChangeBundle;
 | 
			
		||||
import com.google.gerrit.server.notedb.ChangeNotes;
 | 
			
		||||
import com.google.gerrit.server.notedb.ChangeRebuilder;
 | 
			
		||||
import com.google.gerrit.server.schema.DisabledChangesReviewDbWrapper;
 | 
			
		||||
import com.google.inject.Inject;
 | 
			
		||||
import com.google.inject.Provider;
 | 
			
		||||
import com.google.inject.Singleton;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Singleton
 | 
			
		||||
public class NoteDbChecker {
 | 
			
		||||
  private final Provider<ReviewDb> dbProvider;
 | 
			
		||||
  private final TestNotesMigration notesMigration;
 | 
			
		||||
  private final ChangeNotes.Factory notesFactory;
 | 
			
		||||
  private final ChangeRebuilder changeRebuilder;
 | 
			
		||||
  private final PatchLineCommentsUtil plcUtil;
 | 
			
		||||
 | 
			
		||||
  @Inject
 | 
			
		||||
  NoteDbChecker(Provider<ReviewDb> dbProvider,
 | 
			
		||||
      TestNotesMigration notesMigration,
 | 
			
		||||
      ChangeNotes.Factory notesFactory,
 | 
			
		||||
      ChangeRebuilder changeRebuilder,
 | 
			
		||||
      PatchLineCommentsUtil plcUtil) {
 | 
			
		||||
    this.dbProvider = dbProvider;
 | 
			
		||||
    this.notesMigration = notesMigration;
 | 
			
		||||
    this.notesFactory = notesFactory;
 | 
			
		||||
    this.changeRebuilder = changeRebuilder;
 | 
			
		||||
    this.plcUtil = plcUtil;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void checkChanges(Change.Id... changeIds) throws Exception {
 | 
			
		||||
    checkChanges(Arrays.asList(changeIds));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void checkChanges(Iterable<Change.Id> changeIds) throws Exception {
 | 
			
		||||
    ReviewDb db = unwrapDb();
 | 
			
		||||
 | 
			
		||||
    notesMigration.setReadChanges(false);
 | 
			
		||||
    List<ChangeBundle> allExpected = new ArrayList<>();
 | 
			
		||||
    for (Change.Id id : changeIds) {
 | 
			
		||||
      allExpected.add(ChangeBundle.fromReviewDb(db, id));
 | 
			
		||||
    }
 | 
			
		||||
    Collections.sort(allExpected, Ordering.natural().onResultOf(
 | 
			
		||||
        new Function<ChangeBundle, Integer>() {
 | 
			
		||||
          @Override
 | 
			
		||||
          public Integer apply(ChangeBundle in) {
 | 
			
		||||
            return in.getChange().getId().get();
 | 
			
		||||
          }
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
    notesMigration.setWriteChanges(true);
 | 
			
		||||
    notesMigration.setReadChanges(true);
 | 
			
		||||
    for (ChangeBundle expected : allExpected) {
 | 
			
		||||
      Change c = expected.getChange();
 | 
			
		||||
      changeRebuilder.rebuild(db, c.getId());
 | 
			
		||||
      ChangeBundle actual = ChangeBundle.fromNotes(
 | 
			
		||||
          plcUtil, notesFactory.create(db, c.getProject(), c.getId()));
 | 
			
		||||
      List<String> diff = expected.differencesFrom(actual);
 | 
			
		||||
      if (!diff.isEmpty()) {
 | 
			
		||||
        throw new AssertionError(
 | 
			
		||||
            "Differences between ReviewDb and NoteDb for " + c + ":\n"
 | 
			
		||||
            + Joiner.on('\n').join(diff));
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private ReviewDb unwrapDb() {
 | 
			
		||||
    ReviewDb db = dbProvider.get();
 | 
			
		||||
    if (db instanceof DisabledChangesReviewDbWrapper) {
 | 
			
		||||
      db = ((DisabledChangesReviewDbWrapper) db).unsafeGetDelegate();
 | 
			
		||||
    }
 | 
			
		||||
    return db;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user