/**
   * Creates group folders without applying the filter.
   *
   * <p>This is used to create the included folders after filtering.
   *
   * @param groups the folders
   */
  private void createUnfilteredFolders(Collection<GitLabGroupInfo> groups)
      throws ItemNameCollisionException, IOException {
    // get existing group folders
    Map<Integer, GroupFolderInfo> existingFolders = getUnfilteredFolders();

    // groups which item names collide with existing items
    List<String> collidedGroupPaths = new LinkedList<String>();

    for (GitLabGroupInfo group : groups) {
      try {
        // only create folders that don't already exist
        if (!existingFolders.containsKey(group.getId())) {
          createFolder(group);
        }
      } catch (ItemNameCollisionException e) {
        collidedGroupPaths.add(group.getPath());
      }
    }

    if (!collidedGroupPaths.isEmpty()) {
      throw new ItemNameCollisionException(
          "Cannot create folder(s) because items with the names "
              + join(collidedGroupPaths, ", ")
              + " already exist(s)");
    }
  }
 /**
  * Creates a group folder.
  *
  * <p>Does not check if a folder already exists for the GitLab group.
  *
  * @param group the group
  * @throws ItemNameCollisionException if an item name for a new folder already was in use
  * @throws IOException if saving to persistent storage failed
  */
 private void createFolder(GitLabGroupInfo group) throws ItemNameCollisionException, IOException {
   try {
     Folder folder = (Folder) itemGroup.createProject(folderDescriptor, group.getPath(), true);
     folder.addProperty(new GitLabFolderAuthorization(group.getId()));
   } catch (IllegalArgumentException e) {
     throw new ItemNameCollisionException(
         "Cannot create folder because an item with the name "
             + group.getPath()
             + " already exists");
   }
 }
  /** Gets a group. */
  @Test
  public void getGroup() throws Exception {
    // stub for expected request to get the group
    stubFor(
        get(urlEqualTo("/api/v3/groups/1?private_token=" + PRIVATE_TOKEN))
            .willReturn(aResponse().withStatus(200).withBodyFile("/api/v3/groups/1.json")));

    GitLabGroupInfo group = client.getGroup(1);
    assertThat(group.getId(), is(1));
    assertThat(group.getName(), is("Group Name"));
    assertThat(group.getPath(), is("groupname"));
  }
  /** Gets all groups for the authenticated user. */
  @Test
  public void getAllGroups() throws Exception {
    // stub for expected request to get all groups
    stubFor(
        get(urlEqualTo("/api/v3/groups?private_token=" + PRIVATE_TOKEN))
            .willReturn(aResponse().withStatus(200).withBodyFile("/api/v3/groups.json")));

    List<GitLabGroupInfo> groups = client.getGroups();

    assertThat(groups, hasSize(1));

    GitLabGroupInfo group = groups.get(0);
    assertThat(group.getId(), is(1));
    assertThat(group.getName(), is("Group Name"));
    assertThat(group.getPath(), is("groupname"));
  }
  /**
   * Returns group without a group folder.
   *
   * @param groups all groups
   * @return the available groups
   * @throws GitLabApiException if the connection against GitLab failed
   */
  public synchronized Collection<GitLabGroupInfo> getAvailableGroups(
      Collection<GitLabGroupInfo> groups) throws GitLabApiException {
    // filter excluded groups
    Collection<GitLabGroupInfo> availableGroups = filterGroups(groups);

    Map<Integer, GroupFolderInfo> folders = getUnfilteredFolders();
    Iterator<GitLabGroupInfo> iterator = groups.iterator();

    // remove groups having group folders
    while (iterator.hasNext()) {
      GitLabGroupInfo group = iterator.next();
      if (folders.containsKey(group.getId())) {
        iterator.remove();
      }
    }

    return availableGroups;
  }