1919import java .util .ArrayList ;
2020import java .util .Arrays ;
2121import java .util .List ;
22+ import java .util .Optional ;
2223
2324import org .citrusframework .actions .camel .CamelJBangCmdReceiveActionBuilder ;
2425import org .citrusframework .camel .CamelSettings ;
2526import org .citrusframework .context .TestContext ;
2627import org .citrusframework .exceptions .ActionTimeoutException ;
2728import org .citrusframework .exceptions .CitrusRuntimeException ;
2829import org .citrusframework .jbang .ProcessAndOutput ;
30+ import org .citrusframework .message .DefaultMessage ;
31+ import org .citrusframework .message .Message ;
2932import org .citrusframework .util .StringUtils ;
3033import org .slf4j .Logger ;
3134import 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 ;
0 commit comments