public void testRemoveConnection() throws Exception {
    connectionContext.setSecurityContext(new StubSecurityContext());

    authBroker.removeConnection(connectionContext, connectionInfo, new Throwable());

    assertEquals(
        "removeConnection should clear ConnectionContext.",
        null,
        connectionContext.getSecurityContext());

    assertEquals(
        "Incorrect number of calls to underlying broker were made.",
        1,
        receiveBroker.removeConnectionData.size());
  }
  public void testAddConnectionSuccess() {
    String dnUserName = "******";

    HashSet<String> userNames = new HashSet<String>();
    userNames.add(dnUserName);

    HashSet<String> groupNames = new HashSet<String>();
    groupNames.add("testGroup1");
    groupNames.add("testGroup2");
    groupNames.add("tesetGroup3");

    setConfiguration(userNames, groupNames, true);

    try {
      authBroker.addConnection(connectionContext, connectionInfo);
    } catch (Exception e) {
      fail("Call to addConnection failed: " + e.getMessage());
    }

    assertEquals(
        "Number of addConnection calls to underlying Broker must match number of calls made to "
            + "AuthenticationBroker.",
        1,
        receiveBroker.addConnectionData.size());

    ConnectionContext receivedContext =
        receiveBroker.addConnectionData.getFirst().connectionContext;

    assertEquals(
        "The SecurityContext's userName must be set to that of the UserPrincipal.",
        dnUserName,
        receivedContext.getSecurityContext().getUserName());

    Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals();

    for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) {
      Principal currentPrincipal = iter.next();

      if (currentPrincipal instanceof UserPrincipal) {
        if (userNames.remove(currentPrincipal.getName())) {
          // Nothing, we did good.
        } else {
          // Found an unknown userName.
          fail("Unknown UserPrincipal found");
        }
      } else if (currentPrincipal instanceof GroupPrincipal) {
        if (groupNames.remove(currentPrincipal.getName())) {
          // Nothing, we did good.
        } else {
          fail("Unknown GroupPrincipal found.");
        }
      } else {
        fail("Unexpected Principal subclass found.");
      }
    }

    if (!userNames.isEmpty()) {
      fail("Some usernames were not added as UserPrincipals");
    }

    if (!groupNames.isEmpty()) {
      fail("Some group names were not added as GroupPrincipals");
    }
  }