MergePresenter implements MergeView.ActionDelegate { public void showDialog() { ProjectDescriptor project = appContext.getCurrentProject().getRootProject(); selectedReference = null; view.setEnableMergeButton(false); service.branchList(project, LIST_LOCAL, new AsyncRequestCallback<List<Branch>>(dtoUnmarshallerFactory.newListUnmarshaller(Branch.class)) { @Override protected void onSuccess(List<Branch> result) { List<Reference> references = new ArrayList<>(); for (Branch branch : result) { if (!branch.isActive()) { Reference reference = new Reference(branch.getName(), branch.getDisplayName(), LOCAL_BRANCH); references.add(reference); } } view.setLocalBranches(references); } @Override protected void onFailure(Throwable exception) { console.printError(exception.getMessage()); Notification notification = new Notification(exception.getMessage(), ERROR); notificationManager.showNotification(notification); } }); service.branchList(project, LIST_REMOTE, new AsyncRequestCallback<List<Branch>>(dtoUnmarshallerFactory.newListUnmarshaller(Branch.class)) { @Override protected void onSuccess(List<Branch> result) { List<Reference> references = new ArrayList<>(); for (Branch branch : result) { if (!branch.isActive()) { Reference reference = new Reference(branch.getName(), branch.getDisplayName(), REMOTE_BRANCH); references.add(reference); } } view.setRemoteBranches(references); } @Override protected void onFailure(Throwable exception) { console.printError(exception.getMessage()); Notification notification = new Notification(exception.getMessage(), ERROR); notificationManager.showNotification(notification); } }); view.showDialog(); } @Inject  MergePresenter(MergeView view,
                          EventBus eventBus,
                          EditorAgent editorAgent,
                          GitServiceClient service,
                          GitOutputPartPresenter console,
                          GitLocalizationConstant constant,
                          AppContext appContext,
                          NotificationManager notificationManager,
                          DtoUnmarshallerFactory dtoUnmarshallerFactory,
                          ProjectExplorerPresenter projectExplorer); void showDialog(); @Override void onCancelClicked(); @Override void onMergeClicked(); @Override void onReferenceSelected(@NotNull Reference reference); static final String LOCAL_BRANCHES_TITLE; static final String REMOTE_BRANCHES_TITLE; }
@Test public void testShowDialogWhenAllOperationsAreSuccessful() throws Exception { final List<Branch> branches = new ArrayList<>(); branches.add(mock(Branch.class)); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<List<Branch>> callback = (AsyncRequestCallback<List<Branch>>)arguments[2]; Method onSuccess = GwtReflectionUtils.getMethod(callback.getClass(), "onSuccess"); onSuccess.invoke(callback, branches); return callback; }).when(service).branchList((ProjectDescriptor)anyObject(), eq(LIST_LOCAL), (AsyncRequestCallback<List<Branch>>)anyObject()); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<List<Branch>> callback = (AsyncRequestCallback<List<Branch>>)arguments[2]; Method onSuccess = GwtReflectionUtils.getMethod(callback.getClass(), "onSuccess"); onSuccess.invoke(callback, branches); return callback; }).when(service).branchList((ProjectDescriptor)anyObject(), eq(LIST_REMOTE), (AsyncRequestCallback<List<Branch>>)anyObject()); presenter.showDialog(); verify(appContext).getCurrentProject(); verify(view).setEnableMergeButton(eq(DISABLE_BUTTON)); verify(view).showDialog(); verify(service).branchList(eq(rootProjectDescriptor), eq(LIST_LOCAL), (AsyncRequestCallback<List<Branch>>)anyObject()); verify(service).branchList(eq(rootProjectDescriptor), eq(LIST_REMOTE), (AsyncRequestCallback<List<Branch>>)anyObject()); verify(view).setRemoteBranches((List<Reference>)anyObject()); verify(view).setLocalBranches((List<Reference>)anyObject()); verify(console, never()).printError(anyString()); verify(notificationManager, never()).showNotification((Notification) anyObject()); }
@Test public void testShowDialogWhenAllOperationsAreFailed() throws Exception { doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<List<Branch>> callback = (AsyncRequestCallback<List<Branch>>)arguments[2]; Method onFailure = GwtReflectionUtils.getMethod(callback.getClass(), "onFailure"); onFailure.invoke(callback, mock(Throwable.class)); return callback; }).when(service).branchList((ProjectDescriptor) anyObject(), eq(LIST_LOCAL), (AsyncRequestCallback<List<Branch>>)anyObject()); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<List<Branch>> callback = (AsyncRequestCallback<List<Branch>>)arguments[2]; Method onFailure = GwtReflectionUtils.getMethod(callback.getClass(), "onFailure"); onFailure.invoke(callback, mock(Throwable.class)); return callback; }).when(service).branchList((ProjectDescriptor)anyObject(), eq(LIST_REMOTE), (AsyncRequestCallback<List<Branch>>)anyObject()); presenter.showDialog(); verify(service).branchList(eq(rootProjectDescriptor), eq(LIST_LOCAL), (AsyncRequestCallback<List<Branch>>)anyObject()); verify(service).branchList(eq(rootProjectDescriptor), eq(LIST_REMOTE), (AsyncRequestCallback<List<Branch>>)anyObject()); verify(console, times(2)).printError(anyString()); verify(notificationManager, times(2)).showNotification((Notification)anyObject()); }
@Test public void testDialogWhenListOfBranchesAreEmpty() throws Exception { final ArrayList<Reference> emptyReferenceList = new ArrayList<>(); final List<Branch> emptyBranchList = new ArrayList<>(); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<List<Branch>> callback = (AsyncRequestCallback<List<Branch>>)arguments[2]; Method onSuccess = GwtReflectionUtils.getMethod(callback.getClass(), "onSuccess"); onSuccess.invoke(callback, emptyBranchList); return callback; }).when(service).branchList((ProjectDescriptor)anyObject(), eq(LIST_LOCAL), (AsyncRequestCallback<List<Branch>>)anyObject()); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); AsyncRequestCallback<List<Branch>> callback = (AsyncRequestCallback<List<Branch>>)arguments[2]; Method onSuccess = GwtReflectionUtils.getMethod(callback.getClass(), "onSuccess"); onSuccess.invoke(callback, emptyBranchList); return callback; }).when(service).branchList((ProjectDescriptor) anyObject(), eq(LIST_REMOTE), (AsyncRequestCallback<List<Branch>>)anyObject()); presenter.showDialog(); verify(view).showDialog(); verify(view).setLocalBranches(eq(emptyReferenceList)); verify(view).setRemoteBranches(eq(emptyReferenceList)); }