### Question:
AmbariJwtAuthenticationFilter implements AmbariAuthenticationFilter { boolean validateAudiences(SignedJWT jwtToken) { boolean valid = false; try { List<String> tokenAudienceList = jwtToken.getJWTClaimsSet().getAudience(); JwtAuthenticationProperties jwtProperties = propertiesProvider.get(); List<String> audiences = (jwtProperties == null) ? null : jwtProperties.getAudiences(); if (audiences == null) { valid = true; } else { if (tokenAudienceList == null) { LOG.warn("JWT token has no audiences, validation failed."); return false; } LOG.info("Audience List: {}", audiences); for (String aud : tokenAudienceList) { LOG.info("Found audience: {}", aud); if (audiences.contains(aud)) { LOG.debug("JWT token audience has been successfully validated"); valid = true; break; } } if (!valid) { LOG.warn("JWT audience validation failed."); } } } catch (ParseException pe) { LOG.warn("Unable to parse the JWT token.", pe); } return valid; } AmbariJwtAuthenticationFilter(AuthenticationEntryPoint ambariEntryPoint,
                                JwtAuthenticationPropertiesProvider propertiesProvider,
                                AmbariJwtAuthenticationProvider authenticationProvider,
                                AmbariAuthenticationEventHandler eventHandler); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override boolean shouldIncrementFailureCount(); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain); @Override void destroy();  }### Answer:
@Test public void testValidateAudiences() throws Exception { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(createTestProperties()).anyTimes(); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertTrue(filter.validateAudiences(getSignedToken())); assertFalse(filter.validateAudiences(getInvalidToken())); verifyAll(); }
@Test public void testValidateNullAudiences() throws Exception { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(createTestProperties(null)).anyTimes(); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertTrue(filter.validateAudiences(getSignedToken())); assertTrue(filter.validateAudiences(getInvalidToken())); verifyAll(); }
@Test public void testValidateTokenWithoutAudiences() throws Exception { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(createTestProperties()).anyTimes(); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertFalse(filter.validateAudiences(getSignedToken(null))); verifyAll(); }