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-publisher@146759 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-04-11 13:23:45 +00:00
parent 51b4eb5ada
commit 913cce5413
8 changed files with 156 additions and 57 deletions

View File

@ -1,9 +1,13 @@
package org.gcube.informationsystem.resourceregistry.publisher;
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 ResourceRegistryPublisherFactory {
protected static ResourceRegistryPublisher singleton;
protected static Map<String, ResourceRegistryPublisher> publishers;
static {
publishers = 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 ResourceRegistryPublisherFactory {
.setResult("$entry/text()");
}
public static ResourceRegistryPublisher create(){
if(singleton==null){
if(FORCED_URL!=null){
return new ResourceRegistryPublisherImpl(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();
}
ResourceRegistryPublisher publisher = publishers.get(key);
if(publisher==null){
SimpleQuery query = getQuery();
List<String> addresses = ICFactory.client().submit(query);
@ -44,10 +74,11 @@ public class ResourceRegistryPublisherFactory {
Random random = new Random();
int index = random.nextInt(addresses.size());
singleton = new ResourceRegistryPublisherImpl(addresses.get(index));
publisher = new ResourceRegistryPublisherImpl(addresses.get(index));
}
return singleton;
return publisher;
}
}

View File

@ -86,10 +86,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return f;
} catch (ResourceRegistryException e) {
logger.error("Error Creating {}", facet, e);
//logger.error("Error Creating {}", facet, e);
throw e;
} catch (Exception e) {
logger.error("Error Creating {}", facet, e);
//logger.error("Error Creating {}", facet, e);
throw new RuntimeException(e);
}
}
@ -116,10 +116,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return f;
} catch (ResourceRegistryException e) {
logger.error("Error Updating {}", facet, e);
//logger.error("Error Updating {}", facet, e);
throw e;
} catch (Exception e) {
logger.error("Error Updating {}", facet, e);
//logger.error("Error Updating {}", facet, e);
throw new RuntimeException(e);
}
}
@ -145,10 +145,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return deleted;
} catch (ResourceRegistryException e) {
logger.error("Error Removing {}", facet, e);
//logger.error("Error Removing {}", facet, e);
throw e;
} catch (Exception e) {
logger.error("Error Removing {}", facet, e);
//logger.error("Error Removing {}", facet, e);
throw new RuntimeException(e);
}
}
@ -177,11 +177,11 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return r;
} catch (ResourceRegistryException e) {
logger.error("Error Creating {}", resource, e);
//logger.error("Error Creating {}", resource, e);
throw e;
} catch (Exception e) {
logger.error("Error Creating {}", resource, e);
//logger.error("Error Creating {}", resource, e);
throw new RuntimeException(e);
}
}
@ -209,10 +209,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return r;
} catch (ResourceRegistryException e) {
logger.error("Error Creating {}", resource, e);
//logger.error("Error Creating {}", resource, e);
throw e;
} catch (Exception e) {
logger.error("Error Creating {}", resource, e);
//logger.error("Error Creating {}", resource, e);
throw new RuntimeException(e);
}
}
@ -238,10 +238,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return deleted;
} catch (ResourceRegistryException e) {
logger.error("Error Removing {}", resource, e);
//logger.error("Error Removing {}", resource, e);
throw e;
} catch (Exception e) {
logger.error("Error Removing {}", resource, e);
//logger.error("Error Removing {}", resource, e);
throw new RuntimeException(e);
}
}
@ -280,10 +280,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return c;
} catch (ResourceRegistryException e) {
logger.error("Error Creating {}", consistsOf, e);
//logger.error("Error Creating {}", consistsOf, e);
throw e;
} catch (Exception e) {
logger.error("Error Creating {}", consistsOf, e);
//logger.error("Error Creating {}", consistsOf, e);
throw new RuntimeException(e);
}
}
@ -316,10 +316,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return deleted;
} catch (ResourceRegistryException e) {
logger.error("Error Removing {} with UUID {}", ConsistsOf.NAME, uuid, e);
//logger.error("Error Removing {} with UUID {}", ConsistsOf.NAME, uuid, e);
throw e;
} catch (Exception e) {
logger.error("Error Removing {} with UUID {}", ConsistsOf.NAME, uuid, e);
//logger.error("Error Removing {} with UUID {}", ConsistsOf.NAME, uuid, e);
throw new RuntimeException(e);
}
}
@ -359,10 +359,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return i;
} catch (ResourceRegistryException e) {
logger.error("Error Creating {}", isRelatedTo, e);
//logger.error("Error Creating {}", isRelatedTo, e);
throw e;
} catch (Exception e) {
logger.error("Error Creating {}", isRelatedTo, e);
//logger.error("Error Creating {}", isRelatedTo, e);
throw new RuntimeException(e);
}
}
@ -394,10 +394,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return deleted;
} catch (ResourceRegistryException e) {
logger.error("Error Removing {} with UUID {}", IsRelatedTo.NAME, uuid, e);
//logger.error("Error Removing {} with UUID {}", IsRelatedTo.NAME, uuid, e);
throw e;
} catch (Exception e) {
logger.error("Error Removing {} with UUID {}", IsRelatedTo.NAME, uuid, e);
//logger.error("Error Removing {} with UUID {}", IsRelatedTo.NAME, uuid, e);
throw new RuntimeException(e);
}
}
@ -433,12 +433,12 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return added;
} catch (ResourceRegistryException e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Resource.NAME, uuid, Context.NAME, context, e);
//logger.error("Error Adding {} with UUID {} to current {} : {}",
// Resource.NAME, uuid, Context.NAME, context, e);
throw e;
} catch (Exception e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Resource.NAME, uuid, Context.NAME, context, e);
//logger.error("Error Adding {} with UUID {} to current {} : {}",
// Resource.NAME, uuid, Context.NAME, context, e);
throw new RuntimeException(e);
}
}
@ -479,12 +479,12 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return added;
} catch (ResourceRegistryException e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Facet.NAME, uuid, Context.NAME, context, e);
// logger.error("Error Adding {} with UUID {} to current {} : {}",
// Facet.NAME, uuid, Context.NAME, context, e);
throw e;
} catch (Exception e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Facet.NAME, uuid, Context.NAME, context, e);
// logger.error("Error Adding {} with UUID {} to current {} : {}",
// Facet.NAME, uuid, Context.NAME, context, e);
throw new RuntimeException(e);
}
}
@ -526,13 +526,13 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return removed;
} catch (ResourceRegistryException e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Resource.NAME, uuid, Context.NAME, context, e);
// logger.error("Error Adding {} with UUID {} to current {} : {}",
// Resource.NAME, uuid, Context.NAME, context, e);
throw e;
} catch (Exception e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Resource.NAME, uuid, Context.NAME, context, e);
// logger.error("Error Adding {} with UUID {} to current {} : {}",
// Resource.NAME, uuid, Context.NAME, context, e);
throw new RuntimeException(e);
}
}
@ -575,13 +575,12 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return removed;
} catch (ResourceRegistryException e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Facet.NAME, uuid, Context.NAME, context, e);
// logger.error("Error Adding {} with UUID {} to current {} : {}",
// Facet.NAME, uuid, Context.NAME, context, e);
throw e;
} catch (Exception e) {
logger.error("Error Adding {} with UUID {} to current {} : {}",
Facet.NAME, uuid, Context.NAME, context, e);
// logger.error("Error Adding {} with UUID {} to current {} : {}",
// Facet.NAME, uuid, Context.NAME, context, e);
throw new RuntimeException(e);
}
}
@ -593,5 +592,4 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
return removeFacetFromContext(facet.getHeader().getUUID());
}
}

