@Test public void test_should_return_token_when_token_is_successfully_updated_to_validated() { final ValidateRequest validateRequest = new ValidateRequest("tenant", "otpNumber", "identity"); Token token = Token.builder().uuid("").identity("test").validated(false) .timeToLiveInSeconds(300l).number("12345") .tenantId("default").createdTime(new Date().getTime()).build(); List<Token> tokenList = new ArrayList<Token>(); tokenList.add(token); Tokens tokens = new Tokens(tokenList); when(tokenRepository.findByNumberAndIdentityAndTenantId(validateRequest)).thenReturn(tokens); final Token token1 = tokenService.validate(validateRequest); assertThat(token1.isValidated()); }
public Token validate(ValidateRequest validateRequest) { validateRequest.validate(); Tokens tokens = tokenRepository.findByNumberAndIdentityAndTenantId(validateRequest); if (tokens != null && tokens.getTokens().isEmpty()) tokens = tokenRepository.findByNumberAndIdentityAndTenantIdLike(validateRequest); Long currentTime = System.currentTimeMillis() / 1000; Long createdTime = 0l; if (tokens != null && tokens.getTokens() != null && !tokens.getTokens().isEmpty()) { Token token = tokens.getTokens().get(0); if (token.isValidated()) { throw new TokenAlreadyUsedException(); } createdTime = token.getCreatedTime() / 1000; } else if (tokens.getTokens().isEmpty()) { throw new TokenValidationFailureException(); } if (!((currentTime - createdTime) <= TTL_IN_SECONDS)) { log.info("Token validation failure for otp #", validateRequest.getOtp()); throw new TokenValidationFailureException(); } final Token matchingToken = tokens.getTokens().get(0); tokenRepository.markAsValidated(matchingToken); return matchingToken; }
TokenService { public Token validate(ValidateRequest validateRequest) { validateRequest.validate(); Tokens tokens = tokenRepository.findByNumberAndIdentityAndTenantId(validateRequest); if (tokens != null && tokens.getTokens().isEmpty()) tokens = tokenRepository.findByNumberAndIdentityAndTenantIdLike(validateRequest); Long currentTime = System.currentTimeMillis() / 1000; Long createdTime = 0l; if (tokens != null && tokens.getTokens() != null && !tokens.getTokens().isEmpty()) { Token token = tokens.getTokens().get(0); if (token.isValidated()) { throw new TokenAlreadyUsedException(); } createdTime = token.getCreatedTime() / 1000; } else if (tokens.getTokens().isEmpty()) { throw new TokenValidationFailureException(); } if (!((currentTime - createdTime) <= TTL_IN_SECONDS)) { log.info("Token validation failure for otp #", validateRequest.getOtp()); throw new TokenValidationFailureException(); } final Token matchingToken = tokens.getTokens().get(0); tokenRepository.markAsValidated(matchingToken); return matchingToken; } }
TokenService { public Token validate(ValidateRequest validateRequest) { validateRequest.validate(); Tokens tokens = tokenRepository.findByNumberAndIdentityAndTenantId(validateRequest); if (tokens != null && tokens.getTokens().isEmpty()) tokens = tokenRepository.findByNumberAndIdentityAndTenantIdLike(validateRequest); Long currentTime = System.currentTimeMillis() / 1000; Long createdTime = 0l; if (tokens != null && tokens.getTokens() != null && !tokens.getTokens().isEmpty()) { Token token = tokens.getTokens().get(0); if (token.isValidated()) { throw new TokenAlreadyUsedException(); } createdTime = token.getCreatedTime() / 1000; } else if (tokens.getTokens().isEmpty()) { throw new TokenValidationFailureException(); } if (!((currentTime - createdTime) <= TTL_IN_SECONDS)) { log.info("Token validation failure for otp #", validateRequest.getOtp()); throw new TokenValidationFailureException(); } final Token matchingToken = tokens.getTokens().get(0); tokenRepository.markAsValidated(matchingToken); return matchingToken; } @Autowired  TokenService(TokenRepository tokenRepository); }
TokenService { public Token validate(ValidateRequest validateRequest) { validateRequest.validate(); Tokens tokens = tokenRepository.findByNumberAndIdentityAndTenantId(validateRequest); if (tokens != null && tokens.getTokens().isEmpty()) tokens = tokenRepository.findByNumberAndIdentityAndTenantIdLike(validateRequest); Long currentTime = System.currentTimeMillis() / 1000; Long createdTime = 0l; if (tokens != null && tokens.getTokens() != null && !tokens.getTokens().isEmpty()) { Token token = tokens.getTokens().get(0); if (token.isValidated()) { throw new TokenAlreadyUsedException(); } createdTime = token.getCreatedTime() / 1000; } else if (tokens.getTokens().isEmpty()) { throw new TokenValidationFailureException(); } if (!((currentTime - createdTime) <= TTL_IN_SECONDS)) { log.info("Token validation failure for otp #", validateRequest.getOtp()); throw new TokenValidationFailureException(); } final Token matchingToken = tokens.getTokens().get(0); tokenRepository.markAsValidated(matchingToken); return matchingToken; } @Autowired  TokenService(TokenRepository tokenRepository); Token create(TokenRequest tokenRequest); Token validate(ValidateRequest validateRequest); Token search(TokenSearchCriteria searchCriteria); }
TokenService { public Token validate(ValidateRequest validateRequest) { validateRequest.validate(); Tokens tokens = tokenRepository.findByNumberAndIdentityAndTenantId(validateRequest); if (tokens != null && tokens.getTokens().isEmpty()) tokens = tokenRepository.findByNumberAndIdentityAndTenantIdLike(validateRequest); Long currentTime = System.currentTimeMillis() / 1000; Long createdTime = 0l; if (tokens != null && tokens.getTokens() != null && !tokens.getTokens().isEmpty()) { Token token = tokens.getTokens().get(0); if (token.isValidated()) { throw new TokenAlreadyUsedException(); } createdTime = token.getCreatedTime() / 1000; } else if (tokens.getTokens().isEmpty()) { throw new TokenValidationFailureException(); } if (!((currentTime - createdTime) <= TTL_IN_SECONDS)) { log.info("Token validation failure for otp #", validateRequest.getOtp()); throw new TokenValidationFailureException(); } final Token matchingToken = tokens.getTokens().get(0); tokenRepository.markAsValidated(matchingToken); return matchingToken; } @Autowired  TokenService(TokenRepository tokenRepository); Token create(TokenRequest tokenRequest); Token validate(ValidateRequest validateRequest); Token search(TokenSearchCriteria searchCriteria);  }