33
33
import com .epam .ta .reportportal .ws .model .attribute .ItemAttributesRQ ;
34
34
import com .epam .ta .reportportal .ws .model .launch .StartLaunchRQ ;
35
35
import io .reactivex .Maybe ;
36
+ import jakarta .annotation .Nonnull ;
37
+ import jakarta .annotation .Nullable ;
36
38
import org .apache .commons .lang3 .StringUtils ;
37
- import org .junit .jupiter .api .DisplayName ;
38
39
import org .junit .jupiter .api .*;
40
+ import org .junit .jupiter .api .DisplayName ;
39
41
import org .junit .jupiter .api .extension .*;
40
42
import org .opentest4j .TestAbortedException ;
41
43
import org .slf4j .Logger ;
42
44
import org .slf4j .LoggerFactory ;
43
45
44
- import javax .annotation .Nonnull ;
45
- import javax .annotation .Nullable ;
46
46
import java .lang .reflect .AnnotatedElement ;
47
47
import java .lang .reflect .Constructor ;
48
48
import java .lang .reflect .Method ;
49
+ import java .time .Instant ;
50
+ import java .time .temporal .ChronoUnit ;
49
51
import java .util .*;
50
52
import java .util .concurrent .ConcurrentHashMap ;
51
53
import java .util .function .Predicate ;
65
67
*/
66
68
public class ReportPortalExtension
67
69
implements Extension , BeforeAllCallback , BeforeEachCallback , InvocationInterceptor , AfterTestExecutionCallback , AfterAllCallback ,
68
- TestWatcher {
70
+ TestWatcher {
69
71
70
72
private static final Logger LOGGER = LoggerFactory .getLogger (ReportPortalExtension .class );
71
73
72
- private static final Set <String > ASSUMPTION_CLASSES = new HashSet <>(Arrays .asList (TestAbortedException .class .getCanonicalName (),
74
+ private static final Set <String > ASSUMPTION_CLASSES = new HashSet <>(Arrays .asList (
75
+ TestAbortedException .class .getCanonicalName (),
73
76
"org.junit.AssumptionViolatedException"
74
77
));
75
78
@@ -112,7 +115,7 @@ private void finish(String id) {
112
115
113
116
private static void finish (Launch launch ) {
114
117
FinishExecutionRQ rq = new FinishExecutionRQ ();
115
- rq .setEndTime (Calendar . getInstance (). getTime ());
118
+ rq .setEndTime (Instant . now ());
116
119
launch .finish (rq );
117
120
}
118
121
@@ -134,7 +137,7 @@ protected StartLaunchRQ buildStartLaunchRq(ListenerParameters parameters) {
134
137
Set <ItemAttributesRQ > attributes = new HashSet <>(parameters .getAttributes ());
135
138
attributes .addAll (collectSystemAttributes (parameters .getSkippedAnIssue ()));
136
139
rq .setAttributes (attributes );
137
- rq .setStartTime (Calendar . getInstance (). getTime ());
140
+ rq .setStartTime (Instant . now ());
138
141
rq .setRerun (parameters .isRerun ());
139
142
rq .setRerunOf (StringUtils .isNotBlank (parameters .getRerunOf ()) ? parameters .getRerunOf () : null );
140
143
return rq ;
@@ -164,19 +167,21 @@ protected String getLaunchId(ExtensionContext context) {
164
167
* @return represents current launch
165
168
*/
166
169
protected Launch getLaunch (ExtensionContext context ) {
167
- return launchMap .computeIfAbsent (getLaunchId (context ), id -> {
168
- ReportPortal rp = getReporter ();
169
- ListenerParameters params = rp .getParameters ();
170
- StartLaunchRQ rq = buildStartLaunchRq (params );
171
-
172
- Launch launch = rp .newLaunch (rq );
173
- Runtime .getRuntime ().addShutdownHook (getShutdownHook (id ));
174
- Maybe <String > launchIdResponse = launch .start ();
175
- if (params .isCallbackReportingEnabled ()) {
176
- TEST_ITEM_TREE .setLaunchId (launchIdResponse );
177
- }
178
- return launch ;
179
- });
170
+ return launchMap .computeIfAbsent (
171
+ getLaunchId (context ), id -> {
172
+ ReportPortal rp = getReporter ();
173
+ ListenerParameters params = rp .getParameters ();
174
+ StartLaunchRQ rq = buildStartLaunchRq (params );
175
+
176
+ Launch launch = rp .newLaunch (rq );
177
+ Runtime .getRuntime ().addShutdownHook (getShutdownHook (id ));
178
+ Maybe <String > launchIdResponse = launch .start ();
179
+ if (params .isCallbackReportingEnabled ()) {
180
+ TEST_ITEM_TREE .setLaunchId (launchIdResponse );
181
+ }
182
+ return launch ;
183
+ }
184
+ );
180
185
}
181
186
182
187
@ Override
@@ -377,7 +382,7 @@ public void testFailed(ExtensionContext context, Throwable cause) {
377
382
*/
378
383
protected void finishBeforeAll (Invocation <Void > invocation , ReflectiveInvocationContext <Method > invocationContext ,
379
384
ExtensionContext context , Maybe <String > id ) throws Throwable {
380
- Date startTime = Calendar . getInstance (). getTime ();
385
+ Instant startTime = Instant . now ();
381
386
try {
382
387
finishBeforeAfter (invocation , context , id );
383
388
} catch (Throwable throwable ) {
@@ -398,7 +403,7 @@ protected void finishBeforeAll(Invocation<Void> invocation, ReflectiveInvocation
398
403
*/
399
404
protected void finishBeforeEach (Invocation <Void > invocation , ReflectiveInvocationContext <Method > invocationContext ,
400
405
ExtensionContext context , Maybe <String > id ) throws Throwable {
401
- Date startTime = Calendar . getInstance (). getTime ();
406
+ Instant startTime = Instant . now ();
402
407
try {
403
408
finishBeforeAfter (invocation , context , id );
404
409
} catch (Throwable throwable ) {
@@ -465,29 +470,31 @@ protected void startTestItem(ExtensionContext context, List<Object> arguments, I
465
470
* @param startTime a start time of the item
466
471
*/
467
472
protected void startTestItem (@ Nonnull final ExtensionContext context , @ Nonnull final List <Object > arguments ,
468
- @ Nonnull final ItemType itemType , @ Nullable final String description , @ Nullable final Date startTime ) {
469
- idMapping .computeIfAbsent (context , c -> {
470
- StartTestItemRQ rq = buildStartStepRq (c , arguments , itemType , description , startTime );
471
- Launch launch = getLaunch (c );
472
-
473
- Maybe <String > parentId = c .getParent ().flatMap (parent -> Optional .ofNullable (idMapping .get (parent ))).orElse (null );
474
- Maybe <String > itemId ;
475
- TestItemTree .TestItemLeaf leaf ;
476
- if (parentId == null ) {
477
- itemId = launch .startTestItem (rq );
478
- leaf = createTestItemLeaf (itemId );
479
- } else {
480
- itemId = launch .startTestItem (parentId , rq );
481
- leaf = createTestItemLeaf (parentId , itemId );
482
- }
483
- if (getReporter ().getParameters ().isCallbackReportingEnabled ()) {
484
- TEST_ITEM_TREE .getTestItems ().put (createItemTreeKey (rq .getName ()), leaf );
485
- }
486
- if (TEMPLATE == itemType ) {
487
- testTemplates .put (c , itemId );
488
- }
489
- return itemId ;
490
- });
473
+ @ Nonnull final ItemType itemType , @ Nullable final String description , @ Nullable final Instant startTime ) {
474
+ idMapping .computeIfAbsent (
475
+ context , c -> {
476
+ StartTestItemRQ rq = buildStartStepRq (c , arguments , itemType , description , startTime );
477
+ Launch launch = getLaunch (c );
478
+
479
+ Maybe <String > parentId = c .getParent ().flatMap (parent -> Optional .ofNullable (idMapping .get (parent ))).orElse (null );
480
+ Maybe <String > itemId ;
481
+ TestItemTree .TestItemLeaf leaf ;
482
+ if (parentId == null ) {
483
+ itemId = launch .startTestItem (rq );
484
+ leaf = createTestItemLeaf (itemId );
485
+ } else {
486
+ itemId = launch .startTestItem (parentId , rq );
487
+ leaf = createTestItemLeaf (parentId , itemId );
488
+ }
489
+ if (getReporter ().getParameters ().isCallbackReportingEnabled ()) {
490
+ TEST_ITEM_TREE .getTestItems ().put (createItemTreeKey (rq .getName ()), leaf );
491
+ }
492
+ if (TEMPLATE == itemType ) {
493
+ testTemplates .put (c , itemId );
494
+ }
495
+ return itemId ;
496
+ }
497
+ );
491
498
}
492
499
493
500
/**
@@ -648,10 +655,10 @@ protected Optional<Method> getTestMethod(ExtensionContext context) {
648
655
649
656
private Optional <Method > getOptionalTestMethod (ExtensionContext context ) {
650
657
Optional <Method > optionalMethod = context .getTestMethod ();
651
- if (! optionalMethod .isPresent ()) {
658
+ if (optionalMethod .isEmpty ()) {
652
659
//if not present means that we are in the dynamic test, in this case we need to move to the parent context
653
660
Optional <ExtensionContext > parentContext = context .getParent ();
654
- if (! parentContext .isPresent ()) {
661
+ if (parentContext .isEmpty ()) {
655
662
return Optional .empty ();
656
663
}
657
664
return parentContext .get ().getTestMethod ();
@@ -679,7 +686,7 @@ protected String createStepName(ExtensionContext context, ItemType itemType) {
679
686
}
680
687
681
688
Optional <Method > optionalMethod = getOptionalTestMethod (context );
682
- if (! optionalMethod .isPresent ()) {
689
+ if (optionalMethod .isEmpty ()) {
683
690
return defaultValue ;
684
691
}
685
692
Method method = optionalMethod .get ();
@@ -710,9 +717,9 @@ protected String createStepName(ExtensionContext context, ItemType itemType) {
710
717
*/
711
718
@ Nonnull
712
719
protected StartTestItemRQ buildStartStepRq (@ Nonnull final ExtensionContext context , @ Nonnull final List <Object > arguments ,
713
- @ Nonnull final ItemType itemType , @ Nullable final String description , @ Nullable final Date startTime ) {
720
+ @ Nonnull final ItemType itemType , @ Nullable final String description , @ Nullable final Instant startTime ) {
714
721
StartTestItemRQ rq = new StartTestItemRQ ();
715
- rq .setStartTime (ofNullable (startTime ).orElseGet (Calendar . getInstance ():: getTime ));
722
+ rq .setStartTime (ofNullable (startTime ).orElseGet (Instant :: now ));
716
723
rq .setName (createStepName (context , itemType ));
717
724
rq .setDescription (ofNullable (description ).orElseGet (() -> createStepDescription (context , itemType )));
718
725
rq .setType (itemType == TEMPLATE ? SUITE .name () : itemType .name ());
@@ -750,7 +757,7 @@ protected StartTestItemRQ buildStartStepRq(@Nonnull final ExtensionContext conte
750
757
protected StartTestItemRQ buildStartConfigurationRq (@ Nonnull Method method , @ Nonnull ExtensionContext parentContext ,
751
758
@ Nonnull ExtensionContext context , @ Nonnull ItemType itemType ) {
752
759
StartTestItemRQ rq = new StartTestItemRQ ();
753
- rq .setStartTime (Calendar . getInstance (). getTime ());
760
+ rq .setStartTime (Instant . now ());
754
761
Optional <Class <?>> testClass = context .getTestClass ();
755
762
if (testClass .isPresent ()) {
756
763
rq .setName (createConfigurationName (testClass .get (), method ));
@@ -805,15 +812,16 @@ protected FinishTestItemRQ buildFinishTestRq(@Nonnull ExtensionContext context,
805
812
}
806
813
rq .setIssue (getIssue (context ));
807
814
ofNullable (status ).ifPresent (s -> rq .setStatus (s .name ()));
808
- rq .setEndTime (Calendar . getInstance (). getTime ());
815
+ rq .setEndTime (Instant . now ());
809
816
return rq ;
810
817
}
811
818
812
819
@ Nullable
813
820
protected com .epam .ta .reportportal .ws .model .issue .Issue getIssue (@ Nonnull ExtensionContext context ) {
814
821
String stepName = createStepName (context , STEP );
815
822
List <ParameterResource > parameters = testParameters .containsKey (context ) ? testParameters .remove (context ) : Collections .emptyList ();
816
- return getOptionalTestMethod (context ).map (m -> ofNullable (m .getAnnotation (Issues .class )).map (i -> IssueUtils .createIssue (i ,
823
+ return getOptionalTestMethod (context ).map (m -> ofNullable (m .getAnnotation (Issues .class )).map (i -> IssueUtils .createIssue (
824
+ i ,
817
825
stepName ,
818
826
parameters
819
827
)).orElseGet (() -> IssueUtils .createIssue (m .getAnnotation (Issue .class ), stepName , parameters )))
@@ -832,7 +840,7 @@ protected com.epam.ta.reportportal.ws.model.issue.Issue getIssue(@Nonnull Extens
832
840
protected FinishTestItemRQ buildFinishTestItemRq (@ Nonnull ExtensionContext context , @ Nullable ItemStatus status ) {
833
841
FinishTestItemRQ rq = new FinishTestItemRQ ();
834
842
ofNullable (status ).ifPresent (s -> rq .setStatus (s .name ()));
835
- rq .setEndTime (Calendar . getInstance (). getTime ());
843
+ rq .setEndTime (Instant . now ());
836
844
return rq ;
837
845
}
838
846
@@ -879,7 +887,7 @@ protected String createStepDescription(ExtensionContext context, final ItemType
879
887
return defaultValue ;
880
888
}
881
889
Optional <Method > optionalMethod = getOptionalTestMethod (context );
882
- if (! optionalMethod .isPresent ()) {
890
+ if (optionalMethod .isEmpty ()) {
883
891
return defaultValue ;
884
892
}
885
893
Method method = optionalMethod .get ();
@@ -918,11 +926,11 @@ protected String createConfigurationDescription(Class<?> testClass, Method metho
918
926
* @param eventTime <code>@BeforeEach</code> start time
919
927
*/
920
928
protected void reportSkippedStep (ReflectiveInvocationContext <Method > invocationContext , ExtensionContext context , Throwable throwable ,
921
- Date eventTime ) {
922
- Date skipStartTime = Calendar . getInstance (). getTime ();
923
- if (skipStartTime .after (eventTime )) {
929
+ Instant eventTime ) {
930
+ Instant skipStartTime = Instant . now ();
931
+ if (skipStartTime .compareTo (eventTime ) > 0 ) {
924
932
// to fix item ordering when @AfterEach starts in the same millisecond as skipped test
925
- skipStartTime = new Date ( skipStartTime .getTime () - 1 );
933
+ skipStartTime = skipStartTime .minus ( 1 , ChronoUnit . MILLIS );
926
934
}
927
935
final ItemType itemType = STEP ;
928
936
startTestItem (context , invocationContext .getArguments (), itemType , createStepDescription (context , itemType ), skipStartTime );
@@ -941,6 +949,6 @@ protected void reportSkippedStep(ReflectiveInvocationContext<Method> invocationC
941
949
*/
942
950
@ SuppressWarnings ("unused" )
943
951
protected void reportSkippedClassTests (ReflectiveInvocationContext <Method > invocationContext , ExtensionContext context ,
944
- Date eventTime ) {
952
+ Instant eventTime ) {
945
953
}
946
954
}
0 commit comments