From 1029148b8bfbffa601e35efb0f167dbed76e74a7 Mon Sep 17 00:00:00 2001 From: "angelo.minisci" Date: Fri, 1 Nov 2024 13:48:11 +0100 Subject: [PATCH] Refacotr e fix --- .../frontend/e2e/config/CustomHttpClient.java | 33 ++++++++++++----- .../frontend/e2e/config/WebDriverConfig.java | 1 + .../it/pn/frontend/e2e/rest/RestContact.java | 36 +++++++++++-------- .../CucumberSpringIntegration.java | 6 +++- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/main/java/it/pn/frontend/e2e/config/CustomHttpClient.java b/src/main/java/it/pn/frontend/e2e/config/CustomHttpClient.java index dc89928a9..aa2b9d0fb 100644 --- a/src/main/java/it/pn/frontend/e2e/config/CustomHttpClient.java +++ b/src/main/java/it/pn/frontend/e2e/config/CustomHttpClient.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import io.cucumber.java.Before; import it.pn.frontend.e2e.model.address.DigitalAddress; import it.pn.frontend.e2e.model.delegate.DelegateResponsePF; import lombok.Getter; @@ -18,6 +19,12 @@ import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; import java.io.File; import java.io.IOException; @@ -29,9 +36,14 @@ import java.util.Map; @Slf4j +@Configuration public class CustomHttpClient { - private static CustomHttpClient instance; + + private static CustomHttpClient instance ; private final Gson gson = new Gson(); + @Value("${environment}") + private String environment; + @Setter @Getter private String baseUrlApi; @@ -40,32 +52,34 @@ public class CustomHttpClient { @Getter private String apiKey; - @Autowired - private WebDriverConfig webDriverConfig; - - private final CloseableHttpClient httpClient; + private CloseableHttpClient httpClient; private ClassicHttpRequest httpRequest; + public CustomHttpClient() { + System.out.println("CIAO......."); this.baseUrlApi = "https://api.test.notifichedigitali.it"; this.httpClient = HttpClients.createDefault(); this.apiKey = "2b3d47f4-44c1-4b49-b6ef-54dc1c531311"; } public CustomHttpClient(String baseUrlApi, String apiKeyTest) { + System.out.println("CIAO1......."); + this.baseUrlApi = baseUrlApi; this.httpClient = HttpClients.createDefault(); this.apiKey = apiKeyTest; } public CustomHttpClient(String apiKeyTest) { + System.out.println("CIAO2......."); this.baseUrlApi = "https://api.test.notifichedigitali.it"; this.httpClient = HttpClients.createDefault(); this.apiKey = apiKeyTest; } - public static CustomHttpClient getInstance() { + public static CustomHttpClient getInstance() { if (instance == null) { synchronized (CustomHttpClient.class) { if (instance == null) { @@ -76,7 +90,8 @@ public static CustomHttpClient getInstance() { return (CustomHttpClient) instance; } - public static CustomHttpClient getInstanceWithApiKey(String apiKey) { + + public static CustomHttpClient getInstanceWithApiKey(String apiKey) { if (instance == null) { synchronized (CustomHttpClient.class) { if (instance == null) { @@ -250,7 +265,7 @@ public ResponseType sendHttpGetRequest(String endpoint, Map head } public String getJwtToken(String TokenExchange) throws IOException { - String env = webDriverConfig.getEnvironment(); + String env = environment; CloseableHttpClient client = HttpClients.createDefault(); this.httpRequest = ClassicRequestBuilder .post("https://webapi." + env + ".notifichedigitali.it/token-exchange") @@ -280,7 +295,7 @@ public String getJwtToken(String TokenExchange) throws IOException { } public ResponseType sendHttpDeleteRequest(String endpoint, Map headers, Class responseType) throws IOException { - String env = webDriverConfig.getEnvironment(); + String env = environment; try (CloseableHttpClient client = HttpClients.createDefault()) { this.httpRequest = ClassicRequestBuilder .delete(endpoint) diff --git a/src/main/java/it/pn/frontend/e2e/config/WebDriverConfig.java b/src/main/java/it/pn/frontend/e2e/config/WebDriverConfig.java index ed52ce8f7..4dd486c8a 100644 --- a/src/main/java/it/pn/frontend/e2e/config/WebDriverConfig.java +++ b/src/main/java/it/pn/frontend/e2e/config/WebDriverConfig.java @@ -205,6 +205,7 @@ public class WebDriverConfig { @Bean + @Primary public WebDriver webDriver() { var browser = Optional.ofNullable(getBrowser()) diff --git a/src/main/java/it/pn/frontend/e2e/rest/RestContact.java b/src/main/java/it/pn/frontend/e2e/rest/RestContact.java index bffc3ebe9..6b44b5222 100644 --- a/src/main/java/it/pn/frontend/e2e/rest/RestContact.java +++ b/src/main/java/it/pn/frontend/e2e/rest/RestContact.java @@ -9,7 +9,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import java.io.IOException; import java.util.HashMap; @@ -27,19 +30,22 @@ public class RestContact { private static final Logger logger = LoggerFactory.getLogger(RestContact.class); - @Autowired - private CustomHttpClient httpClient; - @Autowired - private WebDriverConfig webDriverConfig; + @Value("${environment}") + private String environment; private final Map headers = new HashMap<>(); // Carica il token all'avvio della classe + + private final CustomHttpClient customHttpClient; + + @Autowired - public RestContact(WebDriverConfig webDriverConfig) { - this.webDriverConfig = webDriverConfig; - this.httpClient.setBaseUrlApi("https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it"); + public RestContact(CustomHttpClient customHttpClient) { + this.customHttpClient = customHttpClient; + + customHttpClient.setBaseUrlApi("https://webapi." + environment + ".notifichedigitali.it"); String token = System.getProperty("token"); if (token != null) { @@ -53,10 +59,10 @@ public RestContact(WebDriverConfig webDriverConfig) { * Rimuove l'indirizzo email di cortesia predefinito. */ public void removeDigitalAddressCourtesyEmail() throws RestContactException { - String url = "https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it/address-book/v1/digital-address/courtesy/default/EMAIL"; + String url = "https://webapi." + environment + ".notifichedigitali.it/address-book/v1/digital-address/courtesy/default/EMAIL"; try { headers.put("Authorization", System.getProperty("token")); - String response = httpClient.sendHttpDeleteRequest(url, headers, String.class); + String response = customHttpClient.sendHttpDeleteRequest(url, headers, String.class); logger.info("Risposta ricevuta: " + response); logger.info("Indirizzo digitale di cortesia rimosso con successo"); } catch (IOException e) { @@ -69,10 +75,10 @@ public void removeDigitalAddressCourtesyEmail() throws RestContactException { * Rimuove l'indirizzo PEC legale predefinito. */ public void removeDigitalAddressLegalPec() throws RestContactException { - String url = "https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it/bff/v1/addresses/LEGAL/default/PEC"; + String url = "https://webapi." + environment + ".notifichedigitali.it/bff/v1/addresses/LEGAL/default/PEC"; try { headers.put("Authorization", System.getProperty("token")); - String response = httpClient.sendHttpDeleteRequest(url, headers, String.class); + String response = customHttpClient.sendHttpDeleteRequest(url, headers, String.class); logger.info("Risposta ricevuta: " + response); logger.info("Indirizzo PEC legale rimosso con successo"); } catch (IOException e) { @@ -87,12 +93,12 @@ public void removeDigitalAddressLegalPec() throws RestContactException { public void removeSpecialContact(DigitalAddress digitalAddress) throws RestDelegationException { String channelType = digitalAddress.getChannelType().toString(); String addressType = digitalAddress.getAddressType().toString().toLowerCase(); - String url = "https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it/address-book/v1/digital-address/" + String url = "https://webapi." +environment + ".notifichedigitali.it/address-book/v1/digital-address/" + addressType + "/" + digitalAddress.getSenderId() + "/" + channelType; try { headers.put("Authorization", System.getProperty("token")); - String response = httpClient.sendHttpDeleteRequest(url, headers, String.class); + String response = customHttpClient.sendHttpDeleteRequest(url, headers, String.class); logger.info("Risposta ricevuta: " + response); logger.info("Indirizzo digitale di 'altri recapiti' rimosso con successo"); } catch (IOException e) { @@ -106,7 +112,7 @@ public void removeSpecialContact(DigitalAddress digitalAddress) throws RestDeleg */ public DigitalAddressResponse getDigitalAddress() throws RestContactException { CustomHttpClient httpClientDigitalAddress = CustomHttpClient.getInstance(); - httpClientDigitalAddress.setBaseUrlApi("https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it"); + httpClientDigitalAddress.setBaseUrlApi("https://webapi." + environment + ".notifichedigitali.it"); String url = "/bff/v1/addresses/LEGAL/default/PEC"; try { @@ -126,7 +132,7 @@ public DigitalAddressResponse getDigitalAddress() throws RestContactException { */ public List getAllDigitalAddress() throws RestContactException { CustomHttpClient httpClientDigitalAddress = CustomHttpClient.getInstance(); - httpClientDigitalAddress.setBaseUrlApi("https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it"); + httpClientDigitalAddress.setBaseUrlApi("https://webapi." + environment + ".notifichedigitali.it"); String url = "/bff/v1/addresses"; try { diff --git a/src/test/java/it/pn/frontend/e2e/stepDefinitions/CucumberSpringIntegration.java b/src/test/java/it/pn/frontend/e2e/stepDefinitions/CucumberSpringIntegration.java index ea56ef861..51455ace2 100644 --- a/src/test/java/it/pn/frontend/e2e/stepDefinitions/CucumberSpringIntegration.java +++ b/src/test/java/it/pn/frontend/e2e/stepDefinitions/CucumberSpringIntegration.java @@ -6,6 +6,7 @@ import it.pn.frontend.e2e.common.NotificheDestinatarioPage; import it.pn.frontend.e2e.common.RecapitiDestinatarioPage; import it.pn.frontend.e2e.config.BearerTokenConfig; +import it.pn.frontend.e2e.config.CustomHttpClient; import it.pn.frontend.e2e.config.UserPasswordConfig; import it.pn.frontend.e2e.config.WebDriverConfig; import it.pn.frontend.e2e.listeners.LoggerStartupListener; @@ -16,6 +17,7 @@ import it.pn.frontend.e2e.pages.destinatario.personaFisica.*; import it.pn.frontend.e2e.pages.destinatario.personaGiuridica.*; import it.pn.frontend.e2e.pages.mittente.*; +import it.pn.frontend.e2e.rest.RestContact; import it.pn.frontend.e2e.rest.RestNotification; import it.pn.frontend.e2e.section.CookiesSection; import it.pn.frontend.e2e.section.destinatario.personaFisica.HeaderPFSection; @@ -103,7 +105,9 @@ LoginPersonaFisicaPagoPA.class, RestNotification.class, NotificationBuilder.class, - MandateSingleton.class + MandateSingleton.class, + RestContact.class, + CustomHttpClient.class }) @EnableScheduling