1
1
package com .mastercard .test .flow .msg .web ;
2
2
3
3
import static java .nio .charset .StandardCharsets .UTF_8 ;
4
- import static java .util .stream .Collectors .joining ;
5
4
6
5
import java .io .IOException ;
7
6
import java .io .UncheckedIOException ;
12
11
import java .util .TreeMap ;
13
12
import java .util .TreeSet ;
14
13
import java .util .function .BiConsumer ;
14
+ import java .util .stream .Collectors ;
15
15
import java .util .stream .Stream ;
16
16
17
17
import org .openqa .selenium .WebDriver ;
@@ -44,8 +44,7 @@ public class WebSequence extends AbstractMessage<WebSequence> {
44
44
45
45
private final WebSequence parent ;
46
46
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 <>();
49
48
50
49
private Map <String , String > results ;
51
50
@@ -56,71 +55,90 @@ public WebSequence() {
56
55
parent = null ;
57
56
}
58
57
59
- private WebSequence ( WebSequence parent ) {
58
+ private WebSequence (WebSequence parent ) {
60
59
this .parent = parent ;
61
60
}
62
61
63
62
@ Override
64
63
public WebSequence child () {
65
- return copyMasksTo ( new WebSequence ( self () ) );
64
+ return copyMasksTo (new WebSequence (self ()) );
66
65
}
67
66
68
67
@ 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 );
72
71
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 (
78
76
"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 );
81
79
}
82
80
return peer ;
83
81
}
84
82
85
83
@ Override
86
84
public byte [] content () {
87
85
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 );
92
89
}
93
90
}
94
91
95
92
@ Override
96
93
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.
100
99
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 "
107
133
+ "%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 );
119
137
}
120
138
121
139
@ Override
122
140
public Set <String > fields () {
123
- return new TreeSet <>( parameters ().keySet () );
141
+ return new TreeSet <>(parameters ().keySet ());
124
142
}
125
143
126
144
/**
@@ -132,37 +150,36 @@ public Set<String> fields() {
132
150
* operation
133
151
* @return <code>this</code>
134
152
*/
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 );
138
156
return self ();
139
157
}
140
158
141
159
@ Override
142
- protected Object access ( String field ) {
143
- return parameters ().get ( field );
160
+ protected Object access (String field ) {
161
+ return parameters ().get (field );
144
162
}
145
163
146
164
private SortedMap <String , BiConsumer <WebDriver , Map <String , String >>> operations () {
147
165
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 ());
150
168
}
151
- op .putAll ( operations );
169
+ op .putAll (operations );
152
170
return op ;
153
171
}
154
172
155
173
private Map <String , String > parameters () {
156
174
Map <String , String > p = new TreeMap <>();
157
- if ( parent != null ) {
158
- p .putAll ( parent .parameters () );
175
+ if ( parent != null ) {
176
+ p .putAll (parent .parameters ());
159
177
}
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 ()));
166
183
}
167
184
}
168
185
return p ;
@@ -174,36 +191,33 @@ private Map<String, String> parameters() {
174
191
* @param driver the browser to drive
175
192
* @return The state of the parameters map after all operations have completed
176
193
*/
177
- public byte [] process ( WebDriver driver ) {
194
+ public byte [] process (WebDriver driver ) {
178
195
Map <String , String > params = parameters ();
179
- operations ().forEach ( ( name , op ) -> {
180
- if ( op != null ) {
196
+ operations ().forEach (( name , op ) -> {
197
+ if ( op != null ) {
181
198
try {
182
- op .accept ( driver , params );
183
- }
184
- catch ( Exception e ) {
199
+ op .accept (driver , params );
200
+ } catch (Exception e ) {
185
201
// our operation has failed!
186
202
String url = "No URL" ;
187
203
String source = "No page source" ;
188
204
try {
189
205
url = driver .getCurrentUrl ();
190
206
source = driver .getPageSource ();
191
- }
192
- catch ( Exception f ) {
207
+ } catch (Exception f ) {
193
208
source = f .getMessage ();
194
209
}
195
- throw new IllegalStateException ( String .format (
210
+ throw new IllegalStateException (String .format (
196
211
"Operation '%s' failed on page '%s'\n %s" ,
197
- name , url , source ),
198
- e );
212
+ name , url , source ),
213
+ e );
199
214
}
200
215
}
201
- } );
216
+ });
202
217
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 );
207
221
}
208
222
}
209
223
0 commit comments