PushToRemotePresenter implements PushToRemoteView.ActionDelegate { void updateRemoteBranches() { getBranchesForCurrentProject(LIST_REMOTE, new AsyncCallback<List<Branch>>() { @Override public void onSuccess(final List<Branch> result) { getUpstreamBranch(new AsyncCallback<Branch>() { @Override public void onSuccess(Branch upstream) { BranchFilterByRemote remoteRefsHandler = new BranchFilterByRemote(view.getRepository()); final List<String> remoteBranches = branchSearcher.getRemoteBranchesToDisplay(remoteRefsHandler, result); String selectedRemoteBranch = null; if (upstream != null && upstream.isRemote() && remoteRefsHandler.isLinkedTo(upstream)) { String simpleUpstreamName = remoteRefsHandler.getBranchNameWithoutRefs(upstream); if (!remoteBranches.contains(simpleUpstreamName)) { remoteBranches.add(simpleUpstreamName); } selectedRemoteBranch = simpleUpstreamName; } final String currentBranch = view.getLocalBranch(); if (!remoteBranches.contains(currentBranch)) { remoteBranches.add(currentBranch); } if (selectedRemoteBranch == null) { selectedRemoteBranch = currentBranch; } view.setRemoteBranches(remoteBranches); view.selectRemoteBranch(selectedRemoteBranch); } @Override public void onFailure(Throwable caught) { console.printError(constant.failedGettingConfig()); notificationManager.showError(constant.failedGettingConfig()); } }); } @Override public void onFailure(Throwable exception) { String errorMessage = exception.getMessage() != null ? exception.getMessage() : constant.remoteBranchesListFailed(); console.printError(errorMessage); notificationManager.showError(errorMessage); view.setEnablePushButton(false); } }); } @Inject  PushToRemotePresenter(DtoFactory dtoFactory,
                                 PushToRemoteView view,
                                 GitServiceClient service,
                                 GitOutputPartPresenter console,
                                 AppContext appContext,
                                 GitLocalizationConstant constant,
                                 NotificationManager notificationManager,
                                 DtoUnmarshallerFactory dtoUnmarshallerFactory,
                                 BranchSearcher branchSearcher); void showDialog(); @Override void onPushClicked(); @Override void onCancelClicked(); @Override void onLocalBranchChanged(); @Override void onRepositoryChanged();  }
