Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/refactor/QA-4215-Refactor-dei-Te…
Browse files Browse the repository at this point in the history
…st-Mittente-con-Gestione-dei-Bean' into refactor/QA-4069-Roadmap-per-l-Integrazione-di-Spring-dopo-l-integrazione-con-JUnit-5
  • Loading branch information
angelominisci committed Nov 4, 2024
2 parents 92f8089 + 43507b1 commit 6050aee
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 66 deletions.
5 changes: 3 additions & 2 deletions src/main/java/it/pn/frontend/e2e/listeners/HooksNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public class HooksNew {
private RestContact restContact;
@Autowired
private CookieConfig cookieConfig;

@Autowired
RestDelegation restDelegation ;

@Before
public void startScenario(Scenario scenario) {
Expand Down Expand Up @@ -133,7 +134,7 @@ public void clearDelegate() {
logger.info("Revoking all delegations...");
var mandateId = mandateSingleton.getMandateId(HooksNew.getScenario());
if (mandateId != null) {
RestDelegation.getInstance().revokeDelegation(mandateId);
restDelegation.revokeDelegation(mandateId);
logger.info("Delegation revoked: {}", mandateId);
} else {
logger.info("Mandate ID not found");
Expand Down
124 changes: 73 additions & 51 deletions src/main/java/it/pn/frontend/e2e/rest/RestDelegation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,124 +16,146 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*Modifiche principali:
Iniezione delle dipendenze: Utilizza @Autowired per l’iniezione automatica di CustomHttpClient e WebDriverConfig, eliminando l'uso di un getInstance() statico.
Costruttore: Il costruttore viene utilizzato per impostare l'URL base e l'intestazione Authorization per tutte le richieste.
Miglioramento gestione errori: Log di errore e gestione delle eccezioni tramite RestDelegationException per comunicare i fallimenti specifici nei metodi.
Metodi ottimizzati: Tutti i metodi sono stati aggiornati per seguire la configurazione dell’intestazione e l’uso di CustomHttpClient.
*
* */
@Component
public class RestDelegation {
private static final Logger logger = LoggerFactory.getLogger("RestDelegation");
final CustomHttpClient<DelegateRequestPF, DelegateResponsePF> httpClientPF = CustomHttpClient.getInstance();
final CustomHttpClient<DelegateRequestPG, DelegateResponsePG> httpClientPG = CustomHttpClient.getInstance();

private static RestDelegation instance;
//final private String env = System.getProperty("environment");
final private String token = System.getProperty("token");
private Map<String, String> headers = new HashMap<>();
private static final Logger logger = LoggerFactory.getLogger(RestDelegation.class);

@Autowired
private CustomHttpClient<DelegateRequestPF, DelegateResponsePF> httpClientPF;

@Autowired
private CustomHttpClient<DelegateRequestPG, DelegateResponsePG> httpClientPG;

@Autowired
private WebDriverConfig webDriverConfig;

public static synchronized RestDelegation getInstance() {
if (instance == null) {
instance = new RestDelegation();
}
return instance;
private final Map<String, String> headers = new HashMap<>();

@Autowired
public RestDelegation(WebDriverConfig webDriverConfig) {
this.webDriverConfig = webDriverConfig;
initializeHeaders();
setupHttpClients();
}

public RestDelegation() {
this.httpClientPF.setBaseUrlApi("https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it");
private void initializeHeaders() {
String token = System.getProperty("token");
if (token != null) {
this.headers.put("Authorization", token);
headers.put("Authorization", token);
} else {
logger.warn("Auth token non trovato, impossibile fare la richiesta HTTP in background!");
}
}

private void setupHttpClients() {
String baseUrl = "https://webapi." + webDriverConfig.getEnvironment() + ".notifichedigitali.it";
httpClientPF.setBaseUrlApi(baseUrl);
httpClientPG.setBaseUrlApi(baseUrl);
}

/**
* Add a new PF delegation
* Aggiunge una nuova delega PF.
*
* @param delegateRequestPF DelegateRequest object with all the data
* @return DelegateResponse object with the response
* @throws RestDelegationException if there is an error during the request
* @param delegateRequestPF DelegateRequest con i dati della delega
* @param tokenExchange token per l'exchange JWT
* @return DelegateResponse con la risposta
* @throws RestDelegationException in caso di errore nella richiesta
*/
public DelegateResponsePF addDelegationPF(DelegateRequestPF delegateRequestPF, String tokenExchange) throws RestDelegationException {
try {
String jwtToken = httpClientPF.getJwtToken(tokenExchange);
this.headers.put("Authorization", "Bearer " + jwtToken);
DelegateResponsePF response = httpClientPF.sendHttpPostRequest("/mandate/api/v1/mandate", this.headers, delegateRequestPF, DelegateResponsePF.class);
headers.put("Authorization", "Bearer " + jwtToken);
DelegateResponsePF response = httpClientPF.sendHttpPostRequest("/mandate/api/v1/mandate", headers, delegateRequestPF, DelegateResponsePF.class);
if (response != null) {
logger.info(String.valueOf(response));
logger.info("Response: {}", response);
return response;
}
} catch (IOException e) {
logger.error("Error during addDelegationPF", e);
logger.error("Errore durante addDelegationPF", e);
throw new RestDelegationException("Errore durante la richiesta di delega PF", e);
}
return null;
}

/**
* Add a new PG delegation
* Aggiunge una nuova delega PG.
*
* @param delegateRequest DelegateRequest object with all the data
* @return DelegateResponse object with the response
* @throws RestDelegationException if there is an error during the request
* @param delegateRequest DelegateRequest con i dati della delega
* @param tokenExchange token per l'exchange JWT
* @return DelegateResponse con la risposta
* @throws RestDelegationException in caso di errore nella richiesta
*/
public DelegateResponsePG addDelegationPG(DelegateRequestPG delegateRequest, String tokenExchange) throws RestDelegationException {
try {
String jwtToken = httpClientPG.getJwtToken(tokenExchange);
this.headers.put("Authorization", "Bearer " + jwtToken);
DelegateResponsePG response = httpClientPG.sendHttpPostRequest("/mandate/api/v1/mandate", this.headers, delegateRequest, DelegateResponsePG.class);
headers.put("Authorization", "Bearer " + jwtToken);
DelegateResponsePG response = httpClientPG.sendHttpPostRequest("/mandate/api/v1/mandate", headers, delegateRequest, DelegateResponsePG.class);
if (response != null) {
logger.info(String.valueOf(response));
logger.info("Response: {}", response);
return response;
}
} catch (IOException e) {
logger.error("Error during addDelegationPG", e);
logger.error("Errore durante addDelegationPG", e);
throw new RestDelegationException("Errore durante la richiesta di delega PG", e);
}
return null;
}

/**
* Revoke a PF delegation
* <br>
* <b>Keep in mind this method works only for the annotation @After into Hooks.java, because there isn't a jwt token set
* if you don't invoke an "addDelegation"</b>
* Revoca una delega.
*
* @param mandateId String with the mandateId
* @throws RestDelegationException if there is an error during the request
* @param mandateId ID della delega da revocare
* @throws RestDelegationException in caso di errore nella richiesta
*/
public void revokeDelegation(String mandateId) throws RestDelegationException {
try {
httpClientPF.sendHttpPatchRequest("/mandate/api/v1/mandate/" + mandateId + "/revoke", this.headers);
httpClientPF.sendHttpPatchRequest("/mandate/api/v1/mandate/" + mandateId + "/revoke", headers);
logger.info("Delega {} revocata con successo", mandateId);
} catch (IOException e) {
logger.error("Error during revokeDelegation", e);
logger.error("Errore durante revokeDelegation", e);
throw new RestDelegationException("Errore durante la revoca della delega", e);
}
}

/**
* Reject a delegation
* <br>
* <b>Keep in mind this method works only for the annotation @After into Hooks.java, because there isn't a jwt token set
* if you don't invoke an "addDelegation"</b>
* Rifiuta una delega.
*
* @param mandateId String with the mandateId
* @throws RestDelegationException if there is an error during the request
* @param mandateId ID della delega da rifiutare
* @throws RestDelegationException in caso di errore nella richiesta
*/
public void rejectDelegation(String mandateId) throws RestDelegationException {
try {
httpClientPG.sendHttpPatchRequest("/mandate/api/v1/mandate/" + mandateId + "/reject", this.headers);
httpClientPG.sendHttpPatchRequest("/mandate/api/v1/mandate/" + mandateId + "/reject", headers);
logger.info("Delega {} rifiutata con successo", mandateId);
} catch (IOException e) {
logger.error("Error during rejectDelegation", e);
logger.error("Errore durante rejectDelegation", e);
throw new RestDelegationException("Errore durante il rifiuto della delega", e);
}
}

/**
* Ottiene le deleghe per il delegante corrente.
*
* @return lista di `DelegateResponsePF` con le deleghe
*/
public List<DelegateResponsePF> getDelegator() {
try {
List<DelegateResponsePF> response = httpClientPF.sendHttpGetRequestListDelegate("/mandate/api/v1/mandates-by-delegator", this.headers, DelegateResponsePF.class);
List<DelegateResponsePF> response = httpClientPF.sendHttpGetRequestListDelegate("/mandate/api/v1/mandates-by-delegator", headers, DelegateResponsePF.class);
if (response != null) {
logger.info(String.valueOf(response));
logger.info("Response: {}", response);
return response;
}
} catch (IOException e) {
logger.error("Error during getDelegator", e);
logger.error("Errore durante getDelegator", e);
}
return null;
}
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/it/pn/frontend/e2e/rest/RestIncident.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
/*Modifiche principali
Iniezione delle dipendenze: @Autowired viene utilizzato per iniettare CustomHttpClient e WebDriverConfig.
Rimozione del Singleton: RestIncident è ora un @Component gestito da Spring, quindi non è necessario il metodo getInstance() o una gestione manuale dell'istanza.
Configurazione URL base: La configurazione dell'URL base è ora gestita nel costruttore per inizializzare httpClientIncident.*/
@Component
public class RestIncident {
private static final Logger logger = LoggerFactory.getLogger("RestIncident");
final CustomHttpClient<NewIncidentRequest, IncidentStatusResponse> httpClientIncident = CustomHttpClient.getInstance();
//final private String env = System.getProperty("environment");

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

@Autowired
private WebDriverConfig webDriverConfig;
private CustomHttpClient<NewIncidentRequest, IncidentStatusResponse> httpClientIncident;

private static RestIncident instance;
@Autowired
private WebDriverConfig webDriverConfig;

public static synchronized RestIncident getInstance() {
if (instance == null) {
instance = new RestIncident();
}
return instance;
@Autowired
public RestIncident(WebDriverConfig webDriverConfig) {
this.webDriverConfig = webDriverConfig;
setupHttpClient();
}

public RestIncident() {
this.httpClientIncident.setBaseUrlApi("https://api.bo." + webDriverConfig.getEnvironment() + ".notifichedigitali.it/");
private void setupHttpClient() {
String baseUrl = "https://api.bo." + webDriverConfig.getEnvironment() + ".notifichedigitali.it/";
httpClientIncident.setBaseUrlApi(baseUrl);
logger.info("Configured HTTP client for Incidents with base URL: {}", baseUrl);
}
}

0 comments on commit 6050aee

Please sign in to comment.