Skip to content

Commit

Permalink
Improve message-web text format issue #596
Browse files Browse the repository at this point in the history
  • Loading branch information
“Rafael committed Jan 8, 2025
1 parent 252f0cf commit f56f2be
Showing 1 changed file with 86 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -44,8 +44,7 @@ public class WebSequence extends AbstractMessage<WebSequence> {

private final WebSequence parent;

private final SortedMap<String,
BiConsumer<WebDriver, Map<String, String>>> operations = new TreeMap<>();
private final SortedMap<String, BiConsumer<WebDriver, Map<String, String>>> operations = new TreeMap<>();

private Map<String, String> results;

Expand All @@ -56,71 +55,90 @@ 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<String, String>) JSON.readValue( bytes, Map.class ))
.forEach( peer::set );
}
catch( IOException ioe ) {
throw new UncheckedIOException( String.format(
((Map<String, String>) 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;
}

@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<String, String> params = results != null
? results
: parameters();
// Get the parameters to use. If 'results' is not null, use 'results', otherwise
// use 'parameters()'.
Map<String, String> 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<String> fields() {
return new TreeSet<>( parameters().keySet() );
return new TreeSet<>(parameters().keySet());
}

/**
Expand All @@ -132,37 +150,36 @@ public Set<String> fields() {
* operation
* @return <code>this</code>
*/
public WebSequence operation( String name,
BiConsumer<WebDriver, Map<String, String>> op ) {
operations.put( name, op );
public WebSequence operation(String name,
BiConsumer<WebDriver, Map<String, String>> 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<String, BiConsumer<WebDriver, Map<String, String>>> operations() {
SortedMap<String, BiConsumer<WebDriver, Map<String, String>>> 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<String, String> parameters() {
Map<String, String> 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;
Expand All @@ -174,36 +191,33 @@ private Map<String, String> 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<String, String> 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);
}
}

Expand Down

0 comments on commit f56f2be

Please sign in to comment.