Skip to content

Instantly share code, notes, and snippets.

Created October 8, 2013 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a720da5a7849ca861f73 to your computer and use it in GitHub Desktop.
Save anonymous/a720da5a7849ca861f73 to your computer and use it in GitHub Desktop.
@Component
@Accessors(chain=true)
public class ResetPasswordTokenServiceImpl implements ResetPasswordTokenService {
@Inject @Setter private RandomStringService randomStringService;
@Inject @Setter private TimeService timeService;
@Inject @Setter private PasswordResetTokenDao passwordResetTokenDao;
@Override
public PasswordResetToken generatePasswordResetTokenForUser(UUID userId){
// instead of using System.currentTimeMillis(), we externalize the call to a service, so we can track it easily
Long creationTime = timeService.getCurrentTime();
PasswordResetToken token = new PasswordResetToken();
token.setUserId(userId)
.setPasswordResetToken(randomStringService.generateRandomString())
.setIssuedTimestamp(creationTime)
.setValidTo(creationTime + DateTimeConstants.MILLIS_PER_DAY);
passwordResetTokenDao.save(token);
return token;
}
}
public class ResetPasswordTokenTest {
private ResetPasswordTokenService resetPasswordTokenService;
private RandomStringService randomStringService;
private TimeService timeService;
private PasswordResetTokenDao passwordResetTokenDao;
@Before
public void setup(){
// use mocks like me or create your own mocked implementations
randomStringService = mock(RandomStringService.class);
timeService = mock(TimeService.class);
passwordResetTokenDao = mock(PasswordResetTokenDao.class);
resetPasswordTokenService = new ResetPasswordTokenServiceImpl()
.setRandomStringService(randomStringService)
.setTimeService(timeService)
.setPasswordResetTokenDao(passwordResetTokenDao);
}
@Test
public void testResetPasswordTokenHasCorrectIssuedTimestampAndValidTo(){
UUID userId = UUID.randomId();
Long currentTime = System.currentTimeMillis();
String randomString = "someRandomString";
// make the mocked service to always return the currentTime argument
when(timeService.getCurrentTime()).thenReturn(currentTime);
when(randomStringService.generateRandomString()).thenReturn(randomString);
ResetPasswordToken token = resetPasswordTokenService.generatePasswordResetTokenForUser(userId);
//mockito will use equality for matching, so you can easily create an instance of what you expect your mock to be called with it and later
//use it in a matcher
PasswordResetToken expectedToken = new PasswordResetToken()
.setUserId(userId)
.setPasswordResetToken(randomString)
.setIssuedTimestamp(creationTime)
.setValidTo(creationTime + DateTimeConstants.MILLIS_PER_DAY);
// verify that the passwordResetTokenDao has been called once with the expected value
verify(passwordResetTokenDao, times(1)).save(eq(expectedToken));
// now we can easily make assertions about the issued time and the valid to
assertEquals(currentTime, token.getIssuedOn);
assertEquals(currentTime + DateTimeConstants.MILLIS_PER_DAY, token validTo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment