use of org.springframework.integration.scripting.ScriptVariableGenerator in project spring-integration by spring-projects.
the class GroovyScriptExecutingMessageProcessorTests method testSimpleExecutionWithScriptVariableGenerator.
@Test
public void testSimpleExecutionWithScriptVariableGenerator() throws Exception {
int count = countHolder.getAndIncrement();
String script = "return \"payload is $payload, header is $headers.testHeader and date is $date\"";
Message<?> message = MessageBuilder.withPayload("foo").setHeader("testHeader", "bar" + count).build();
TestResource resource = new TestResource(script, "simpleTest");
ScriptSource scriptSource = new ResourceScriptSource(resource);
Object result = null;
class CustomScriptVariableGenerator implements ScriptVariableGenerator {
@Override
public Map<String, Object> generateScriptVariables(Message<?> message) {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("date", System.nanoTime());
variables.put("payload", message.getPayload());
variables.put("headers", message.getHeaders());
return variables;
}
}
for (int i = 0; i < 5; i++) {
ScriptVariableGenerator scriptVariableGenerator = new CustomScriptVariableGenerator();
MessageProcessor<Object> processor = new GroovyScriptExecutingMessageProcessor(scriptSource, scriptVariableGenerator);
Object newResult = processor.processMessage(message);
// make sure that we get different nanotime verifying that generateScriptVariables() is invoked
assertFalse(newResult.equals(result));
result = newResult;
}
}
use of org.springframework.integration.scripting.ScriptVariableGenerator in project spring-integration by spring-projects.
the class GroovyScriptPayloadMessageProcessorTests method testBindingOverwriteWithContext.
// INT-2567
@Test
public void testBindingOverwriteWithContext() throws Exception {
final String defaultValue = "default";
Binding binding = new Binding() {
@Override
public Object getVariable(String name) {
try {
return super.getVariable(name);
} catch (MissingPropertyException e) {
// ignore
}
return defaultValue;
}
};
ScriptVariableGenerator scriptVariableGenerator = new DefaultScriptVariableGenerator(Collections.singletonMap("spam", (Object) "bucket"));
Message<?> message = MessageBuilder.withPayload("\"spam is $spam, foo is $foo\"").build();
processor = new GroovyCommandMessageProcessor(binding, scriptVariableGenerator);
Object result = processor.processMessage(message);
assertEquals("spam is bucket, foo is default", result.toString());
}
use of org.springframework.integration.scripting.ScriptVariableGenerator in project spring-integration by spring-projects.
the class GroovyScriptPayloadMessageProcessorTests method testSimpleExecutionWithContext.
@Test
public void testSimpleExecutionWithContext() throws Exception {
Message<?> message = MessageBuilder.withPayload("\"spam is $spam foo is $headers.foo\"").setHeader("foo", "bar").build();
ScriptVariableGenerator scriptVariableGenerator = new DefaultScriptVariableGenerator(Collections.singletonMap("spam", (Object) "bucket"));
MessageProcessor<Object> processor = new GroovyCommandMessageProcessor(scriptVariableGenerator);
Object result = processor.processMessage(message);
assertEquals("spam is bucket foo is bar", result.toString());
}
Aggregations