Search in sources :

Example 6 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class SmtpTaskV2Test method testWithBothDefaults.

@Test
public void testWithBothDefaults() throws Exception {
    Map<String, Object> policyDefaults = new HashMap<>();
    policyDefaults.put("host", "badserver");
    policyDefaults.put("port", -1);
    // Process arg defaults override policy defaults
    Map<String, Object> processArgsDefaults = new HashMap<>();
    processArgsDefaults.put("host", "localhost");
    processArgsDefaults.put("port", mailServer.getSmtp().getPort());
    Map<String, Object> mail = new HashMap<>();
    mail.put("from", "me@localhost");
    mail.put("to", "you@localhost");
    mail.put("message", "Default vars from process arguments.");
    Context ctx = mock(Context.class);
    when(ctx.workingDirectory()).thenReturn(Paths.get(System.getProperty("user.dir")));
    when(ctx.variables()).thenReturn(new MapBackedVariables(Collections.singletonMap("smtpParams", processArgsDefaults)));
    when(ctx.defaultVariables()).thenReturn(new MapBackedVariables(policyDefaults));
    SmtpTaskV2 t = new SmtpTaskV2(ctx);
    t.execute(new MapBackedVariables(Collections.singletonMap("mail", mail)));
    MimeMessage[] messages = mailServer.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("Default vars from process arguments.\r\n", messages[0].getContent());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 7 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class SmtpTaskV2Test method testWithPolicyDefaults.

@Test
public void testWithPolicyDefaults() throws Exception {
    Map<String, Object> smtpParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", mailServer.getSmtp().getPort());
    Map<String, Object> mail = new HashMap<>();
    mail.put("from", "me@localhost");
    mail.put("to", "you@localhost");
    mail.put("message", "Default vars from policy.");
    Context ctx = mock(Context.class);
    when(ctx.workingDirectory()).thenReturn(Paths.get(System.getProperty("user.dir")));
    when(ctx.variables()).thenReturn(new MapBackedVariables(Collections.emptyMap()));
    when(ctx.defaultVariables()).thenReturn(new MapBackedVariables(smtpParams));
    SmtpTaskV2 t = new SmtpTaskV2(ctx);
    t.execute(new MapBackedVariables(Collections.singletonMap("mail", mail)));
    MimeMessage[] messages = mailServer.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("Default vars from policy.\r\n", messages[0].getContent());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 8 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class SmtpTaskV2Test method testWithProcessDefaults.

@Test
public void testWithProcessDefaults() throws Exception {
    Map<String, Object> smtpParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", mailServer.getSmtp().getPort());
    Map<String, Object> mail = new HashMap<>();
    mail.put("from", "me@localhost");
    mail.put("to", "you@localhost");
    mail.put("message", "Default vars from process arguments.");
    Context ctx = mock(Context.class);
    when(ctx.workingDirectory()).thenReturn(Paths.get(System.getProperty("user.dir")));
    when(ctx.variables()).thenReturn(new MapBackedVariables(Collections.singletonMap("smtpParams", smtpParams)));
    when(ctx.defaultVariables()).thenReturn(new MapBackedVariables(Collections.emptyMap()));
    SmtpTaskV2 t = new SmtpTaskV2(ctx);
    t.execute(new MapBackedVariables(Collections.singletonMap("mail", mail)));
    MimeMessage[] messages = mailServer.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("Default vars from process arguments.\r\n", messages[0].getContent());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 9 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class LoopWrapper method eval.

@Override
@SuppressWarnings("unchecked")
public void eval(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    Serializable value = items;
    if (value == null) {
        // value is null, not going to run the wrapped command at all
        return;
    }
    Step currentStep = null;
    if (cmd instanceof StepCommand) {
        currentStep = ((StepCommand<?>) cmd).getStep();
    }
    // create the context explicitly
    ContextFactory contextFactory = runtime.getService(ContextFactory.class);
    Context ctx = contextFactory.create(runtime, state, threadId, currentStep);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    value = ee.eval(EvalContextFactory.global(ctx), value, Serializable.class);
    // prepare items
    // store items in an ArrayList because it is Serializable
    ArrayList<Serializable> items;
    if (value == null) {
        // value is null, not going to run the wrapped command at all
        return;
    } else if (value instanceof Collection) {
        Collection<Serializable> v = (Collection<Serializable>) value;
        if (v.isEmpty()) {
            // no items, nothing to do
            return;
        }
        items = new ArrayList<>(v);
    } else if (value instanceof Map) {
        Map<Serializable, Serializable> m = (Map<Serializable, Serializable>) value;
        items = m.entrySet().stream().map(e -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), e.getValue())).collect(Collectors.toCollection(ArrayList::new));
    } else if (value.getClass().isArray()) {
        items = new ArrayList<>(Arrays.asList((Serializable[]) value));
    } else {
        throw new IllegalArgumentException("'withItems' accepts only Lists of items, Java Maps or arrays of values. Got: " + value.getClass());
    }
    items.forEach(LoopWrapper::assertItem);
    if (items.isEmpty()) {
        return;
    }
    eval(state, threadId, items);
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) CompilerContext(com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerContext) Serializable(java.io.Serializable) Step(com.walmartlabs.concord.runtime.v2.model.Step) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) ContextFactory(com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)

Example 10 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class RetryWrapper method eval.

@Override
public void eval(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    // wrap the command into a frame with an exception handler
    Frame inner = Frame.builder().nonRoot().exceptionHandler(new NextRetry(cmd)).commands(cmd).build();
    // create the context explicitly
    ContextFactory contextFactory = runtime.getService(ContextFactory.class);
    Context ctx = contextFactory.create(runtime, state, threadId, getCurrentStep());
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    int times = retry.times();
    if (retry.timesExpression() != null) {
        Number n = ee.eval(EvalContextFactory.global(ctx), retry.timesExpression(), Number.class);
        if (n != null) {
            times = n.intValue();
        }
    }
    Duration delay = retry.delay();
    if (retry.delayExpression() != null) {
        Number n = ee.eval(EvalContextFactory.global(ctx), retry.delayExpression(), Number.class);
        if (n != null) {
            delay = Duration.ofSeconds(n.longValue());
        }
    }
    inner.setLocal(Constants.Runtime.RETRY_ATTEMPT_NUMBER, 0);
    inner.setLocal(RETRY_CFG, Retry.builder().from(retry).times(times).delay(delay).build());
    state.pushFrame(threadId, inner);
}
Also used : ContextFactory(com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory) Context(com.walmartlabs.concord.runtime.v2.sdk.Context) Duration(java.time.Duration) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Aggregations

Context (com.walmartlabs.concord.runtime.v2.sdk.Context)18 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)15 MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)6 HashMap (java.util.HashMap)6 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)5 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)4 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)4 TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)4 Map (java.util.Map)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 ApiClient (com.walmartlabs.concord.ApiClient)3 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)3 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)3 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)3 Command (com.walmartlabs.concord.svm.Command)3 MimeMessage (javax.mail.internet.MimeMessage)3 Test (org.junit.jupiter.api.Test)3 HashiVaultTask (com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask)2 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 LogContext (com.walmartlabs.concord.runtime.v2.runner.logging.LogContext)2