23
23
import io .vertx .core .MultiMap ;
24
24
import io .vertx .core .http .HttpHeaders ;
25
25
import io .vertx .core .http .HttpServerResponse ;
26
+ import io .vertx .ext .web .RequestBody ;
26
27
import io .vertx .ext .web .Router ;
27
28
import io .vertx .ext .web .RoutingContext ;
28
29
import io .vertx .ext .web .handler .BodyHandler ;
32
33
import org .citrusframework .TestClass ;
33
34
import org .citrusframework .main .CitrusAppConfiguration ;
34
35
import org .citrusframework .main .TestRunConfiguration ;
35
- import org .citrusframework .remote .controller .RunController ;
36
36
import org .citrusframework .remote .job .RunJob ;
37
37
import org .citrusframework .remote .model .RemoteResult ;
38
- import org .citrusframework .remote .reporter . RemoteTestResultReporter ;
38
+ import org .citrusframework .remote .listener . RemoteTestListener ;
39
39
import org .citrusframework .remote .transformer .JsonRequestTransformer ;
40
40
import org .citrusframework .remote .transformer .JsonResponseTransformer ;
41
41
import org .citrusframework .report .JUnitReporter ;
48
48
import java .net .URLDecoder ;
49
49
import java .nio .file .Files ;
50
50
import java .nio .file .Path ;
51
- import java .util .ArrayList ;
52
51
import java .util .Collections ;
53
52
import java .util .List ;
54
53
import java .util .Optional ;
@@ -84,8 +83,8 @@ public class CitrusRemoteApplication extends AbstractVerticle {
84
83
private Future <List <RemoteResult >> remoteResultFuture ;
85
84
86
85
/** Latest test reports */
87
- private final RemoteTestResultReporter remoteTestResultReporter =
88
- new RemoteTestResultReporter ();
86
+ private final RemoteTestListener remoteTestListener =
87
+ new RemoteTestListener ();
89
88
90
89
/** Router customizations */
91
90
private final List <Consumer <Router >> routerCustomizations ;
@@ -111,16 +110,13 @@ public CitrusRemoteApplication(
111
110
@ Override
112
111
public void start () {
113
112
CitrusInstanceManager .mode (CitrusInstanceStrategy .SINGLETON );
114
- CitrusInstanceManager . addInstanceProcessor ( citrus ->
115
- citrus . addTestReporter ( remoteTestResultReporter ));
113
+ CitrusInstanceManager
114
+ . addInstanceProcessor ( citrus -> citrus . addTestListener ( remoteTestListener ));
116
115
117
116
Router router = Router .router (getVertx ());
118
117
router .route ().handler (BodyHandler .create ());
119
118
router .route ().handler (ctx -> {
120
- logger .info (
121
- "{} {}" ,
122
- ctx .request ().method (),
123
- ctx .request ().uri ());
119
+ logger .info ("{} {}" , ctx .request ().method (), ctx .request ().uri ());
124
120
ctx .next ();
125
121
});
126
122
addHealthEndpoint (router );
@@ -140,7 +136,8 @@ public void start() {
140
136
private static void addHealthEndpoint (Router router ) {
141
137
router .get ("/health" )
142
138
.handler (wrapThrowingHandler (ctx ->
143
- ctx .response ().putHeader (HttpHeaders .CONTENT_TYPE , APPLICATION_JSON )
139
+ ctx .response ()
140
+ .putHeader (HttpHeaders .CONTENT_TYPE , APPLICATION_JSON )
144
141
.end ("{ \" status\" : \" UP\" }" )));
145
142
}
146
143
@@ -178,17 +175,17 @@ private void addResultsEndpoints(Router router) {
178
175
response .end (responseTransformer .render (results )))
179
176
.onFailure (throwable -> response
180
177
.setStatusCode (HttpResponseStatus .PARTIAL_CONTENT .code ())
181
- .end (responseTransformer .render (Collections .emptyList ())));
178
+ .end (responseTransformer
179
+ .render (remoteTestListener .toRemoteResults ())));
182
180
} else {
183
- final List <RemoteResult > results = new ArrayList <>();
184
- remoteTestResultReporter .getLatestResults ().doWithResults (result ->
185
- results .add (RemoteResult .fromTestResult (result )));
181
+ final List <RemoteResult > results = remoteTestListener .toRemoteResults ();
182
+ logger .info ("results = {}" , results .size ());
186
183
response .end (responseTransformer .render (results ));
187
184
}
188
185
}));
189
186
router .get ("/results" )
190
- .handler (ctx -> ctx .response (). end (
191
- responseTransformer .render (remoteTestResultReporter . getTestReport ())));
187
+ .handler (ctx -> ctx .response ()
188
+ . end ( responseTransformer .render (remoteTestListener . generateTestReport ())));
192
189
router .get ("/results/files" )
193
190
.handler (wrapThrowingHandler (ctx -> {
194
191
File junitReportsFolder = new File (getJUnitReportsFolder ());
@@ -239,31 +236,37 @@ private void addResultsEndpoints(Router router) {
239
236
240
237
private void addRunEndpoints (Router router ) {
241
238
router .get ("/run" )
242
- .handler (wrapThrowingHandler (ctx -> {
243
- TestRunConfiguration runConfiguration = constructRunConfig (ctx );
244
- runTestsAsync (runConfiguration , ctx .response ());
245
- }));
239
+ .handler (wrapThrowingHandler (ctx ->
240
+ runTestsAsync (constructRunConfig (ctx .request ().params ()), ctx .response ())));
241
+ router .post ("/run" )
242
+ .handler (wrapThrowingHandler (ctx ->
243
+ runTestsAsync (constructRunConfig (ctx .body ()), ctx .response ())));
246
244
router .put ("/run" )
247
245
.handler (wrapThrowingHandler (ctx -> {
248
- remoteResultFuture = Future .fromCompletionStage (CompletableFuture .supplyAsync (
249
- constructTestRun (ctx )::call ,
250
- executorService ));
246
+ remoteTestListener .reset ();
247
+ remoteResultFuture = startTestsAsync (constructRunConfig (ctx .body ()));
251
248
ctx .response ().end ("" );
252
249
}));
253
- router .post ("/run" )
254
- .handler (wrapThrowingHandler (ctx -> {
255
- HttpServerResponse response = ctx .response ();
256
- TestRunConfiguration runConfiguration = requestTransformer .read (
257
- ctx .body ().asString (),
258
- TestRunConfiguration .class );
259
- runTestsAsync (runConfiguration , response );
260
- }));
261
250
}
262
251
263
- private TestRunConfiguration constructRunConfig (RoutingContext ctx )
252
+ public static Handler <RoutingContext > wrapThrowingHandler (
253
+ ThrowingHandler <RoutingContext > handler ) {
254
+ return ctx -> {
255
+ try {
256
+ handler .handle (ctx );
257
+ } catch (Exception e ) {
258
+ ctx .response ()
259
+ .setStatusCode (HttpResponseStatus .INTERNAL_SERVER_ERROR .code ())
260
+ .end (e .getMessage ());
261
+ }
262
+ };
263
+ }
264
+
265
+
266
+
267
+ private TestRunConfiguration constructRunConfig (MultiMap queryParams )
264
268
throws UnsupportedEncodingException {
265
269
TestRunConfiguration runConfiguration = new TestRunConfiguration ();
266
- MultiMap queryParams = ctx .request ().params ();
267
270
if (queryParams .contains ("engine" )) {
268
271
String engine = queryParams .get ("engine" );
269
272
runConfiguration .setEngine (URLDecoder .decode (engine , ENCODING ));
@@ -291,12 +294,16 @@ private TestRunConfiguration constructRunConfig(RoutingContext ctx)
291
294
return runConfiguration ;
292
295
}
293
296
297
+ private TestRunConfiguration constructRunConfig (RequestBody body ) {
298
+ return requestTransformer .read (body .asString (), TestRunConfiguration .class );
299
+ }
300
+
294
301
private void runTestsAsync (
295
302
TestRunConfiguration runConfiguration ,
296
303
HttpServerResponse response ) {
297
304
Future
298
305
.fromCompletionStage (CompletableFuture .supplyAsync (
299
- () -> runTests ( runConfiguration ),
306
+ new RunJob ( configuration , runConfiguration , remoteTestListener ),
300
307
executorService ))
301
308
.onSuccess (results ->
302
309
response .end (responseTransformer .render (results )))
@@ -305,16 +312,10 @@ private void runTestsAsync(
305
312
.end (error .getMessage ()));
306
313
}
307
314
308
- private RunJob constructTestRun (RoutingContext ctx ) {
309
- TestRunConfiguration config = requestTransformer .read (
310
- ctx .body ().asString (),
311
- TestRunConfiguration .class );
312
- return new RunJob (config ) {
313
- @ Override
314
- public List <RemoteResult > run (TestRunConfiguration runConfiguration ) {
315
- return runTests (runConfiguration );
316
- }
317
- };
315
+ private Future <List <RemoteResult >> startTestsAsync (TestRunConfiguration testRunConfiguration ) {
316
+ return Future .fromCompletionStage (CompletableFuture .supplyAsync (
317
+ new RunJob (configuration , testRunConfiguration , remoteTestListener ),
318
+ executorService ));
318
319
}
319
320
320
321
private void addConfigEndpoints (Router router ) {
@@ -330,61 +331,18 @@ private void addConfigEndpoints(Router router) {
330
331
CitrusAppConfiguration .class ))));
331
332
}
332
333
333
- public static Handler <RoutingContext > wrapThrowingHandler (
334
- ThrowingHandler <RoutingContext > handler ) {
335
- return ctx -> {
336
- try {
337
- handler .handle (ctx );
338
- } catch (Exception e ) {
339
- ctx .response ().setStatusCode (HttpResponseStatus .INTERNAL_SERVER_ERROR .code ())
340
- .end (e .getMessage ());
341
- }
342
- };
343
- }
344
-
345
- /**
346
- * Construct run controller and execute with given configuration.
347
- * @param runConfiguration
348
- * @return remote results
349
- */
350
- private List <RemoteResult > runTests (TestRunConfiguration runConfiguration ) {
351
- RunController runController = new RunController (configuration );
352
-
353
- runController .setEngine (runConfiguration .getEngine ());
354
- runController .setIncludes (runConfiguration .getIncludes ());
355
-
356
- if (!runConfiguration .getDefaultProperties ().isEmpty ()) {
357
- runController .addDefaultProperties (runConfiguration .getDefaultProperties ());
358
- }
359
-
360
- if (runConfiguration .getPackages ().isEmpty () && runConfiguration .getTestSources ().isEmpty ()) {
361
- runController .runAll ();
362
- }
363
-
364
- if (!runConfiguration .getPackages ().isEmpty ()) {
365
- runController .runPackages (runConfiguration .getPackages ());
366
- }
367
-
368
- if (!runConfiguration .getTestSources ().isEmpty ()) {
369
- runController .runClasses (runConfiguration .getTestSources ());
370
- }
371
-
372
- List <RemoteResult > results = new ArrayList <>();
373
- remoteTestResultReporter .getLatestResults ().doWithResults (result -> results .add (RemoteResult .fromTestResult (result )));
374
- return results ;
375
- }
376
-
377
334
/**
378
335
* Find reports folder based in unit testing framework present on classpath.
379
336
* @return
380
337
*/
381
338
private String getJUnitReportsFolder () {
382
-
383
339
if (isPresent ("org.testng.annotations.Test" )) {
384
340
return "test-output" + File .separator + "junitreports" ;
385
341
} else if (isPresent ("org.junit.Test" )) {
386
342
JUnitReporter jUnitReporter = new JUnitReporter ();
387
- return jUnitReporter .getReportDirectory () + File .separator + jUnitReporter .getOutputDirectory ();
343
+ return jUnitReporter .getReportDirectory () +
344
+ File .separator +
345
+ jUnitReporter .getOutputDirectory ();
388
346
} else {
389
347
return new LoggingReporter ().getReportDirectory ();
390
348
}
0 commit comments