Skip to content

Commit

Permalink
chore: Improve Apache Camel JBang integration verification
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Oct 11, 2024
1 parent 15b0cf0 commit 9573fd1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void doExecute(TestContext context) {
context.setVariable(name + ":pid", pid);
context.setVariable(name + ":process:" + pid, pao);

logger.info("Started Camel integration '%s'".formatted(name));
logger.info("Started Camel integration '%s' (%s)".formatted(name, pid));
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to create temporary file from Camel integration");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void doExecute(TestContext context) {

camelJBang().stop(pid);

logger.info("Stopped Camel integration '%s'".formatted(name));
logger.info("Stopped Camel integration '%s' (%s)".formatted(name, pid));
}

public String getIntegrationName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void doExecute(TestContext context) {

private void verifyRouteLogs(Long pid, String name, String message, TestContext context) {
if (printLogs) {
INTEGRATION_LOG.info(String.format("Waiting for integration '%s' to log message", name));
INTEGRATION_LOG.info(String.format("Waiting for Camel integration '%s' to log message", name));
}

String log;
Expand All @@ -98,55 +98,66 @@ private void verifyRouteLogs(Long pid, String name, String message, TestContext
}

if (log.contains(message)) {
logger.info("Verified integration logs - All values OK!");
logger.info("Verified Camel integration logs - All values OK!");
return;
}

if (!printLogs) {
logger.warn(String.format("Waiting for integration '%s' to log message - retry in %s ms", name, delayBetweenAttempts));
logger.warn(String.format("Waiting for Camel integration '%s' to log message - retry in %s ms", name, delayBetweenAttempts));
}

try {
Thread.sleep(delayBetweenAttempts);
} catch (InterruptedException e) {
logger.warn("Interrupted while waiting for integration logs", e);
logger.warn("Interrupted while waiting for Camel integration logs", e);
}
}

throw new ActionTimeoutException((maxAttempts * delayBetweenAttempts),
new CitrusRuntimeException(String.format("Failed to verify integration '%s' - " +
new CitrusRuntimeException(String.format("Failed to verify Camel integration '%s' - " +
"has not printed message '%s' after %d attempts", name, message, maxAttempts)));
}

private Long verifyRouteStatus(String name, String phase, TestContext context) {
INTEGRATION_STATUS_LOG.info(String.format("Waiting for integration '%s' to be in state '%s'", name, phase));
INTEGRATION_STATUS_LOG.info(String.format("Waiting for Camel integration '%s' to be in state '%s'", name, phase));

for (int i = 0; i < maxAttempts; i++) {
if (context.getVariables().containsKey(name + ":pid")) {
Long pid = context.getVariable(name + ":pid", Long.class);
Map<String, String> properties = camelJBang().get(pid);
if ((phase.equals("Stopped") && properties.isEmpty()) || (!properties.isEmpty() && properties.get("STATUS").equals(phase))) {
logger.info(String.format("Verified integration '%s' state '%s' - All values OK!", name, phase));
logger.info(String.format("Verified Camel integration '%s' state '%s' - All values OK!", name, phase));
return pid;
} else if (phase.equals("Error")) {
logger.info(String.format("Integration '%s' is in state 'Error'", name));
logger.info(String.format("Camel integration '%s' is in state 'Error'", name));
if (stopOnErrorStatus) {
throw new CitrusRuntimeException(String.format("Failed to verify integration '%s' - is in state 'Error'", name));
throw new CitrusRuntimeException(String.format("Failed to verify Camel integration '%s' - is in state 'Error'", name));
}
}

if (context.getVariables().containsKey(name + ":process:" + pid)) {
// check if process is still alive
ProcessAndOutput pao = context.getVariable(name + ":process:" + pid, ProcessAndOutput.class);
if (!pao.getProcess().isAlive()) {
logger.info("Failed to verify Camel integration '%s' - exit code %s".formatted(name, pao.getProcess().exitValue()));
logger.info(pao.getOutput());

throw new CitrusRuntimeException(String.format("Failed to verify Camel integration '%s' - exit code %s", name, pao.getProcess().exitValue()));
}
}
}

logger.info(System.lineSeparator() + camelJBang().ps());
logger.info(String.format("Waiting for integration '%s' to be in state '%s'- retry in %s ms", name, phase, delayBetweenAttempts));
logger.info(String.format("Waiting for Camel integration '%s' to be in state '%s'- retry in %s ms", name, phase, delayBetweenAttempts));
try {
Thread.sleep(delayBetweenAttempts);
} catch (InterruptedException e) {
logger.warn("Interrupted while waiting for integration state", e);
logger.warn("Interrupted while waiting for Camel integration state", e);
}
}

throw new ActionTimeoutException((maxAttempts * delayBetweenAttempts),
new CitrusRuntimeException(String.format("Failed to verify integration '%s' - " +
new CitrusRuntimeException(String.format("Failed to verify Camel integration '%s' - " +
"is not in state '%s' after %d attempts", name, phase, maxAttempts)));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@
import java.util.Map;
import java.util.stream.Stream;

import org.citrusframework.camel.actions.CamelVerifyIntegrationAction;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.jbang.JBangSupport;
import org.citrusframework.jbang.ProcessAndOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Camel JBang app.
*/
public class CamelJBang {

/** Logger */
private static final Logger logger = LoggerFactory.getLogger(CamelVerifyIntegrationAction.class);

private final JBangSupport camelApp = JBangSupport.jbang().app(CamelJBangSettings.getCamelApp());

/**
Expand All @@ -56,6 +62,8 @@ private CamelJBang() {
for (String url : CamelJBangSettings.getTrustUrl()) {
camelApp.trust(url);
}

logger.info("Camel JBang version: " + version());
}

/**
Expand Down

0 comments on commit 9573fd1

Please sign in to comment.