Skip to content

Commit 2c8c3b2

Browse files
committed
fix(#1471): Support json output in Camel cmd receive
- Add option to use Json command output to verify message content - Store received Json message into message store for later reference
1 parent 1685913 commit 2c8c3b2

File tree

6 files changed

+378
-3
lines changed

6 files changed

+378
-3
lines changed

core/citrus-api/src/main/java/org/citrusframework/actions/camel/CamelJBangCmdReceiveActionBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public interface CamelJBangCmdReceiveActionBuilder<T extends TestAction, B exten
4747

4848
B loggingColor(boolean enabled);
4949

50+
B jsonOutput(boolean enabled);
51+
5052
B grep(String filter);
5153

5254
B since(String duration);

endpoints/citrus-camel/src/main/java/org/citrusframework/camel/actions/CamelCmdReceiveAction.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.Optional;
2223

2324
import org.citrusframework.actions.camel.CamelJBangCmdReceiveActionBuilder;
2425
import org.citrusframework.camel.CamelSettings;
2526
import org.citrusframework.context.TestContext;
2627
import org.citrusframework.exceptions.ActionTimeoutException;
2728
import org.citrusframework.exceptions.CitrusRuntimeException;
2829
import org.citrusframework.jbang.ProcessAndOutput;
30+
import org.citrusframework.message.DefaultMessage;
31+
import org.citrusframework.message.Message;
2932
import org.citrusframework.util.StringUtils;
3033
import org.slf4j.Logger;
3134
import org.slf4j.LoggerFactory;
@@ -60,6 +63,9 @@ public class CamelCmdReceiveAction extends AbstractCamelJBangAction {
6063
/** The number of messages from the end to show */
6164
private final String tail;
6265

66+
/** Should use Json output and verify */
67+
private final boolean jsonOutput;
68+
6369
/** Camel JBang command arguments */
6470
private final List<String> args;
6571

@@ -82,6 +88,7 @@ public CamelCmdReceiveAction(CamelCmdReceiveAction.Builder builder) {
8288
this.grep = builder.grep;
8389
this.since = builder.since;
8490
this.tail = builder.tail;
91+
this.jsonOutput = builder.jsonOutput;
8592
this.maxAttempts = builder.maxAttempts;
8693
this.delayBetweenAttempts = builder.delayBetweenAttempts;
8794
this.printLogs = builder.printLogs;
@@ -124,6 +131,12 @@ public void doExecute(TestContext context) {
124131
commandArgs.add(tail);
125132
}
126133

134+
if (jsonOutput) {
135+
commandArgs.add("--output=json");
136+
commandArgs.add("--compact=false");
137+
commandArgs.add("--pretty");
138+
}
139+
127140
if (!args.contains("--logging-color") && args.stream().noneMatch(it -> it.startsWith("--logging-color"))) {
128141
// disable logging colors by default
129142
commandArgs.add("--logging-color=false");
@@ -155,8 +168,18 @@ public void doExecute(TestContext context) {
155168
CAMEL_JBANG_LOG.info(log);
156169
}
157170

158-
if (log.contains("Received Message:")) {
159-
logger.info("Verified Camel received message - All values OK!");
171+
boolean verified = false;
172+
if (jsonOutput) {
173+
if (log.contains("\"message\": {")) {
174+
verified = true;
175+
storeMessageFromLog(log, context);
176+
}
177+
} else if (log.contains("Received Message:")) {
178+
verified = true;
179+
}
180+
181+
if (verified) {
182+
logger.info("Verified Camel receive command - received at least one message matching the criteria - All values OK!");
160183
return;
161184
}
162185

@@ -175,14 +198,35 @@ public void doExecute(TestContext context) {
175198

176199
throw new ActionTimeoutException((maxAttempts * delayBetweenAttempts),
177200
new CitrusRuntimeException(String.format("Failed to verify Camel receive command '%s' - " +
178-
"has not received message with matching '%s' after %d attempts", integrationName, grep, maxAttempts)));
201+
"has not received message with matching '%s' after %d attempts", integrationName, Optional.ofNullable(grep).orElse("any message"), maxAttempts)));
179202
} finally {
180203
if (pao != null && pao.getProcess().isAlive()) {
181204
pao.getProcess().destroy();
182205
}
183206
}
184207
}
185208

209+
private void storeMessageFromLog(String log, TestContext context) {
210+
String[] messages = log.trim().split("^\\s+$");
211+
if (messages.length > 0) {
212+
// Store last message
213+
String message = messages[messages.length - 1];
214+
if (!message.startsWith("{") && message.contains("{")) {
215+
// Remove plain text content - usually something like 'Waiting for messages ...'
216+
message = message.substring(message.indexOf("{"));
217+
}
218+
219+
String messageName = integrationName + ".message";
220+
logger.info("Storing last message '{}' received from Camel integration '{}'", messageName, integrationName);
221+
222+
Message msg = new DefaultMessage(message);
223+
context.getMessageStore()
224+
.storeMessage(messageName, msg);
225+
context.getMessageStore()
226+
.storeMessage(integrationName + "." + Optional.ofNullable(endpoint).orElse(endpointUri), msg);
227+
}
228+
}
229+
186230
/**
187231
* Action builder.
188232
*/
@@ -195,6 +239,7 @@ public static final class Builder extends AbstractCamelJBangAction.Builder<Camel
195239
private String grep;
196240
private String since;
197241
private String tail = "0";
242+
private boolean jsonOutput;
198243
private final List<String> args = new ArrayList<>();
199244

200245
private int maxAttempts = CamelSettings.getMaxAttempts();
@@ -246,6 +291,12 @@ public Builder loggingColor(boolean enabled) {
246291
return this;
247292
}
248293

294+
@Override
295+
public Builder jsonOutput(boolean enabled) {
296+
this.jsonOutput = enabled;
297+
return this;
298+
}
299+
249300
@Override
250301
public Builder grep(String filter) {
251302
this.grep = filter;

endpoints/citrus-camel/src/main/java/org/citrusframework/camel/xml/Camel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ public void setJBang(JBang jbang) {
383383

384384
builder.loggingColor(jbang.getCmd().getReceive().isLoggingColor());
385385

386+
builder.jsonOutput(jbang.getCmd().getReceive().isJsonOutput());
387+
386388
if (jbang.getCmd().getReceive().getSince() != null) {
387389
builder.since(jbang.getCmd().getReceive().getSince());
388390
}

endpoints/citrus-camel/src/main/java/org/citrusframework/camel/xml/JBang.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,8 @@ public static class Receive {
11801180
protected String argLine;
11811181
@XmlAttribute(name = "logging-color")
11821182
protected boolean loggingColor;
1183+
@XmlAttribute(name = "json-output")
1184+
protected boolean jsonOutput;
11831185
@XmlAttribute(name = "grep")
11841186
protected String grep;
11851187
@XmlAttribute(name = "since")
@@ -1232,6 +1234,14 @@ public boolean isLoggingColor() {
12321234
return loggingColor;
12331235
}
12341236

1237+
public void setJsonOutput(boolean jsonOutput) {
1238+
this.jsonOutput = jsonOutput;
1239+
}
1240+
1241+
public boolean isJsonOutput() {
1242+
return jsonOutput;
1243+
}
1244+
12351245
public void setGrep(String grep) {
12361246
this.grep = grep;
12371247
}

endpoints/citrus-camel/src/main/java/org/citrusframework/camel/yaml/JBang.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ public void setReceive(Receive receive) {
761761

762762
builder.loggingColor(receive.isLoggingColor());
763763

764+
builder.jsonOutput(receive.isJsonOutput());
765+
764766
if (receive.getSince() != null) {
765767
builder.since(receive.getSince());
766768
}
@@ -905,6 +907,7 @@ public static class Receive {
905907
protected String uri;
906908
protected List<String> args;
907909
protected boolean loggingColor;
910+
protected boolean jsonOutput;
908911

909912
protected String grep;
910913
protected String since;
@@ -964,6 +967,15 @@ public void setLoggingColor(boolean loggingColor) {
964967
this.loggingColor = loggingColor;
965968
}
966969

970+
public boolean isJsonOutput() {
971+
return loggingColor;
972+
}
973+
974+
@SchemaProperty(advanced = true, description = "When enabled action stores messages from json output.", defaultValue = "false")
975+
public void setJsonOutput(boolean loggingColor) {
976+
this.jsonOutput = jsonOutput;
977+
}
978+
967979
public String getGrep() {
968980
return grep;
969981
}

0 commit comments

Comments
 (0)