/** Test add follower. */
  @Test
  public void testAddFollower() {
    DomainGroup group = jpaGroupMapper.findByShortName("group1");

    Person fordp2 = jpaPersonMapper.findByAccountId("fordp2");
    Person csagan = jpaPersonMapper.findByAccountId("csagan");

    // Verify initial state
    assertFalse(jpaGroupMapper.isFollowing("fordp2", "group1"));
    assertFalse(jpaGroupMapper.isFollowing("csagan", "group1"));
    assertEquals(0, fordp2.getFollowingCount());
    assertEquals(0, fordp2.getGroupCount());
    assertEquals(0, csagan.getFollowingCount());
    assertEquals(1, csagan.getGroupCount());

    // ford wants to follow the group
    jpaGroupMapper.addFollower(fordp2.getId(), group.getId());

    getEntityManager().clear();

    fordp2 = jpaPersonMapper.findByAccountId("fordp2");
    csagan = jpaPersonMapper.findByAccountId("csagan");

    // verify new state
    assertTrue(jpaGroupMapper.isFollowing("fordp2", "group1"));
    // Test case insensitivity (everything should be lower cased by the mapper).
    assertTrue(jpaGroupMapper.isFollowing("fordp2", "Group1"));
    assertFalse(jpaGroupMapper.isFollowing("csagan", "group1"));
    assertEquals(0, fordp2.getFollowingCount());
    assertEquals(1, fordp2.getGroupCount());
    assertEquals(0, csagan.getFollowingCount());
    assertEquals(1, csagan.getGroupCount());

    // csagan wants to follow the group
    jpaGroupMapper.addFollower(csagan.getId(), group.getId());

    getEntityManager().clear();

    fordp2 = jpaPersonMapper.findByAccountId("fordp2");
    csagan = jpaPersonMapper.findByAccountId("csagan");

    // verify new state
    assertTrue(jpaGroupMapper.isFollowing("fordp2", "group1"));
    assertTrue(jpaGroupMapper.isFollowing("csagan", "group1"));
    // and verify csagan's counts after the change
    assertEquals(0, csagan.getFollowingCount());
    assertEquals(2, csagan.getGroupCount());
  }
  /** Test remove follower. */
  @Test
  @Transactional
  public void testRemoveFollower() {
    DomainGroup group = jpaGroupMapper.findByShortName("group1");
    Person burns = jpaPersonMapper.findByAccountId("mrburns");

    assertEquals(0, burns.getFollowingCount());
    assertEquals(1, burns.getGroupCount());
    assertEquals(3, group.getFollowersCount());
    assertTrue(jpaGroupMapper.isFollowing("mrburns", "group1"));

    jpaGroupMapper.removeFollower(burns.getId(), group.getId());
    getEntityManager().clear();
    group = jpaGroupMapper.findByShortName("group1");
    burns = jpaPersonMapper.findByAccountId("mrburns");

    assertEquals(0, burns.getFollowingCount());
    assertEquals(0, burns.getGroupCount());
    assertEquals(2, group.getFollowersCount());
    assertFalse(jpaGroupMapper.isFollowing("mrburns", "group1"));
  }
  /** Test addFollower when the person's already following the group. */
  @Test
  public void testAddFollowerWhenAlreadyFollowing() {
    final long personId = 99L;
    final long groupId = 1L;

    Person p = jpaPersonMapper.findById(personId);
    DomainGroup g = jpaGroupMapper.findById(groupId);

    int initialGroupsCount = p.getGroupCount();
    int initialFollowersCount = g.getFollowersCount();

    // invoke SUT
    jpaGroupMapper.addFollower(p.getId(), g.getId());

    // clear the entity manager, reload the entities, and assert the counts haven't changed
    getEntityManager().clear();

    p = jpaPersonMapper.findById(personId);
    g = jpaGroupMapper.findById(groupId);

    assertEquals(initialGroupsCount, p.getGroupCount());
    assertEquals(initialFollowersCount, g.getFollowersCount());
  }