Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve message-web text format issue #596 #1029

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -43,7 +43,6 @@ public class WebSequence extends AbstractMessage<WebSequence> {
private static final ObjectMapper JSON = new ObjectMapper();

private final WebSequence parent;

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

Expand Down Expand Up @@ -94,28 +93,67 @@ public byte[] content() {

@Override
protected String asHuman() {
Map<String, String> params = results != null
? results
: parameters();
int nameWidth = params.keySet().stream()
// 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, values, and operations. This
// is used for formatting.
int nameWidth = Math.max( params.keySet().stream()
.mapToInt( String::length )
.max().orElse( 1 ), "Parameters".length() );
int valueWidth = Math.max( params.values().stream()
.flatMap( value -> Stream.of( value.split( "\n" ) ) )
.mapToInt( String::length )
.max().orElse( 1 ), "Values".length() );
int operationsWidth = Math.max( operations().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"
+ "%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" ) ) );
.max().orElse( 1 ), "Operations".length() );

// Define the format for name-value pairs and padding for multi-line values.
String nvpFmt = "│ %" + nameWidth + "s │ %" + valueWidth + "s │";
String padFmt = "\n│ %" + nameWidth + "s │ %-" + valueWidth + "s │";

// Create the formatted string for operations.
String operationsStr = operations().keySet().stream()
.map( o -> String.format( "│ %" + operationsWidth + "s │", o ) )
.collect( Collectors.joining( "\n" ) );

// Create the formatted string for parameters.
String paramsStr = params.entrySet().stream()
.map( e -> {
String key = e.getKey();
String value = e.getValue();
String[] lines = value.split( "\n" );
return String.format( nvpFmt, key, lines[0] ) +
Stream.of( lines ).skip( 1 )
.map( line -> String.format( padFmt, "", line ) )
.collect( Collectors.joining() );
} )
.collect( Collectors.joining( "\n" ) );

// Calculate the width for the box drawing based on the longest line in
// operations and parameters.
int maxOperationsWidth = Math.max( "│ Operations │".length(),
operationsStr.lines().mapToInt( String::length ).max().orElse( 0 ) );
int maxParamsWidth = Math.max( "│ Parameters │ Values │".length(),
paramsStr.lines().mapToInt( String::length ).max().orElse( 0 ) );

String operationsBorder = "─".repeat( maxOperationsWidth - 2 );
String parametersBorder = "─".repeat( maxParamsWidth - 2 );

// Conditionally include the top, middle, and bottom lines only when there is
// data.
String operationsBox = operationsStr.isEmpty() ? "│ Operations │\n"
: String.format( "┌%s┐\n│ %-" + (maxOperationsWidth - 4) + "s │\n├%s┤\n%s\n└%s┘\n",
operationsBorder,
"Operations", operationsBorder, operationsStr, operationsBorder );
String parametersBox = paramsStr.isEmpty() ? "│ Parameters │ Values │"
: String.format( "┌%s┐\n│ %-" + (maxParamsWidth - 4) + "s │\n├%s┤\n%s\n└%s┘",
parametersBorder,
"Parameters │ Values", parametersBorder, paramsStr, parametersBorder );

return operationsBox + parametersBox;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ class WebSequenceTest {
@Test
void empty() {
WebSequence ws = new WebSequence();
Assertions.assertEquals( ""
+ "Operations:\n"
+ "\n"
+ "Parameters:\n"
+ "",
Assertions.assertEquals(
"│ Operations │\n"
+ "│ Parameters │ Values │",
ws.assertable() );
}

Expand All @@ -46,14 +44,16 @@ void unprocessed() {
WebSequence ws = new WebSequence()
.set( "foo", "bar" )
.set( "multiline", "so\nmany\nlines" );
Assertions.assertEquals( ""
+ "Operations:\n"
+ "\n"
+ "Parameters:\n"
+ " foo : bar\n"
+ " multiline : so\n"
+ " many\n"
+ " lines",
Assertions.assertEquals(
"│ Operations │\n"
+ "┌─────────────────────┐\n"
+ "│ Parameters │ Values │\n"
+ "├─────────────────────┤\n"
+ "│ foo │ bar │\n"
+ "│ multiline │ so │\n"
+ "│ │ many │\n"
+ "│ │ lines │\n"
+ "└─────────────────────┘",
ws.assertable() );
}

Expand All @@ -74,13 +74,19 @@ void processed() {

WebSequence results = ws.peer( ws.process( null ) );

Assertions.assertEquals( ""
+ "Operations:\n"
+ " param update\n"
+ "Parameters:\n"
+ " a : b\n"
+ " c : i\n"
+ " g : h",
Assertions.assertEquals(
"┌──────────────┐\n"
+ "│ Operations │\n"
+ "├──────────────┤\n"
+ "│ param update │\n"
+ "└──────────────┘\n"
+ "┌─────────────────────┐\n"
+ "│ Parameters │ Values │\n"
+ "├─────────────────────┤\n"
+ "│ a │ b │\n"
+ "│ c │ i │\n"
+ "│ g │ h │\n"
+ "└─────────────────────┘",
results.assertable() );
}

Expand Down Expand Up @@ -127,11 +133,18 @@ void peer() {

WebSequence peer = ws.peer( ws.content() );

Assertions.assertEquals( ""
+ "Operations:\n"
+ " op\n"
+ "Parameters:\n"
+ " a : b", peer.assertable() );
Assertions.assertEquals(
"┌────────────┐\n"
+ "│ Operations │\n"
+ "├────────────┤\n"
+ "│ op │\n"
+ "└────────────┘\n"
+ "┌─────────────────────┐\n"
+ "│ Parameters │ Values │\n"
+ "├─────────────────────┤\n"
+ "│ a │ b │\n"
+ "└─────────────────────┘",
peer.assertable() );

Assertions.assertEquals( "Operation has not been invoked!", ref.get() );

Expand Down Expand Up @@ -170,11 +183,13 @@ void masking() {
.masking( rng, m -> m.delete( "c" ) );

WebSequence peer = ws.peer( ws.content() );
Assertions.assertEquals( ""
+ "Operations:\n"
+ "\n"
+ "Parameters:\n"
+ " a : b",
Assertions.assertEquals(
"│ Operations │\n"
+ "┌─────────────────────┐\n"
+ "│ Parameters │ Values │\n"
+ "├─────────────────────┤\n"
+ "│ a │ b │\n"
+ "└─────────────────────┘",
peer.assertable( rng ) );
}

Expand Down Expand Up @@ -207,15 +222,21 @@ void sequence() {
+ "third operation invoked with {a=b, c=d}]",
operations.toString() );

Assertions.assertEquals( ""
+ "Operations:\n"
+ " first\n"
+ " second\n"
+ " third\n"
+ "Parameters:\n"
+ " a : b\n"
+ " c : d\n"
+ " e : f",
Assertions.assertEquals(
"┌────────────┐\n"
+ "│ Operations │\n"
+ "├────────────┤\n"
+ "│ first │\n"
+ "│ second │\n"
+ "│ third │\n"
+ "└────────────┘\n"
+ "┌─────────────────────┐\n"
+ "│ Parameters │ Values │\n"
+ "├─────────────────────┤\n"
+ "│ a │ b │\n"
+ "│ c │ d │\n"
+ "│ e │ f │\n"
+ "└─────────────────────┘",
results.assertable() );
}

Expand Down
Loading