From 4e59b78f6b2178e738d29d43dbb93caf0fce36bb Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 13 Nov 2017 11:46:08 +0000 Subject: [PATCH] Refs #10318: Refactor Resource Regsitry Client API Task-Url: https://support.d4science.org/issues/10318 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-client@158403 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/ResourceRegistryClient.java | 42 ++-- .../client/ResourceRegistryClientImpl.java | 212 +++++++++++------- .../client/ResourceRegistryClientTest.java | 70 +++++- 3 files changed, 222 insertions(+), 102 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java index b796916..459accb 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -24,33 +24,45 @@ import org.gcube.informationsystem.types.TypeBinder.TypeDefinition; */ public interface ResourceRegistryClient { - public String query(final String query, final int limit, final String fetchPlan) - throws InvalidQueryException, ResourceRegistryException; - - public void exists(Class clazz, UUID uuid) + public boolean exists(Class clazz, UUID uuid) + throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException; + + public boolean exists(String type, UUID uuid) throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException; public ERType getInstance(Class clazz, UUID uuid) throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException; - public List getInstances(String type, Boolean polymorphic) throws ResourceRegistryException; + public String getInstance(String type, UUID uuid) + throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException; - public List getInstancesFromEntity(String relationType, Boolean polymorphic, UUID reference, - Direction direction) throws ResourceRegistryException; + public List getInstances(Class clazz, Boolean polymorphic) + throws ResourceRegistryException; - public List getFilteredResources(Class resourceClass, - @SuppressWarnings("rawtypes") Class consistsOfClass, - Class facetClass, + public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException; + + public List getInstancesFromEntity(Class clazz, + Boolean polymorphic, E reference, Direction direction) throws ResourceRegistryException; + + public List getInstancesFromEntity(Class clazz, + Boolean polymorphic, UUID reference, Direction direction) throws ResourceRegistryException; + + public String getInstancesFromEntity(String type, Boolean polymorphic, UUID reference, Direction direction) + throws ResourceRegistryException; + + public > List getFilteredResources( + Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, + Map map) throws ResourceRegistryException; + + public String getFilteredResources(String resourceType, String consistsOfType, String facetType, boolean polymorphic, Map map) throws ResourceRegistryException; - public List getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map map) throws ResourceRegistryException; + public String query(final String query, final int limit, final String fetchPlan) + throws InvalidQueryException, ResourceRegistryException; public List getSchema(Class clazz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException; - public Context getContext(UUID uuid) - throws ContextNotFoundException, ResourceRegistryException; - + public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException; } 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 e9475c7..92c1280 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -1,6 +1,3 @@ -/** - * - */ package org.gcube.informationsystem.resourceregistry.client; import java.io.StringWriter; @@ -58,9 +55,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public void exists(Class clazz, UUID uuid) + public boolean exists(Class clazz, UUID uuid) throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { String type = Utility.getType(clazz); + return exists(type, uuid); + } + + @Override + public boolean exists(String type, UUID uuid) + throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { try { logger.info("Going to check if {} with UUID {} exists", type, uuid); StringWriter stringWriter = new StringWriter(); @@ -74,9 +77,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(uuid.toString()); HTTPCall httpCall = getHTTPCall(); - httpCall.call(clazz, stringWriter.toString(), HTTPMETHOD.HEAD); + httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.HEAD); logger.debug("{} with UUID {} exists", type, uuid); + return true; } catch (ResourceRegistryException e) { // logger.trace("Error while checking if {} with UUID {} exists.", type, uuid, // e); @@ -92,6 +96,16 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { public ERType getInstance(Class clazz, UUID uuid) throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { String type = Utility.getType(clazz); + String ret = getInstance(type, uuid); + try { + return ISMapper.unmarshal(clazz, ret); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public String getInstance(String type, UUID uuid) + throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { try { logger.info("Going to get {} with UUID {}", type, uuid); StringWriter stringWriter = new StringWriter(); @@ -105,10 +119,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { stringWriter.append(uuid.toString()); HTTPCall httpCall = getHTTPCall(); - ERType erType = httpCall.call(clazz, stringWriter.toString(), HTTPMETHOD.GET); + String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET); - logger.debug("Got {} with UUID {} is {}", type, uuid, erType); - return erType; + logger.debug("Got {} with UUID {} is {}", type, uuid, ret); + return ret; } catch (ResourceRegistryException e) { // logger.trace("Error while getting {} with UUID {}", type, uuid, e); throw e; @@ -118,8 +132,21 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } } + @SuppressWarnings("unchecked") @Override - public List getInstances(String type, Boolean polymorphic) throws ResourceRegistryException { + public List getInstances(Class clazz, Boolean polymorphic) + throws ResourceRegistryException { + String type = Utility.getType(clazz); + String ret = getInstances(type, polymorphic); + try { + return (List) ISMapper.unmarshalList(Resource.class, ret); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException { try { logger.info("Going to get all instances of {} ", type); StringWriter stringWriter = new StringWriter(); @@ -137,7 +164,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); logger.debug("Got instances of {} are {}", type, ret); - return ISMapper.unmarshalList(Entity.class, ret); + return ret; } catch (ResourceRegistryException e) { // logger.trace("Error while getting {} instances", type, e); throw e; @@ -148,17 +175,36 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } @Override - public List getInstancesFromEntity(String relationType, Boolean polymorphic, UUID reference, - Direction direction) throws ResourceRegistryException { + public List getInstancesFromEntity(Class clazz, + Boolean polymorphic, E reference, Direction direction) throws ResourceRegistryException { + return getInstancesFromEntity(clazz, polymorphic, reference.getHeader().getUUID(), direction); + } + + @SuppressWarnings("unchecked") + @Override + public List getInstancesFromEntity(Class clazz, + Boolean polymorphic, UUID reference, Direction direction) throws ResourceRegistryException { + String type = Utility.getType(clazz); + String ret = getInstancesFromEntity(type, polymorphic, reference, direction); try { - logger.info("Going to get all instances of {} from/to {}", relationType, reference.toString()); + return (List) ISMapper.unmarshalList(Resource.class, ret); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public String getInstancesFromEntity(String type, Boolean polymorphic, UUID reference, Direction direction) + throws ResourceRegistryException { + try { + logger.info("Going to get all instances of {} from/to {}", type, reference.toString()); StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(AccessPath.ACCESS_PATH_PART); stringWriter.append(PATH_SEPARATOR); stringWriter.append(AccessPath.INSTANCES_PATH_PART); stringWriter.append(PATH_SEPARATOR); - stringWriter.append(relationType); + stringWriter.append(type); Map parameters = new HashMap<>(); parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); @@ -168,8 +214,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { HTTPCall httpCall = getHTTPCall(); String ret = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); - logger.debug("Got instances of {} from/to {} are {}", relationType, reference.toString(), ret); - return ISMapper.unmarshalList(Resource.class, ret); + logger.debug("Got instances of {} from/to {} are {}", type, reference.toString(), ret); + return ret; } catch (ResourceRegistryException e) { // logger.trace("Error while getting instances of {} from/to {}", relationType, // e); @@ -181,37 +227,54 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } } + @SuppressWarnings("unchecked") @Override - public List getSchema(Class clazz, Boolean polymorphic) - throws SchemaNotFoundException, ResourceRegistryException { - String type = Utility.getType(clazz); + public > List getFilteredResources( + Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, + Map map) throws ResourceRegistryException { + String resourceType = Utility.getType(resourceClass); + String consistsOfType = Utility.getType(consistsOfClass); + String facetType = Utility.getType(facetClass); + String ret = getFilteredResources(resourceType, consistsOfType, facetType, polymorphic, map); try { - logger.info("Going to get {} schema", type); + return (List) ISMapper.unmarshalList(Resource.class, ret); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + @Override + public String getFilteredResources(String resourceType, String consistsOfType, String facetType, + boolean polymorphic, Map map) throws ResourceRegistryException { + try { + logger.info("Going to get Filtered Resources ({}) linked by a ConsistsOf Relation ({}) to a Facet ({})" + + " having {}", resourceType, consistsOfType, facetType, map); StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(AccessPath.ACCESS_PATH_PART); stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.SCHEMA_PATH_PART); + stringWriter.append(AccessPath.RESOURCE_INSTANCES_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(resourceType); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(consistsOfType); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(facetType); stringWriter.append(PATH_SEPARATOR); - stringWriter.append(type); - Map parameters = new HashMap<>(); - parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); + Map parameters = new HashMap<>(map); + parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic); HTTPCall httpCall = getHTTPCall(); String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); - logger.debug("Got schema for {} is {}", type, json); - return TypeBinder.deserializeTypeDefinitions(json); + logger.info("Filtered Resources ({}) linked by a ConsistsOf Relation ({}) to a Facet ({})" + + " having {} are : {}", resourceType, consistsOfType, facetType, map, json); + return json; } catch (ResourceRegistryException e) { - // logger.trace("Error while getting {} schema for {}", polymorphic ? - // AccessPath.POLYMORPHIC_PARAM + " " : "", - // type, e); throw e; } catch (Exception e) { - // logger.trace("Error while getting {}schema for {}", polymorphic ? - // AccessPath.POLYMORPHIC_PARAM + " " : "", - // type, e); throw new RuntimeException(e); } } @@ -251,56 +314,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } } - @SuppressWarnings("unchecked") @Override - public List getFilteredResources(Class resourceClass, - @SuppressWarnings("rawtypes") Class consistsOfClass, Class facetClass, - boolean polymorphic, Map map) throws ResourceRegistryException { - String resourceType = Utility.getType(resourceClass); - String consistsOfType = Utility.getType(consistsOfClass); - String facetType = Utility.getType(facetClass); - return (List) getFilteredResources(resourceType, consistsOfType, facetType, polymorphic, map); - } - - @Override - public List getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map map) throws ResourceRegistryException { - try { - logger.info("Going to get Filtered Resources ({}) linked by a ConsistsOf Relation ({}) to a Facet ({})" - + " having {}", resourceType, consistsOfType, facetType, map); - StringWriter stringWriter = new StringWriter(); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.ACCESS_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.RESOURCE_INSTANCES_PATH_PART); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(resourceType); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(consistsOfType); - stringWriter.append(PATH_SEPARATOR); - stringWriter.append(facetType); - stringWriter.append(PATH_SEPARATOR); - - Map parameters = new HashMap<>(map); - parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic); - - HTTPCall httpCall = getHTTPCall(); - String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); - - logger.info("Filtered Resources ({}) linked by a ConsistsOf Relation ({}) to a Facet ({})" - + " having {} are : {}", resourceType, consistsOfType, facetType, map, json); - return ISMapper.unmarshalList(Resource.class, json); - } catch (ResourceRegistryException e) { - throw e; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - - @Override - public Context getContext(UUID uuid) - throws ContextNotFoundException, ResourceRegistryException { + public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException { try { logger.info("Going to get {} with UUID {}", Context.NAME, uuid.toString()); StringWriter stringWriter = new StringWriter(); @@ -329,4 +344,39 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } } + @Override + public List getSchema(Class clazz, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + String type = Utility.getType(clazz); + try { + logger.info("Going to get {} schema", type); + StringWriter stringWriter = new StringWriter(); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(AccessPath.ACCESS_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(AccessPath.SCHEMA_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(type); + + Map parameters = new HashMap<>(); + parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); + + HTTPCall httpCall = getHTTPCall(); + String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); + + logger.debug("Got schema for {} is {}", type, json); + return TypeBinder.deserializeTypeDefinitions(json); + } catch (ResourceRegistryException e) { + // logger.trace("Error while getting {} schema for {}", polymorphic ? + // AccessPath.POLYMORPHIC_PARAM + " " : "", + // type, e); + throw e; + } catch (Exception e) { + // logger.trace("Error while getting {}schema for {}", polymorphic ? + // AccessPath.POLYMORPHIC_PARAM + " " : "", + // type, e); + throw new RuntimeException(e); + } + } + } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java index aa59597..f082096 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java @@ -8,7 +8,10 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.informationsystem.impl.embedded.HeaderImpl; +import org.gcube.informationsystem.impl.entity.resource.HostingNodeImpl; import org.gcube.informationsystem.impl.utils.ISMapper; +import org.gcube.informationsystem.model.embedded.Header; import org.gcube.informationsystem.model.entity.facet.ContactFacet; import org.gcube.informationsystem.model.entity.facet.SoftwareFacet; import org.gcube.informationsystem.model.entity.resource.EService; @@ -67,25 +70,79 @@ public class ResourceRegistryClientTest extends ScopedTest { } @Test - public void testGetResource() throws ResourceRegistryException { + public void testExists() throws ResourceRegistryException { + UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); + resourceRegistryClient.exists(EService.NAME, uuid); + } + + @Test + public void testExistsByClass() throws ResourceRegistryException { + UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); + resourceRegistryClient.exists(EService.class, uuid); + } + + @Test + public void testGetInstance() throws ResourceRegistryException { + UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); + String eService = resourceRegistryClient.getInstance(EService.NAME, uuid); + logger.trace("{}", eService); + } + + @Test + public void testGetInstanceByClass() throws ResourceRegistryException { UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); EService eService = resourceRegistryClient.getInstance(EService.class, uuid); logger.trace("{}", eService); } @Test - public void testExists() throws ResourceRegistryException { - UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233"); - resourceRegistryClient.exists(EService.class, uuid); + public void testGetInstances() throws ResourceRegistryException { + String eServices = resourceRegistryClient.getInstances(EService.NAME, true); + logger.trace("{}", eServices); } @Test - public void testGetInstances() throws ResourceRegistryException { - resourceRegistryClient.getInstances(EService.NAME, true); + public void testGetInstancesByClass() throws ResourceRegistryException { + List eServices = resourceRegistryClient.getInstances(EService.class, true); + logger.trace("{}", eServices); } + @Test + public void testGetInstancesFromEntity() throws ResourceRegistryException { + UUID uuid = UUID.fromString("b0d15e45-62af-4221-b785-7d014f10e631"); + String eServices = resourceRegistryClient.getInstancesFromEntity(EService.NAME, true, uuid, Direction.out); + logger.trace("{}", eServices); + } + + @Test + public void testGetInstancesFromEntityByClass() throws ResourceRegistryException { + UUID uuid = UUID.fromString("b0d15e45-62af-4221-b785-7d014f10e631"); + List eServices = resourceRegistryClient.getInstancesFromEntity(EService.class, true, uuid, Direction.out); + logger.trace("{}", eServices); + } + + @Test + public void testGetInstancesFromEntityByClasses() throws ResourceRegistryException { + UUID uuid = UUID.fromString("b0d15e45-62af-4221-b785-7d014f10e631"); + HostingNode hostingNode = new HostingNodeImpl(); + Header header = new HeaderImpl(uuid); + hostingNode.setHeader(header); + List eServices = resourceRegistryClient.getInstancesFromEntity(EService.class, true, hostingNode, Direction.out); + logger.trace("{}", eServices); + } + + @Test public void testGetFilteredResources() throws ResourceRegistryException, JsonProcessingException { + Map map = new HashMap<>(); + map.put("group", "VREManagement"); + map.put("name", "SmartExecutor"); + String json = resourceRegistryClient.getFilteredResources(EService.NAME, IsIdentifiedBy.NAME, SoftwareFacet.NAME, true, map); + logger.trace(json); + } + + @Test + public void testGetFilteredResourcesByClasses() throws ResourceRegistryException, JsonProcessingException { Map map = new HashMap<>(); map.put("group", "VREManagement"); map.put("name", "SmartExecutor"); @@ -94,4 +151,5 @@ public class ResourceRegistryClientTest extends ScopedTest { logger.trace("{}", ISMapper.marshal(eService)); } } + }