Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Use it from a scenario step via the protocol mapping, e.g. in your
* zerocode properties:
* <pre>
* java.api.protocol.mappings=util://DelayExecutor|org.jsmart.zerocode.core.utils.DelayExecutor
* </pre>
* and a step such as:
* <pre>
* {
* "name": "delay_3_sec",
* "url": "util://DelayExecutor",
* "method": "DELAY",
* "request": 3000,
* "assertions": {
* "status": "Success"
* }
* }
* </pre>
*/
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<String, Object> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> 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<Class<?>> 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)));
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}