Convert CommentsTest to Truth

Change-Id: I655f8e645ff14d733475eebeeeace8a9c2564229
This commit is contained in:
Dave Borowitz
2015-04-29 16:55:42 -07:00
parent d7c1727a07
commit b37f9bec74
3 changed files with 43 additions and 69 deletions

View File

@@ -51,7 +51,7 @@ public class Comments implements ChildCollection<RevisionResource, CommentResour
}
@Override
public RestView<RevisionResource> list() {
public ListComments list() {
return list;
}

View File

@@ -56,7 +56,7 @@ public class DraftComments implements ChildCollection<RevisionResource, DraftCom
}
@Override
public RestView<RevisionResource> list() throws AuthException {
public ListDraftComments list() throws AuthException {
checkIdentifiedUser();
return list;
}

View File

@@ -14,13 +14,12 @@
package com.google.gerrit.server.change;
import static com.google.common.truth.Truth.assertThat;
import static com.google.inject.Scopes.SINGLETON;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import com.google.common.base.MoreObjects;
@@ -33,7 +32,6 @@ import com.google.gerrit.extensions.common.CommentInfo;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
@@ -94,7 +92,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -325,7 +322,7 @@ public class CommentsTest {
// test ListComments for patch set 2
assertListComments(revRes2,
Collections.<String, ArrayList<PatchLineComment>>emptyMap());
Collections.<String, List<PatchLineComment>>emptyMap());
}
@Test
@@ -341,7 +338,7 @@ public class CommentsTest {
public void testListDrafts() throws Exception {
// test ListDrafts for patch set 1
assertListDrafts(revRes1,
Collections.<String, ArrayList<PatchLineComment>> emptyMap());
Collections.<String, List<PatchLineComment>> emptyMap());
// test ListDrafts for patch set 2
assertListDrafts(revRes2, ImmutableMap.of(
@@ -350,18 +347,10 @@ public class CommentsTest {
@Test
public void testPatchLineCommentsUtilByCommentStatus() throws OrmException {
List<PatchLineComment> publishedActual =
plcUtil.publishedByChange(db, revRes2.getNotes());
List<PatchLineComment> draftActual =
plcUtil.draftByChange(db, revRes2.getNotes());
List<PatchLineComment> publishedExpected =
Lists.newArrayList(plc1, plc2, plc3);
List<PatchLineComment> draftExpected =
Lists.newArrayList(plc4, plc5);
assertEquals(publishedExpected.size(), publishedActual.size());
assertEquals(draftExpected.size(), draftActual.size());
assertEquals(publishedExpected, publishedActual);
assertEquals(draftExpected, draftActual);
assertThat(plcUtil.publishedByChange(db, revRes2.getNotes()))
.containsExactly(plc1, plc2, plc3).inOrder();
assertThat(plcUtil.draftByChange(db, revRes2.getNotes()))
.containsExactly(plc4, plc5).inOrder();
}
private static IAnswer<ResultSet<PatchLineComment>> results(
@@ -390,66 +379,51 @@ public class CommentsTest {
}
private void assertListComments(RevisionResource res,
Map<String, ArrayList<PatchLineComment>> expected) throws Exception {
RestReadView<RevisionResource> listView =
(RestReadView<RevisionResource>) comments.list();
@SuppressWarnings("unchecked")
Map<String, List<CommentInfo>> actual =
(Map<String, List<CommentInfo>>) listView.apply(res);
assertNotNull(actual);
assertEquals(expected.size(), actual.size());
assertEquals(expected.keySet(), actual.keySet());
for (Map.Entry<String, ArrayList<PatchLineComment>> entry : expected.entrySet()) {
List<PatchLineComment> expectedComments = entry.getValue();
List<CommentInfo> actualComments = actual.get(entry.getKey());
assertNotNull(actualComments);
assertEquals(expectedComments.size(), actualComments.size());
for (int i = 0; i < expectedComments.size(); i++) {
assertComment(expectedComments.get(i), actualComments.get(i), true);
}
}
Map<String, ? extends List<PatchLineComment>> expected) throws Exception {
assertCommentMap(comments.list().apply(res), expected, true);
}
private void assertListDrafts(RevisionResource res,
Map<String, ArrayList<PatchLineComment>> expected) throws Exception {
RestReadView<RevisionResource> listView =
(RestReadView<RevisionResource>) drafts.list();
@SuppressWarnings("unchecked")
Map<String, List<CommentInfo>> actual =
(Map<String, List<CommentInfo>>) listView.apply(res);
assertNotNull(actual);
assertEquals(expected.size(), actual.size());
assertEquals(expected.keySet(), actual.keySet());
for (Map.Entry<String, ArrayList<PatchLineComment>> entry : expected.entrySet()) {
List<PatchLineComment> expectedComments = entry.getValue();
List<CommentInfo> actualComments = actual.get(entry.getKey());
assertNotNull(actualComments);
assertEquals(expectedComments.size(), actualComments.size());
for (int i = 0; i < expectedComments.size(); i++) {
assertComment(expectedComments.get(i), actualComments.get(i), false);
Map<String, ? extends List<PatchLineComment>> expected) throws Exception {
assertCommentMap(drafts.list().apply(res), expected, false);
}
private void assertCommentMap(Map<String, List<CommentInfo>> actual,
Map<String, ? extends List<PatchLineComment>> expected,
boolean isPublished) {
assertThat(actual.keySet()).containsExactlyElementsIn(expected.keySet());
for (Map.Entry<String, List<CommentInfo>> entry : actual.entrySet()) {
List<CommentInfo> actualList = entry.getValue();
List<PatchLineComment> expectedList = expected.get(entry.getKey());
assertThat(actualList).hasSize(expectedList.size());
for (int i = 0; i < expectedList.size(); i++) {
assertComment(expectedList.get(i), actualList.get(i), isPublished);
}
}
}
private static void assertComment(PatchLineComment plc, CommentInfo ci,
boolean isPublished) {
assertEquals(plc.getKey().get(), ci.id);
assertEquals(plc.getParentUuid(), ci.inReplyTo);
assertEquals(plc.getMessage(), ci.message);
assertThat(ci.id).isEqualTo(plc.getKey().get());
assertThat(ci.inReplyTo).isEqualTo(plc.getParentUuid());
assertThat(ci.message).isEqualTo(plc.getMessage());
if (isPublished) {
assertNotNull(ci.author);
assertEquals(plc.getAuthor(), new Account.Id(ci.author._accountId));
assertThat(ci.author).isNotNull();
assertThat(new Account.Id(ci.author._accountId))
.isEqualTo(plc.getAuthor());
}
assertEquals(plc.getLine(), (int) ci.line);
assertEquals(plc.getSide() == 0 ? Side.PARENT : Side.REVISION,
MoreObjects.firstNonNull(ci.side, Side.REVISION));
assertEquals(TimeUtil.roundToSecond(plc.getWrittenOn()),
TimeUtil.roundToSecond(ci.updated));
assertEquals(plc.getWrittenOn(), ci.updated);
assertEquals(plc.getRange().getStartLine(), ci.range.startLine);
assertEquals(plc.getRange().getStartCharacter(), ci.range.startCharacter);
assertEquals(plc.getRange().getEndLine(), ci.range.endLine);
assertEquals(plc.getRange().getEndCharacter(), ci.range.endCharacter);
assertThat((int) ci.line).isEqualTo(plc.getLine());
assertThat(MoreObjects.firstNonNull(ci.side, Side.REVISION))
.isEqualTo(plc.getSide() == 0 ? Side.PARENT : Side.REVISION);
assertThat(TimeUtil.roundToSecond(ci.updated))
.isEqualTo(TimeUtil.roundToSecond(plc.getWrittenOn()));
assertThat(ci.updated).isEqualTo(plc.getWrittenOn());
assertThat(ci.range.startLine).isEqualTo(plc.getRange().getStartLine());
assertThat(ci.range.startCharacter)
.isEqualTo(plc.getRange().getStartCharacter());
assertThat(ci.range.endLine).isEqualTo(plc.getRange().getEndLine());
assertThat(ci.range.endCharacter)
.isEqualTo(plc.getRange().getEndCharacter());
}
private static PatchLineComment newPatchLineComment(PatchSet.Id psId,