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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations