diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ContextTest.java index 02a2d32..6a36dc7 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ContextTest.java @@ -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.SecretUtility; import org.gcube.common.keycloak.KeycloakClientFactory; +import org.gcube.common.keycloak.KeycloakClientHelper; 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.BeforeClass; import org.slf4j.Logger; @@ -24,7 +23,6 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -@SuppressWarnings("deprecation") public class ContextTest { 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 DEVSEC; public static final String DEVVRE; - + protected static final Properties properties; - protected static final String CLIENT_ID_PROPERTY_KEY = "client_id"; - protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret"; - - protected static final String clientID; - protected static final String clientSecret; + public static final String TYPE_PROPERTY_KEY = "type"; + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String PASSWORD_PROPERTY_KEY = "password"; + public static final String CLIENT_ID_PROPERTY_KEY = "clientId"; public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL"; - public static final String RESOURCE_REGISTRY_URL; static { GCUBE = "/gcube"; @@ -59,7 +55,7 @@ public class ContextTest { DEVSEC = GCUBE + "/devsec"; DEVVRE = DEVSEC + "/devVRE"; - PARENT_DEFAULT_TEST_SCOPE = "/gcube"; + PARENT_DEFAULT_TEST_SCOPE = GCUBE; DEFAULT_TEST_SCOPE = DEVNEXT; ALTERNATIVE_TEST_SCOPE = NEXTNEXT; @@ -68,18 +64,16 @@ public class ContextTest { try { // load the properties file 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) { throw new RuntimeException(e); } } + private enum Type{ + USER, CLIENT_ID + }; + public static void set(Secret secret) throws Exception { SecretManagerProvider.instance.reset(); SecretManager secretManager = new SecretManager(); @@ -89,15 +83,56 @@ public class ContextTest { } public static void setContextByName(String fullContextName) throws Exception { + logger.debug("Going to set credentials for context {}", fullContextName); Secret secret = getSecretByContextName(fullContextName); set(secret); } private static TokenResponse getJWTAccessToken(String context) throws Exception { - ScopeProvider.instance.set(context); - TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null); - return tr; + Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString()); + + 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 { @@ -117,7 +152,7 @@ public class ContextTest { } public static String getUser() { - String user = Metadata.UNKNOWN_USER; + String user = "UNKNOWN"; try { user = SecretManagerProvider.instance.get().getUser().getUsername(); } catch(Exception e) { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ERManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ERManagementTest.java index cc04c34..242f2ce 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ERManagementTest.java @@ -86,8 +86,9 @@ public class ERManagementTest extends ContextTest { protected ResourceRegistryPublisher resourceRegistryPublisher; public ERManagementTest() { - if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { - resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); + Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY); + if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) { + resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString()); }else { resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); } @@ -585,5 +586,11 @@ public class ERManagementTest extends ContextTest { // 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); +// } } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java index 2552198..cf7be15 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/SmartgearResourcesTest.java @@ -85,8 +85,9 @@ public class SmartgearResourcesTest extends ERManagementTest { protected ResourceRegistryPublisher resourceRegistryPublisher; public SmartgearResourcesTest() { - if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { - resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); + Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY); + if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) { + resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString()); }else { resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/ERManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/ERManagementTest.java index c5ca2dd..3633f47 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/ERManagementTest.java @@ -84,9 +84,10 @@ public class ERManagementTest extends ContextTest { protected ResourceRegistryClient resourceRegistryClient; public ERManagementTest() { - if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { - resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); - resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL); + Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY); + if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) { + resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString()); + resourceRegistryClient = new ResourceRegistryClientImpl(rrURLOBj.toString()); }else { resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryClient = ResourceRegistryClientFactory.create(); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/EntityManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/EntityManagementTest.java index b2c2e18..8af5034 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/EntityManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/EntityManagementTest.java @@ -42,8 +42,9 @@ public class EntityManagementTest extends ContextTest { protected ResourceRegistryPublisher resourceRegistryPublisher; public EntityManagementTest(){ - if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { - resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); + Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY); + if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) { + resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString()); }else { resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/MultiContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/MultiContextTest.java index 3b3f30e..700039f 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/MultiContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/old/MultiContextTest.java @@ -72,10 +72,10 @@ public class MultiContextTest extends ContextTest { protected ResourceRegistryClient resourceRegistryClient; public MultiContextTest() throws Exception { - // ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE); - if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) { - resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL); - resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL); + Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY); + if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) { + resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString()); + resourceRegistryClient = new ResourceRegistryClientImpl(rrURLOBj.toString()); }else { resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryClient = ResourceRegistryClientFactory.create(); diff --git a/src/test/resources/config.ini b/src/test/resources/config.ini index 7127852..905df0e 100644 --- a/src/test/resources/config.ini +++ b/src/test/resources/config.ini @@ -1,7 +1,22 @@ -client_id=junit.tests.luca.frosini -client_secret=6b0d0222-3130-41f1-a86d-6b23993547be -#RESOURCE_REGISTRY_URL=http://localhost:8080/resource-registry -#RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry -#RESOURCE_REGISTRY_URL=http://resourceregistry1-d-d4s.d4science.org/resource-registry -#RESOURCE_REGISTRY_URL=https://resource-registry.dev.d4science.org/resource-registry -#RESOURCE_REGISTRY_URL=http://url.gcube.d4science.org/resource-registry \ No newline at end of file +type=USER +#type=CLIENT_ID + +username=luca.frosini +password=Pupp4M3l0 + +#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= \ No newline at end of file