resource-registry-database-.../src/test/java/org/gcube/informationsystem/resourceregistry/ContextTest.java

100 lines
2.7 KiB
Java
Raw Normal View History

/**
*
*/
package org.gcube.informationsystem.resourceregistry;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
2022-05-24 11:01:51 +02:00
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
2019-10-18 11:57:14 +02:00
protected static Properties properties;
protected static final String PROPERTIES_FILENAME = "token.properties";
2022-11-11 15:09:15 +01:00
public static final String ROOT_DEV;
public static final String ROOT_PREPROD;
public static final String ROOT_PROD;
static {
2019-10-18 11:57:14 +02:00
properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
2019-10-18 11:57:14 +02:00
try {
// load the properties file
properties.load(input);
2019-10-18 11:57:14 +02:00
} catch(IOException e) {
throw new RuntimeException(e);
}
2022-11-11 15:09:15 +01:00
ROOT_DEV = "/gcube";
ROOT_PREPROD = "/pred4s";
ROOT_PROD = "/d4science.research-infrastructures.eu";
2019-11-08 18:43:02 +01:00
2019-11-08 18:17:43 +01:00
}
2022-05-24 11:01:51 +02:00
public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset();
2022-05-27 17:41:11 +02:00
SecretManager secretManager = new SecretManager();
SecretManagerProvider.instance.set(secretManager);
2022-05-24 11:01:51 +02:00
secretManager.addSecret(secret);
secretManager.set();
}
public static void setContext(String token) throws Exception {
Secret secret = getSecret(token);
set(secret);
}
public static void setContextByName(String fullContextName) throws Exception {
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static Secret getSecret(String token) throws Exception {
Secret secret = SecretUtility.getSecretByTokenString(token);
return secret;
}
2022-05-24 11:01:51 +02:00
private static Secret getSecretByContextName(String fullContextName) throws Exception {
2019-10-18 11:57:14 +02:00
String token = ContextTest.properties.getProperty(fullContextName);
2022-05-24 11:01:51 +02:00
return getSecret(token);
2019-10-18 11:57:14 +02:00
}
2022-05-24 11:01:51 +02:00
public static String getUser() {
String user = Header.UNKNOWN_USER;
try {
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
return user;
}
@BeforeClass
2019-10-18 11:57:14 +02:00
public static void beforeClass() throws Exception {
2022-11-11 15:09:15 +01:00
setContextByName(ROOT_DEV);
}
@AfterClass
2019-10-18 11:57:14 +02:00
public static void afterClass() throws Exception {
2022-05-24 11:01:51 +02:00
SecretManagerProvider.instance.reset();
}
}