@Test
  public void willSaveAndMoveToNextRow() {
    // Given: selected display and there is only one entry(no plural or last entry of plural)
    selectedTU = currentPageRows.get(0);
    when(display.getNewTargets()).thenReturn(NEW_TARGETS);
    when(display.getCachedTargets()).thenReturn(CACHED_TARGETS);
    when(display.getId()).thenReturn(selectedTU.getId());
    when(display.getEditors()).thenReturn(Lists.newArrayList(editor));
    presenter.setStatesForTesting(selectedTU.getId(), 0, display);

    // When:
    presenter.saveAsApprovedAndMoveNext(selectedTU.getId());

    // Then:
    verify(eventBus, atLeastOnce()).fireEvent(eventCaptor.capture());

    TransUnitSaveEvent saveEvent =
        TestFixture.extractFromEvents(eventCaptor.getAllValues(), TransUnitSaveEvent.class);
    assertThat(saveEvent.getTransUnitId(), equalTo(selectedTU.getId()));
    assertThat(saveEvent.getTargets(), Matchers.equalTo(NEW_TARGETS));
    assertThat(saveEvent.getStatus(), equalTo(ContentState.Translated));

    NavTransUnitEvent navEvent =
        TestFixture.extractFromEvents(eventCaptor.getAllValues(), NavTransUnitEvent.class);
    assertThat(navEvent.getRowType(), equalTo(NavTransUnitEvent.NavigationType.NextEntry));
  }
 private void saveFailure(String errorMessage) {
   queue.removeAllPending(event.getTransUnitId());
   targetContentsPresenter.setEditingState(
       event.getTransUnitId(), TargetContentsDisplay.EditingState.UNSAVED);
   eventBus.fireEvent(
       new NotificationEvent(
           NotificationEvent.Severity.Error,
           messages.notifyUpdateFailed("id " + id, errorMessage),
           goToRowLink));
 }
 private boolean stateHasNotChanged(TransUnitSaveEvent event) {
   TransUnit transUnit = navigationService.getByIdOrNull(event.getTransUnitId());
   if (transUnit == null) {
     return false;
   }
   Log.info(
       "id:"
           + transUnit.getId()
           + " old contents: "
           + transUnit.getTargets()
           + " state: "
           + transUnit.getStatus());
   return Objects.equal(transUnit.getStatus(), event.getAdjustedStatus())
       && Objects.equal(transUnit.getTargets(), event.getTargets());
 }
  @Override
  public void onTransUnitSave(TransUnitSaveEvent event) {
    TransUnitId idToSave = event.getTransUnitId();
    Log.info("TU save event: new[" + event.getTargets() + "] + old: [" + event.getOldContents());
    if (stateHasNotChanged(event)) {
      Log.info("NO OP! state has not changed for " + idToSave);
      return;
    }

    queue.push(event);
    if (queue.isSaving(idToSave)) {
      Log.info(idToSave + " has saving event. Put in queue and return.");
      return;
    }

    performSave(idToSave);
  }
  @Test
  public void canSaveAsFuzzy() {
    // Given: selected one trans unit with some new targets inputted
    selectedTU = currentPageRows.get(0);
    when(display.getNewTargets()).thenReturn(NEW_TARGETS);
    when(display.getCachedTargets()).thenReturn(CACHED_TARGETS);
    when(display.getId()).thenReturn(selectedTU.getId());
    when(display.getEditors()).thenReturn(Lists.newArrayList(editor));
    presenter.setStatesForTesting(selectedTU.getId(), 0, display);

    // When:
    presenter.saveAsFuzzy(selectedTU.getId());

    // Then:
    verify(eventBus, atLeastOnce()).fireEvent(eventCaptor.capture());
    TransUnitSaveEvent event =
        TestFixture.extractFromEvents(eventCaptor.getAllValues(), TransUnitSaveEvent.class);

    assertThat(event.getTransUnitId(), equalTo(selectedTU.getId()));
    assertThat(event.getTargets(), Matchers.equalTo(NEW_TARGETS));
    assertThat(event.getStatus(), equalTo(ContentState.NeedReview));
  }
  private void performSave(TransUnitId idToSave) {
    TransUnitSaveEvent forSaving = queue.getNextPendingForSaving(idToSave);
    if (forSaving == null) {
      Log.info("no pending save for " + idToSave);
      return;
    }

    targetContentsPresenter.setEditingState(idToSave, TargetContentsDisplay.EditingState.SAVING);

    UpdateTransUnit updateTransUnit =
        new UpdateTransUnit(
            new TransUnitUpdateRequest(
                idToSave,
                forSaving.getTargets(),
                forSaving.getAdjustedStatus(),
                forSaving.getVerNum(),
                sourceType),
            TransUnitUpdated.UpdateType.WebEditorSave);
    Log.info("about to save translation: " + updateTransUnit);
    dispatcher.execute(
        updateTransUnit,
        new UpdateTransUnitCallback(
            forSaving, documentListPresenter.getCurrentDocument(), idToSave));
  }