@Test
public void testValidateToken() {

    KeyStore keystore = Mockito.mock(KeyStore.class);
    Mockito.when(keystore.getPublicKey("sports", "api", "v0")).thenReturn(servicePublicKeyStringK0);
    InstanceZTSProvider provider = new InstanceZTSProvider();
    provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, keystore);
    StringBuilder errMsg = new StringBuilder(256);
    String token = "invalidtoken";
    assertFalse(provider.validateToken(token, "sports", "api", servicePublicKeyStringK0, errMsg));
    assertTrue(errMsg.toString().contains("Invalid token"));
    errMsg.setLength(0);
    PrincipalToken tokenToSign = new PrincipalToken.Builder("1", "sports", "api")
            .keyId("v0").salt("salt").issueTime(System.currentTimeMillis() / 1000)
            .expirationWindow(3600).build();
    tokenToSign.sign(servicePrivateKeyStringK0);
    errMsg.setLength(0);
    assertTrue(provider.validateToken(tokenToSign.getSignedToken(), "sports", "api",
            servicePublicKeyStringK0, errMsg));
    errMsg.setLength(0);
    assertFalse(provider.validateToken(tokenToSign.getSignedToken(), "sports", "ui",
            servicePublicKeyStringK0, errMsg));
    assertTrue(errMsg.toString().contains("service mismatch"));
    errMsg.setLength(0);
    assertFalse(provider.validateToken(tokenToSign.getSignedToken(), "weather", "api",
            servicePublicKeyStringK0, errMsg));
    assertTrue(errMsg.toString().contains("domain mismatch"));
    provider.close();
}