@Test public void testShowListOfRemoteBranchesAndSelectUpstreamBranch() throws Exception { final List<Branch> remoteBranches = new ArrayList<>(); remoteBranches.add(remoteBranch); final String upstreamBranchName = "upstream"; Map<String, String> configs = new HashMap<>(); configs.put("branch." + view.getLocalBranch() + ".remote", REPOSITORY_NAME); configs.put("branch." + view.getLocalBranch() + ".merge", upstreamBranchName); Branch upstream = mock(Branch.class); when(dtoFactory.createDto(eq(Branch.class))).thenReturn(upstream); when(upstream.withActive(anyBoolean())).thenReturn(upstream); when(upstream.withRemote(anyBoolean())).thenReturn(upstream); when(upstream.withDisplayName(anyString())).thenReturn(upstream); when(upstream.withName(anyString())).thenReturn(upstream); when(upstream.getDisplayName()).thenReturn(upstreamBranchName); when(upstream.getName()).thenReturn("refs/remotes/" + REPOSITORY_NAME + "/" + upstreamBranchName); when(upstream.isRemote()).thenReturn(true); List<String> list = new ArrayList<>(); list.add(remoteBranch.getDisplayName()); when(branchSearcher.getRemoteBranchesToDisplay((BranchFilterByRemote)anyObject(), (List<Branch>)anyObject())).thenReturn(list); presenter.updateRemoteBranches(); verify(service).branchList((ProjectDescriptor)anyObject(), eq("r"), asyncRequestCallbackArrayBranchCaptor.capture()); AsyncRequestCallback<List<Branch>> value = asyncRequestCallbackArrayBranchCaptor.getValue(); Method onSuccessRemotes = GwtReflectionUtils.getMethod(value.getClass(), "onSuccess"); onSuccessRemotes.invoke(value, remoteBranches); verify(service).config((ProjectDescriptor)anyObject(), anyListOf(String.class), asyncRequestCallbackMapCaptor.capture()); AsyncRequestCallback<Map<String, String>> mapCallback = asyncRequestCallbackMapCaptor.getValue(); Method onSuccessConfig = GwtReflectionUtils.getMethod(mapCallback.getClass(), "onSuccess"); onSuccessConfig.invoke(mapCallback, configs); verify(branchSearcher).getRemoteBranchesToDisplay((BranchFilterByRemote)anyObject(), eq(remoteBranches)); verify(view).setRemoteBranches((List<String>)anyObject()); verify(view).selectRemoteBranch(upstreamBranchName); }
@Test public void testShowListOfRemoteBranchesAndSelectActiveLocalBranch() throws Exception { final List<Branch> remoteBranches = new ArrayList<>(); remoteBranches.add(remoteBranch); List<String> list = new ArrayList<>(); list.add(remoteBranch.getDisplayName()); when(branchSearcher.getRemoteBranchesToDisplay((BranchFilterByRemote)anyObject(), (List<Branch>)anyObject())).thenReturn(list); presenter.updateRemoteBranches(); verify(service).branchList((ProjectDescriptor)anyObject(), eq("r"), asyncRequestCallbackArrayBranchCaptor.capture()); AsyncRequestCallback<List<Branch>> value = asyncRequestCallbackArrayBranchCaptor.getValue(); Method onSuccessRemotes = GwtReflectionUtils.getMethod(value.getClass(), "onSuccess"); onSuccessRemotes.invoke(value, remoteBranches); verify(service).config((ProjectDescriptor)anyObject(), anyListOf(String.class), asyncRequestCallbackMapCaptor.capture()); AsyncRequestCallback<Map<String, String>> mapCallback = asyncRequestCallbackMapCaptor.getValue(); Method onSuccessConfig = GwtReflectionUtils.getMethod(mapCallback.getClass(), "onSuccess"); onSuccessConfig.invoke(mapCallback, new HashMap()); verify(branchSearcher).getRemoteBranchesToDisplay((BranchFilterByRemote)anyObject(), eq(remoteBranches)); verify(view).setRemoteBranches((List<String>)anyObject()); verify(view).selectRemoteBranch(LOCAL_BRANCH); }
@Test public void testShowErrorNotificationWhenListOfRemoteBranchesFailedToLoad() throws Exception { presenter.updateRemoteBranches(); verify(service).branchList((ProjectDescriptor)anyObject(), anyString(), asyncRequestCallbackArrayBranchCaptor.capture()); AsyncRequestCallback<List<Branch>> value = asyncRequestCallbackArrayBranchCaptor.getValue(); Method onFailureRemotes = GwtReflectionUtils.getMethod(value.getClass(), "onFailure"); onFailureRemotes.invoke(value, mock(Throwable.class)); verify(constant,times(2)).remoteBranchesListFailed(); verify(console).printError(anyString()); verify(notificationManager).showError(anyString()); }
@Test public void testShowErrorNotificationWhenUpstreanBranchFailedToLoad() throws Exception { presenter.updateRemoteBranches(); verify(service).branchList((ProjectDescriptor)anyObject(), eq("r"), asyncRequestCallbackArrayBranchCaptor.capture()); AsyncRequestCallback<List<Branch>> remoteBranches = asyncRequestCallbackArrayBranchCaptor.getValue(); Method onSuccess = GwtReflectionUtils.getMethod(remoteBranches.getClass(), "onSuccess"); onSuccess.invoke(remoteBranches, new ArrayList<>()); verify(service).config((ProjectDescriptor)anyObject(), anyListOf(String.class), asyncRequestCallbackMapCaptor.capture()); AsyncRequestCallback<Map<String, String>> mapCallback = asyncRequestCallbackMapCaptor.getValue(); Method onFailureConfig = GwtReflectionUtils.getMethod(mapCallback.getClass(), "onFailure"); onFailureConfig.invoke(mapCallback, mock(Throwable.class)); verify(constant, times(2)).failedGettingConfig(); verify(console).printError(anyString()); verify(notificationManager).showError(anyString()); }