### Question:
CachedRolesProvider implements RolesProvider { @Override public Set<String> getUserRoles(String username) throws Exception { try { Set<String> roles = rolesCache.get(username); if (roles != null) { LOGGER.debug("Found roles in cache for uid={}", username); return roles; } LOGGER.debug("Cache missed for uid={}. Calling ldap.", username); roles = rolesProvider.getUserRoles(username); rolesCache.put(username, roles); return roles; } catch (Exception e) { Set<String> roles = rolesCache.getFromFallback(username); if (roles != null) { LOGGER.error("There was an error getting roles for "+username+", taking roles from fallback cache.", e); return roles; } throw e; } } CachedRolesProvider(RolesProvider rolesProvider);  CachedRolesProvider(RolesProvider rolesProvider, RolesCache rolesCache); @Override Set<String> getUserRoles(String username); @Override RolesProviderHealth checkHealth();  }### Answer:
@Test public void testRolesCache() throws Exception { Mockito.when(rolesProvider.getUserRoles("user")).thenReturn(roles); Set<String> returnedRoles = cachedRolesProvider.getUserRoles("user"); Assert.assertEquals(roles, returnedRoles); returnedRoles = cachedRolesProvider.getUserRoles("user"); Assert.assertEquals(roles, returnedRoles); Thread.sleep(1000); returnedRoles = cachedRolesProvider.getUserRoles("user"); Assert.assertEquals(roles, returnedRoles); Mockito.verify(rolesCache, Mockito.times(3)).get("user"); Mockito.verify(rolesCache, Mockito.times(2)).put("user", roles); Mockito.verify(rolesProvider, Mockito.times(2)).getUserRoles("user"); Mockito.verify(rolesCache, Mockito.times(0)).getFromFallback("user"); }
@Test public void testFallBackRolesCache() throws Exception { Mockito.when(rolesProvider.getUserRoles("user")).thenThrow(new LDAPException(ResultCode.SERVER_DOWN)); rolesCache.put("user", roles); Thread.sleep(1000); Assert.assertNull("rolesCache should have expired", rolesCache.get("user")); Set<String> returnedRoles = cachedRolesProvider.getUserRoles("user"); Assert.assertEquals(roles, returnedRoles); Mockito.verify(rolesCache, Mockito.times(1)).getFromFallback("user"); }