Skip to content

Commit

Permalink
Issue #139: Add a sleep input to executeForEachEntryOf
Browse files Browse the repository at this point in the history
Add the possibility to wait between each request of an executeForEachEntryOf source
  • Loading branch information
TNohaic committed Mar 28, 2024
1 parent 13ad3b1 commit 4c0f23d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,29 @@ public class ExecuteForEachEntryOf implements Serializable {
@JsonSetter(nulls = SKIP)
private IEntryConcatMethod concatMethod = EntryConcatMethod.LIST;

/**
* The sleep integer in ms.
*/
@JsonSetter(nulls = SKIP)
private Integer sleep;

/**
* Constructs an instance of {@link ExecuteForEachEntryOf}.
*
* @param source The source for executing the operation for each entry.
* @param concatMethod The method used to concatenate entries.
* @param sleep The sleep integer in ms.
*/
@Builder
@JsonCreator
public ExecuteForEachEntryOf(
@JsonProperty(value = "source", required = true) @NonNull String source,
@JsonProperty("concatMethod") IEntryConcatMethod concatMethod
@JsonProperty("concatMethod") IEntryConcatMethod concatMethod,
@JsonProperty(value = "sleep", required = false) Integer sleep
) {
this.source = source;
this.concatMethod = concatMethod == null ? EntryConcatMethod.LIST : concatMethod;
this.sleep = sleep == null ? 0 : sleep;
}

/**
Expand All @@ -84,7 +93,7 @@ public ExecuteForEachEntryOf(
* @return A new instance of {@link ExecuteForEachEntryOf} with the same source and concatenation method.
*/
public ExecuteForEachEntryOf copy() {
return ExecuteForEachEntryOf.builder().source(source).concatMethod(concatMethod.copy()).build();
return ExecuteForEachEntryOf.builder().source(source).concatMethod(concatMethod.copy()).sleep(sleep).build();
}

@Override
Expand All @@ -93,6 +102,7 @@ public String toString() {

addNonNull(stringJoiner, "- executeForEachEntryOf=", source);
addNonNull(stringJoiner, "- concatMethod=", concatMethod != null ? concatMethod.getDescription() : EMPTY);
addNonNull(stringJoiner, "- sleep=", sleep);

return stringJoiner.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,13 @@ public IEntryConcatMethod getEntryConcatMethod() {
public String getExecuteForEachEntryOf() {
return executeForEachEntryOf != null ? executeForEachEntryOf.getSource() : null;
}

/**
* Get the executeForEachEntryOf sleep Integer value
*
* @return Integer value
*/
public Integer getSleepExecuteForEachEntryOf() {
return executeForEachEntryOf != null ? executeForEachEntryOf.getSleep() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ private SourceTable processExecuteForEachEntryOf(final Source source, final Stri

final SourceTable result = SourceTable.builder().rawData(EMPTY).build();

final Integer sleep = source.getSleepExecuteForEachEntryOf();

for (List<String> row : maybeSourceTable.get().getTable()) {
final Source copy = source.copy();

Expand All @@ -505,6 +507,14 @@ private SourceTable processExecuteForEachEntryOf(final Source source, final Stri
copy.update(value -> replaceSourceReference(value, copy));

concatEntryResult(source, result, row, copy.accept(sourceProcessor));
if (sleep != null && sleep > 0) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
log.error("threadSleep", "Thread interrupted during sleep between two requests.");
Thread.currentThread().interrupt();
}
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void testDeserializeHttpSourceWithExecuteForEachEntryOf() throws IOException {
.type("http")
.url("/device-detail/$entry.column(1)$")
.method(GET)
.executeForEachEntryOf(new ExecuteForEachEntryOf("${source::pre.devices}", EntryConcatMethod.LIST))
.executeForEachEntryOf(new ExecuteForEachEntryOf("${source::pre.devices}", EntryConcatMethod.LIST, 0))
.build()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,45 @@ void testProcessHTTPSourceCustomExecuteForEachEntry() {
assertEquals(expectedResult, result.getRawData());
}

@Test
void testProcessHTTPSourceExecuteForEachEntrySleep() {
final HttpSource httpSource = HttpSource.builder().url(URL).build();
assertNull(httpSource.getSleepExecuteForEachEntryOf());

httpSource.setExecuteForEachEntryOf(
ExecuteForEachEntryOf.builder().source(ENCLOSURE_COLLECT_SOURCE_1).concatMethod(EntryConcatMethod.LIST).build()
);

assertEquals(0, httpSource.getSleepExecuteForEachEntryOf());

httpSource.setExecuteForEachEntryOf(
ExecuteForEachEntryOf
.builder()
.source(ENCLOSURE_COLLECT_SOURCE_1)
.concatMethod(EntryConcatMethod.LIST)
.sleep(200)
.build()
);

assertEquals(200, httpSource.getSleepExecuteForEachEntryOf());

final CustomConcatMethod customConcatMethod = CustomConcatMethod
.builder()
.concatStart("concatStart:{")
.concatEnd("}concatEnd;")
.build();

httpSource.setExecuteForEachEntryOf(
ExecuteForEachEntryOf
.builder()
.source(ENCLOSURE_COLLECT_SOURCE_1)
.concatMethod(customConcatMethod)
.sleep(400)
.build()
);
assertEquals(400, httpSource.getSleepExecuteForEachEntryOf());
}

@Test
void testProcessSNMPGetSource() {
final SnmpConfiguration snmpConfiguration = SnmpConfiguration.builder().port(161).timeout(120L).build();
Expand Down

0 comments on commit 4c0f23d

Please sign in to comment.