From f56f2be9fbb5148576c7aff8c63081cb58fd010c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRafael?= <“francisco.lezama@encora.com”> Date: Wed, 8 Jan 2025 11:11:16 -0600 Subject: [PATCH] Improve message-web text format issue #596 --- .../test/flow/msg/web/WebSequence.java | 158 ++++++++++-------- 1 file changed, 86 insertions(+), 72 deletions(-) diff --git a/message/message-web/src/main/java/com/mastercard/test/flow/msg/web/WebSequence.java b/message/message-web/src/main/java/com/mastercard/test/flow/msg/web/WebSequence.java index 7950e86d88..d2c304ddb0 100644 --- a/message/message-web/src/main/java/com/mastercard/test/flow/msg/web/WebSequence.java +++ b/message/message-web/src/main/java/com/mastercard/test/flow/msg/web/WebSequence.java @@ -1,7 +1,6 @@ package com.mastercard.test.flow.msg.web; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.stream.Collectors.joining; import java.io.IOException; import java.io.UncheckedIOException; @@ -12,6 +11,7 @@ import java.util.TreeMap; import java.util.TreeSet; import java.util.function.BiConsumer; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.openqa.selenium.WebDriver; @@ -44,8 +44,7 @@ public class WebSequence extends AbstractMessage { private final WebSequence parent; - private final SortedMap>> operations = new TreeMap<>(); + private final SortedMap>> operations = new TreeMap<>(); private Map results; @@ -56,28 +55,27 @@ public WebSequence() { parent = null; } - private WebSequence( WebSequence parent ) { + private WebSequence(WebSequence parent) { this.parent = parent; } @Override public WebSequence child() { - return copyMasksTo( new WebSequence( self() ) ); + return copyMasksTo(new WebSequence(self())); } @Override - public WebSequence peer( byte[] bytes ) { - WebSequence peer = copyMasksTo( new WebSequence( parent ) ); - peer.operations.putAll( operations ); + public WebSequence peer(byte[] bytes) { + WebSequence peer = copyMasksTo(new WebSequence(parent)); + peer.operations.putAll(operations); try { - ((Map) JSON.readValue( bytes, Map.class )) - .forEach( peer::set ); - } - catch( IOException ioe ) { - throw new UncheckedIOException( String.format( + ((Map) JSON.readValue(bytes, Map.class)) + .forEach(peer::set); + } catch (IOException ioe) { + throw new UncheckedIOException(String.format( "Failed to parse '%s' (%s)", - new String( bytes, UTF_8 ), Arrays.toString( bytes ) ), - ioe ); + new String(bytes, UTF_8), Arrays.toString(bytes)), + ioe); } return peer; } @@ -85,42 +83,62 @@ public WebSequence peer( byte[] bytes ) { @Override public byte[] content() { try { - return JSON.writeValueAsBytes( parameters() ); - } - catch( IOException ioe ) { - throw new UncheckedIOException( ioe ); + return JSON.writeValueAsBytes(parameters()); + } catch (IOException ioe) { + throw new UncheckedIOException(ioe); } } @Override protected String asHuman() { - Map params = results != null - ? results - : parameters(); + // Get the parameters to use. If 'results' is not null, use 'results', otherwise + // use 'parameters()'. + Map params = results != null ? results : parameters(); + + // Find the maximum length of the parameter names. This is used for formatting. int nameWidth = params.keySet().stream() - .mapToInt( String::length ) - .max().orElse( 1 ); - String nvpFmt = " %" + nameWidth + "s : %s"; - String padFmt = "\n %" + nameWidth + "s "; - String pad = String.format( padFmt, "" ); - return String.format( "Operations:\n" + .mapToInt(String::length) + .max().orElse(1); + + // Define the format for name-value pairs and padding for multi-line values. + String nvpFmt = " │ %" + nameWidth + "s │ %s │"; + String padFmt = "\n │ %" + nameWidth + "s "; + String pad = String.format(padFmt, ""); + + // Create the formatted string for operations. + String operationsStr = operations().keySet().stream() + .map(o -> " │ " + o + " │") + .collect(Collectors.joining("\n")); + + // Create the formatted string for parameters. + String paramsStr = params.entrySet().stream() + .map(e -> String.format(nvpFmt, + e.getKey(), + Stream.of(e.getValue().split("\n")) + .collect(Collectors.joining(pad)))) + .collect(Collectors.joining("\n")); + + // Create a borderline for the box drawing. + String border = "─".repeat(nameWidth + 4); + + // Return the final formatted string with box drawing characters. + return String.format("┌%s┐\n" + + "│ Operations │\n" + + "├%s┤\n" + + "%s\n" + + "└%s┘\n" + + "┌%s┐\n" + + "│ Parameters │ Values │\n" + + "├%s┤\n" + "%s\n" - + "Parameters:\n" - + "%s", - operations().keySet().stream() - .map( o -> " " + o ) - .collect( joining( "\n" ) ), - params.entrySet().stream() - .map( e -> String.format( nvpFmt, - e.getKey(), - Stream.of( e.getValue().split( "\n" ) ) - .collect( joining( pad ) ) ) ) - .collect( joining( "\n" ) ) ); + + "└%s┘", + border, border, operationsStr, border, + border, border, paramsStr, border); } @Override public Set fields() { - return new TreeSet<>( parameters().keySet() ); + return new TreeSet<>(parameters().keySet()); } /** @@ -132,37 +150,36 @@ public Set fields() { * operation * @return this */ - public WebSequence operation( String name, - BiConsumer> op ) { - operations.put( name, op ); + public WebSequence operation(String name, + BiConsumer> op) { + operations.put(name, op); return self(); } @Override - protected Object access( String field ) { - return parameters().get( field ); + protected Object access(String field) { + return parameters().get(field); } private SortedMap>> operations() { SortedMap>> op = new TreeMap<>(); - if( parent != null ) { - op.putAll( parent.operations() ); + if (parent != null) { + op.putAll(parent.operations()); } - op.putAll( operations ); + op.putAll(operations); return op; } private Map parameters() { Map p = new TreeMap<>(); - if( parent != null ) { - p.putAll( parent.parameters() ); + if (parent != null) { + p.putAll(parent.parameters()); } - for( Update update : updates ) { - if( update.value() == DELETE ) { - p.remove( update.field() ); - } - else { - p.put( update.field(), String.valueOf( update.value() ) ); + for (Update update : updates) { + if (update.value() == DELETE) { + p.remove(update.field()); + } else { + p.put(update.field(), String.valueOf(update.value())); } } return p; @@ -174,36 +191,33 @@ private Map parameters() { * @param driver the browser to drive * @return The state of the parameters map after all operations have completed */ - public byte[] process( WebDriver driver ) { + public byte[] process(WebDriver driver) { Map params = parameters(); - operations().forEach( ( name, op ) -> { - if( op != null ) { + operations().forEach((name, op) -> { + if (op != null) { try { - op.accept( driver, params ); - } - catch( Exception e ) { + op.accept(driver, params); + } catch (Exception e) { // our operation has failed! String url = "No URL"; String source = "No page source"; try { url = driver.getCurrentUrl(); source = driver.getPageSource(); - } - catch( Exception f ) { + } catch (Exception f) { source = f.getMessage(); } - throw new IllegalStateException( String.format( + throw new IllegalStateException(String.format( "Operation '%s' failed on page '%s'\n%s", - name, url, source ), - e ); + name, url, source), + e); } } - } ); + }); try { - return JSON.writeValueAsBytes( params ); - } - catch( IOException ioe ) { - throw new UncheckedIOException( ioe ); + return JSON.writeValueAsBytes(params); + } catch (IOException ioe) { + throw new UncheckedIOException(ioe); } }