DefaultAuthenticationService implements AuthenticationService { @Override public String authenticationToken(final String username, final String password) { final UserEntity userEntity = userService.findByName(username); final DateTime now = dateTimeService.now(); return Jwts.builder() .setSubject(userEntity.getName()) .setId(userEntity.getId().toString()) .claim("roles", userEntity.getRoleEntities()) .setIssuedAt(now.toLocalDateTime().toDate()) .setExpiration(dateTimeService.plusDaysAsDate(now, TOKEN_INTERVAL_DAYS)) .signWith(SignatureAlgorithm.HS256, JwtFilter.SECRET_KEY) .compact(); } @Autowired  DefaultAuthenticationService(final UserService userService, final DateTimeService dateTimeService); @Override boolean userPasswordCheck(final String username, final String password); @Override String authenticationToken(final String username, final String password); @Override String anonymousToken();  }
@Ignore("Remove until difference in time on build machine is figured out.") @Test public void testAuthenticationToken() { final UserEntity userEntity = new UserEntity(); userEntity.setName(VALID_USERNAME); userEntity.setPassword(VALID_PASSWORD); userEntity.setRoleEntities(Collections.emptySet()); final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); final DateTime now = formatter.parseDateTime("1985-10-26 01:24:00").withZone(DateTimeZone.UTC); final Date expiry = new Date(1_000_000_000L); Mockito.when(mockUserService.findByName(VALID_USERNAME)).thenReturn(userEntity); Mockito.when(mockDateTimeService.now()).thenReturn(now); Mockito.when(mockDateTimeService.plusDaysAsDate(now, 1)).thenReturn(expiry); final String response = defaultAuthenticationService.authenticationToken(VALID_USERNAME, VALID_PASSWORD); Assert.assertEquals( "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwicm9sZXMiOltdLCJpYXQiOjQ5OTE2NjY0MCwiZXhwIjoxMDAwMDAwfQ.e14TR4Umu8UUz3GrYyXHQTkFickDFV1Vpgb8JaCz8EY", response); Mockito.verify(mockUserService).findByName(VALID_USERNAME); Mockito.verify(mockDateTimeService).now(); Mockito.verify(mockDateTimeService).plusDaysAsDate(now, 1); }