@Test
public void testRoleAuthority() throws IOException, CryptoException {

    RoleAuthority rollAuthority = new RoleAuthority();
    KeyStore keyStore = new KeyStoreMock();
    rollAuthority.setKeyStore(keyStore);
    assertEquals(rollAuthority.getDomain(), "sys.auth");
    assertEquals(rollAuthority.getHeader(), "Athenz-Role-Auth");
    // Add some roles
    List<String> roles = new ArrayList<>();
    roles.add("storage.tenant.weather.updater");
    roles.add("fantasy.tenant.sports.admin");
    roles.add("fantasy.tenant.sports.reader");
    roles.add("fantasy.tenant.sports.writer");
    roles.add("fantasy.tenant.sports.scanner");
    // Create and sign token with no key version
    RoleToken rollToken = new RoleToken.Builder(rolVersion, svcDomain, roles)
            .salt(salt).ip("127.0.0.1").expirationWindow(expirationTime)
            .principal("coretech.storage").build();
    rollToken.sign(ztsPrivateKeyStringK0);
    StringBuilder errMsg = new StringBuilder();
    Principal principal = rollAuthority.authenticate(rollToken.getSignedToken(),
            "127.0.0.1", "GET", errMsg);
    assertNotNull(principal);
    assertNotNull(principal.getAuthority());
    assertEquals(principal.getCredentials(),
            rollToken.getSignedToken());
    assertEquals(principal.getDomain(), rollToken.getDomain());
    principal = rollAuthority.authenticate(rollToken.getSignedToken(),
            "127.0.0.1", "GET", null);
    assertNotNull(principal);
    List<String> rolesToValidate = principal.getRoles();
    assertEquals(rolesToValidate.size(), roles.size());
    assertEquals(rolesToValidate, roles);
    // Create and sign token with keyVersion = 0
    rollToken = new RoleToken.Builder(rolVersion, svcDomain, roles)
        .salt(salt).ip("127.0.0.1").expirationWindow(expirationTime)
        .principal("coretech.storage").keyId(testKeyVersionK0).build();
    rollToken.sign(ztsPrivateKeyStringK0);
    principal = rollAuthority.authenticate(rollToken.getSignedToken(),
            "127.0.0.1", "GET", errMsg);
    assertNotNull(principal);
    assertEquals(principal.getCredentials(),
            rollToken.getSignedToken());
    
    // Create and sign token with keyVersion = 1
    rollToken = new RoleToken.Builder(rolVersion, svcDomain, roles)
        .salt(salt).ip("127.0.0.1").expirationWindow(expirationTime)
        .principal("coretech.storage").keyId(testKeyVersionK1).build();
    rollToken.sign(ztsPrivateKeyStringK1);
    principal = rollAuthority.authenticate(rollToken.getSignedToken(),
            "127.0.0.1", "GET", errMsg);
    assertNotNull(principal);
    assertEquals(principal.getCredentials(),
            rollToken.getSignedToken());
}