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 @Override
public RestView<RevisionResource> list() { public ListComments list() {
return list; return list;
} }

View File

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

View File

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