SubstituteTransformRuntime implements TransformRuntime { @Override public List<StreamlineEvent> execute(StreamlineEvent input) { List<StreamlineEvent> result; try { result = substitute(input); } catch (Exception ex) { LOG.error("Variable substitution failed", ex); LOG.error("Returning the input event as is without replacing the variables"); result = Collections.singletonList(input); } return result; } SubstituteTransformRuntime();  SubstituteTransformRuntime(SubstituteTransform substituteTransform); @Override List<StreamlineEvent> execute(StreamlineEvent input); @Override String toString();  }
@Test public void testSubstituteNoVars() throws Exception { Map<String, Object> fieldsAndValues = new HashMap<>(); fieldsAndValues.put("1", "one"); fieldsAndValues.put("2", "two"); fieldsAndValues.put("3", "three"); StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("dsrcid").build(); TransformRuntime transformRuntime = new SubstituteTransformRuntime(); List<StreamlineEvent> result = transformRuntime.execute(event); assertEquals(1, result.size()); assertEquals(3, result.get(0).size()); assertEquals("one", result.get(0).get("1")); assertEquals("two", result.get(0).get("2")); assertEquals("three", result.get(0).get("3")); }
@Test public void testSubstituteSimpleVars() throws Exception { Map<String, Object> fieldsAndValues = new HashMap<>(); fieldsAndValues.put("1", "one"); fieldsAndValues.put("2", "two"); fieldsAndValues.put("3", "${1} plus ${2}"); StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("dsrcid").build(); TransformRuntime transformRuntime = new SubstituteTransformRuntime(); List<StreamlineEvent> result = transformRuntime.execute(event); assertEquals(1, result.size()); assertEquals(3, result.get(0).size()); assertEquals("one", result.get(0).get("1")); assertEquals("two", result.get(0).get("2")); assertEquals("one plus two", result.get(0).get("3")); }
@Test public void testSubstituteSpecificVars() throws Exception { Map<String, Object> fieldsAndValues = new HashMap<>(); fieldsAndValues.put("1", "one"); fieldsAndValues.put("2", "${1} plus ${1}"); fieldsAndValues.put("3", "${1} plus two"); StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("dsrcid").build(); TransformRuntime transformRuntime = new SubstituteTransformRuntime(new SubstituteTransform(Collections.singleton("3"))); List<StreamlineEvent> result = transformRuntime.execute(event); assertEquals(1, result.size()); assertEquals(3, result.get(0).size()); assertEquals("one", result.get(0).get("1")); assertEquals("${1} plus ${1}", result.get(0).get("2")); assertEquals("one plus two", result.get(0).get("3")); }
@Test public void testSubstituteSimpleObj() throws Exception { Map<String, Object> fieldsAndValues = new HashMap<>(); fieldsAndValues.put("1", 1); fieldsAndValues.put("2", 2); fieldsAndValues.put("3", "${1} plus ${2}"); StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("dsrcid").build(); TransformRuntime transformRuntime = new SubstituteTransformRuntime(); List<StreamlineEvent> result = transformRuntime.execute(event); assertEquals(1, result.size()); assertEquals(3, result.get(0).size()); assertEquals(1, result.get(0).get("1")); assertEquals(2, result.get(0).get("2")); assertEquals("1 plus 2", result.get(0).get("3")); }
@Test public void testSubstituteRefVars() throws Exception { Map<String, Object> fieldsAndValues = new HashMap<>(); fieldsAndValues.put("1", "one"); fieldsAndValues.put("2", "${1} plus ${1}"); fieldsAndValues.put("3", "${1} plus ${2}"); StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("dsrcid").build(); TransformRuntime transformRuntime = new SubstituteTransformRuntime(); List<StreamlineEvent> result = transformRuntime.execute(event); assertEquals(1, result.size()); assertEquals(3, result.get(0).size()); assertEquals("one", result.get(0).get("1")); assertEquals("one plus one", result.get(0).get("2")); assertEquals("one plus one plus one", result.get(0).get("3")); }
@Test public void testCyclicRef() throws Exception { Map<String, Object> fieldsAndValues = new HashMap<>(); fieldsAndValues.put("1", "${2} minus one"); fieldsAndValues.put("2", "${1} plus one"); StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("dsrcid").build(); TransformRuntime transformRuntime = new SubstituteTransformRuntime(); List<StreamlineEvent> result = transformRuntime.execute(event); System.out.println(result); assertEquals(1, result.size()); assertEquals(event, result.get(0)); }