-
-
Save anonymous/a720da5a7849ca861f73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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