From 73f7d5bc92cd5c1bf08a1b934d1bca5b505905b9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 11 Apr 2017 13:23:27 +0000 Subject: [PATCH] Changed code to allow to pass ResourceRegistry URL for testing purpose git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@146758 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/ResourceRegistryClientFactory.java | 41 ++++++++++++++++--- .../client/ResourceRegistryClientImpl.java | 2 +- .../resourceregistry/client/ScopedTest.java | 32 ++++++++++++++- src/test/resources/registry.properties | 1 + src/test/resources/registry.properties.empty | 0 src/test/resources/registry.properties.test | 1 + 6 files changed, 69 insertions(+), 8 deletions(-) create mode 120000 src/test/resources/registry.properties create mode 100644 src/test/resources/registry.properties.empty create mode 100644 src/test/resources/registry.properties.test diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactory.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactory.java index 779f63f..e52b189 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactory.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactory.java @@ -1,9 +1,13 @@ package org.gcube.informationsystem.resourceregistry.client; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Random; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.resourceregistry.api.Constants; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; @@ -13,7 +17,17 @@ import org.gcube.resources.discovery.icclient.ICFactory; */ public class ResourceRegistryClientFactory { - protected static ResourceRegistryClient singleton; + protected static Map clients; + + static { + clients = new HashMap<>(); + } + + private static String FORCED_URL = null; + + protected static void forceToURL(String url){ + FORCED_URL = url; + } private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'"; private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'"; @@ -30,9 +44,25 @@ public class ResourceRegistryClientFactory { .setResult("$entry/text()"); } - public static ResourceRegistryClient create(){ - if(singleton==null){ + if(FORCED_URL!=null){ + return new ResourceRegistryClientImpl(FORCED_URL); + } + + String key = null; + if (SecurityTokenProvider.instance.get() == null) { + if (ScopeProvider.instance.get() == null) { + throw new RuntimeException( + "Null Token and Scope. Please set your token first."); + } + key = ScopeProvider.instance.get(); + } else { + key = SecurityTokenProvider.instance.get(); + } + + ResourceRegistryClient client = clients.get(key); + + if(client==null){ SimpleQuery query = getQuery(); List addresses = ICFactory.client().submit(query); @@ -44,10 +74,11 @@ public class ResourceRegistryClientFactory { Random random = new Random(); int index = random.nextInt(addresses.size()); - singleton = new ResourceRegistryClientImpl(addresses.get(index)); + client = new ResourceRegistryClientImpl(addresses.get(index)); } - return singleton; + + return client; } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java index f5d7119..ef5e83a 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -223,7 +223,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { Map parameters = new HashMap<>(); parameters.put(AccessPath.QUERY_PARAM, query); if (limit <= 0) { - limit = AccessPath.DEFAULT_LIMIT; + limit = AccessPath.UNBOUNDED; } parameters.put(AccessPath.LIMIT_PARAM, Integer.toString(limit)); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ScopedTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ScopedTest.java index 49d1964..bd67582 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ScopedTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ScopedTest.java @@ -39,9 +39,18 @@ public class ScopedTest { public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE"; public static final String GCUBE_DEVSEC_DEVVRE; + public static final String GCUBE_VARNAME = "GCUBE"; + public static final String GCUBE; + + public static final String DEFAULT_TEST_SCOPE; public static final String ALTERNATIVE_TEST_SCOPE; + + protected static final String REGISTRY_PROPERTIES_FILENAME = "registry.properties"; + public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL"; + public static final String RESOURCE_REGISTRY_URL; + static { Properties properties = new Properties(); InputStream input = ScopedTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME); @@ -59,8 +68,27 @@ public class ScopedTest { GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME); GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME); - DEFAULT_TEST_SCOPE = GCUBE_DEVNEXT_NEXTNEXT; - ALTERNATIVE_TEST_SCOPE = GCUBE_DEVNEXT; + GCUBE = properties.getProperty(GCUBE_VARNAME); + + DEFAULT_TEST_SCOPE = GCUBE_DEVSEC; + ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC_DEVVRE; + + + properties = new Properties(); + input = ScopedTest.class.getClassLoader().getResourceAsStream(REGISTRY_PROPERTIES_FILENAME); + try { + // load the properties file + properties.load(input); + } catch (IOException e) { + throw new RuntimeException(e); + } + + RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY); + + if(RESOURCE_REGISTRY_URL!=null){ + ResourceRegistryClientFactory.forceToURL(RESOURCE_REGISTRY_URL); + } + } public static String getCurrentScope(String token) throws ObjectNotFound, Exception{ diff --git a/src/test/resources/registry.properties b/src/test/resources/registry.properties new file mode 120000 index 0000000..972b730 --- /dev/null +++ b/src/test/resources/registry.properties @@ -0,0 +1 @@ +registry.properties.test \ No newline at end of file diff --git a/src/test/resources/registry.properties.empty b/src/test/resources/registry.properties.empty new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/registry.properties.test b/src/test/resources/registry.properties.test new file mode 100644 index 0000000..cd70085 --- /dev/null +++ b/src/test/resources/registry.properties.test @@ -0,0 +1 @@ +RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry \ No newline at end of file