3333import com .epam .ta .reportportal .ws .model .attribute .ItemAttributesRQ ;
3434import com .epam .ta .reportportal .ws .model .launch .StartLaunchRQ ;
3535import io .reactivex .Maybe ;
36+ import jakarta .annotation .Nonnull ;
37+ import jakarta .annotation .Nullable ;
3638import org .apache .commons .lang3 .StringUtils ;
37- import org .junit .jupiter .api .DisplayName ;
3839import org .junit .jupiter .api .*;
40+ import org .junit .jupiter .api .DisplayName ;
3941import org .junit .jupiter .api .extension .*;
4042import org .opentest4j .TestAbortedException ;
4143import org .slf4j .Logger ;
4244import org .slf4j .LoggerFactory ;
4345
44- import javax .annotation .Nonnull ;
45- import javax .annotation .Nullable ;
4646import java .lang .reflect .AnnotatedElement ;
4747import java .lang .reflect .Constructor ;
4848import java .lang .reflect .Method ;
49+ import java .time .Instant ;
50+ import java .time .temporal .ChronoUnit ;
4951import java .util .*;
5052import java .util .concurrent .ConcurrentHashMap ;
5153import java .util .function .Predicate ;
6567 */
6668public class ReportPortalExtension
6769 implements Extension , BeforeAllCallback , BeforeEachCallback , InvocationInterceptor , AfterTestExecutionCallback , AfterAllCallback ,
68- TestWatcher {
70+ TestWatcher {
6971
7072 private static final Logger LOGGER = LoggerFactory .getLogger (ReportPortalExtension .class );
7173
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 (),
7376 "org.junit.AssumptionViolatedException"
7477 ));
7578
@@ -112,7 +115,7 @@ private void finish(String id) {
112115
113116 private static void finish (Launch launch ) {
114117 FinishExecutionRQ rq = new FinishExecutionRQ ();
115- rq .setEndTime (Calendar . getInstance (). getTime ());
118+ rq .setEndTime (Instant . now ());
116119 launch .finish (rq );
117120 }
118121
@@ -134,7 +137,7 @@ protected StartLaunchRQ buildStartLaunchRq(ListenerParameters parameters) {
134137 Set <ItemAttributesRQ > attributes = new HashSet <>(parameters .getAttributes ());
135138 attributes .addAll (collectSystemAttributes (parameters .getSkippedAnIssue ()));
136139 rq .setAttributes (attributes );
137- rq .setStartTime (Calendar . getInstance (). getTime ());
140+ rq .setStartTime (Instant . now ());
138141 rq .setRerun (parameters .isRerun ());
139142 rq .setRerunOf (StringUtils .isNotBlank (parameters .getRerunOf ()) ? parameters .getRerunOf () : null );
140143 return rq ;
@@ -164,19 +167,21 @@ protected String getLaunchId(ExtensionContext context) {
164167 * @return represents current launch
165168 */
166169 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+ );
180185 }
181186
182187 @ Override
@@ -377,7 +382,7 @@ public void testFailed(ExtensionContext context, Throwable cause) {
377382 */
378383 protected void finishBeforeAll (Invocation <Void > invocation , ReflectiveInvocationContext <Method > invocationContext ,
379384 ExtensionContext context , Maybe <String > id ) throws Throwable {
380- Date startTime = Calendar . getInstance (). getTime ();
385+ Instant startTime = Instant . now ();
381386 try {
382387 finishBeforeAfter (invocation , context , id );
383388 } catch (Throwable throwable ) {
@@ -398,7 +403,7 @@ protected void finishBeforeAll(Invocation<Void> invocation, ReflectiveInvocation
398403 */
399404 protected void finishBeforeEach (Invocation <Void > invocation , ReflectiveInvocationContext <Method > invocationContext ,
400405 ExtensionContext context , Maybe <String > id ) throws Throwable {
401- Date startTime = Calendar . getInstance (). getTime ();
406+ Instant startTime = Instant . now ();
402407 try {
403408 finishBeforeAfter (invocation , context , id );
404409 } catch (Throwable throwable ) {
@@ -465,29 +470,31 @@ protected void startTestItem(ExtensionContext context, List<Object> arguments, I
465470 * @param startTime a start time of the item
466471 */
467472 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+ );
491498 }
492499
493500 /**
@@ -648,10 +655,10 @@ protected Optional<Method> getTestMethod(ExtensionContext context) {
648655
649656 private Optional <Method > getOptionalTestMethod (ExtensionContext context ) {
650657 Optional <Method > optionalMethod = context .getTestMethod ();
651- if (! optionalMethod .isPresent ()) {
658+ if (optionalMethod .isEmpty ()) {
652659 //if not present means that we are in the dynamic test, in this case we need to move to the parent context
653660 Optional <ExtensionContext > parentContext = context .getParent ();
654- if (! parentContext .isPresent ()) {
661+ if (parentContext .isEmpty ()) {
655662 return Optional .empty ();
656663 }
657664 return parentContext .get ().getTestMethod ();
@@ -679,7 +686,7 @@ protected String createStepName(ExtensionContext context, ItemType itemType) {
679686 }
680687
681688 Optional <Method > optionalMethod = getOptionalTestMethod (context );
682- if (! optionalMethod .isPresent ()) {
689+ if (optionalMethod .isEmpty ()) {
683690 return defaultValue ;
684691 }
685692 Method method = optionalMethod .get ();
@@ -710,9 +717,9 @@ protected String createStepName(ExtensionContext context, ItemType itemType) {
710717 */
711718 @ Nonnull
712719 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 ) {
714721 StartTestItemRQ rq = new StartTestItemRQ ();
715- rq .setStartTime (ofNullable (startTime ).orElseGet (Calendar . getInstance ():: getTime ));
722+ rq .setStartTime (ofNullable (startTime ).orElseGet (Instant :: now ));
716723 rq .setName (createStepName (context , itemType ));
717724 rq .setDescription (ofNullable (description ).orElseGet (() -> createStepDescription (context , itemType )));
718725 rq .setType (itemType == TEMPLATE ? SUITE .name () : itemType .name ());
@@ -750,7 +757,7 @@ protected StartTestItemRQ buildStartStepRq(@Nonnull final ExtensionContext conte
750757 protected StartTestItemRQ buildStartConfigurationRq (@ Nonnull Method method , @ Nonnull ExtensionContext parentContext ,
751758 @ Nonnull ExtensionContext context , @ Nonnull ItemType itemType ) {
752759 StartTestItemRQ rq = new StartTestItemRQ ();
753- rq .setStartTime (Calendar . getInstance (). getTime ());
760+ rq .setStartTime (Instant . now ());
754761 Optional <Class <?>> testClass = context .getTestClass ();
755762 if (testClass .isPresent ()) {
756763 rq .setName (createConfigurationName (testClass .get (), method ));
@@ -805,15 +812,16 @@ protected FinishTestItemRQ buildFinishTestRq(@Nonnull ExtensionContext context,
805812 }
806813 rq .setIssue (getIssue (context ));
807814 ofNullable (status ).ifPresent (s -> rq .setStatus (s .name ()));
808- rq .setEndTime (Calendar . getInstance (). getTime ());
815+ rq .setEndTime (Instant . now ());
809816 return rq ;
810817 }
811818
812819 @ Nullable
813820 protected com .epam .ta .reportportal .ws .model .issue .Issue getIssue (@ Nonnull ExtensionContext context ) {
814821 String stepName = createStepName (context , STEP );
815822 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 ,
817825 stepName ,
818826 parameters
819827 )).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
832840 protected FinishTestItemRQ buildFinishTestItemRq (@ Nonnull ExtensionContext context , @ Nullable ItemStatus status ) {
833841 FinishTestItemRQ rq = new FinishTestItemRQ ();
834842 ofNullable (status ).ifPresent (s -> rq .setStatus (s .name ()));
835- rq .setEndTime (Calendar . getInstance (). getTime ());
843+ rq .setEndTime (Instant . now ());
836844 return rq ;
837845 }
838846
@@ -879,7 +887,7 @@ protected String createStepDescription(ExtensionContext context, final ItemType
879887 return defaultValue ;
880888 }
881889 Optional <Method > optionalMethod = getOptionalTestMethod (context );
882- if (! optionalMethod .isPresent ()) {
890+ if (optionalMethod .isEmpty ()) {
883891 return defaultValue ;
884892 }
885893 Method method = optionalMethod .get ();
@@ -918,11 +926,11 @@ protected String createConfigurationDescription(Class<?> testClass, Method metho
918926 * @param eventTime <code>@BeforeEach</code> start time
919927 */
920928 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 ) {
924932 // 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 );
926934 }
927935 final ItemType itemType = STEP ;
928936 startTestItem (context , invocationContext .getArguments (), itemType , createStepDescription (context , itemType ), skipStartTime );
@@ -941,6 +949,6 @@ protected void reportSkippedStep(ReflectiveInvocationContext<Method> invocationC
941949 */
942950 @ SuppressWarnings ("unused" )
943951 protected void reportSkippedClassTests (ReflectiveInvocationContext <Method > invocationContext , ExtensionContext context ,
944- Date eventTime ) {
952+ Instant eventTime ) {
945953 }
946954}
0 commit comments