From 6c1d7316f3e9a75e3e129ff986794dfa5a0c8d9d Mon Sep 17 00:00:00 2001 From: "vincenzo.giammona" Date: Mon, 27 May 2024 11:40:16 +0200 Subject: [PATCH 1/4] add ThreadUtils --- .../pn/cucumber/steps/DataTableTypeUtil.java | 78 +++++--------- .../pagopa/pn/cucumber/steps/SharedSteps.java | 80 +++++++------- .../steps/dataTable/DataTableTypeRaddAlt.java | 22 ++-- .../steps/pa/AnagraficaRaddAltSteps.java | 21 ++-- .../pn/cucumber/steps/pa/AppIOB2bSteps.java | 28 +++-- .../pn/cucumber/steps/pa/AsyncSteps.java | 17 ++- .../pa/AvanzamentoNotificheB2bSteps.java | 101 +++++------------- .../AvanzamentoNotificheWebhookB2bSteps.java | 4 +- .../steps/pa/InvioNotificheB2bSteps.java | 15 ++- .../pn/cucumber/steps/pa/RaddAltSteps.java | 10 +- .../serviceDesk/ApiServiceDeskSteps.java | 44 ++++---- .../steps/utilitySteps/ThreadUtils.java | 38 +++++++ 12 files changed, 186 insertions(+), 272 deletions(-) create mode 100644 src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/DataTableTypeUtil.java b/src/test/java/it/pagopa/pn/cucumber/steps/DataTableTypeUtil.java index 9aa254aef..29126e1d8 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/DataTableTypeUtil.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/DataTableTypeUtil.java @@ -9,6 +9,8 @@ import java.util.*; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitSeconds; import static it.pagopa.pn.cucumber.utils.NotificationValue.*; @@ -52,11 +54,8 @@ public synchronized NewNotificationRequestV23 convertNotificationRequest(Map(); for (int i = 0; i < Integer.parseInt(getValue(data, PAYMENT_MULTY_NUMBER.key)); i++) { - try { - Thread.sleep(1000); - } - catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(1000); + NotificationPaymentItem addPaymentsItem = new NotificationPaymentItem(); addPaymentsItem.pagoPa(getValue(data, PAYMENT_PAGOPA_FORM.key) == null ? null : (getValue(data, PAYMENT_PAGOPA_FORM.key).equalsIgnoreCase("NO") ? @@ -328,11 +316,8 @@ public synchronized NotificationRecipientV23 convertNotificationRecipient(Map(); for (int i = 0; i < Integer.parseInt(getValue(data, PAYMENT_MULTY_NUMBER.key)); i++) { - try { - Thread.sleep(1000); - } - catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + + threadWaitMilliseconds(1000); + it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externalb2bpa.model_v21.NotificationPaymentItem addPaymentsItem = new it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externalb2bpa.model_v21.NotificationPaymentItem(); addPaymentsItem.pagoPa(getValue(data, PAYMENT_PAGOPA_FORM.key) == null ? null : (getValue(data, PAYMENT_PAGOPA_FORM.key).equalsIgnoreCase("NO") ? @@ -616,11 +593,8 @@ public synchronized it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externa notificationRecipient.setPayments(listPayment); } - try { - Thread.sleep(2); - } catch (InterruptedException e) { - e.printStackTrace(); - } + threadWaitSeconds(2); + return notificationRecipient; } diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java index 5cc803bc8..771f5661d 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java @@ -14,27 +14,30 @@ import io.cucumber.java.en.When; import it.pagopa.pn.client.b2b.pa.PnPaB2bUtils; import it.pagopa.pn.client.b2b.pa.config.PnB2bClientTimingConfigs; +import it.pagopa.pn.client.b2b.pa.config.springconfig.RestTemplateConfiguration; import it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externalb2bpa.model.*; import it.pagopa.pn.client.b2b.pa.polling.design.PnPollingFactory; import it.pagopa.pn.client.b2b.pa.service.IPnPaB2bClient; import it.pagopa.pn.client.b2b.pa.service.IPnWebPaClient; import it.pagopa.pn.client.b2b.pa.service.IPnWebRecipientClient; import it.pagopa.pn.client.b2b.pa.service.IPnWebUserAttributesClient; -import it.pagopa.pn.client.b2b.pa.service.impl.*; +import it.pagopa.pn.client.b2b.pa.service.impl.PnExternalServiceClientImpl; +import it.pagopa.pn.client.b2b.pa.service.impl.PnGPDClientImpl; +import it.pagopa.pn.client.b2b.pa.service.impl.PnPaymentInfoClientImpl; +import it.pagopa.pn.client.b2b.pa.service.impl.PnServiceDeskClientImpl; import it.pagopa.pn.client.b2b.pa.service.utils.SettableApiKey; import it.pagopa.pn.client.b2b.pa.service.utils.SettableBearerToken; -import it.pagopa.pn.client.b2b.pa.config.springconfig.RestTemplateConfiguration; +import it.pagopa.pn.client.b2b.webhook.generated.openapi.clients.externalb2bwebhook.model_v2.ProgressResponseElement; +import it.pagopa.pn.client.b2b.webhook.generated.openapi.clients.externalb2bwebhook.model_v2_3.ProgressResponseElementV23; +import it.pagopa.pn.client.b2b.webhook.generated.openapi.clients.externalb2bwebhook.model_v2_3.StreamMetadataResponseV23; import it.pagopa.pn.client.web.generated.openapi.clients.externalApiKeyManager.model.RequestNewApiKey; import it.pagopa.pn.client.web.generated.openapi.clients.externalApiKeyManager.model.ResponseNewApiKey; import it.pagopa.pn.client.web.generated.openapi.clients.externalUserAttributes.addressBook.model.LegalAndUnverifiedDigitalAddress; import it.pagopa.pn.client.web.generated.openapi.clients.externalUserAttributes.addressBook.model.LegalChannelType; -import it.pagopa.pn.client.b2b.webhook.generated.openapi.clients.externalb2bwebhook.model_v2.ProgressResponseElement; -import it.pagopa.pn.client.b2b.webhook.generated.openapi.clients.externalb2bwebhook.model_v2_3.ProgressResponseElementV23; -import it.pagopa.pn.client.b2b.webhook.generated.openapi.clients.externalb2bwebhook.model_v2_3.StreamMetadataResponseV23; import it.pagopa.pn.cucumber.utils.*; -import lombok.extern.slf4j.Slf4j; import lombok.Getter; import lombok.Setter; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; import org.opentest4j.AssertionFailedError; import org.slf4j.MDC; @@ -45,17 +48,17 @@ import org.springframework.context.annotation.Scope; import org.springframework.util.Base64Utils; import org.springframework.web.client.HttpStatusCodeException; + import java.io.IOException; import java.security.SecureRandom; import java.time.Duration; import java.time.OffsetDateTime; import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.TimeUnit; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; import static it.pagopa.pn.cucumber.utils.FiscalCodeGenerator.generateCF; import static it.pagopa.pn.cucumber.utils.NotificationValue.*; -import static org.awaitility.Awaitility.await; @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @@ -371,14 +374,14 @@ public void vengonoInviateNotifichePerLUtenteSignorCasualeConIlESiAspettaFinoAll Thread t = new Thread(() -> { //INVIO NOTIFICA ED ATTESA ACCEPTED NewNotificationResponse internalNotificationResponse = Assertions.assertDoesNotThrow(() -> b2bUtils.uploadNotification(notification)); - threadWait(getWait()); + threadWaitMilliseconds(getWait()); FullSentNotificationV23 fullSentNotificationV23 = b2bUtils.waitForRequestAcceptation(internalNotificationResponse); Assertions.assertNotNull(fullSentNotificationV23); //ATTESA ELEMENTO DI TIMELINE TimelineElementV23 timelineElementV23 = null; for (int i = 0; i < 33; i++) { - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); fullSentNotificationV23 = b2bClient.getSentNotification(fullSentNotificationV23.getIun()); log.info("NOTIFICATION_TIMELINE: " + fullSentNotificationV23.getTimeline()); timelineElementV23 = fullSentNotificationV23.getTimeline().stream().filter(elem -> Objects.requireNonNull(elem.getCategory()).equals(TimelineElementCategoryV23.COMPLETELY_UNREACHABLE)).findAny().orElse(null); @@ -397,7 +400,7 @@ public void vengonoInviateNotifichePerLUtenteSignorCasualeConIlESiAspettaFinoAll boolean completed = false; while (attempts < 50) { - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); int counter = 0; for (Thread thread : threadList) { if (!thread.isAlive()) counter++; @@ -681,7 +684,7 @@ public void destinatarioCucumberSocietyParam(Map data) { @And("destinatario Signor casuale e:") public void destinatarioSignorCasualeMap(Map data) { - threadWait(new Random().nextInt(500)); + threadWaitMilliseconds(new Random().nextInt(500)); NotificationRecipientV23 notificationRecipientV23 = dataTableTypeUtil.convertNotificationRecipient(data); addRecipientToNotification(this.notificationRequest, updateNotificationRecipient(notificationRecipientV23, @@ -1097,12 +1100,12 @@ private void sendNotification(int wait) { notificationCreationDate = OffsetDateTime.now(); newNotificationResponse = b2bUtils.uploadNotification(notificationRequest); - threadWait(wait); + threadWaitMilliseconds(wait); notificationResponseComplete = b2bUtils.waitForRequestAcceptation(newNotificationResponse); }); - threadWait(wait); + threadWaitMilliseconds(wait); Assertions.assertNotNull(notificationResponseComplete); } catch (AssertionFailedError assertionFailedError) { @@ -1117,12 +1120,12 @@ private void sendNotificationNoAccept(int wait) { Assertions.assertDoesNotThrow(() -> { notificationCreationDate = OffsetDateTime.now(); - threadWait(wait); + threadWaitMilliseconds(wait); notificationResponseComplete = b2bUtils.waitForRequestNoAcceptation(newNotificationResponse); }); - threadWait(wait); + threadWaitMilliseconds(wait); Assertions.assertNull(notificationResponseComplete); } catch (AssertionFailedError assertionFailedError) { @@ -1138,13 +1141,13 @@ private void sendNotificationRapid(int wait) { notificationCreationDate = OffsetDateTime.now(); newNotificationResponse = b2bUtils.uploadNotification(notificationRequest); - threadWait(wait); + threadWaitMilliseconds(wait); notificationResponseComplete = b2bUtils.waitForRequestAcceptationShort(newNotificationResponse); }); - threadWait(wait); + threadWaitMilliseconds(wait); Assertions.assertNotNull(notificationResponseComplete); } catch (AssertionFailedError assertionFailedError) { @@ -1170,7 +1173,7 @@ private void sendNotificationRapidCancellPreRefused() { }); rifiutata = b2bUtils.waitForRequestNotRefused(newNotificationResponse); - threadWait(wait); + threadWaitMilliseconds(wait); Assertions.assertFalse(rifiutata); } catch (AssertionFailedError assertionFailedError) { @@ -1196,12 +1199,12 @@ private void sendNotificationV1() { notificationCreationDate = OffsetDateTime.now(); newNotificationResponseV1 = b2bUtils.uploadNotificationV1(notificationRequestV1); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); notificationResponseCompleteV1 = b2bUtils.waitForRequestAcceptationV1(newNotificationResponseV1); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationResponseCompleteV1); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1216,12 +1219,12 @@ private void sendNotificationV2() { notificationCreationDate = OffsetDateTime.now(); newNotificationResponseV2 = b2bUtils.uploadNotificationV2(notificationRequestV2); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); notificationResponseCompleteV2 = b2bUtils.waitForRequestAcceptationV2(newNotificationResponseV2); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationResponseCompleteV2); } catch (AssertionFailedError assertionFailedError) { @@ -1237,12 +1240,12 @@ private void sendNotificationV21() { notificationCreationDate = OffsetDateTime.now(); newNotificationResponseV21 = b2bUtils.uploadNotificationV21(notificationRequestV21); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); notificationResponseCompleteV21 = b2bUtils.waitForRequestAcceptationV21(newNotificationResponseV21); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationResponseCompleteV21); } catch (AssertionFailedError assertionFailedError) { @@ -1312,7 +1315,7 @@ private void sendNotificationWithErrorNotFindAllegato(boolean noUpload) { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(errorCode); } catch (AssertionFailedError assertionFailedError) { @@ -1330,7 +1333,7 @@ private void sendNotificationWithErrorNotFindAllegatoJson() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(errorCode); } catch (AssertionFailedError assertionFailedError) { @@ -1348,7 +1351,7 @@ private void sendNotificationWithErrorSha() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(errorCode); } catch (AssertionFailedError assertionFailedError) { @@ -1376,7 +1379,7 @@ private void sendNotificationWithErrorShaJson() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); @@ -1396,7 +1399,7 @@ private void sendNotificationWithWrongExtension() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); @@ -1415,7 +1418,7 @@ private void sendNotificationRefused() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); } catch (AssertionFailedError assertionFailedError) { @@ -1432,7 +1435,7 @@ private void sendNotificationRefusedOverSizeAllegato() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); @@ -1450,7 +1453,7 @@ private void sendNotificationRefusedInjectionAllegato() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); @@ -1468,7 +1471,7 @@ private void sendNotificationRefusedOver15Allegato() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); @@ -1987,15 +1990,6 @@ public List getDatiPagamentoVersionamento(Integer destinatario,Integer p return DatiPagamento; } - private static void threadWait(int wait) { - try { - await().atMost(wait, TimeUnit.MILLISECONDS); - } catch (RuntimeException exception) { - log.error("await error exeption"); - throw exception; - } - } - private void configureAndSendNotification(String paType, String version) { selectPaAndSenderTaxId(paType, version); switch (version.toLowerCase()) { diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/dataTable/DataTableTypeRaddAlt.java b/src/test/java/it/pagopa/pn/cucumber/steps/dataTable/DataTableTypeRaddAlt.java index c8aea340e..23e6fa262 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/dataTable/DataTableTypeRaddAlt.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/dataTable/DataTableTypeRaddAlt.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitSeconds; import static it.pagopa.pn.cucumber.utils.RaddAltValue.*; @@ -45,11 +46,8 @@ public synchronized CreateRegistryRequest convertRegistryRequestData(Map convertToListRegistryRequestData } - - try { - Thread.sleep(2); - } catch (InterruptedException e) { - e.printStackTrace(); - } + threadWaitSeconds(2); return listaSportelli; } @@ -102,11 +95,8 @@ public synchronized UpdateRegistryRequest convertUpdateRegistryRequest(Mapelem.getRequestId().equalsIgnoreCase(this.requestid) && elem.getStatus().equalsIgnoreCase(status)).findAny().orElse(null); if(status.equalsIgnoreCase("accepted")){ - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + threadWaitMilliseconds(20000); + } if (dato!=null && dato.getStatus().equalsIgnoreCase(status)) { diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AppIOB2bSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AppIOB2bSteps.java index ec7848961..f570042e5 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AppIOB2bSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AppIOB2bSteps.java @@ -19,6 +19,8 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; + @Slf4j public class AppIOB2bSteps { @@ -104,14 +106,11 @@ public void downloadPAGOPAAppIo(String typeDocument, String recipient) { iPnAppIOB2bClient.getReceivedNotificationAttachment(sharedSteps.getSentNotification().getIun(), typeDocument, selectTaxIdUser(recipient), Integer.parseInt(documents.get(0).getDocIdx())); if ((downloadResponse!= null && downloadResponse.getRetryAfter()!= null && downloadResponse.getRetryAfter()>0)){ - try { - System.out.println("SECONDO TENTATIVO"); - Thread.sleep(downloadResponse.getRetryAfter()*3); - downloadResponse = iPnAppIOB2bClient.getReceivedNotificationAttachment(sharedSteps.getSentNotification().getIun(), typeDocument, selectTaxIdUser(recipient), Integer.parseInt(documents.get(0).getDocIdx())); - - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + + System.out.println("SECONDO TENTATIVO"); + threadWaitMilliseconds(downloadResponse.getRetryAfter()*3); + downloadResponse = iPnAppIOB2bClient.getReceivedNotificationAttachment(sharedSteps.getSentNotification().getIun(), typeDocument, selectTaxIdUser(recipient), Integer.parseInt(documents.get(0).getDocIdx())); + } System.out.println(downloadResponse.toString()); @@ -157,14 +156,11 @@ public void downloadF24AppIo(String typeDocument, String recipient) { iPnAppIOB2bClient.getReceivedNotificationAttachmentByUrl(url, selectTaxIdUser(recipient)); if ((downloadResponse!= null && downloadResponse.getRetryAfter()!= null && downloadResponse.getRetryAfter()>0)){ - try { - System.out.println("SECONDO TENTATIVO"); - Thread.sleep(downloadResponse.getRetryAfter()*3); - downloadResponse = iPnAppIOB2bClient.getReceivedNotificationAttachmentByUrl(url, selectTaxIdUser(recipient)); - - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + + System.out.println("SECONDO TENTATIVO"); + threadWaitMilliseconds(downloadResponse.getRetryAfter()*3); + downloadResponse = iPnAppIOB2bClient.getReceivedNotificationAttachmentByUrl(url, selectTaxIdUser(recipient)); + } System.out.println(downloadResponse.toString()); diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AsyncSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AsyncSteps.java index 3213ea714..8ec35fda7 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AsyncSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AsyncSteps.java @@ -21,6 +21,8 @@ import java.time.format.DateTimeFormatter; import java.util.*; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; + @Slf4j public class AsyncSteps { @@ -51,11 +53,8 @@ public AsyncSteps(AvanzamentoNotificheB2bSteps avanzamentoNotificheB2bSteps, Sha private String generateRandomIuv(){ int randomSleepToRandomize = new Random().nextInt(100); - try { - Thread.sleep(randomSleepToRandomize); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + threadWaitMilliseconds(randomSleepToRandomize); + String threadNumber = (Thread.currentThread().getId()+""); String numberOfThread = threadNumber.length() < 2 ? "0"+threadNumber: threadNumber.substring(0, 2); String timeNano = System.nanoTime()+""; @@ -135,11 +134,9 @@ public void letturaAmountPosizioneDebitoria(String user) { amountGPD = paymentInfoResponse.get(0).getAmount(); break; } - try { - Thread.sleep(WAITING_PAYMENT_INFO); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + + threadWaitMilliseconds(WAITING_PAYMENT_INFO); + } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + "{la posizione debitoria " + (paymentInfoResponse == null ? "NULL" : paymentInfoResponse.toString()) + " }"; diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java index 01ef47769..5b2b791ce 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java @@ -32,6 +32,8 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.*; + +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; import static java.time.OffsetDateTime.now; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static org.awaitility.Awaitility.await; @@ -1183,12 +1185,8 @@ public void userDownloadLegalFactError(String user, String legalFactCategory,Str } private void downloadLegalFact(String legalFactCategory, boolean pa, boolean appIO, boolean webRecipient, String deliveryDetailCode) { - try { - Thread.sleep(sharedSteps.getWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(sharedSteps.getWait()); PnTimelineLegalFactV23 categoriesV23 = pnTimelineAndLegalFactV23.getCategory(legalFactCategory); @@ -1250,11 +1248,7 @@ private void downloadLegalFact(String legalFactCategory, boolean pa, boolean app } private void downloadLegalFactPecRecipient(String legalFactCategory, boolean pa, boolean appIO, boolean webRecipient, String deliveryDetailCode) { - try { - Thread.sleep(sharedSteps.getWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(sharedSteps.getWait()); TimelineElementV23 timelineElement = null; @@ -1324,11 +1318,8 @@ private void downloadLegalFactPecRecipient(String legalFactCategory, boolean pa, } private void downloadLegalFactId(String legalFactCategory, boolean pa, boolean appIO, boolean webRecipient, String deliveryDetailCode) { - try { - Thread.sleep(sharedSteps.getWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + + threadWaitMilliseconds(sharedSteps.getWait()); PnTimelineLegalFactV23 categoriesV23 = pnTimelineAndLegalFactV23.getCategory(legalFactCategory); @@ -1417,22 +1408,15 @@ public void checksNotificationNotHaveStatus(String status) { @Then("vengono verificati costo = {string} e data di perfezionamento della notifica") public void notificationPriceAndDateVerification(String price) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + + threadWaitMilliseconds(sharedSteps.getWait() * 2); priceVerification(price, null, 0); } @Then("vengono verificati costo = {string} e data di perfezionamento della notifica {string}") public void notificationPriceAndDateVerificationV1(String price,String versione) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + threadWaitMilliseconds(sharedSteps.getWait() * 2); if(versione.equalsIgnoreCase("V1")) { priceVerificationV1(price, null, 0); @@ -1445,33 +1429,22 @@ public void notificationPriceAndDateVerificationV1(String price,String versione) @Then("vengono verificati costo = {string} e data di perfezionamento della notifica V2") public void notificationPriceAndDateVerificationV2(String price) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + threadWaitMilliseconds(sharedSteps.getWait() * 2); priceVerificationV2(price, null, 0); } @Then("viene verificato il costo = {string} della notifica") public void notificationPriceVerification(String price) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + threadWaitMilliseconds(sharedSteps.getWait() * 2); priceVerification(price, null, 0); } @And("viene verificato il costo = {string} della notifica con un errore {string}") public void attachmentRetrievedError(String price, String errorCode) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + threadWaitMilliseconds(sharedSteps.getWait() * 2); + try { priceVerification(price, null, 0); } catch (HttpStatusCodeException e) { @@ -1484,11 +1457,7 @@ public void attachmentRetrievedError(String price, String errorCode) { @Then("viene verificato il costo = {string} della notifica per l'utente {int}") public void notificationPriceVerificationPerDestinatario(String price, Integer destinatario) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + threadWaitMilliseconds(sharedSteps.getWait() * 2); priceVerification(price, null, destinatario); } @@ -1601,11 +1570,7 @@ public List priceVerificationV23(Integer price, St @Then("viene calcolato il costo = {string} della notifica per l'utente {int}") public void notificationPriceProcessPerDestinatario(String price, Integer destinatario) { - try { - Thread.sleep(sharedSteps.getWait() * 2); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } + threadWaitMilliseconds(sharedSteps.getWait() * 2); priceVerificationProcessCost(price, null, destinatario); } @@ -1657,11 +1622,8 @@ public void userReadReceivedNotification(String recipient) { webRecipientClient.getReceivedNotification(iun, null); }); - try { - Thread.sleep(sharedSteps.getWorkFlowWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(sharedSteps.getWorkFlowWait()); + } @And("{string} legge la notifica ricevuta {string}") @@ -1677,11 +1639,7 @@ public void userReadReceivedNotificationVersioning(String recipient,String versi webRecipientClient.getReceivedNotificationV2(iun, null); } - try { - Thread.sleep(sharedSteps.getWorkFlowWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(sharedSteps.getWorkFlowWait()); }catch (HttpStatusCodeException e) { if (e instanceof HttpStatusCodeException) { @@ -1694,11 +1652,8 @@ public void userReadReceivedNotificationVersioning(String recipient,String versi @Then("viene verificato che la chiave dell'attestazione opponibile {string} รจ {string}") public void verifiedThatTheKeyOfTheLegalFactIs(String legalFactCategory, String key) { - try { - Thread.sleep(sharedSteps.getWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + + threadWaitMilliseconds(sharedSteps.getWait()); PnTimelineLegalFactV23 categoriesV23 = pnTimelineAndLegalFactV23.getCategory(legalFactCategory); TimelineElementV23 timelineElement = sharedSteps.getSentNotification().getTimeline().stream().filter(elem -> elem.getCategory().equals(categoriesV23.getTimelineElementInternalCategory())).findAny().orElse(null); @@ -2496,7 +2451,7 @@ public void siAttendePresenzaPerfezionamentoDecorrenzaTermini(@Transpose DataTes OffsetDateTime currentDate = now().atZoneSameInstant(ZoneId.of("UTC")).toOffsetDateTime(); long remainingTime = ChronoUnit.MILLIS.between(currentDate, schedulingDate); if (remainingTime > 0) { - Thread.sleep(remainingTime + 30 * 1000); + threadWaitMilliseconds(remainingTime + 30 * 1000); } // get the updated notification sharedSteps.setSentNotification(b2bClient.getSentNotification(iun)); @@ -2516,7 +2471,7 @@ public void siAttendeCheSiRitentiInvio(String timelineEventCategory, @Transpose OffsetDateTime currentDate = now().atZoneSameInstant(ZoneId.of("UTC")).toOffsetDateTime(); Long remainingTime = ChronoUnit.MILLIS.between(currentDate, nextSend); if (remainingTime > 0) { - Thread.sleep(remainingTime + 30 * 1000); + threadWaitMilliseconds(remainingTime + 30 * 1000); } // get the updated notification sharedSteps.setSentNotification(b2bClient.getSentNotification(iun)); @@ -2647,11 +2602,7 @@ public void verificaTimestamp(String timelineEventCategory, @Transpose DataTest @And("download attestazione opponibile AAR") public void downloadLegalFactIdAAR() { - try { - Thread.sleep(sharedSteps.getWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(sharedSteps.getWait()); TimelineElementCategoryV23 timelineElementInternalCategory= TimelineElementCategoryV23.AAR_GENERATION; TimelineElementV23 timelineElement = null; @@ -3135,11 +3086,7 @@ public String getKeyLegalFact(String key) { } private LegalFactDownloadMetadataResponse takeLegalFact(String legalFactCategory, String deliveryDetailCode) { - try { - Thread.sleep(sharedSteps.getWait()); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } + threadWaitMilliseconds(sharedSteps.getWait()); PnTimelineLegalFactV23 categoriesV23 = pnTimelineAndLegalFactV23.getCategory(legalFactCategory); diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheWebhookB2bSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheWebhookB2bSteps.java index 12fe74cd7..47ba3b3c9 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheWebhookB2bSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheWebhookB2bSteps.java @@ -1499,9 +1499,7 @@ private StatusElementSearchResult getStatusEventForStream(StreamVersion private void sleepTest(){ sleepTest(sharedSteps.getWait()); } - private void sleepTest(int wait){ - sleepTest(Long.valueOf(wait)); - } + private void sleepTest(long wait){ try { Thread.sleep(wait); diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java index 6e437f29b..e7c0faa23 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java @@ -26,8 +26,8 @@ import org.springframework.mail.javamail.JavaMailSender; import org.springframework.util.Base64Utils; import org.springframework.web.client.HttpStatusCodeException; + import java.io.ByteArrayInputStream; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.time.*; import java.time.temporal.ChronoUnit; @@ -36,6 +36,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; import static org.awaitility.Awaitility.await; @@ -263,12 +264,8 @@ public void preLoadingOfMetaDatiAttachmentF24() { NotificationMetadataAttachment notificationPaymentAttachment = b2bUtils.newMetadataAttachment("classpath:/METADATA_CORRETTO.json"); AtomicReference notificationDocumentAtomic = new AtomicReference<>(); Assertions.assertDoesNotThrow(() -> notificationDocumentAtomic.set(b2bUtils.preloadMetadataAttachment(notificationPaymentAttachment))); - try { - Thread.sleep( sharedSteps.getWait()); - } catch (InterruptedException e) { - log.error("Thread.sleep error retry"); - throw new RuntimeException(e); - } + threadWaitMilliseconds( sharedSteps.getWait()); + this.notificationMetadataAttachment = notificationDocumentAtomic.get(); } @@ -937,8 +934,8 @@ private void sendEmail() { @Given("si invia una email alla pec mittente e si attendono {int} minuti") public void siInviaUnaEmailAllaPecMittenteESiAttendonoMinuti(int wait) { Assertions.assertDoesNotThrow(this::sendEmail); - long waiting = ((wait*60)*1000); - Assertions.assertDoesNotThrow(() -> Thread.sleep(waiting)); + long waiting = ((wait*60L)*1000); + Assertions.assertDoesNotThrow(() -> threadWaitMilliseconds(waiting)); } @Given("si richiama checkout con dati:") diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/RaddAltSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/RaddAltSteps.java index 9ef04ca07..a8ea32fa0 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/RaddAltSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/RaddAltSteps.java @@ -30,6 +30,8 @@ import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; + +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; import static it.pagopa.pn.cucumber.utils.FiscalCodeGenerator.generateCF; import static it.pagopa.pn.cucumber.utils.NotificationValue.generateRandomNumber; @@ -247,11 +249,9 @@ private void startTransactionActRaddAlternative(String operationid) { this.startTransactionResponse = raddAltClient.startActTransaction(uid, actStartTransactionRequest); if(this.startTransactionResponse.getStatus().getCode().equals(StartTransactionResponseStatus.CodeEnum.NUMBER_2)){ - try { - Thread.sleep(this.startTransactionResponse.getStatus().getRetryAfter().longValue()); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + + threadWaitMilliseconds(this.startTransactionResponse.getStatus().getRetryAfter().longValue()); + this.startTransactionResponse = raddAltClient.startActTransaction(uid, actStartTransactionRequest); } System.out.println("startTransactionResponse: " + startTransactionResponse); diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java index 7174482ea..bc93e7710 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java @@ -27,6 +27,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; + import java.io.IOException; import java.net.URI; import java.time.LocalDate; @@ -38,8 +39,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.concurrent.TimeUnit; -import static org.awaitility.Awaitility.await; + +import static it.pagopa.pn.cucumber.steps.utilitySteps.ThreadUtils.threadWaitMilliseconds; @Slf4j @@ -120,7 +121,7 @@ public void NotificationsUnreachableResponse() { Assertions.assertDoesNotThrow(() -> { notificationsUnreachableResponse = ipServiceDeskClient.notification(notificationRequest); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationsUnreachableResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -219,7 +220,7 @@ public void createOperationResponse() { Assertions.assertDoesNotThrow(() -> { operationsResponse = ipServiceDeskClient.createOperation(createOperationRequest); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(operationsResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -252,7 +253,7 @@ public void preUploadVideoResponse() { Assertions.assertDoesNotThrow(() -> { videoUploadResponse = ipServiceDeskClient.presignedUrlVideoUpload(operationsResponse.getOperationId(), videoUploadRequest); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(videoUploadResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -270,7 +271,7 @@ public void preUploadVideoResponse(String operationId) { public void preUploadVideoResponseOperationIdNull() { try { videoUploadResponse = ipServiceDeskClient.presignedUrlVideoUpload(null, videoUploadRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(videoUploadResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -282,7 +283,7 @@ public void preUploadVideoResponseWithError() { try { log.error("Operation id:" + operationsResponse.getOperationId()); videoUploadResponse = ipServiceDeskClient.presignedUrlVideoUpload(operationsResponse.getOperationId(), videoUploadRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(videoUploadResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -354,7 +355,7 @@ public void searchResponse() { @When("viene invocato il servizio SEARCH con delay") public void searchResponseWithDelay() { - threadWait(delay); + threadWaitMilliseconds(delay); searchResponseSteps(); } @@ -452,7 +453,7 @@ public void loadFileSafeStorage() { @Then("il video viene caricato su SafeStorage con url scaduta") public void loadFileSafeStorageUrlExpired() { try { - threadWait(3720000);//aspetta 62 minuti + threadWaitMilliseconds(3720000);//aspetta 62 minuti loadFileSafeStorageSteps(); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -472,7 +473,7 @@ public void loadFileSafeStorageWithError() { public void retentionCheckPreload() { String key = notificationDocument.getRef().getKey(); log.info("Resouce name" + key); - threadWait(900000); + threadWaitMilliseconds(900000); log.info("Fine delay"); Assertions.assertTrue(checkRetetion(key, retentionTimePreLoad)); } @@ -614,7 +615,7 @@ public void lOperatoreRichiedeLElencoDiDiTuttiIMessaggiDiCortesiaInviatiConRecip searchNotificationsRequest.setTaxId(CF_corretto); searchNotificationsRequest.setRecipientType(null); searchNotificationsResponse = ipServiceDeskClient.searchNotificationsFromTaxId(10, null, null, null, searchNotificationsRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(searchNotificationsResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -1060,7 +1061,7 @@ private void createPreUploadVideoRequestDocumentSteps() throws Exception { private void preUploadVideoResponseSteps(String operationId) { try { videoUploadResponse = ipServiceDeskClient.presignedUrlVideoUpload(operationId, videoUploadRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(videoUploadResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -1075,7 +1076,7 @@ private void operationProducedAnErrorSteps(String statusCode) { private void createOperationResponseWithErrorSteps() { try { operationsResponse = ipServiceDeskClient.createOperation(createOperationRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationsUnreachableResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -1085,7 +1086,7 @@ private void createOperationResponseWithErrorSteps() { private void notificationsUnreachableResponseWithErrorSteps() { try { notificationsUnreachableResponse = ipServiceDeskClient.notification(notificationRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationsUnreachableResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -1095,7 +1096,7 @@ private void notificationsUnreachableResponseWithErrorSteps() { private void searchResponseWithErrorSteps() { try { searchResponse = ipServiceDeskClient.searchOperationsFromTaxId(searchNotificationRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(searchResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -1123,7 +1124,7 @@ private void searchResponseSteps() { Assertions.assertDoesNotThrow(() -> { searchResponse = ipServiceDeskClient.searchOperationsFromTaxId(searchNotificationRequest); }); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(searchResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1138,7 +1139,7 @@ private void lOperatoreRichiedeLElencoDiDiTuttiIMessaggiDiCortesiaInviatiSteps(S searchNotificationsRequest.setTaxId(cf); searchNotificationsRequest.setRecipientType(RecipientType.PF); searchNotificationsResponse = ipServiceDeskClient.searchNotificationsFromTaxId(10, null, null, null, searchNotificationsRequest); - threadWait(getWorkFlowWait()); + threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(searchNotificationsResponse); } catch (HttpStatusCodeException exception) { this.notificationError = exception; @@ -1389,13 +1390,4 @@ public String setNextPagesKey(String searchNextPagesKey) { } return nextPagesKey; } - - private void threadWait(int wait) { - try { - await().atMost(wait, TimeUnit.MILLISECONDS); - } catch (RuntimeException exception) { - log.error("Await error exception"); - throw exception; - } - } } \ No newline at end of file diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java b/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java new file mode 100644 index 000000000..d6ad73db2 --- /dev/null +++ b/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java @@ -0,0 +1,38 @@ +package it.pagopa.pn.cucumber.steps.utilitySteps; + +import static org.awaitility.Awaitility.await; + +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +import org.awaitility.core.ConditionTimeoutException; + +@Slf4j +public class ThreadUtils { + + private static void threadWait(long wait, TimeUnit timeUnit) { + try { + await() + .atMost(isValidWait(wait), timeUnit) + .with() + .until(() -> false); + } catch (ConditionTimeoutException exception) { + log.info("Thread wait ended."); + } + } + + private static long isValidWait(long wait) { + return wait <= 100 ? 101 : wait; + } + + public static void threadWaitMilliseconds(long wait) { + threadWait(wait, TimeUnit.MILLISECONDS); + } + + public static void threadWaitSeconds(long wait) { + threadWait(wait, TimeUnit.SECONDS); + } + + public static void threadWaitMinutes(long wait) { + threadWait(wait, TimeUnit.MINUTES); + } +} From dd854e2c757e120d91164c8143e9ac0376ee50f0 Mon Sep 17 00:00:00 2001 From: "vincenzo.giammona" Date: Mon, 27 May 2024 15:21:34 +0200 Subject: [PATCH 2/4] removed useless threadWait --- .../pagopa/pn/cucumber/steps/SharedSteps.java | 30 ------------------- .../pa/AvanzamentoNotificheB2bSteps.java | 2 -- .../steps/pa/InvioNotificheB2bSteps.java | 1 - .../serviceDesk/ApiServiceDeskSteps.java | 8 ++--- 4 files changed, 4 insertions(+), 37 deletions(-) diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java index 771f5661d..f422ec009 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/SharedSteps.java @@ -1105,8 +1105,6 @@ private void sendNotification(int wait) { notificationResponseComplete = b2bUtils.waitForRequestAcceptation(newNotificationResponse); }); - threadWaitMilliseconds(wait); - Assertions.assertNotNull(notificationResponseComplete); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1125,8 +1123,6 @@ private void sendNotificationNoAccept(int wait) { notificationResponseComplete = b2bUtils.waitForRequestNoAcceptation(newNotificationResponse); }); - threadWaitMilliseconds(wait); - Assertions.assertNull(notificationResponseComplete); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1146,9 +1142,6 @@ private void sendNotificationRapid(int wait) { notificationResponseComplete = b2bUtils.waitForRequestAcceptationShort(newNotificationResponse); }); - - threadWaitMilliseconds(wait); - Assertions.assertNotNull(notificationResponseComplete); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1173,8 +1166,6 @@ private void sendNotificationRapidCancellPreRefused() { }); rifiutata = b2bUtils.waitForRequestNotRefused(newNotificationResponse); - threadWaitMilliseconds(wait); - Assertions.assertFalse(rifiutata); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1204,7 +1195,6 @@ private void sendNotificationV1() { notificationResponseCompleteV1 = b2bUtils.waitForRequestAcceptationV1(newNotificationResponseV1); }); - threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationResponseCompleteV1); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1224,7 +1214,6 @@ private void sendNotificationV2() { notificationResponseCompleteV2 = b2bUtils.waitForRequestAcceptationV2(newNotificationResponseV2); }); - threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertNotNull(notificationResponseCompleteV2); } catch (AssertionFailedError assertionFailedError) { @@ -1245,8 +1234,6 @@ private void sendNotificationV21() { notificationResponseCompleteV21 = b2bUtils.waitForRequestAcceptationV21(newNotificationResponseV21); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertNotNull(notificationResponseCompleteV21); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1315,8 +1302,6 @@ private void sendNotificationWithErrorNotFindAllegato(boolean noUpload) { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertNotNull(errorCode); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1333,8 +1318,6 @@ private void sendNotificationWithErrorNotFindAllegatoJson() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertNotNull(errorCode); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1351,8 +1334,6 @@ private void sendNotificationWithErrorSha() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertNotNull(errorCode); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1379,8 +1360,6 @@ private void sendNotificationWithErrorShaJson() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertFalse(errorCode.isEmpty()); } catch (AssertionFailedError assertionFailedError) { @@ -1399,8 +1378,6 @@ private void sendNotificationWithWrongExtension() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertFalse(errorCode.isEmpty()); } catch (AssertionFailedError assertionFailedError) { @@ -1418,7 +1395,6 @@ private void sendNotificationRefused() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); } catch (AssertionFailedError assertionFailedError) { @@ -1435,10 +1411,8 @@ private void sendNotificationRefusedOverSizeAllegato() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); - } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + "{RequestID: " + (newNotificationResponse == null ? "NULL" : newNotificationResponse.getNotificationRequestId()) + " }"; @@ -1453,10 +1427,8 @@ private void sendNotificationRefusedInjectionAllegato() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); Assertions.assertFalse(errorCode.isEmpty()); - } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + "{RequestID: " + (newNotificationResponse == null ? "NULL" : newNotificationResponse.getNotificationRequestId()) + " }"; @@ -1471,8 +1443,6 @@ private void sendNotificationRefusedOver15Allegato() { errorCode = b2bUtils.waitForRequestRefused(newNotificationResponse); }); - threadWaitMilliseconds(getWorkFlowWait()); - Assertions.assertFalse(errorCode.isEmpty()); } catch (AssertionFailedError assertionFailedError) { diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java index 5b2b791ce..77389c8e6 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/AvanzamentoNotificheB2bSteps.java @@ -1622,8 +1622,6 @@ public void userReadReceivedNotification(String recipient) { webRecipientClient.getReceivedNotification(iun, null); }); - threadWaitMilliseconds(sharedSteps.getWorkFlowWait()); - } @And("{string} legge la notifica ricevuta {string}") diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java index e7c0faa23..e39dfe658 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/pa/InvioNotificheB2bSteps.java @@ -264,7 +264,6 @@ public void preLoadingOfMetaDatiAttachmentF24() { NotificationMetadataAttachment notificationPaymentAttachment = b2bUtils.newMetadataAttachment("classpath:/METADATA_CORRETTO.json"); AtomicReference notificationDocumentAtomic = new AtomicReference<>(); Assertions.assertDoesNotThrow(() -> notificationDocumentAtomic.set(b2bUtils.preloadMetadataAttachment(notificationPaymentAttachment))); - threadWaitMilliseconds( sharedSteps.getWait()); this.notificationMetadataAttachment = notificationDocumentAtomic.get(); } diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java b/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java index bc93e7710..06659a4eb 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/serviceDesk/ApiServiceDeskSteps.java @@ -121,7 +121,7 @@ public void NotificationsUnreachableResponse() { Assertions.assertDoesNotThrow(() -> { notificationsUnreachableResponse = ipServiceDeskClient.notification(notificationRequest); }); - threadWaitMilliseconds(getWorkFlowWait()); + Assertions.assertNotNull(notificationsUnreachableResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -220,7 +220,7 @@ public void createOperationResponse() { Assertions.assertDoesNotThrow(() -> { operationsResponse = ipServiceDeskClient.createOperation(createOperationRequest); }); - threadWaitMilliseconds(getWorkFlowWait()); + Assertions.assertNotNull(operationsResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -253,7 +253,7 @@ public void preUploadVideoResponse() { Assertions.assertDoesNotThrow(() -> { videoUploadResponse = ipServiceDeskClient.presignedUrlVideoUpload(operationsResponse.getOperationId(), videoUploadRequest); }); - threadWaitMilliseconds(getWorkFlowWait()); + Assertions.assertNotNull(videoUploadResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + @@ -1124,7 +1124,7 @@ private void searchResponseSteps() { Assertions.assertDoesNotThrow(() -> { searchResponse = ipServiceDeskClient.searchOperationsFromTaxId(searchNotificationRequest); }); - threadWaitMilliseconds(getWorkFlowWait()); + Assertions.assertNotNull(searchResponse); } catch (AssertionFailedError assertionFailedError) { String message = assertionFailedError.getMessage() + From caba1ffe25da702bf6d2d1e2a1b806d84965248b Mon Sep 17 00:00:00 2001 From: "vincenzo.giammona" Date: Mon, 27 May 2024 17:11:02 +0200 Subject: [PATCH 3/4] fix checkWait in threadUtils --- .../pn/cucumber/steps/utilitySteps/ThreadUtils.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java b/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java index d6ad73db2..e9de46401 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java @@ -12,7 +12,7 @@ public class ThreadUtils { private static void threadWait(long wait, TimeUnit timeUnit) { try { await() - .atMost(isValidWait(wait), timeUnit) + .atMost(checkWait(wait, timeUnit), timeUnit) .with() .until(() -> false); } catch (ConditionTimeoutException exception) { @@ -20,8 +20,11 @@ private static void threadWait(long wait, TimeUnit timeUnit) { } } - private static long isValidWait(long wait) { - return wait <= 100 ? 101 : wait; + private static long checkWait(long wait, TimeUnit timeUnit) { + if (timeUnit.equals(TimeUnit.MILLISECONDS)) { + return wait <= 100 ? 101 : wait; + } + return wait; } public static void threadWaitMilliseconds(long wait) { From 3232b9753e15c84c5fa841d6067315b9f0f53f20 Mon Sep 17 00:00:00 2001 From: "vincenzo.giammona" Date: Tue, 28 May 2024 17:42:00 +0200 Subject: [PATCH 4/4] fix pollDelay --- .../steps/utilitySteps/ThreadUtils.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java b/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java index e9de46401..a9b256511 100644 --- a/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java +++ b/src/test/java/it/pagopa/pn/cucumber/steps/utilitySteps/ThreadUtils.java @@ -12,7 +12,8 @@ public class ThreadUtils { private static void threadWait(long wait, TimeUnit timeUnit) { try { await() - .atMost(checkWait(wait, timeUnit), timeUnit) + .atMost(wait, timeUnit) + .pollDelay(getWait(wait-1, timeUnit), getTimeUnit(wait-1, timeUnit)) .with() .until(() -> false); } catch (ConditionTimeoutException exception) { @@ -20,12 +21,23 @@ private static void threadWait(long wait, TimeUnit timeUnit) { } } - private static long checkWait(long wait, TimeUnit timeUnit) { + private static long getWait(long wait, TimeUnit timeUnit) { if (timeUnit.equals(TimeUnit.MILLISECONDS)) { - return wait <= 100 ? 101 : wait; + return checkWait(wait); } return wait; } + private static long checkWait(long wait) { + + return wait == 0 ? 999 : wait; + } + + private static TimeUnit getTimeUnit(long wait, TimeUnit timeUnit) { + if (getWait(wait, timeUnit) == 999) { + return TimeUnit.NANOSECONDS; + } + return timeUnit; + } public static void threadWaitMilliseconds(long wait) { threadWait(wait, TimeUnit.MILLISECONDS); @@ -38,4 +50,13 @@ public static void threadWaitSeconds(long wait) { public static void threadWaitMinutes(long wait) { threadWait(wait, TimeUnit.MINUTES); } + + public static void main(String[] args) { + long start = System.currentTimeMillis(); + ThreadUtils.threadWaitMilliseconds(1); + long end = System.currentTimeMillis(); + long duration = (end-start); + System.out.println("End execution: " + end); + System.out.println("total duration: " + duration + "ms"); + } }