View File

@ -0,0 +1,9 @@
package org.gcube.informationsystem.resourceregistry.client;
public class ResourceRegistryClientFactorySetter {
public static void forceToURL(String url) {
ResourceRegistryClientFactory.forceToURL(url);
}
}

View File

@ -38,11 +38,12 @@ import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.model.relation.consistsof.HasPersistentMemory;
import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory;
import org.gcube.informationsystem.model.relation.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
@ -95,8 +96,8 @@ public class MultiContextTest extends ScopedTest {
licenseFacet.setTextURL(new URL("https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11"));
eService.addFacet(licenseFacet);
EService createEService = resourceRegistryPublisher.createResource(EService.class, eService);
logger.debug("Created : {}", createEService);
EService createdEService = resourceRegistryPublisher.createResource(EService.class, eService);
logger.debug("Created : {}", createdEService);
HostingNode hostingNode = new HostingNodeImpl();
@ -137,26 +138,55 @@ public class MultiContextTest extends ScopedTest {
new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(hostingNode, disk, null);
hostingNode.addFacet(hasPersistentMemory);
Hosts<HostingNode, EService> hosts = new HostsImpl<HostingNode, EService>(hostingNode, createEService, null);
Hosts<HostingNode, EService> hosts = new HostsImpl<HostingNode, EService>(hostingNode, createdEService, null);
hostingNode.attachResource(hosts);
HostingNode createdHN = resourceRegistryPublisher.createResource(HostingNode.class, hostingNode);
logger.debug("Created : {}", createdHN);
logger.debug("Changing token to test add to scope");
ScopedTest.setContext(ScopedTest.ALTERNATIVE_TEST_SCOPE);
UUID uuid = createdHN.getHeader().getUUID();
boolean addedToContext = resourceRegistryPublisher.addResourceToContext(uuid);
Assert.assertTrue(addedToContext);
UUID eServiceUUID = createEService.getHeader().getUUID();
UUID hostingNodeUUID = createdHN.getHeader().getUUID();
UUID eServiceUUID = createdEService.getHeader().getUUID();
logger.debug("Changing token to test add to scope");
ScopedTest.setContext(ScopedTest.ALTERNATIVE_TEST_SCOPE);
try {
resourceRegistryClient.getInstance(EService.class, eServiceUUID);
}catch(ResourceNotFoundException e){
logger.debug("Resource with {} Not Found as Expected", uuid.toString());
resourceRegistryClient.exists(EService.class, eServiceUUID);
} catch (ERNotFoundException e) {
throw e;
} catch (ERAvailableInAnotherContextException e) {
// Good
} catch (ResourceRegistryException e) {
throw e;
}
try {
resourceRegistryClient.exists(HostingNode.class, hostingNodeUUID);
} catch (ERNotFoundException e) {
throw e;
} catch (ERAvailableInAnotherContextException e) {
// Good
} catch (ResourceRegistryException e) {
throw e;
}
boolean addedToContext = resourceRegistryPublisher.addResourceToContext(hostingNodeUUID);
Assert.assertTrue(addedToContext);
try {
resourceRegistryClient.exists(EService.class, eServiceUUID);
}catch(ResourceAvailableInAnotherContextException e){
logger.debug("Resource with {} Not Found as Expected", eServiceUUID.toString());
}
boolean deleted = resourceRegistryPublisher.deleteResource(createdHN);
@ -166,7 +196,7 @@ public class MultiContextTest extends ScopedTest {
logger.debug("Restoring original scope");
ScopedTest.setContext(ScopedTest.DEFAULT_TEST_SCOPE);
deleted = resourceRegistryPublisher.deleteResource(createEService);
deleted = resourceRegistryPublisher.deleteResource(createdEService);
Assert.assertTrue(deleted);
}

View File

@ -12,6 +12,7 @@ import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactorySetter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
@ -39,9 +40,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 +69,27 @@ public class ScopedTest {
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
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){
ResourceRegistryPublisherFactory.forceToURL(RESOURCE_REGISTRY_URL);
ResourceRegistryClientFactorySetter.forceToURL(RESOURCE_REGISTRY_URL);
}
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{

View File

@ -0,0 +1 @@
registry.properties.test

View File

@ -0,0 +1 @@
RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry