### Question:
TokenToParsedTokenCache implements Cache<OAuthToken> { public OAuthToken parseToken(String token) { TokenIssuer tokenIssuer = null; String clientId = null; String type = null; Long expiration = null; try { final DecodedJWT jwt = JWT.decode(token); final String issuer = jwt.getIssuer(); final Claim azpClaim = jwt.getClaim("azp"); if (azpClaim != null) { clientId = azpClaim.asString(); } else { LOG.warn(ReasonCode.APIM_2041.pattern(), token); } final Claim expirationClaim = jwt.getClaim("exp"); final Claim typeClaim = jwt.getClaim("typ"); type = typeClaim != null ? typeClaim.asString() : null; expiration = expirationClaim != null ? expirationClaim.asLong() : null; final Optional<TokenIssuer> optionalTokenIssuer = tokenIssuerStore.resolve(issuer); if (optionalTokenIssuer.isPresent()) { tokenIssuer = optionalTokenIssuer.get(); tokenIssuer.getVerifier().verify(jwt); return new OAuthToken(TokenStateEnum.VALID, tokenIssuer, token, clientId, expiration, type); } else { return new OAuthToken(TokenStateEnum.UNKNOWN_TOKEN_ISSUER, tokenIssuer, token, clientId, expiration, type); } } catch (SignatureVerificationException e) { LOG.info(ReasonCode.APIM_1010.pattern()); LOG.debug("Exception for failed signature is: ", e); return new OAuthToken(TokenStateEnum.INVALID_SIGNATURE, tokenIssuer, token, clientId, expiration, type); } catch (JWTVerificationException e) { LOG.debug("Unable to parse Token token='{}'", token, e); return new OAuthToken(TokenStateEnum.INVALID, tokenIssuer, token, clientId, expiration, type); } catch (Exception e) { LOG.error(ReasonCode.APIM_3023.pattern(), e); return null; } } TokenToParsedTokenCache(ApimAdapterConfig config, RestConfig restConfig, OfflineConfigurationCacheRepo offlineConfigurationCacheRepo); @Override OAuthToken get(String token); @Override long size(); @Override com.github.benmanes.caffeine.cache.Cache<?, ?> get(); OAuthToken parseToken(String token); void invalidateCache();  }### Answer:
@Test public void testTokenParsing_validToken() { OAuthToken oAuthToken = cache.parseToken(TokenGenerator.getInstance().generateToken(clientId, 30L, "Bearer")); assertNotNull("Result from TokenToParsedTokenCache.parseToken(...) should never be null.", oAuthToken); assertEquals("ClientId does not match.", clientId, oAuthToken.getClientId()); assertTrue("Token is expected to be valid.", oAuthToken.isValid()); assertFalse("Token is expected not to be expired.", oAuthToken.isExpired()); assertEquals("Token type should be BEARER.", "Bearer", oAuthToken.getType()); }
@Test public void testTokenParsing_withCorruptToken() { OAuthToken oAuthToken = cache.parseToken("some-invalid-string-token"); assertNotNull("Result from TokenToParsedTokenCache.parseToken(...) should never be null.", oAuthToken); assertNull("ClientId does not match.", oAuthToken.getClientId()); assertFalse("Token is expected to be invalid.", oAuthToken.isValid()); assertTrue("Token is expected to be expired.", oAuthToken.isExpired()); }
@Test public void testTokenParsing_withNonBearerToken() { OAuthToken oAuthToken = cache.parseToken(TokenGenerator.getInstance().generateToken(clientId, 30L, "Refresh")); assertNotNull("Result from TokenToParsedTokenCache.parseToken(...) should never be null.", oAuthToken); assertNotNull("ClientId does not match.", oAuthToken.getClientId()); assertFalse("Token is expected to be invalid.", oAuthToken.isValid()); assertFalse("Token is expected to be expired.", oAuthToken.isExpired()); }