TokenValidator { public void checkOnlyOnce(String key, String value, Runnable runnable){ checkOnlyOnce(key, value, () -> { runnable.run(); return null; }); }  String generate(String key, int length); String generate(String key, int length, int expiredInSeconds); String generateAlphanumeric(String key, int length); String generateAlphanumeric(String key, int length, int expiredInSeconds); String save(String key, Supplier<String> valueGenerator); String save(String key, int expiredInSeconds, Supplier<String> valueGenerator); Long clear(String key); boolean check(String key, String value, boolean deleteCode); void check(String key, String value, Runnable runnable); T check(String key, String value, Supplier<T> supplier); T check(String key, String value, Supplier<T> successSupplier, boolean deleteCode); T check(String key, String value, Supplier<T> successSupplier, Function<StoreContext, T> failFunc, boolean deleteCode); void checkOnlyOnce(String key, String value, Runnable runnable); T checkOnlyOnce(String key, String value, Supplier<T> supplier); void setRedisOperationService(RedisOperationService redisOperationService); void setExpiredInSeconds(int expiredInSeconds); void setTokenKeyPrefix(String tokenKeyPrefix);  }
@Test public void testCheckOnlyOnce(){ String key = "addOrder"; String value = "709394"; this.tokenValidator.clear(key); this.tokenValidator.save(key, ()->value); Assertions.assertThatExceptionOfType(ServiceException.class) .isThrownBy(()->{ this.tokenValidator.checkOnlyOnce(key, "error code", ()->{ Assert.fail("will not run..."); }); }) .withMessage(TokenValidatorErrors.TOKEN_INVALID_OR_EXPIRED.getErrorMessage()); Assertions.assertThatExceptionOfType(ServiceException.class) .isThrownBy(()->{ this.tokenValidator.checkOnlyOnce(key, value, ()->{ Assert.fail("will not run..."); }); }) .withMessage(TokenValidatorErrors.TOKEN_INVALID_OR_EXPIRED.getErrorMessage()); this.tokenValidator.save(key, ()->value); this.tokenValidator.checkOnlyOnce(key, value, ()->{ System.out.println("run..."); }); }