Skip to content

Commit

Permalink
QA-5209: Defined the junit-platform.properties file to enable paralle…
Browse files Browse the repository at this point in the history
…l test execution and reviewed the bean scope to prevent conflicts with parallelism
  • Loading branch information
mmancini95 committed Dec 23, 2024
1 parent 1fcd7ba commit 23a39a5
Show file tree
Hide file tree
Showing 50 changed files with 420 additions and 275 deletions.
13 changes: 13 additions & 0 deletions interop-qa-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
</configuration>
</plugin>

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@
import it.pagopa.interop.generated.openapi.clients.bff.ApiClient;
import it.pagopa.interop.generated.openapi.clients.bff.api.AgreementsApi;
import it.pagopa.interop.generated.openapi.clients.bff.model.*;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.UUID;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AgreementClientImpl implements IAgreementClient {
private final AgreementsApi agreementsApi;
private final RestTemplate restTemplate;
private final InteropClientConfigs interopClientConfigs;
private final String basePath;
private final String bearerToken;

public AgreementClientImpl(RestTemplate restTemplate, InteropClientConfigs interopClientConfigs) {
this.restTemplate = restTemplate;
this.interopClientConfigs = interopClientConfigs;
this.basePath = interopClientConfigs.getBaseUrl();
this.bearerToken = "bearerToken";
this.agreementsApi = new AgreementsApi(createApiClient(bearerToken));
this.agreementsApi = new AgreementsApi(createApiClient("dummyBearer"));
}

private ApiClient createApiClient(String bearerToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@
import it.pagopa.interop.generated.openapi.clients.bff.ApiClient;
import it.pagopa.interop.generated.openapi.clients.bff.api.EservicesApi;
import it.pagopa.interop.generated.openapi.clients.bff.model.*;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class EServiceApiClientImpl implements IEServiceClient {
private final EservicesApi eservicesApi;
private final RestTemplate restTemplate;
private final InteropClientConfigs interopClientConfigs;
private final String basePath;
private final String bearerToken;

public EServiceApiClientImpl(RestTemplate restTemplate, InteropClientConfigs interopClientConfigs) {
this.restTemplate = restTemplate;
this.interopClientConfigs = interopClientConfigs;
this.basePath = interopClientConfigs.getBaseUrl();
this.bearerToken = "bearerToken";
this.eservicesApi = new EservicesApi(createApiClient(bearerToken));
this.eservicesApi = new EservicesApi(createApiClient("dummyBearer"));
}

private ApiClient createApiClient(String bearerToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@
import it.pagopa.interop.generated.openapi.clients.bff.ApiClient;
import it.pagopa.interop.generated.openapi.clients.bff.api.AttributesApi;
import it.pagopa.interop.generated.openapi.clients.bff.model.*;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AttributeApiClientImpl implements IAttributeApiClient {
private final AttributesApi attributesApi;
private final RestTemplate restTemplate;
private final InteropClientConfigs interopClientConfigs;
private final String basePath;
private final String bearerToken;

public AttributeApiClientImpl(RestTemplate restTemplate, InteropClientConfigs interopClientConfigs) {
this.restTemplate = restTemplate;
this.interopClientConfigs = interopClientConfigs;
this.basePath = interopClientConfigs.getBaseUrl();
this.bearerToken = "bearerToken";
this.attributesApi = new AttributesApi(createApiClient(bearerToken));
this.attributesApi = new AttributesApi(createApiClient("dummyBearer"));
}

private ApiClient createApiClient(String bearerToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import com.google.gson.reflect.TypeToken;
import it.pagopa.interop.authorization.domain.Tenant;
import it.pagopa.interop.authorization.domain.ExternalId;
import it.pagopa.interop.authorization.service.utils.ConfigFileReader;
import it.pagopa.interop.conf.springconfig.InteropClientConfigs;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.kms.KmsClient;
import software.amazon.awssdk.services.kms.model.*;
Expand All @@ -22,6 +27,8 @@
import java.util.*;

@Slf4j
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class SessionTokenFactory {
private static final Map<String, Map<String, String>> CONFIG = new HashMap<>();
private static final Map<String, Object> SESSION_TOKEN_PAYLOAD_TEMPLATE;
Expand Down Expand Up @@ -61,9 +68,26 @@ public class SessionTokenFactory {
}

private final InteropClientConfigs interopClientConfigs;
@Getter
private Map<String, Map<String, String>> cachedTokens = null;
private ConfigFileReader configFileReader;

public SessionTokenFactory(InteropClientConfigs interopClientConfigs) {

public SessionTokenFactory(InteropClientConfigs interopClientConfigs, ConfigFileReader configFileReader) {
this.interopClientConfigs = interopClientConfigs;
this.configFileReader = configFileReader;
this.cachedTokens = loadToken();
}

private Map<String, Map<String, String>> loadToken() {
try {
if (cachedTokens == null) {
cachedTokens = generateSessionToken(configFileReader.getTenantList());
}
} catch (Exception ex) {
throw new IllegalArgumentException("There was an error while creating the session token: " + ex.getMessage(), ex);
}
return cachedTokens;
}

public Map<String, Map<String, String>> generateSessionToken(List<Tenant> configFile) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.UUID;

import it.pagopa.interop.generated.openapi.clients.bff.model.*;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import it.pagopa.interop.authorization.service.IAuthorizationClient;
Expand All @@ -13,17 +16,17 @@
import it.pagopa.interop.generated.openapi.clients.bff.api.ClientsApi;
import it.pagopa.interop.generated.openapi.clients.bff.api.PurposesApi;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AuthorizationClientImpl implements IAuthorizationClient {
private final ClientsApi clientsApi;
private final RestTemplate restTemplate;
private final String basePath;
private final String bearerToken;

public AuthorizationClientImpl(RestTemplate restTemplate, InteropClientConfigs interopClientConfigs) {
this.restTemplate = restTemplate;
this.basePath = interopClientConfigs.getBaseUrl();
this.bearerToken = "apiBearerToken";
this.clientsApi = new ClientsApi(createApiClient(bearerToken));
this.clientsApi = new ClientsApi(createApiClient("dummyBearer"));
}

private ApiClient createApiClient(String bearerToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
import it.pagopa.interop.generated.openapi.clients.bff.ApiClient;
import it.pagopa.interop.generated.openapi.clients.bff.api.EservicesApi;
import it.pagopa.interop.generated.openapi.clients.bff.model.ProducerEServiceDescriptor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ProducerClientImpl implements IProducerClient {
private final EservicesApi eservicesApi;
private final RestTemplate restTemplate;
private final InteropClientConfigs interopClientConfigs;
private final String basePath;
private final String bearerToken;

public ProducerClientImpl(RestTemplate restTemplate, InteropClientConfigs interopClientConfigs) {
this.restTemplate = restTemplate;
this.interopClientConfigs = interopClientConfigs;
this.basePath = interopClientConfigs.getBaseUrl();
this.bearerToken = "apiBearerToken";
this.eservicesApi = new EservicesApi(createApiClient(bearerToken));
this.eservicesApi = new EservicesApi(createApiClient("dummyBearer"));
}

private ApiClient createApiClient(String bearerToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,47 @@

import it.pagopa.interop.authorization.domain.Tenant;
import it.pagopa.interop.authorization.service.factory.SessionTokenFactory;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import it.pagopa.interop.conf.springconfig.InteropClientConfigs;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Slf4j
public class CommonUtils {
private final SessionTokenFactory sessionTokenFactory;
private final KeyPairGeneratorUtil keyPairGeneratorUtil;
private final ClientTokenConfigurator clientTokenConfigurator;
private final InteropClientConfigs interopClientConfigs;
private final List<Tenant> configFile;

private Map<String, Map<String, String>> cachedTokens = null;
private final List<Tenant> tenantList;

public CommonUtils(SessionTokenFactory sessionTokenFactory,
KeyPairGeneratorUtil keyPairGeneratorUtil,
ClientTokenConfigurator clientTokenConfigurator,
InteropClientConfigs interopClientConfigs) {
InteropClientConfigs interopClientConfigs,
ConfigFileReader configFileReader) {
this.sessionTokenFactory = sessionTokenFactory;
this.keyPairGeneratorUtil = keyPairGeneratorUtil;
this.clientTokenConfigurator = clientTokenConfigurator;
this.interopClientConfigs = interopClientConfigs;
this.configFile = readProperty();
this.tenantList = configFileReader.getTenantList();
}

public String getToken(String tenantType, String role) {
try {
if (cachedTokens == null) {
cachedTokens = sessionTokenFactory.generateSessionToken(configFile);
}
} catch (Exception ex) {
throw new IllegalArgumentException("There was an error while creating the session token: " + ex.getMessage(), ex);
}
Map<String, String> tenantTokens = cachedTokens.get(tenantType);

String token = Optional.ofNullable(cachedTokens)
String token = Optional.ofNullable(sessionTokenFactory.getCachedTokens())
.map(m -> m.get(tenantType))
.filter(Objects::nonNull)
.map(m -> (role == null) ? m.get("admin") : m.get(role))
.filter(Objects::nonNull)
.orElse(null);

if (token == null) {
throw new IllegalArgumentException("Token not found for tenant: " + tenantType + " and role: " + role);
}
.orElseThrow(() -> new IllegalArgumentException("Token not found for tenant: " + tenantType + " and role: " + role));
return token;
}

public void setBearerToken(String token) {
clientTokenConfigurator.setBearerToken(token);
}

public UUID getUserId(String tenantType, String role) {
return configFile.stream()
return tenantList.stream()
.filter(tenant -> tenantType.equals(tenant.getName()))
.map(Tenant::getUserRoles)
.map(userRole -> userRole.get(role))
Expand All @@ -76,7 +52,7 @@ public UUID getUserId(String tenantType, String role) {
}

public UUID getOrganizationId(String tenantType) {
return configFile.stream()
return tenantList.stream()
.filter(tenant -> tenantType.equals(tenant.getName()))
.map(tenant -> tenant.getOrganizationId())
.map(o -> o.get("dev"))
Expand Down Expand Up @@ -105,18 +81,4 @@ public <T> void makePolling(Supplier<T> promise, Predicate<T> shouldStop, String
throw new IllegalArgumentException("Eventual consistency error: " + errorMessage);
}

private List<Tenant> readProperty() {
InputStream inputStream = null;
List<Tenant> tenantList = new ArrayList<>();
try {
inputStream = new FileInputStream(new File("config/tenants-ids.yaml"));
Yaml yaml = new Yaml(new Constructor(Tenant.class));
yaml.loadAll(inputStream).forEach(i -> tenantList.add((Tenant) i));
} catch (Exception e) {
throw new RuntimeException(e);
}
return tenantList;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package it.pagopa.interop.authorization.service.utils;

import it.pagopa.interop.authorization.domain.Tenant;
import lombok.Getter;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@Getter
public class ConfigFileReader {
private final List<Tenant> tenantList;

public ConfigFileReader() {
this.tenantList = readProperty();
}

private List<Tenant> readProperty() {
String filePath = "config/tenants-ids.yaml";
List<Tenant> tenantList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
Yaml yaml = new Yaml(new Constructor(Tenant.class));
yaml.loadAll(reader).forEach(i -> tenantList.add((Tenant) i));
} catch (IOException exception) {
exception.printStackTrace();
}
return tenantList;
}

}
Loading

0 comments on commit 23a39a5

Please sign in to comment.