Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz978666 - retrofit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jul 18, 2013
1 parent 9fd7fb7 commit 74264e1
Showing 1 changed file with 93 additions and 20 deletions.
Expand Up @@ -3,34 +3,38 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.List;

import net.customware.gwt.presenter.client.EventBus;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.ContentState;
import org.zanata.webtrans.client.events.CopyDataToEditorEvent;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
import org.zanata.webtrans.client.service.GetTransUnitActionContextHolder;
import org.zanata.webtrans.client.ui.TranslationHistoryDisplay;
import org.zanata.webtrans.shared.model.ComparableByDate;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.ReviewComment;
import org.zanata.webtrans.shared.model.ReviewCommentId;
import org.zanata.webtrans.shared.model.TransHistoryItem;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.rpc.AddReviewCommentAction;
import org.zanata.webtrans.shared.rpc.AddReviewCommentResult;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryAction;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryResult;

Expand All @@ -54,11 +58,6 @@ public class TranslationHistoryPresenterTest
private CachingDispatchAsync dispatcher;
@Mock
private WebTransMessages messages;
@Mock
private TransHistorySelectionModel selectionModel;
@Mock
private TransHistoryDataProvider dataProvider;

@Mock
private TargetContentsPresenter targetContentsPresenter;
@Mock
Expand All @@ -70,7 +69,7 @@ public class TranslationHistoryPresenterTest
@Captor
private ArgumentCaptor<AsyncCallback<GetTranslationHistoryResult>> resultCaptor;
private final TransUnitId transUnitId = new TransUnitId(1L);
@Mock
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private GetTransUnitActionContextHolder contextHolder;

@BeforeMethod
Expand All @@ -80,7 +79,6 @@ public void beforeMethod()
presenter = new TranslationHistoryPresenter(display, eventBus, dispatcher, messages, contextHolder);
presenter.setCurrentValueHolder(targetContentsPresenter);

when(dataProvider.getList()).thenReturn(Lists.<TransHistoryItem>newArrayList());
doNothing().when(dispatcher).execute(actionCaptor.capture(), resultCaptor.capture());
}

Expand All @@ -99,9 +97,7 @@ public void willNotifyErrorAndHideTranslationHistoryOnFailure()
presenter.showTranslationHistory(transUnitId);

// Then:
verify(dataProvider).setLoading(true);
verify(display).setTitle("translation history");
verify(selectionModel).clear();
verify(display).resetView();
verify(display).center();
assertThat(actionCaptor.getValue().getTransUnitId(), Matchers.equalTo(transUnitId));
Expand Down Expand Up @@ -130,15 +126,12 @@ public void willShowTranslationHistoryOnSuccess()
presenter.showTranslationHistory(transUnitId);

// Then:on success
verify(dataProvider).setLoading(true);
verify(display).setTitle("translation history");
verify(selectionModel).clear();
verify(display).resetView();
verify(display).center();
AsyncCallback<GetTranslationHistoryResult> result = resultCaptor.getValue();
result.onSuccess(createTranslationHistory(latest, historyItem));
MatcherAssert.assertThat(dataProvider.getList(), Matchers.contains(latest, historyItem));
verify(dataProvider).setLoading(false);
verify(display).setData(Lists.<ComparableByDate>newArrayList(latest, historyItem));
}

@Test
Expand All @@ -159,12 +152,92 @@ public void willShowTranslationHistoryWithUnsavedValueOnSuccess()
// Then: on success
AsyncCallback<GetTranslationHistoryResult> result = resultCaptor.getValue();
result.onSuccess(createTranslationHistory(latest, historyItem));
MatcherAssert.assertThat(dataProvider.getList(), Matchers.hasSize(3));
MatcherAssert.assertThat(dataProvider.getList().get(0).getVersionNum(), Matchers.equalTo("unsaved"));

ArgumentCaptor<List> listArgumentCaptor = ArgumentCaptor.forClass(List.class);
verify(display).setData(listArgumentCaptor.capture());
assertThat((List<ComparableByDate>) listArgumentCaptor.getValue(), Matchers.<ComparableByDate>hasSize(3));
}

private static GetTranslationHistoryResult createTranslationHistory(TransHistoryItem latest, TransHistoryItem... historyItems)
{
return new GetTranslationHistoryResult(Lists.newArrayList(historyItems), latest, Lists.<ReviewComment>newArrayList());
}

@Test
public void canCopyIntoEditor()
{
List<String> contents = Lists.newArrayList("a");

presenter.copyIntoEditor(contents);

ArgumentCaptor<CopyDataToEditorEvent> eventCaptor = ArgumentCaptor.forClass(CopyDataToEditorEvent.class);
verify(eventBus).fireEvent(eventCaptor.capture());

assertThat(eventCaptor.getValue().getTargetResult(), Matchers.equalTo(contents));
}

@Test
public void testAddComment() throws Exception
{
when(contextHolder.getContext().getDocument().getId()).thenReturn(new DocumentId(1L, "doc"));
ArgumentCaptor<AddReviewCommentAction> actionCaptor = ArgumentCaptor.forClass(AddReviewCommentAction.class);
ArgumentCaptor<AsyncCallback> resultCaptor = ArgumentCaptor.forClass(AsyncCallback.class);

presenter.addComment("some comment");

verify(dispatcher).execute(actionCaptor.capture(), resultCaptor.capture());
assertThat(actionCaptor.getValue().getContent(), Matchers.equalTo("some comment"));

AsyncCallback<AddReviewCommentResult> callback = resultCaptor.getValue();
AddReviewCommentResult result = new AddReviewCommentResult(new ReviewComment());
callback.onSuccess(result);

verify(display).addCommentToList(result.getComment());
verify(display).clearInput();
}

@Test
public void canDisplayEntriesInOrder()
{
// no unsaved content
when(targetContentsPresenter.getNewTargets()).thenReturn(Lists.newArrayList("a"));
long now = new Date().getTime();
// items in time order
TransHistoryItem latest = new TransHistoryItem("5", Lists.newArrayList("a"), ContentState.Approved, "admin",
new Date(now - 1000));
TransHistoryItem item = new TransHistoryItem("4", Lists.newArrayList("a"), ContentState.Approved, "admin",
new Date(now - 2000));
ReviewComment comment = new ReviewComment(new ReviewCommentId(1L), "comment", "admin", new Date(now), 5);

presenter.displayEntries(latest, Lists.newArrayList(item), Lists.newArrayList(comment));

ArgumentCaptor<List> listArgumentCaptor = ArgumentCaptor.forClass(List.class);
verify(display).setData(listArgumentCaptor.capture());
List<ComparableByDate> result = (List<ComparableByDate>) listArgumentCaptor.getValue();
assertThat(result, Matchers.<ComparableByDate>contains(comment, latest, item));
}

@Test
public void onCompareClickedWhenThePairIsNotFull()
{
// the pair is empty initially
presenter.compareClicked(historyItem("5"));

verify(display).disableComparison();
}

@Test
public void onCompareClickedWhichMakesTwoItems()
{
when(messages.translationHistoryComparison("5", "4")).thenReturn("comparison of 5 and 4");
TransHistoryItem one = historyItem("5");
presenter.compareClicked(one);
verify(display).disableComparison();

TransHistoryItem two = historyItem("4");
presenter.compareClicked(two);
verify(display).showDiff(one, two, "comparison of 5 and 4");

}

}

0 comments on commit 74264e1

Please sign in to comment.