/** Test saveSingleTeamTag */
  @Test
  public void testSaveSingleTeamTag() {
    Client client = createClient();
    Group group = createGroup(client);
    List<Tag> tagList = createTagList(group, 1);
    Tag tag = tagList.get(0);
    Team team1 = createTeam(client);

    TeamTag teamTag = teamTagService.saveSingleTeamTag(team1, tag);
    TeamTag newTeamTag = teamTagService.reload(teamTag);
    assertThat("teamTagPage was saved", newTeamTag.getTag().getId(), is(tag.getId()));
  }
  /** Test the save method deletes items in between saves */
  @Test
  public void testSaveDeletesItems() {
    Client client = createClient();
    Group group = createGroup(client);

    // create 4 tags in a group
    List<Tag> tagListComplete = createTagList(group, 4);

    // split into two groups
    List<Tag> tagListFirstHalf = tagListComplete.subList(0, 2);
    List<Tag> tagListSecondHalf = tagListComplete.subList(2, 4);

    // make a team
    Team team1 = createTeam(client);

    // associate half of the tags to this team
    Set<Tag> tagSet = new HashSet<>();
    tagSet.addAll(tagListFirstHalf);
    teamTagService.save(team1, tagSet);

    Page<TeamTag> tags = teamTagService.findAllTeamTagsWithTeam(null, team1);
    List<Tag> savedTags = new ArrayList<>();
    for (TeamTag tag : tags) {
      savedTags.add(tag.getTag());
    }
    assertThat(
        "tags should be found",
        savedTags,
        hasItems(tagListFirstHalf.toArray(new Tag[tagListFirstHalf.size()])));

    // check that new tag save, overwrites the first save
    Set<Tag> overwriteTags = new HashSet<>();
    overwriteTags.addAll(tagListSecondHalf);
    teamTagService.save(team1, overwriteTags);

    // load the tags again
    savedTags = new ArrayList<>();
    tags = teamTagService.findAllTeamTagsWithTeam(null, team1);
    for (TeamTag tag : tags) {
      savedTags.add(tag.getTag());
    }
    assertThat(
        "first set should not be found",
        savedTags,
        not(hasItems(tagListFirstHalf.toArray(new Tag[tagListFirstHalf.size()]))));
    assertThat(
        "second set should be found",
        savedTags,
        hasItems(tagListSecondHalf.toArray(new Tag[tagListSecondHalf.size()])));
  }
  /** Save success */
  @Test
  public void save() {
    Client client = createClient();
    Group group = createGroup(client);

    Tag tag = createTagList(group, 1).get(0);
    Team team = createTeam(client);
    tag = tagService.reload(tag);

    TeamTag afterSaveTeamTag = teamTagService.saveSingleTeamTag(team, tag);
    assertThat("TeamTag was given an id", afterSaveTeamTag.getId(), is(notNullValue()));

    TeamTagSearchFilter searchFilter = new TeamTagSearchFilter();
    HashSet<Tag> tagSet = new HashSet<>();
    tagSet.add(tag);
    searchFilter.setTagSet(tagSet);
    Page<TeamTag> teamsWithTag = teamTagService.findTeamsWithTag(null, searchFilter);
    assertThat(
        "we can find the team tag by the team tag id", teamsWithTag, hasItem(afterSaveTeamTag));
  }