SpelKeyGenerator extends CachedExpressionEvaluator implements KeyGenerator { @Override public List<String> resolveKeys(final String lockKeyPrefix, final String expression, final Object object, final Method method, final Object[] args) { final Object expressionValue = evaluateExpression(expression, object, method, args); final List<String> keys = convertResultToList(expressionValue); if (keys.stream().anyMatch(Objects::isNull)) { throw new EvaluationConvertException("null keys are not supported: " + keys); } if (StringUtils.isEmpty(lockKeyPrefix)) { return keys; } return keys.stream().map(key -> lockKeyPrefix + key).collect(Collectors.toList()); }  @Override List<String> resolveKeys(final String lockKeyPrefix, final String expression, final Object object, final Method method, final Object[] args);  }
@Test public void shouldGenerateExecutionPath() { assertThat(keyGenerator.resolveKeys("lock_", "#executionPath", service, sendMessageMethod, new Object[]{"hello"})) .containsExactly("lock_com.github.alturkovic.lock.key.SpelKeyGeneratorTest.MessageService.sendMessage"); }
@Test public void shouldGenerateSingleKeyFromContextAndVariables() { assertThat(keyGenerator.resolveKeys("lock_", "#p0", service, sendMessageMethod, new Object[]{"hello"})) .containsExactly("lock_hello"); assertThat(keyGenerator.resolveKeys("lock_", "#a0", service, sendMessageMethod, new Object[]{"hello"})) .containsExactly("lock_hello"); assertThat(keyGenerator.resolveKeys("lock_", "#message", service, sendMessageMethod, new Object[]{"hello"})) .containsExactly("lock_hello"); }
@Test public void shouldGenerateMultipleKeysFromContextAndVariablesWithList() { final String expression = "T(com.github.alturkovic.lock.key.SpelKeyGeneratorTest).generateKeys(#message)"; assertThat(keyGenerator.resolveKeys("lock_", expression, service, sendMessageMethod, new Object[]{"p_"})) .containsExactly("lock_p_first", "lock_p_second"); }
@Test public void shouldGenerateMultipleKeysFromContextAndVariablesWithArray() { final String expression = "T(com.github.alturkovic.lock.key.SpelKeyGeneratorTest).generateArrayKeys(#message)"; assertThat(keyGenerator.resolveKeys("lock_", expression, service, sendMessageMethod, new Object[]{"p_"})) .containsExactly("lock_p_first", "lock_15"); }
@Test public void shouldGenerateMultipleKeysFromContextAndVariablesWithMixedTypeValues() { final String expression = "T(com.github.alturkovic.lock.key.SpelKeyGeneratorTest).generateMixedKeys(#message)"; assertThat(keyGenerator.resolveKeys("lock_", expression, service, sendMessageMethod, new Object[]{"p_"})) .containsExactly("lock_p_first", "lock_15"); }
@Test(expected = EvaluationConvertException.class) public void shouldFailWithExpressionThatEvaluatesInNull() { keyGenerator.resolveKeys("lock_", "null", service, sendMessageMethod, new Object[]{"hello"}); fail("Expected exception with expression that evaluated in null"); }
@Test(expected = EvaluationConvertException.class) public void shouldFailWithExpressionThatEvaluatesInEmptyList() { keyGenerator.resolveKeys("lock_", "T(java.util.Collections).emptyList()", service, sendMessageMethod, new Object[]{"hello"}); fail("Expected exception with expression that evaluated in empty list"); }
@Test(expected = EvaluationConvertException.class) public void shouldFailWithExpressionThatEvaluatesInListWithNullValue() { keyGenerator.resolveKeys("lock_", "T(java.util.Collections).singletonList(null)", service, sendMessageMethod, new Object[]{"hello"}); fail("Expected exception with expression that evaluated in a list with a null value"); }