IMplementing get all instances of a specified type

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@141511 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-01-11 16:11:28 +00:00
parent f005fbfb1d
commit 7ede9a4688
3 changed files with 102 additions and 2 deletions

View File

@ -217,7 +217,7 @@ public abstract class EntityManagement<E extends Entity> extends
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
JSONArray jsonArray = new JSONArray();
Iterable<Vertex> iterable = orientGraph.query().has("@class", erType).vertices();
Iterable<Vertex> iterable = orientGraph.getVerticesOfClass(erType, polymorphic);
for(Vertex vertex : iterable){
@SuppressWarnings("rawtypes")
EntityManagement entityManagement = getEntityManagement(orientGraph, vertex);

View File

@ -485,7 +485,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
Iterable<Edge> edges = orientGraph.query().has("@class", erType).edges();
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
return serializeEdges(edges);
}

View File

@ -5,8 +5,10 @@ package org.gcube.informationsystem.resourceregistry.er;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -39,6 +41,7 @@ import org.gcube.informationsystem.model.entity.resource.EService;
import org.gcube.informationsystem.model.entity.resource.HostingNode;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.model.relation.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
@ -46,6 +49,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
@ -406,5 +410,101 @@ public class ERManagementTest extends ScopedTest {
resourceManagement.delete();
}
@SuppressWarnings("rawtypes")
@Test
public void testGetAll() throws Exception{
Map<String, List<Resource>> resources = new HashMap<>();
final int MAX = 5;
int typeNumber = 0;
for(int i=0; i<MAX; i++){
Map<String, Resource> map = createHostingNodeAndEService();
if(typeNumber==0){
typeNumber = map.size();
}
for(String key : map.keySet()){
if(!resources.containsKey(key)){
resources.put(key, new ArrayList<Resource>());
}
resources.get(key).add(map.get(key));
}
}
/* Getting all instances of created specific Resources*/
for(String key : resources.keySet()){
ResourceManagement resourceManagement = (ResourceManagement) ERManagement.getERManagement(key);
String json = resourceManagement.all(false);
List<Resource> list = Entities.unmarshalList(Resource.class, json);
logger.debug("{}s are {} : {} ", key, list.size(), list);
Assert.assertTrue(list.size()==MAX);
}
/* Getting all Resources polymorphic and non polymorphic */
ResourceManagement resourceManagement = (ResourceManagement) ERManagement.getERManagement(Resource.NAME);
String json = resourceManagement.all(true);
List<Resource> list = Entities.unmarshalList(Resource.class, json);
logger.debug("{}s are {} : {} ", Resource.NAME, list.size(), list);
Assert.assertTrue(list.size()==(MAX*typeNumber));
json = resourceManagement.all(false);
list = Entities.unmarshalList(Resource.class, json);
Assert.assertTrue(list.size()==0);
/* Getting all IsRelatedTo polymorphic and non polymorphic */
IsRelatedToManagement isRelatedToManagement = (IsRelatedToManagement) ERManagement.getERManagement(IsRelatedTo.NAME);
json = isRelatedToManagement.all(true);
List<IsRelatedTo> isRelatedToList = Entities.unmarshalList(IsRelatedTo.class, json);
logger.debug("{}s are {} : {} ", IsRelatedTo.NAME, isRelatedToList.size(), isRelatedToList);
Assert.assertTrue(isRelatedToList.size()==MAX);
json = isRelatedToManagement.all(false);
isRelatedToList = Entities.unmarshalList(IsRelatedTo.class, json);
Assert.assertTrue(isRelatedToList.size()==0);
/* Getting all ConsistsOf polymorphic and non polymorphic */
ConsistsOfManagement consistsOfManagement = (ConsistsOfManagement) ERManagement.getERManagement(ConsistsOf.NAME);
json = consistsOfManagement.all(true);
List<ConsistsOf> consistsOfPolimorphicList = Entities.unmarshalList(ConsistsOf.class, json);
logger.debug("{}s are {} : {} ", IsRelatedTo.NAME, consistsOfPolimorphicList.size(), consistsOfPolimorphicList);
json = consistsOfManagement.all(false);
List<ConsistsOf> consistsOfNonPolimorphicList = Entities.unmarshalList(ConsistsOf.class, json);
logger.debug("{}s are {} : {} ", IsRelatedTo.NAME, consistsOfNonPolimorphicList.size(), consistsOfNonPolimorphicList);
Assert.assertTrue(consistsOfPolimorphicList.size()>=consistsOfNonPolimorphicList.size());
/* Removing created Entity and Realtion to have a clean DB */
list = resources.get(HostingNode.NAME);
for(Resource r : list){
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(r.getHeader().getUUID());
resourceManagement.delete();
}
}
}