Skip to content

Commit

Permalink
Refacotr e fix
Browse files Browse the repository at this point in the history
  • Loading branch information
angelominisci committed Nov 1, 2024
1 parent 96aaa4f commit 1029148
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
33 changes: 24 additions & 9 deletions src/main/java/it/pn/frontend/e2e/config/CustomHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -29,9 +36,14 @@
import java.util.Map;

@Slf4j
@Configuration
public class CustomHttpClient<RequestType, ResponseType> {
private static CustomHttpClient<?, ?> instance;

private static CustomHttpClient<?, ?> instance ;
private final Gson gson = new Gson();
@Value("${environment}")
private String environment;

@Setter
@Getter
private String baseUrlApi;
Expand All @@ -40,32 +52,34 @@ public class CustomHttpClient<RequestType, ResponseType> {
@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 <R, S> CustomHttpClient<R, S> getInstance() {
public static <R, S> CustomHttpClient<R, S> getInstance() {
if (instance == null) {
synchronized (CustomHttpClient.class) {
if (instance == null) {
Expand All @@ -76,7 +90,8 @@ public static <R, S> CustomHttpClient<R, S> getInstance() {
return (CustomHttpClient<R, S>) instance;
}

public static <R, S> CustomHttpClient<R, S> getInstanceWithApiKey(String apiKey) {

public static <R, S> CustomHttpClient<R, S> getInstanceWithApiKey(String apiKey) {
if (instance == null) {
synchronized (CustomHttpClient.class) {
if (instance == null) {
Expand Down Expand Up @@ -250,7 +265,7 @@ public ResponseType sendHttpGetRequest(String endpoint, Map<String, String> 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")
Expand Down Expand Up @@ -280,7 +295,7 @@ public String getJwtToken(String TokenExchange) throws IOException {
}

public ResponseType sendHttpDeleteRequest(String endpoint, Map<String, String> headers, Class<ResponseType> responseType) throws IOException {
String env = webDriverConfig.getEnvironment();
String env = environment;
try (CloseableHttpClient client = HttpClients.createDefault()) {
this.httpRequest = ClassicRequestBuilder
.delete(endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public class WebDriverConfig {


@Bean
@Primary
public WebDriver webDriver() {

var browser = Optional.ofNullable(getBrowser())
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/it/pn/frontend/e2e/rest/RestContact.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,19 +30,22 @@ public class RestContact {

private static final Logger logger = LoggerFactory.getLogger(RestContact.class);

@Autowired
private CustomHttpClient<?, String> httpClient;

@Autowired
private WebDriverConfig webDriverConfig;
@Value("${environment}")
private String environment;

private final Map<String, String> headers = new HashMap<>();

// Carica il token all'avvio della classe

private final CustomHttpClient<?, String> customHttpClient;


@Autowired
public RestContact(WebDriverConfig webDriverConfig) {
this.webDriverConfig = webDriverConfig;
this.httpClient.setBaseUrlApi("https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it");
public RestContact(CustomHttpClient<?, String> customHttpClient) {
this.customHttpClient = customHttpClient;

customHttpClient.setBaseUrlApi("https://webapi." + environment + ".notifichedigitali.it");

String token = System.getProperty("token");
if (token != null) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -106,7 +112,7 @@ public void removeSpecialContact(DigitalAddress digitalAddress) throws RestDeleg
*/
public DigitalAddressResponse getDigitalAddress() throws RestContactException {
CustomHttpClient<?, DigitalAddressResponse> 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 {
Expand All @@ -126,7 +132,7 @@ public DigitalAddressResponse getDigitalAddress() throws RestContactException {
*/
public List<DigitalAddress> getAllDigitalAddress() throws RestContactException {
CustomHttpClient<?, DigitalAddress> httpClientDigitalAddress = CustomHttpClient.getInstance();
httpClientDigitalAddress.setBaseUrlApi("https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it");
httpClientDigitalAddress.setBaseUrlApi("https://webapi." + environment + ".notifichedigitali.it");
String url = "/bff/v1/addresses";

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -103,7 +105,9 @@
LoginPersonaFisicaPagoPA.class,
RestNotification.class,
NotificationBuilder.class,
MandateSingleton.class
MandateSingleton.class,
RestContact.class,
CustomHttpClient.class

})
@EnableScheduling
Expand Down

0 comments on commit 1029148

Please sign in to comment.