diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClient.java index e490b99..d77e18d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClient.java @@ -13,39 +13,28 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.context.Conte */ public interface ResourceRegistryContextClient { - public Context create(Context context) - throws ContextAlreadyPresentException, ResourceRegistryException; + public Context create(Context context) throws ContextAlreadyPresentException, ResourceRegistryException; - public String create(String context) - throws ContextAlreadyPresentException, ResourceRegistryException; - - - public Context read(Context context) - throws ContextNotFoundException, ResourceRegistryException; - - public Context read(UUID uuid) - throws ContextNotFoundException, ResourceRegistryException; - - public String read(String uuid) - throws ContextNotFoundException, ResourceRegistryException; - - - public Context update(Context context) - throws ResourceRegistryException; + public String create(String context) throws ContextAlreadyPresentException, ResourceRegistryException; + + public Context read(Context context) throws ContextNotFoundException, ResourceRegistryException; + + public Context read(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; + + public String read(String uuid) throws ContextNotFoundException, ResourceRegistryException; + + public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException; + + public Context update(Context context) throws ResourceRegistryException; + + public String update(String context) throws ResourceRegistryException; + + public boolean delete(Context context) throws ContextNotFoundException, ResourceRegistryException; + + public boolean delete(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; + + public boolean delete(String uuid) throws ContextNotFoundException, ResourceRegistryException; - public String update(String context) - throws ResourceRegistryException; - - - public boolean delete(Context context) - throws ContextNotFoundException, ResourceRegistryException; - - public boolean delete(UUID uuid) - throws ContextNotFoundException, ResourceRegistryException; - - public boolean delete(String uuid) - throws ContextNotFoundException, ResourceRegistryException; - public List all() throws ResourceRegistryException; - + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientImpl.java index 35c9da9..bb98af7 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientImpl.java @@ -47,6 +47,16 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex contextCache.setContextCacheRenewal(contextCacheRenewal); } + private void forceCacheRefresh() { + try { + ContextCache contextCache = ContextCache.getInstance(); + contextCache.cleanCache(); + contextCache.refreshContextsIfNeeded(); + }catch (Exception e) { + logger.warn("Unable to force cache refresh.", e); + } + } + protected String internalCreate(Context context) throws ContextAlreadyPresentException, ResourceRegistryException { try { Header header = context.getHeader(); @@ -70,8 +80,9 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString); String c = HTTPUtility.getResponse(String.class, httpURLConnection); - logger.trace("{} successfully created", c); + forceCacheRefresh(); + logger.trace("{} successfully created", c); return c; } catch(ResourceRegistryException e) { @@ -80,14 +91,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } catch(Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); - } finally { - try { - ContextCache contextCache = ContextCache.getInstance(); - contextCache.cleanCache(); - contextCache.refreshContextsIfNeeded(); - }catch (Exception e) { - - } } } @@ -135,8 +138,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } catch (IOException e) { throw new RuntimeException(e); } - contextCache.cleanCache(); - contextCache.refreshContextsIfNeeded(); + forceCacheRefresh(); Context c = contextCache.getContextByUUID(context.getHeader().getUUID()); if(c!=null){ context = c; @@ -147,7 +149,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex return context; } - public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException { + @Override + public Context readCurrentContext() throws ContextNotFoundException, ResourceRegistryException { String contextFullName = ResourceRegistryContextClientFactory.getCurrentContextFullName(); ContextCache contextCache = ContextCache.getInstance(); UUID uuid = contextCache.getUUIDByFullName(contextFullName); @@ -226,6 +229,8 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(contextString); String c = HTTPUtility.getResponse(String.class, httpURLConnection); + forceCacheRefresh(); + logger.trace("{} successfully updated", c); return c; @@ -235,14 +240,6 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } catch(Exception e) { // logger.trace("Error Creating {}", facet, e); throw new RuntimeException(e); - } finally { - try { - ContextCache contextCache = ContextCache.getInstance(); - contextCache.cleanCache(); - contextCache.refreshContextsIfNeeded(); - }catch (Exception e) { - - } } } @@ -321,7 +318,7 @@ public class ResourceRegistryContextClientImpl implements ResourceRegistryContex } } - public List getAllContextFromServer() throws ResourceRegistryException { + protected List getAllContextFromServer() throws ResourceRegistryException { try { logger.trace("Going to read {} with UUID {}", Context.NAME); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientTest.java index 64bfb55..7f9b575 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ResourceRegistryContextClientTest.java @@ -14,7 +14,9 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.context.impl.entities.ContextImpl; import org.gcube.informationsystem.context.reference.entities.Context; +import org.gcube.informationsystem.context.reference.relations.IsParentOf; import org.gcube.informationsystem.model.reference.properties.Header; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; @@ -450,4 +452,47 @@ public class ResourceRegistryContextClientTest extends ContextTest { } + + @Test + public void testGetAllContexts() throws Exception { + List contexts = resourceRegistryContextClient.all(); + logger.debug("{}", contexts); + + ContextCache contextCache = ContextCache.getInstance(); + Map uuidToContextFullName = contextCache.getUUIDToContextFullNameAssociation(); + logger.debug("{}", uuidToContextFullName); + + + for(Context c : contexts) { + UUID uuid = c.getHeader().getUUID(); + if(c.getParent()!=null) { + IsParentOf isParentOf = c.getParent(); + Context parentContext = isParentOf.getSource(); + UUID parentUUID = parentContext.getHeader().getUUID(); + Assert.assertEquals(parentContext, contextCache.getContextByUUID(parentUUID)); + List children = parentContext.getChildren(); + boolean found = false; + for(IsParentOf ipo : children) { + if(ipo.equals(isParentOf)) { + found = true; + break; + } + } + Assert.assertTrue(found); + logger.debug("{} : {} (parent {} : {})", c.getHeader().getUUID(), contextCache.getContextFullNameByUUID(uuid), parentUUID, contextCache.getContextFullNameByUUID(parentUUID)); + }else { + logger.debug("{} : {}", c.getHeader().getUUID(), contextCache.getContextFullNameByUUID(uuid)); + } + } + + Context currentContext = resourceRegistryContextClient.readCurrentContext(); + logger.debug("Current context : {}", currentContext); + + for(Context c : contexts) { + UUID uuid = c.getHeader().getUUID(); + Context context = resourceRegistryContextClient.read(uuid); + String fullName = ContextCache.getInstance().getContextFullNameByUUID(uuid); + logger.debug("{} - {} : {}", uuid, fullName, context); + } + } }