From 8f77af7ff9a7eb90a3f7e7d8a3cb40b970b12a1a Mon Sep 17 00:00:00 2001 From: brig Date: Mon, 25 Sep 2023 21:57:05 +0200 Subject: [PATCH] runtime-v2: uuid function --- .../v2/runner/el/LazyExpressionEvaluator.java | 1 + .../v2/runner/el/functions/UuidFunction.java | 39 +++++++++++++++++++ .../concord/runtime/v2/runner/MainTest.java | 11 ++++++ .../runtime/v2/runner/uuid/concord.yml | 3 ++ 4 files changed, 54 insertions(+) create mode 100644 runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/UuidFunction.java create mode 100644 runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/uuid/concord.yml diff --git a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java index e4bedce799..3cc7887674 100644 --- a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java +++ b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java @@ -209,6 +209,7 @@ private static FunctionMapper createFunctionMapper() { functions.put("evalAsMap", EvalAsMapFunction.getMethod()); functions.put("isDebug", IsDebugFunction.getMethod()); functions.put("throw", ThrowFunction.getMethod()); + functions.put("uuid", UuidFunction.getMethod()); return new FunctionMapper(functions); } diff --git a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/UuidFunction.java b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/UuidFunction.java new file mode 100644 index 0000000000..2da97e2cb1 --- /dev/null +++ b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/UuidFunction.java @@ -0,0 +1,39 @@ +package com.walmartlabs.concord.runtime.v2.runner.el.functions; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 Walmart Inc. + * ----- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ===== + */ + +import java.lang.reflect.Method; +import java.util.UUID; + +public final class UuidFunction { + + public static Method getMethod() { + try { + return UuidFunction.class.getMethod("uuid"); + } catch (Exception e) { + throw new RuntimeException("Method not found"); + } + } + + public static String uuid() { + return UUID.randomUUID().toString(); + } +} diff --git a/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java b/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java index b804e05c10..c780f857f3 100644 --- a/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java +++ b/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java @@ -1530,6 +1530,17 @@ public void testArrayEvalSerialize() throws Exception { assertLog(log, ".*" + Pattern.quote("{dev=dev-cloud1}, {prod=prod-cloud1}, {test=test-cloud1}, {perf=perf-cloud2}, {ci=perf-ci}") + ".*"); } + @Test + public void testUuidFunc() throws Exception { + deploy("uuid"); + + save(ProcessConfiguration.builder() + .build()); + + byte[] log = run(); + assertLog(log, ".*uuid: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.*"); + } + private void deploy(String resource) throws URISyntaxException, IOException { Path src = Paths.get(MainTest.class.getResource(resource).toURI()); IOUtils.copy(src, workDir); diff --git a/runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/uuid/concord.yml b/runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/uuid/concord.yml new file mode 100644 index 0000000000..028368f8cd --- /dev/null +++ b/runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/uuid/concord.yml @@ -0,0 +1,3 @@ +flows: + default: + - log: "uuid: ${uuid()}" \ No newline at end of file