ResetFilesPresenter implements ResetFilesView.ActionDelegate { public void showDialog() { project = appContext.getCurrentProject(); service.status(project.getRootProject(), new AsyncRequestCallback<Status>(dtoUnmarshallerFactory.newUnmarshaller(Status.class)) { @Override protected void onSuccess(Status result) { if (result.isClean()) { dialogFactory.createMessageDialog(constant.messagesWarningTitle(), constant.indexIsEmpty(), null).show(); return; } List<IndexFile> values = new ArrayList<>(); List<String> valuesTmp = new ArrayList<>(); valuesTmp.addAll(result.getAdded()); valuesTmp.addAll(result.getChanged()); valuesTmp.addAll(result.getRemoved()); for (String value : valuesTmp) { IndexFile indexFile = dtoFactory.createDto(IndexFile.class); indexFile.setPath(value); indexFile.setIndexed(true); values.add(indexFile); } if (values.isEmpty()) { dialogFactory.createMessageDialog(constant.messagesWarningTitle(), constant.indexIsEmpty(), null).show(); return; } view.setIndexedFiles(values); indexedFiles = values; view.showDialog(); } @Override protected void onFailure(Throwable exception) { String errorMassage = exception.getMessage() != null ? exception.getMessage() : constant.statusFailed(); console.printError(errorMassage); Notification notification = new Notification(errorMassage, ERROR); notificationManager.showNotification(notification); } }); } @Inject  ResetFilesPresenter(ResetFilesView view,
                               GitServiceClient service,
                               AppContext appContext,
                               GitOutputPartPresenter console,
                               GitLocalizationConstant constant,
                               NotificationManager notificationManager,
                               DtoFactory dtoFactory,
                               DtoUnmarshallerFactory dtoUnmarshallerFactory,
                               DialogFactory dialogFactory); void showDialog(); @Override void onResetClicked(); @Override void onCancelClicked();  }
@Test public void testShowDialogWhenStatusRequestIsSuccessful() throws Exception { final Status status = mock(Status.class); List<String> changes = new ArrayList<String>(); changes.add("Change"); when(status.getAdded()).thenReturn(changes); when(status.getChanged()).thenReturn(changes); when(status.getRemoved()).thenReturn(changes); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<Status> callback = (AsyncRequestCallback<Status>)arguments[1]; Method onSuccess = GwtReflectionUtils.getMethod(callback.getClass(), "onSuccess"); onSuccess.invoke(callback, status); return callback; } }).when(service).status((ProjectDescriptor)anyObject(), (AsyncRequestCallback<Status>)anyObject()); presenter.showDialog(); verify(appContext).getCurrentProject(); verify(service).status(eq(rootProjectDescriptor), (AsyncRequestCallback<Status>)anyObject()); verify(view).setIndexedFiles((List<IndexFile>)anyObject()); verify(view).showDialog(); }
@Test public void testShowDialogWhenStatusRequestIsSuccessfulButIndexIsEmpty() throws Exception { MessageDialog messageDialog = mock(MessageDialog.class); when(constant.messagesWarningTitle()).thenReturn("Warning"); when(constant.indexIsEmpty()).thenReturn("Index is Empty"); when(dialogFactory.createMessageDialog(anyString(), anyString(), (ConfirmCallback)anyObject())) .thenReturn(messageDialog); final Status status = mock(Status.class); List<String> changes = new ArrayList<>(); when(status.getAdded()).thenReturn(changes); when(status.getChanged()).thenReturn(changes); when(status.getRemoved()).thenReturn(changes); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<Status> callback = (AsyncRequestCallback<Status>)arguments[1]; Method onSuccess = GwtReflectionUtils.getMethod(callback.getClass(), "onSuccess"); onSuccess.invoke(callback, status); return callback; } }).when(service).status((ProjectDescriptor)anyObject(), (AsyncRequestCallback<Status>)anyObject()); presenter.showDialog(); verify(appContext).getCurrentProject(); verify(service).status(eq(rootProjectDescriptor), (AsyncRequestCallback<Status>)anyObject()); verify(dialogFactory).createMessageDialog(eq("Warning"), eq("Index is Empty"), (ConfirmCallback)anyObject()); verify(view, never()).setIndexedFiles((List<IndexFile>)anyObject()); verify(view, never()).showDialog(); }
@Test public void testShowDialogWhenStatusRequestIsFailed() throws Exception { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<Status> callback = (AsyncRequestCallback<Status>)arguments[1]; Method onFailure = GwtReflectionUtils.getMethod(callback.getClass(), "onFailure"); onFailure.invoke(callback, mock(Throwable.class)); return callback; } }).when(service).status((ProjectDescriptor)anyObject(), (AsyncRequestCallback<Status>)anyObject()); presenter.showDialog(); verify(appContext).getCurrentProject(); verify(service).status(eq(rootProjectDescriptor), (AsyncRequestCallback<Status>)anyObject()); verify(console).printError(anyString()); verify(notificationManager).showNotification((Notification)anyObject()); verify(constant).statusFailed(); }