SagaKeyReaderExtractor implements KeyExtractor { @Override public Object findSagaInstanceKey(final Class<? extends Saga> sagaClazz, final LookupContext context) { Object keyValue = null; KeyReader reader = tryGetKeyReader(sagaClazz, context.message()); if (reader != null) { keyValue = reader.readKey(context.message(), context); } return keyValue; } @Inject  SagaKeyReaderExtractor(final SagaProviderFactory sagaProviderFactory); @Override Object findSagaInstanceKey(final Class<? extends Saga> sagaClazz, final LookupContext context);  }
@Test public void findSagaInstanceKey_sagaHasReaderWithExactMessage_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader reader = FunctionKeyReader.create(String.class, new KeyReadFunction<String>() { @Override public String key(final String s) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage("my input event message")); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object)keyValue)); }
@Test public void findSagaInstanceKey_sagaCalledWithContext_contextUsedAsReaderParameter() { LookupContext originalContext = SagaLookupContext.forMessage("my input event message"); final LookupContext[] providedReaderContext = new LookupContext[1]; KeyReader reader = KeyReaders.forMessage(String.class, new ContextKeyExtractFunction<String, String>() { @Nullable @Override public String key(final String message, final LookupContext context) { providedReaderContext[0] = context; return null; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader)); sut.findSagaInstanceKey(Saga.class, originalContext); assertThat("Expected context in key reader to be the same as the original.", providedReaderContext[0], sameInstance(originalContext)); }
@Test public void findSagaInstanceKey_readerForMessageNotPartOfSaga_returnsNull() { final String keyValue = "theKeyValue"; KeyReader reader = FunctionKeyReader.create(Integer.class, new KeyReadFunction<Integer>() { @Override public String key(Integer intVal) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage("my input event message")); assertThat("Expected returned key to be null.", foundKey, nullValue()); }
@Test public void findSagaInstanceKey_searchAlreadyDoneForOtherType_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader intReader = FunctionKeyReader.create(Integer.class, new KeyReadFunction<Integer>() { @Override public String key(final Integer intValue) { return "42"; } }); KeyReader reader = FunctionKeyReader.create(String.class, new KeyReadFunction<String>() { @Override public String key(final String s) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader, intReader)); sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage(new Integer(5))); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage("my input event message")); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object) keyValue)); }
@Test public void findSagaInstanceKey_sagaHasReaderWithSuperclassOfMessage_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader reader = FunctionKeyReader.create(Number.class, new KeyReadFunction<Number>() { @Override public String key(final Number number) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage(Integer.valueOf(123))); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object)keyValue)); }
@Test public void findSagaInstanceKey_sagaHasBothReadesrWithSuperclassOfMessageAndMessageClass_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader numberReader = FunctionKeyReader.create(Number.class, new KeyReadFunction<Number>() { @Override public String key(final Number number) { return "some key"; } }); KeyReader integerReader = FunctionKeyReader.create(Integer.class, new KeyReadFunction<Integer>() { @Override public String key(final Integer integer) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(numberReader, integerReader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage(Integer.valueOf(123))); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object)keyValue)); }
@Test public void findSagaInstanceKey_sagaHasReaderWithInterfaceImplementedByMessage_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader reader = FunctionKeyReader.create(CharSequence.class, new KeyReadFunction<CharSequence>() { @Override public String key(final CharSequence charSequence) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage("my input event message")); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object) keyValue)); }
@Test public void findSagaInstanceKey_sagaHasBothReadesrWithInterfaceImplementedByMessageAndMessageClass_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader reader = FunctionKeyReader.create(CharSequence.class, new KeyReadFunction<CharSequence>() { @Override public String key(final CharSequence charSequence) { return "some key"; } }); KeyReader stringReader = FunctionKeyReader.create(String.class, new KeyReadFunction<String>() { @Override public String key(final String string) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(reader, stringReader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage("my input event message")); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object) keyValue)); }
@Test public void findSagaInstanceKey_sagaHasBothReadersWithSuperclassOfMessageAndMessageClass_returnsDefinedKey() { final String keyValue = "theKeyValue"; KeyReader comparableReader = FunctionKeyReader.create(Comparable.class, new KeyReadFunction<Comparable>() { @Override public String key(final Comparable number) { return "some key"; } }); KeyReader numberReader = FunctionKeyReader.create(Number.class, new KeyReadFunction<Number>() { @Override public String key(final Number number) { return "another key"; } }); KeyReader integerReader = FunctionKeyReader.create(Integer.class, new KeyReadFunction<Integer>() { @Override public String key(final Integer integer) { return keyValue; } }); when(testSaga.keyReaders()).thenReturn(Lists.newArrayList(comparableReader, numberReader, integerReader)); Object foundKey = sut.findSagaInstanceKey(Saga.class, SagaLookupContext.forMessage(Integer.valueOf(123))); assertThat("Expected returned key to match key value provided reader.", foundKey, equalTo((Object) keyValue)); }