improved tests

This commit is contained in:
Luca Frosini 2024-04-15 11:00:02 +02:00
parent 179013ec5b
commit 483826685c
7 changed files with 101 additions and 41 deletions

View File

@ -13,9 +13,8 @@ import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory; import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.KeycloakClientHelper;
import org.gcube.common.keycloak.model.TokenResponse; import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -24,7 +23,6 @@ import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("deprecation")
public class ContextTest { public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class); private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
@ -40,17 +38,15 @@ public class ContextTest {
public static final String NEXTNEXT; public static final String NEXTNEXT;
public static final String DEVSEC; public static final String DEVSEC;
public static final String DEVVRE; public static final String DEVVRE;
protected static final Properties properties; protected static final Properties properties;
protected static final String CLIENT_ID_PROPERTY_KEY = "client_id"; public static final String TYPE_PROPERTY_KEY = "type";
protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret"; public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
protected static final String clientID; public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
protected static final String clientSecret;
public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL"; public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL";
public static final String RESOURCE_REGISTRY_URL;
static { static {
GCUBE = "/gcube"; GCUBE = "/gcube";
@ -59,7 +55,7 @@ public class ContextTest {
DEVSEC = GCUBE + "/devsec"; DEVSEC = GCUBE + "/devsec";
DEVVRE = DEVSEC + "/devVRE"; DEVVRE = DEVSEC + "/devVRE";
PARENT_DEFAULT_TEST_SCOPE = "/gcube"; PARENT_DEFAULT_TEST_SCOPE = GCUBE;
DEFAULT_TEST_SCOPE = DEVNEXT; DEFAULT_TEST_SCOPE = DEVNEXT;
ALTERNATIVE_TEST_SCOPE = NEXTNEXT; ALTERNATIVE_TEST_SCOPE = NEXTNEXT;
@ -68,18 +64,16 @@ public class ContextTest {
try { try {
// load the properties file // load the properties file
properties.load(input); properties.load(input);
clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
clientSecret = properties.getProperty(CLIENT_SECRET_PROPERTY_KEY);
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private enum Type{
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception { public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset(); SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager(); SecretManager secretManager = new SecretManager();
@ -89,15 +83,56 @@ public class ContextTest {
} }
public static void setContextByName(String fullContextName) throws Exception { public static void setContextByName(String fullContextName) throws Exception {
logger.debug("Going to set credentials for context {}", fullContextName);
Secret secret = getSecretByContextName(fullContextName); Secret secret = getSecretByContextName(fullContextName);
set(secret); set(secret);
} }
private static TokenResponse getJWTAccessToken(String context) throws Exception { private static TokenResponse getJWTAccessToken(String context) throws Exception {
ScopeProvider.instance.set(context); Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
return tr; TokenResponse tr = null;
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
switch (type) {
case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientSecret = properties.getProperty(root);
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
break;
case USER:
default:
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
switch (root) {
case "/gcube":
default:
clientId = "next.d4science.org";
break;
case "/pred4s":
clientId = "pre.d4science.org";
break;
case "/d4science.research-infrastructures.eu":
clientId = "services.d4science.org";
break;
}
clientSecret = null;
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
break;
}
return tr;
} }
public static Secret getSecretByContextName(String context) throws Exception { public static Secret getSecretByContextName(String context) throws Exception {
@ -117,7 +152,7 @@ public class ContextTest {
} }
public static String getUser() { public static String getUser() {
String user = Metadata.UNKNOWN_USER; String user = "UNKNOWN";
try { try {
user = SecretManagerProvider.instance.get().getUser().getUsername(); user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) { } catch(Exception e) {

View File

@ -86,8 +86,9 @@ public class ERManagementTest extends ContextTest {
protected ResourceRegistryPublisher resourceRegistryPublisher; protected ResourceRegistryPublisher resourceRegistryPublisher;
public ERManagementTest() { public ERManagementTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else { }else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
} }
@ -585,5 +586,11 @@ public class ERManagementTest extends ContextTest {
// resourceManagement.update(); // resourceManagement.update();
// //
// } // }
// @Test
// public void testCreateResourceFromJson() throws Exception {
// String json = "{\"type\":\"VirtualService\",\"consistsOf\":[{\"type\":\"IsIdentifiedBy\",\"target\":{\"name\":\"aaaaaa\",\"optional\":\"true\",\"group\":\"xxxxx\",\"description\":\"\",\"version\":\"cvcvvv\",\"qualifier\":\"\",\"type\":\"SoftwareFacet\",\"aaaa\":\"dddddd\"}}]}";
// String ret = resourceRegistryPublisher.create(json);
// }
} }

View File

@ -85,8 +85,9 @@ public class SmartgearResourcesTest extends ERManagementTest {
protected ResourceRegistryPublisher resourceRegistryPublisher; protected ResourceRegistryPublisher resourceRegistryPublisher;
public SmartgearResourcesTest() { public SmartgearResourcesTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else { }else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
} }

View File

@ -84,9 +84,10 @@ public class ERManagementTest extends ContextTest {
protected ResourceRegistryClient resourceRegistryClient; protected ResourceRegistryClient resourceRegistryClient;
public ERManagementTest() { public ERManagementTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL); resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
resourceRegistryClient = new ResourceRegistryClientImpl(rrURLOBj.toString());
}else { }else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
resourceRegistryClient = ResourceRegistryClientFactory.create(); resourceRegistryClient = ResourceRegistryClientFactory.create();

View File

@ -42,8 +42,9 @@ public class EntityManagementTest extends ContextTest {
protected ResourceRegistryPublisher resourceRegistryPublisher; protected ResourceRegistryPublisher resourceRegistryPublisher;
public EntityManagementTest(){ public EntityManagementTest(){
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else { }else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
} }

View File

@ -72,10 +72,10 @@ public class MultiContextTest extends ContextTest {
protected ResourceRegistryClient resourceRegistryClient; protected ResourceRegistryClient resourceRegistryClient;
public MultiContextTest() throws Exception { public MultiContextTest() throws Exception {
// ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE); Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL); resourceRegistryClient = new ResourceRegistryClientImpl(rrURLOBj.toString());
}else { }else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
resourceRegistryClient = ResourceRegistryClientFactory.create(); resourceRegistryClient = ResourceRegistryClientFactory.create();

View File

@ -1,7 +1,22 @@
client_id=junit.tests.luca.frosini type=USER
client_secret=6b0d0222-3130-41f1-a86d-6b23993547be #type=CLIENT_ID
#RESOURCE_REGISTRY_URL=http://localhost:8080/resource-registry
#RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry username=luca.frosini
#RESOURCE_REGISTRY_URL=http://resourceregistry1-d-d4s.d4science.org/resource-registry password=Pupp4M3l0
#RESOURCE_REGISTRY_URL=https://resource-registry.dev.d4science.org/resource-registry
#RESOURCE_REGISTRY_URL=http://url.gcube.d4science.org/resource-registry #username=grsf.publisher
#password=grsfcambiami
#clientId=grsf-publisher
#/gcube=54cc7454-1a84-11ee-be56-0242ac120002
#/pred4s=de293f60-e004-45ce-b64f-20de77fad888
#/d4science.research-infrastructures.eu=
#client_id=junit.tests.luca.frosini
#/gcube=6b0d0222-3130-41f1-a86d-6b23993547be
#clientId=gcat
#/gcube=712add63-c642-4a6a-8ddc-283dc978120a
#/pred4s=4e35a122-8f25-48f9-9788-8588ee86633f
#/d4science.research-infrastructures.eu=