From 9909a876446fd03c85a2cde716e853a2d27e8266 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 7 Jul 2021 17:35:49 +0200 Subject: [PATCH] Improving client --- .../client/ResourceRegistryClient.java | 8 +- .../client/ResourceRegistryClientImpl.java | 30 +-- .../client/ResourceRegistryClientTest.java | 5 +- ...esourceRegistryClientTestWikiExamples.java | 193 ++++++++++++++++++ 4 files changed, 216 insertions(+), 20 deletions(-) create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java 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 0bab975..d97a20c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -43,7 +43,7 @@ public interface ResourceRegistryClient { throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; public , F extends Facet> List getResourcesFromReferenceFacet( - Class resourceClass, Class consistsOfClass, Class facetClass, F referenceFacet, + Class resourceClass, Class consistsOfClass, F referenceFacet, boolean polymorphic) throws ResourceRegistryException; public , F extends Facet> List getResourcesFromReferenceFacet( @@ -56,14 +56,14 @@ public interface ResourceRegistryClient { public , F extends Facet> List getFilteredResources( Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, - Map map) throws ResourceRegistryException; + Map facetConstraint) throws ResourceRegistryException; public String getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map map) throws ResourceRegistryException; + boolean polymorphic, Map facetConstraint) throws ResourceRegistryException; public , RR extends Resource> List getRelatedResourcesFromReferenceResource( - Class resourceClass, Class isRelatedToClass, Class referenceResourceClass, RR referenceResource, + Class resourceClass, Class isRelatedToClass, RR referenceResource, Direction direction, boolean polymorphic) throws ResourceRegistryException; public , RR extends Resource> List getRelatedResourcesFromReferenceResource( 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 05fc829..9d639ba 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -385,7 +385,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { } protected String getRelated(String entityType, String relationType, String referenceEntityType, - UUID referenceEntity, Direction direction, Boolean polymorphic, Map map) + UUID referenceEntity, Direction direction, Boolean polymorphic, Map facetConstraints) throws ResourceRegistryException { try { @@ -403,10 +403,10 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { parameters.put(AccessPath._POLYMORPHIC_PARAM, polymorphic.toString()); if(referenceEntity == null) { - if(map != null && map.size() > 0) { + if(facetConstraints != null && facetConstraints.size() > 0) { logger.info("Going to get {} linked by a {} Relation to a {} having {}", entityType, relationType, - referenceEntityType, map); - parameters.putAll(map); + referenceEntityType, facetConstraints); + parameters.putAll(facetConstraints); } else { logger.info("Going to get {} linked by a {} Relation to a {}", entityType, relationType, referenceEntityType); @@ -425,7 +425,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { if(referenceEntity == null) { logger.info("{} linked by {} to/from {} having {} are {}", entityType, relationType, - referenceEntityType, map, json); + referenceEntityType, facetConstraints, json); } else { logger.info("{} linked by {} to/from {} with UUID {} are", entityType, relationType, @@ -443,9 +443,11 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public , F extends Facet> List getResourcesFromReferenceFacet( - Class resourceClass, Class consistsOfClass, Class facetClass, F referenceFacet, + Class resourceClass, Class consistsOfClass, F referenceFacet, boolean polymorphic) throws ResourceRegistryException { UUID referenceFacetUUID = referenceFacet.getHeader().getUUID(); + @SuppressWarnings("unchecked") + Class facetClass = (Class) referenceFacet.getClass(); return getResourcesFromReferenceFacet(resourceClass, consistsOfClass, facetClass, referenceFacetUUID, polymorphic); } @@ -476,11 +478,11 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public , F extends Facet> List getFilteredResources( Class resourceClass, Class consistsOfClass, Class facetClass, boolean polymorphic, - Map map) throws ResourceRegistryException { + Map facetConstraints) throws ResourceRegistryException { String resourceType = Utility.getTypeName(resourceClass); String consistsOfType = Utility.getTypeName(consistsOfClass); String facetType = Utility.getTypeName(facetClass); - String ret = getFilteredResources(resourceType, consistsOfType, facetType, polymorphic, map); + String ret = getFilteredResources(resourceType, consistsOfType, facetType, polymorphic, facetConstraints); try { return (List) ElementMapper.unmarshalList(Resource.class, ret); } catch(Exception e) { @@ -490,15 +492,17 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public String getFilteredResources(String resourceType, String consistsOfType, String facetType, - boolean polymorphic, Map map) throws ResourceRegistryException { - return getRelated(resourceType, consistsOfType, facetType, Direction.OUT, polymorphic, map); + boolean polymorphic, Map facetConstraints) throws ResourceRegistryException { + return getRelated(resourceType, consistsOfType, facetType, Direction.OUT, polymorphic, facetConstraints); } @Override public , RR extends Resource> List getRelatedResourcesFromReferenceResource( - Class resourceClass, Class isRelatedToClass, Class referenceResourceClass, RR referenceResource, + Class resourceClass, Class isRelatedToClass, RR referenceResource, Direction direction, boolean polymorphic) throws ResourceRegistryException { UUID referenceResourceUUID = referenceResource.getHeader().getUUID(); + @SuppressWarnings("unchecked") + Class referenceResourceClass = (Class) referenceResource.getClass(); return getRelatedResourcesFromReferenceResource(resourceClass, isRelatedToClass, referenceResourceClass, referenceResourceUUID, direction, polymorphic); } @@ -568,8 +572,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { // @Override protected String getRelated(String entityType, String relationType, String referenceEntityType, Direction direction, - boolean polymorphic, Map map) throws ResourceRegistryException { - return getRelated(entityType, relationType, referenceEntityType, null, direction, polymorphic, map); + boolean polymorphic, Map facetConstraints) throws ResourceRegistryException { + return getRelated(entityType, relationType, referenceEntityType, null, direction, polymorphic, facetConstraints); } // @Override 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 5c659bb..3d0927e 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java @@ -129,7 +129,7 @@ public class ResourceRegistryClientTest extends ContextTest { Header header = new HeaderImpl(uuid); hostingNode.setHeader(header); List eServices = resourceRegistryClient.getRelatedResourcesFromReferenceResource(EService.class, - IsRelatedTo.class, HostingNode.class, hostingNode, Direction.OUT, true); + IsRelatedTo.class, hostingNode, Direction.OUT, true); logger.trace("{}", eServices); } @@ -152,8 +152,7 @@ public class ResourceRegistryClientTest extends ContextTest { UUID uuid = UUID.fromString("cbdf3e61-524c-4800-91a6-3ff3e06fbee3"); Header header = new HeaderImpl(uuid); softwareFacet.setHeader(header); - List eServices = resourceRegistryClient.getResourcesFromReferenceFacet(EService.class, IsIdentifiedBy.class, - SoftwareFacet.class, softwareFacet, true); + List eServices = resourceRegistryClient.getResourcesFromReferenceFacet(EService.class, IsIdentifiedBy.class, softwareFacet, true); for(EService eService : eServices) { logger.trace("{}", ElementMapper.marshal(eService)); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java new file mode 100644 index 0000000..e9e15c7 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTestWikiExamples.java @@ -0,0 +1,193 @@ +/** + * + */ +package org.gcube.informationsystem.resourceregistry.client; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.gcube.informationsystem.model.impl.properties.HeaderImpl; +import org.gcube.informationsystem.model.reference.entities.Resource; +import org.gcube.informationsystem.model.reference.properties.Header; +import org.gcube.informationsystem.model.reference.relations.ConsistsOf; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.utils.ElementMapper; +import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl; +import org.gcube.resourcemanagement.model.reference.entities.facets.ContactFacet; +import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; +import org.gcube.resourcemanagement.model.reference.entities.resources.EService; +import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; +import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; +import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Hosts; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ResourceRegistryClientTestWikiExamples extends ContextTest { + + private static Logger logger = LoggerFactory.getLogger(ResourceRegistryClientTestWikiExamples.class); + + protected ResourceRegistryClient resourceRegistryClient; + + public ResourceRegistryClientTestWikiExamples() { + resourceRegistryClient = ResourceRegistryClientFactory.create(); + } + + @Before + public void before() throws Exception { + setContextByName(PARENT_DEFAULT_TEST_SCOPE); + } + + @Test + public void testExample1() throws ResourceRegistryException { + List list = resourceRegistryClient.getFilteredResources(EService.class, IsIdentifiedBy.class, SoftwareFacet.class, true, null); + logger.debug("{}", list); + } + + @Test + public void testExample1Alt() throws ResourceRegistryException, Exception { + String jsonString = resourceRegistryClient.getFilteredResources("EService", "IsIdentifiedBy", "SoftwareFacet", true, null); + List list = ElementMapper.unmarshalList(EService.class, jsonString); + logger.debug("{}", list); + } + + @Test + public void testExample2() throws ResourceRegistryException { + UUID uuid = UUID.fromString("97984812-90e6-4eb7-b804-50a9ad3fe4fb"); + List list = resourceRegistryClient.getResourcesFromReferenceFacet(EService.class, IsIdentifiedBy.class, SoftwareFacet.class, uuid, true); + logger.debug("{}", list); + } + + @Test + public void testExample2Alt() throws ResourceRegistryException, Exception{ + SoftwareFacet softwareFacetInstance = new SoftwareFacetImpl(); + UUID uuid = UUID.fromString("97984812-90e6-4eb7-b804-50a9ad3fe4fb"); + Header header = new HeaderImpl(uuid); + softwareFacetInstance.setHeader(header); + List list = resourceRegistryClient.getResourcesFromReferenceFacet(EService.class, IsIdentifiedBy.class, softwareFacetInstance, true); + logger.debug("{}", list); + } + + @Test + public void testExample2Alt2() throws ResourceRegistryException, Exception{ + UUID uuid = UUID.fromString("97984812-90e6-4eb7-b804-50a9ad3fe4fb"); + String jsonString = resourceRegistryClient.getResourcesFromReferenceFacet("EService", "IsIdentifiedBy", "SoftwareFacet", uuid, true); + List list = ElementMapper.unmarshalList(EService.class, jsonString); + logger.debug("{}", list); + } + + @Test + public void testExample3() throws ResourceRegistryException, Exception{ + Map facetConstraints = new HashMap<>(); + facetConstraints.put("group", "VREManagement"); + facetConstraints.put("name", "WhnManager"); + + List list = resourceRegistryClient.getFilteredResources(EService.class, IsIdentifiedBy.class, SoftwareFacet.class, true, facetConstraints); + logger.debug("{}", list); + } + + @Test + public void testExample3Alt() throws ResourceRegistryException, Exception{ + Map facetConstraints = new HashMap<>(); + facetConstraints.put("group", "VREManagement"); + facetConstraints.put("name", "WhnManager"); + + String jsonString = resourceRegistryClient.getFilteredResources("EService", "IsIdentifiedBy", "SoftwareFacet", true, facetConstraints); + List list = ElementMapper.unmarshalList(EService.class, jsonString); + logger.debug("{}", list); + } + + @Test + public void testExample4() throws ResourceRegistryException { + List list = resourceRegistryClient.getFilteredResources(Resource.class, IsIdentifiedBy.class, ContactFacet.class, true, null); + logger.debug("{}", list); + } + + @Test + public void testExample4Alt() throws ResourceRegistryException, Exception { + String jsonString = resourceRegistryClient.getFilteredResources("Resource", "IsIdentifiedBy", "ContactFacet", true, null); + List list = ElementMapper.unmarshalList(Resource.class, jsonString); + logger.debug("{}", list); + } + + + @Test + public void testExample5() throws ResourceRegistryException { + List list = resourceRegistryClient.getFilteredResources(Resource.class, ConsistsOf.class, ContactFacet.class, true, null); + logger.debug("{}", list); + } + + @Test + public void testExample5Alt() throws ResourceRegistryException, Exception { + String jsonString = resourceRegistryClient.getFilteredResources("Resource", "ConsistsOf", "ContactFacet", true, null); + List list = ElementMapper.unmarshalList(Resource.class, jsonString); + logger.debug("{}", list); + } + + @Test + public void testExample6() throws ResourceRegistryException, Exception{ + Map facetConstraints = new HashMap<>(); + facetConstraints.put("name", "Luca"); + facetConstraints.put("surname", "Frosini"); + + List list = resourceRegistryClient.getFilteredResources(Resource.class, ConsistsOf.class, ContactFacet.class, true, facetConstraints); + logger.debug("{}", list); + } + + @Test + public void testExample6Alt() throws ResourceRegistryException, Exception{ + Map facetConstraints = new HashMap<>(); + facetConstraints.put("name", "Luca"); + facetConstraints.put("surname", "Frosini"); + + String jsonString = resourceRegistryClient.getFilteredResources("Resource", "ConsistsOf", "ContactFacet", true, facetConstraints); + List list = ElementMapper.unmarshalList(Resource.class, jsonString); + logger.debug("{}", list); + } + + @Test + public void testExample7() throws ResourceRegistryException { + List list = resourceRegistryClient.getRelatedResources(EService.class, Hosts.class, HostingNode.class, Direction.IN, true); + logger.debug("{}", list); + } + + @Test + public void testExample7Alt() throws ResourceRegistryException, Exception { + String jsonString = resourceRegistryClient.getRelatedResources("EService", "Hosts", "HostingNode", Direction.IN, true); + List list = ElementMapper.unmarshalList(EService.class, jsonString); + logger.debug("{}", list); + } + + @Test + public void testExample8() throws ResourceRegistryException { + UUID uuid = UUID.fromString("16032d09-3823-444e-a1ff-a67de4f350a"); + List list = resourceRegistryClient.getRelatedResourcesFromReferenceResource(EService.class, Hosts.class, HostingNode.class, uuid, Direction.IN, true); + logger.debug("{}", list); + } + + @Test + public void testExample8Alt() throws ResourceRegistryException, Exception{ + HostingNode hostingNodeInstance = new HostingNodeImpl(); + UUID uuid = UUID.fromString("16032d09-3823-444e-a1ff-a67de4f350a"); + Header header = new HeaderImpl(uuid); + hostingNodeInstance.setHeader(header); + List list = resourceRegistryClient.getRelatedResourcesFromReferenceResource(EService.class, Hosts.class, hostingNodeInstance, Direction.IN, true); + logger.debug("{}", list); + } + + @Test + public void testExample8Alt2() throws ResourceRegistryException, Exception{ + UUID uuid = UUID.fromString("16032d09-3823-444e-a1ff-a67de4f350a"); + String jsonString = resourceRegistryClient.getRelatedResourcesFromReferenceResource("EService", "Hosts", "HostingNode", uuid, Direction.IN, true); + List list = ElementMapper.unmarshalList(EService.class, jsonString); + logger.debug("{}", list); + } + +}