private void loadRepositoriesInternal() {
    if (!authenticationProvider.isAuthenticated()) {
      addError(
          ModelValidationInfo.createWithResource(
              PROP_SERVER_NAME,
              TfPluginBundle.KEY_LOGIN_PAGE_ERRORS_TFS_CONNECT_FAILED,
              getServerName()));
      signOut();
      return;
    }

    setConnectionStatus(true);
    setLoading(true);
    setUserName(authenticationProvider.getAuthenticationInfo().getUserNameForDisplay());
    clearContexts();

    final URI serverUrl = UrlHelper.getBaseUri(getServerName());
    final ServerContext context =
        new ServerContextBuilder()
            .type(ServerContext.Type.TFS)
            .uri(serverUrl)
            .authentication(authenticationProvider.getAuthenticationInfo())
            .build();

    // successfully logged in and loading repositories, save this context if there is no active
    // context
    if (ServerContextManager.getInstance().getActiveContext() == ServerContext.NO_CONTEXT) {
      ServerContextManager.getInstance().setActiveContext(context);
    }

    getRepositoryProvider()
        .loadContexts(
            Collections.singletonList(context),
            ServerContextLookupOperation.ContextScope.REPOSITORY);
  }
  private void setupLocalTests(List<WorkItem> workItems) {
    MockitoAnnotations.initMocks(this);

    WorkItemReference ref = new WorkItemReference();
    ref.setId(1);
    List<WorkItemReference> workItemRefs = new ArrayList<WorkItemReference>();
    workItemRefs.add(ref);
    WorkItemQueryResult result = new WorkItemQueryResult();
    result.setWorkItems(workItemRefs);

    WorkItemTrackingHttpClient witHttpClient = Mockito.mock(WorkItemTrackingHttpClient.class);
    when(witHttpClient.queryByWiql(any(Wiql.class), any(UUID.class))).thenReturn(result);
    when(witHttpClient.getWorkItems(
            anyList(), anyList(), any(Date.class), any(WorkItemExpand.class)))
        .thenReturn(workItems);

    AuthenticationInfo authInfo = new AuthenticationInfo("user", "pass", "serverURI", "user");
    ServerContext authenticatedContext = Mockito.mock(ServerContext.class);
    when(authenticatedContext.getWitHttpClient()).thenReturn(witHttpClient);
    when(authenticatedContext.getTeamProjectReference()).thenReturn(new TeamProjectReference());
    when(authenticatedContext.getGitRepository()).thenReturn(new GitRepository());

    serverContextManager = Mockito.mock(ServerContextManager.class);
    when(serverContextManager.getAuthenticatedContext(anyString(), anyBoolean()))
        .thenReturn(authenticatedContext);
    when(serverContextManager.getUpdatedContext(anyString(), anyBoolean()))
        .thenReturn(authenticatedContext);
    when(serverContextManager.createContextFromGitRemoteUrl(anyString(), anyBoolean()))
        .thenReturn(authenticatedContext);

    PowerMockito.mockStatic(ServerContextManager.class);
    when(ServerContextManager.getInstance()).thenReturn(serverContextManager);
  }
 private ServerContextState[] getServerContextStates() {
   if (!serverContextsRestored) {
     // return the same state that we loaded
     return state.serverContexts;
   } else {
     final Collection<ServerContext> serverContexts =
         ServerContextManager.getInstance().getAllServerContexts();
     final List<ServerContextState> contextStates = new ArrayList<ServerContextState>();
     for (ServerContext context : serverContexts) {
       contextStates.add(new ServerContextState(context));
     }
     return contextStates.toArray(new ServerContextState[contextStates.size()]);
   }
 }
  public TfsCheckoutPageModel(final CheckoutModel checkoutModel) {
    super(checkoutModel, ServerContextTableModel.TFS_REPO_COLUMNS);

    setConnectionStatus(false);
    setAuthenticating(false);

    // check to see if the activeContext is a TFS context, if so, use it
    final ServerContext activeContext = ServerContextManager.getInstance().getActiveTfsContext();
    if (ServerContext.NO_CONTEXT != activeContext) {
      setServerNameInternal(activeContext.getUri().toString());
      setAuthenticationProvider(
          new TfsAuthenticationProvider(activeContext.getAuthenticationInfo()));
      loadRepositories();
    } else {
      setAuthenticationProvider(new TfsAuthenticationProvider());
    }
  }
  /**
   * Get a fully authenticated context from the provided git remote url. Note that if a context does
   * not exist, one will be created and the user will be prompted if necessary. Run this on a
   * background thread.
   */
  public ServerContext getAuthenticatedContext(
      String gitRemoteUrl, String patDescription, boolean setAsActiveContext) {
    // get context from builder, create PAT if needed, and store in active context
    ServerContext context = createContextFromRemoteUrl(gitRemoteUrl);
    if (context != null) {
      if (context.getType() == ServerContext.Type.VSO_DEPLOYMENT) {
        // Generate a PAT and get the new context
        final ServerContext newContext =
            createVsoContext(context, VsoAuthenticationProvider.getInstance(), patDescription);
        if (newContext != null) {
          context = newContext;
        } else {
          logger.error("Unable to create PAT token");
        }
      }

      if (setAsActiveContext) {
        // Set the active context for later use
        ServerContextManager.getInstance().setActiveContext(context);
      }
    }

    return context;
  }