From 29c5e820308e0303ac97a4c25adfaf233bb7abeb Mon Sep 17 00:00:00 2001 From: nagibatorz Date: Fri, 6 Nov 2026 16:54:10 -0800 Subject: [PATCH] feat: add DelayExecutor with DELAY method for #810 Fixes #810 --- .../zerocode/core/utils/DelayExecutor.java | 62 ++++++++++++++ .../core/utils/DelayExecutorTest.java | 84 +++++++++++++++++++ .../04_test_json_java_service_delay.json | 15 ++++ 3 files changed, 161 insertions(+) create mode 100644 core/src/main/java/org/jsmart/zerocode/core/utils/DelayExecutor.java create mode 100644 core/src/test/java/org/jsmart/zerocode/core/utils/DelayExecutorTest.java create mode 100644 core/src/test/resources/unit_test_files/java_apis/04_test_json_java_service_delay.json diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/DelayExecutor.java b/core/src/main/java/org/jsmart/zerocode/core/utils/DelayExecutor.java new file mode 100644 index 000000000..1b3fd4a35 --- /dev/null +++ b/core/src/main/java/org/jsmart/zerocode/core/utils/DelayExecutor.java @@ -0,0 +1,62 @@ +package org.jsmart.zerocode.core.utils; + +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Java executor that pauses scenario execution for a requested number of + * milliseconds. + * + *

Use it from a scenario step via the protocol mapping, e.g. in your + * zerocode properties: + *

+ * java.api.protocol.mappings=util://DelayExecutor|org.jsmart.zerocode.core.utils.DelayExecutor
+ * 
+ * and a step such as: + *
+ * {
+ *   "name": "delay_3_sec",
+ *   "url": "util://DelayExecutor",
+ *   "method": "DELAY",
+ *   "request": 3000,
+ *   "assertions": {
+ *     "status": "Success"
+ *   }
+ * }
+ * 
+ */ +public class DelayExecutor { + private static final Logger LOGGER = LoggerFactory.getLogger(DelayExecutor.class); + + /** + * Pauses execution for the requested number of milliseconds and returns + * a result containing the status and the requested delay. + * + * @param millis the number of milliseconds to pause; must not be null + * and must not be negative + * @return a map with {@code status} {@code "Success"} and the requested + * {@code delayMillis} + * @throws IllegalArgumentException if {@code millis} is null or negative + * @throws InterruptedException if the sleeping thread is interrupted; + * the interrupt status is restored + * before throwing + */ + public static Map DELAY(Long millis) throws InterruptedException { + if (millis == null) { + throw new IllegalArgumentException("Delay millis must not be null"); + } + if (millis < 0) { + throw new IllegalArgumentException("Delay millis must not be negative: " + millis); + } + + LOGGER.debug("Delaying execution for {} ms", millis); + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw e; + } + return Map.of("status", "Success", "delayMillis", millis); + } +} diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/DelayExecutorTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/DelayExecutorTest.java new file mode 100644 index 000000000..b0ab6649e --- /dev/null +++ b/core/src/test/java/org/jsmart/zerocode/core/utils/DelayExecutorTest.java @@ -0,0 +1,84 @@ +package org.jsmart.zerocode.core.utils; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.inject.Guice; +import com.google.inject.Injector; +import java.util.List; +import java.util.Map; +import org.jsmart.zerocode.core.di.main.ApplicationMainModule; +import org.jsmart.zerocode.core.di.provider.ObjectMapperProvider; +import org.jsmart.zerocode.core.domain.ScenarioSpec; +import org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class DelayExecutorTest { + + JavaMethodExecutorImpl methodExecutor; + ObjectMapper mapper; + SmartUtils smartUtils; + + @Before + public void setUp() throws Exception { + Injector injector = Guice.createInjector(new ApplicationMainModule("config_hosts_test.properties")); + mapper = new ObjectMapperProvider().get(); + methodExecutor = new JavaMethodExecutorImpl(injector, mapper); + smartUtils = injector.getInstance(SmartUtils.class); + } + + @Test + public void willDelayForRequestedMillisAndReturnSuccess() throws Exception { + long start = System.currentTimeMillis(); + Map result = DelayExecutor.DELAY(50L); + long elapsed = System.currentTimeMillis() - start; + + assertThat(result.get("status"), is("Success")); + assertThat(result.get("delayMillis"), is(50L)); + assertTrue("Expected at least 50ms delay, was " + elapsed, elapsed >= 50); + } + + @Test + public void willReturnImmediatelyForZeroDelay() throws Exception { + Map result = DelayExecutor.DELAY(0L); + assertThat(result.get("status"), is("Success")); + assertThat(result.get("delayMillis"), is(0L)); + } + + @Test + public void willRejectNegativeDelay() { + assertThrows(IllegalArgumentException.class, () -> DelayExecutor.DELAY(-1L)); + } + + @Test + public void willRejectNullDelay() { + assertThrows(IllegalArgumentException.class, () -> DelayExecutor.DELAY(null)); + } + + @Test + public void willResolveUtilProtocolMappingToDelayExecutor() { + ApiTypeUtils apiTypeUtils = new ApiTypeUtils("util://DelayExecutor|org.jsmart.zerocode.core.utils.DelayExecutor"); + assertThat(apiTypeUtils.getQualifiedJavaApi("util://DelayExecutor"), + is("org.jsmart.zerocode.core.utils.DelayExecutor")); + } + + @Test + public void willExecuteDelayViaScenarioDsl() throws Exception { + String scenariosJsonAsString = SmartUtils.readJsonAsString("unit_test_files/java_apis/04_test_json_java_service_delay.json"); + final ScenarioSpec scenarioSpec = smartUtils.getMapper().readValue(scenariosJsonAsString, ScenarioSpec.class); + + String serviceName = scenarioSpec.getSteps().get(0).getUrl(); + String methodName = scenarioSpec.getSteps().get(0).getOperation(); + String requestJson = scenarioSpec.getSteps().get(0).getRequest().toString(); + List> argumentTypes = methodExecutor.getParameterTypes(serviceName, methodName); + + Object request = mapper.readValue(requestJson, argumentTypes.get(0)); + Object result = methodExecutor.executeWithParams(serviceName, methodName, request); + + assertThat(result, is(Map.of("status", "Success", "delayMillis", 50L))); + } +} diff --git a/core/src/test/resources/unit_test_files/java_apis/04_test_json_java_service_delay.json b/core/src/test/resources/unit_test_files/java_apis/04_test_json_java_service_delay.json new file mode 100644 index 000000000..cd4a99967 --- /dev/null +++ b/core/src/test/resources/unit_test_files/java_apis/04_test_json_java_service_delay.json @@ -0,0 +1,15 @@ +{ + "scenarioName": "Given_When_Then-Flow name For Java Service Delay", + "steps": [ + { + "name": "delay_50_millis", + "url": "org.jsmart.zerocode.core.utils.DelayExecutor", + "operation": "DELAY", + "request": "50", + "assertions": { + "status": "Success", + "delayMillis": 50 + } + } + ] +}