Search in sources :

Example 1 with ScriptVariableGenerator

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;
    }
}
Also used : RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) HashMap(java.util.HashMap) ScriptVariableGenerator(org.springframework.integration.scripting.ScriptVariableGenerator) StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ScriptSource(org.springframework.scripting.ScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) Test(org.junit.Test)

Example 2 with ScriptVariableGenerator

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());
}
Also used : Binding(groovy.lang.Binding) ScriptVariableGenerator(org.springframework.integration.scripting.ScriptVariableGenerator) DefaultScriptVariableGenerator(org.springframework.integration.scripting.DefaultScriptVariableGenerator) MissingPropertyException(groovy.lang.MissingPropertyException) DefaultScriptVariableGenerator(org.springframework.integration.scripting.DefaultScriptVariableGenerator) Test(org.junit.Test)

Example 3 with ScriptVariableGenerator

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());
}
Also used : ScriptVariableGenerator(org.springframework.integration.scripting.ScriptVariableGenerator) DefaultScriptVariableGenerator(org.springframework.integration.scripting.DefaultScriptVariableGenerator) DefaultScriptVariableGenerator(org.springframework.integration.scripting.DefaultScriptVariableGenerator) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 ScriptVariableGenerator (org.springframework.integration.scripting.ScriptVariableGenerator)3 DefaultScriptVariableGenerator (org.springframework.integration.scripting.DefaultScriptVariableGenerator)2 Binding (groovy.lang.Binding)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 HashMap (java.util.HashMap)1 RefreshableResourceScriptSource (org.springframework.integration.scripting.RefreshableResourceScriptSource)1 Message (org.springframework.messaging.Message)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 ScriptSource (org.springframework.scripting.ScriptSource)1 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)1 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)1