TokenValidityResolver { public Date resolve(String clientId) { Integer tokenValiditySeconds = ofNullable( clientTokenValidity.getValiditySeconds(clientId) ).orElse( clientTokenValidity.getZoneValiditySeconds() ); if (tokenValiditySeconds == DEFAULT_TO_GLOBAL_POLICY) { tokenValiditySeconds = globalTokenValiditySeconds; } return new DateTime(timeService.getCurrentTimeMillis()).plusSeconds(tokenValiditySeconds).toDate(); } TokenValidityResolver(ClientTokenValidity clientTokenValidity,
                                 int globalTokenValiditySeconds,
                                 TimeService timeService); Date resolve(String clientId); void setTimeService(TimeService timeService); static final int DEFAULT_TO_GLOBAL_POLICY; }
@Test public void whenClientValidityConfigured() { Date validity = resolver.resolve("clientId"); assertThat(validity.getTime(), is(101_000l)); }
@Test public void whenClientValidityNotConfigured_fallsBackToZoneConfiguration() { when(clientTokenValidity.getZoneValiditySeconds()).thenReturn(50); when(clientTokenValidity.getValiditySeconds("clientId")).thenReturn(null); Date validity = resolver.resolve("clientId"); assertThat(validity.getTime(), is(51_000l)); }
@Test public void whenZoneValidityNotConfigured_fallsBackToGlobalPolicy() { when(clientTokenValidity.getZoneValiditySeconds()).thenReturn(-1); when(clientTokenValidity.getValiditySeconds("clientId")).thenReturn(null); Date validity = resolver.resolve("clientId"); assertThat(validity.getTime(), is(121_000l)); }