Skip to content

Commit f56f2be

Browse files
author
“Rafael
committed
Improve message-web text format issue Mastercard#596
1 parent 252f0cf commit f56f2be

File tree

1 file changed

+86
-72
lines changed
  • message/message-web/src/main/java/com/mastercard/test/flow/msg/web

1 file changed

+86
-72
lines changed

message/message-web/src/main/java/com/mastercard/test/flow/msg/web/WebSequence.java

+86-72
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.mastercard.test.flow.msg.web;
22

33
import static java.nio.charset.StandardCharsets.UTF_8;
4-
import static java.util.stream.Collectors.joining;
54

65
import java.io.IOException;
76
import java.io.UncheckedIOException;
@@ -12,6 +11,7 @@
1211
import java.util.TreeMap;
1312
import java.util.TreeSet;
1413
import java.util.function.BiConsumer;
14+
import java.util.stream.Collectors;
1515
import java.util.stream.Stream;
1616

1717
import org.openqa.selenium.WebDriver;
@@ -44,8 +44,7 @@ public class WebSequence extends AbstractMessage<WebSequence> {
4444

4545
private final WebSequence parent;
4646

47-
private final SortedMap<String,
48-
BiConsumer<WebDriver, Map<String, String>>> operations = new TreeMap<>();
47+
private final SortedMap<String, BiConsumer<WebDriver, Map<String, String>>> operations = new TreeMap<>();
4948

5049
private Map<String, String> results;
5150

@@ -56,71 +55,90 @@ public WebSequence() {
5655
parent = null;
5756
}
5857

59-
private WebSequence( WebSequence parent ) {
58+
private WebSequence(WebSequence parent) {
6059
this.parent = parent;
6160
}
6261

6362
@Override
6463
public WebSequence child() {
65-
return copyMasksTo( new WebSequence( self() ) );
64+
return copyMasksTo(new WebSequence(self()));
6665
}
6766

6867
@Override
69-
public WebSequence peer( byte[] bytes ) {
70-
WebSequence peer = copyMasksTo( new WebSequence( parent ) );
71-
peer.operations.putAll( operations );
68+
public WebSequence peer(byte[] bytes) {
69+
WebSequence peer = copyMasksTo(new WebSequence(parent));
70+
peer.operations.putAll(operations);
7271
try {
73-
((Map<String, String>) JSON.readValue( bytes, Map.class ))
74-
.forEach( peer::set );
75-
}
76-
catch( IOException ioe ) {
77-
throw new UncheckedIOException( String.format(
72+
((Map<String, String>) JSON.readValue(bytes, Map.class))
73+
.forEach(peer::set);
74+
} catch (IOException ioe) {
75+
throw new UncheckedIOException(String.format(
7876
"Failed to parse '%s' (%s)",
79-
new String( bytes, UTF_8 ), Arrays.toString( bytes ) ),
80-
ioe );
77+
new String(bytes, UTF_8), Arrays.toString(bytes)),
78+
ioe);
8179
}
8280
return peer;
8381
}
8482

8583
@Override
8684
public byte[] content() {
8785
try {
88-
return JSON.writeValueAsBytes( parameters() );
89-
}
90-
catch( IOException ioe ) {
91-
throw new UncheckedIOException( ioe );
86+
return JSON.writeValueAsBytes(parameters());
87+
} catch (IOException ioe) {
88+
throw new UncheckedIOException(ioe);
9289
}
9390
}
9491

9592
@Override
9693
protected String asHuman() {
97-
Map<String, String> params = results != null
98-
? results
99-
: parameters();
94+
// Get the parameters to use. If 'results' is not null, use 'results', otherwise
95+
// use 'parameters()'.
96+
Map<String, String> params = results != null ? results : parameters();
97+
98+
// Find the maximum length of the parameter names. This is used for formatting.
10099
int nameWidth = params.keySet().stream()
101-
.mapToInt( String::length )
102-
.max().orElse( 1 );
103-
String nvpFmt = " %" + nameWidth + "s : %s";
104-
String padFmt = "\n %" + nameWidth + "s ";
105-
String pad = String.format( padFmt, "" );
106-
return String.format( "Operations:\n"
100+
.mapToInt(String::length)
101+
.max().orElse(1);
102+
103+
// Define the format for name-value pairs and padding for multi-line values.
104+
String nvpFmt = " │ %" + nameWidth + "s │ %s │";
105+
String padFmt = "\n │ %" + nameWidth + "s ";
106+
String pad = String.format(padFmt, "");
107+
108+
// Create the formatted string for operations.
109+
String operationsStr = operations().keySet().stream()
110+
.map(o -> " │ " + o + " │")
111+
.collect(Collectors.joining("\n"));
112+
113+
// Create the formatted string for parameters.
114+
String paramsStr = params.entrySet().stream()
115+
.map(e -> String.format(nvpFmt,
116+
e.getKey(),
117+
Stream.of(e.getValue().split("\n"))
118+
.collect(Collectors.joining(pad))))
119+
.collect(Collectors.joining("\n"));
120+
121+
// Create a borderline for the box drawing.
122+
String border = "─".repeat(nameWidth + 4);
123+
124+
// Return the final formatted string with box drawing characters.
125+
return String.format("┌%s┐\n"
126+
+ "│ Operations │\n"
127+
+ "├%s┤\n"
128+
+ "%s\n"
129+
+ "└%s┘\n"
130+
+ "┌%s┐\n"
131+
+ "│ Parameters │ Values │\n"
132+
+ "├%s┤\n"
107133
+ "%s\n"
108-
+ "Parameters:\n"
109-
+ "%s",
110-
operations().keySet().stream()
111-
.map( o -> " " + o )
112-
.collect( joining( "\n" ) ),
113-
params.entrySet().stream()
114-
.map( e -> String.format( nvpFmt,
115-
e.getKey(),
116-
Stream.of( e.getValue().split( "\n" ) )
117-
.collect( joining( pad ) ) ) )
118-
.collect( joining( "\n" ) ) );
134+
+ "└%s┘",
135+
border, border, operationsStr, border,
136+
border, border, paramsStr, border);
119137
}
120138

121139
@Override
122140
public Set<String> fields() {
123-
return new TreeSet<>( parameters().keySet() );
141+
return new TreeSet<>(parameters().keySet());
124142
}
125143

126144
/**
@@ -132,37 +150,36 @@ public Set<String> fields() {
132150
* operation
133151
* @return <code>this</code>
134152
*/
135-
public WebSequence operation( String name,
136-
BiConsumer<WebDriver, Map<String, String>> op ) {
137-
operations.put( name, op );
153+
public WebSequence operation(String name,
154+
BiConsumer<WebDriver, Map<String, String>> op) {
155+
operations.put(name, op);
138156
return self();
139157
}
140158

141159
@Override
142-
protected Object access( String field ) {
143-
return parameters().get( field );
160+
protected Object access(String field) {
161+
return parameters().get(field);
144162
}
145163

146164
private SortedMap<String, BiConsumer<WebDriver, Map<String, String>>> operations() {
147165
SortedMap<String, BiConsumer<WebDriver, Map<String, String>>> op = new TreeMap<>();
148-
if( parent != null ) {
149-
op.putAll( parent.operations() );
166+
if (parent != null) {
167+
op.putAll(parent.operations());
150168
}
151-
op.putAll( operations );
169+
op.putAll(operations);
152170
return op;
153171
}
154172

155173
private Map<String, String> parameters() {
156174
Map<String, String> p = new TreeMap<>();
157-
if( parent != null ) {
158-
p.putAll( parent.parameters() );
175+
if (parent != null) {
176+
p.putAll(parent.parameters());
159177
}
160-
for( Update update : updates ) {
161-
if( update.value() == DELETE ) {
162-
p.remove( update.field() );
163-
}
164-
else {
165-
p.put( update.field(), String.valueOf( update.value() ) );
178+
for (Update update : updates) {
179+
if (update.value() == DELETE) {
180+
p.remove(update.field());
181+
} else {
182+
p.put(update.field(), String.valueOf(update.value()));
166183
}
167184
}
168185
return p;
@@ -174,36 +191,33 @@ private Map<String, String> parameters() {
174191
* @param driver the browser to drive
175192
* @return The state of the parameters map after all operations have completed
176193
*/
177-
public byte[] process( WebDriver driver ) {
194+
public byte[] process(WebDriver driver) {
178195
Map<String, String> params = parameters();
179-
operations().forEach( ( name, op ) -> {
180-
if( op != null ) {
196+
operations().forEach((name, op) -> {
197+
if (op != null) {
181198
try {
182-
op.accept( driver, params );
183-
}
184-
catch( Exception e ) {
199+
op.accept(driver, params);
200+
} catch (Exception e) {
185201
// our operation has failed!
186202
String url = "No URL";
187203
String source = "No page source";
188204
try {
189205
url = driver.getCurrentUrl();
190206
source = driver.getPageSource();
191-
}
192-
catch( Exception f ) {
207+
} catch (Exception f) {
193208
source = f.getMessage();
194209
}
195-
throw new IllegalStateException( String.format(
210+
throw new IllegalStateException(String.format(
196211
"Operation '%s' failed on page '%s'\n%s",
197-
name, url, source ),
198-
e );
212+
name, url, source),
213+
e);
199214
}
200215
}
201-
} );
216+
});
202217
try {
203-
return JSON.writeValueAsBytes( params );
204-
}
205-
catch( IOException ioe ) {
206-
throw new UncheckedIOException( ioe );
218+
return JSON.writeValueAsBytes(params);
219+
} catch (IOException ioe) {
220+
throw new UncheckedIOException(ioe);
207221
}
208222
}
209223

0 commit comments

Comments
 (0)