/** Save success */
  @Test
  public void saveTagsInSameGroup() {
    Client client = createClient();
    Group group1 = createGroup(client);
    Group group2 = createGroup(client);

    List<Tag> tagList = createTagList(group1, 2);
    List<Tag> tagList2 = createTagList(group2, 1);
    Tag tag1 = tagList.get(0);
    Tag tag2 = tagList.get(1);
    Tag tag3 = tagList2.get(0);

    Team team1 = createTeam(client);
    Team team2 = createTeam(client);
    Team team3 = createTeam(client);

    TeamTag afterSaveTeamTag1 = teamTagService.saveSingleTeamTag(team1, tag1);
    TeamTag afterSaveTeamTag2 = teamTagService.saveSingleTeamTag(team2, tag2);
    TeamTag afterSaveTeamTag3 = teamTagService.saveSingleTeamTag(team3, tag3);

    TeamTagSearchFilter searchFilter = new TeamTagSearchFilter();
    HashSet<Tag> tagSet = new HashSet<>();
    tagSet.add(tag1);
    tagSet.add(tag2);
    searchFilter.setTagSet(tagSet);
    Page<TeamTag> returnedTeamTags = teamTagService.findTeamsWithTag(null, searchFilter);
    assertThat("afterSaveTeamTag1 team is returned", returnedTeamTags, hasItem(afterSaveTeamTag1));
    assertThat("afterSaveTeamTag2 team is returned", returnedTeamTags, hasItem(afterSaveTeamTag2));
    assertThat(
        "afterSaveTeamTag3 team is not returned",
        returnedTeamTags,
        not(hasItem(afterSaveTeamTag3)));
  }
  /** 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()])));
  }
  /** Test saveSingleTeamTag with null tag */
  @Test
  public void testSaveSingleTeamTagWithNullTag() {
    Client client = createClient();
    Team team1 = createTeam(client);
    team1.setId(1l);

    assertThat("null was returned", teamTagService.saveSingleTeamTag(team1, null), is(nullValue()));
  }
  /** Test List tags by group id */
  @Test
  public void testSaveAndFind() {
    Client client = createClient();
    Group group = createGroup(client);

    List<Tag> tagList = createTagList(group, 2);

    Team team1 = createTeam(client);

    Set<Tag> tagSet = new HashSet<>();
    tagSet.add(tagList.get(0));
    tagSet.add(tagList.get(1));

    teamTagService.save(team1, tagSet);

    Page<TeamTag> teamTagPage = teamTagService.findAllTeamTagsWithTeam(null, team1);
    assertThat("teamTagPage has two teams", teamTagPage.getTotalElements(), is(2L));
  }
  /** Test saveSingleTeamTag with null team */
  @Test
  public void testSaveSingleTeamTagWitNullTeam() {
    Client client = createClient();
    Group group = createGroup(client);
    List<Tag> tagList = createTagList(group, 1);
    Tag tag = tagList.get(0);

    teamTagService.saveSingleTeamTag(null, tag);
  }
  /** 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));
  }