resource-registry-client/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest....

213 lines
8.3 KiB
Java

/**
*
*/
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.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.impl.properties.MetadataImpl;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.UUIDManager;
import org.gcube.informationsystem.utils.UUIDUtility;
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.entities.resources.Service;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ResourceRegistryClientTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ResourceRegistryClientTest.class);
protected ResourceRegistryClient resourceRegistryClient;
public ResourceRegistryClientTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL);
}else {
resourceRegistryClient = ResourceRegistryClientFactory.create();
}
}
@Test
public void testQuery() throws ResourceRegistryException {
String res = resourceRegistryClient.rawQuery("SELECT FROM V");
logger.trace(res);
}
@Test
public void testGetFacetSchema() throws SchemaNotFoundException, ResourceRegistryException {
List<Type> typeDefinitions = resourceRegistryClient.getType(ContactFacet.class, true);
logger.trace("{}", typeDefinitions);
}
@Test
public void testGetResourceSchema() throws SchemaNotFoundException, ResourceRegistryException {
List<Type> typeDefinitions = resourceRegistryClient.getType(HostingNode.class, true);
logger.trace("{}", typeDefinitions);
}
interface Aux extends Service {
}
@Test(expected = SchemaNotFoundException.class)
public void testException() throws SchemaNotFoundException, ResourceRegistryException {
resourceRegistryClient.getType(Aux.class, true);
}
/* The following tests are commented because we need to create the instances for tests. this is done in
* resource registry publisher, which uses client APis to test the publishing as weel as this client.
*/
// @Test
public void testExists() throws ResourceRegistryException {
UUID uuid = UUID.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233");
resourceRegistryClient.existInstance(EService.NAME, uuid);
}
// @Test
public void testExistsByClass() throws Exception {
UUID uuid = UUIDUtility.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233");
resourceRegistryClient.existInstance(EService.class, uuid);
}
// @Test
public void testGetInstance() throws Exception {
UUID uuid = UUIDUtility.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233");
String eService = resourceRegistryClient.getInstance(EService.NAME, uuid);
logger.trace("{}", eService);
}
// @Test
public void testGetInstanceByClass() throws Exception {
UUID uuid = UUIDUtility.fromString("bdaccb35-7f27-45a6-8ca9-11d467cb9233");
EService eService = resourceRegistryClient.getInstance(EService.class, uuid);
logger.trace("{}", eService);
}
@Test
public void testGetInstances() throws ResourceRegistryException {
String eServices = resourceRegistryClient.getInstances(EService.NAME, true);
logger.trace("{}", eServices);
}
@Test
public void testGetInstancesByClass() throws ResourceRegistryException {
List<EService> eServices = resourceRegistryClient.getInstances(EService.class, true);
logger.trace("{}", eServices);
}
@Test
public void testGetRelatedResourcesByClasses() throws ResourceRegistryException {
List<EService> eServices = resourceRegistryClient.getRelatedResources(EService.class, IsRelatedTo.class,
Resource.class, Direction.OUT, true);
logger.trace("{}", eServices);
}
// @Test
public void testGetRelatedResourcesFromReferenceResourceByClasses() throws Exception {
UUID uuid = UUIDUtility.fromString("b0d15e45-62af-4221-b785-7d014f10e631");
HostingNode hostingNode = new HostingNodeImpl();
hostingNode.setUUID(uuid);
Metadata metadata = new MetadataImpl();
hostingNode.setMetadata(metadata);
List<EService> eServices = resourceRegistryClient.getRelatedResourcesFromReferenceResource(EService.class,
IsRelatedTo.class, hostingNode, Direction.OUT, true);
logger.trace("{}", eServices);
}
@Test
public void testGetFilteredResourcesByClasses() throws ResourceRegistryException, JsonProcessingException {
Map<String,String> map = new HashMap<>();
map.put("group", "VREManagement");
map.put("name", "SmartExecutor");
List<EService> eServices = resourceRegistryClient.getFilteredResources(EService.class, IsIdentifiedBy.class,
SoftwareFacet.class, true, map);
for(EService eService : eServices) {
logger.trace("{}", ElementMapper.marshal(eService));
}
}
// @Test
public void testGetResourcesFromReferenceFacet() throws ResourceRegistryException, JsonProcessingException {
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setUUID(UUIDManager.getInstance().generateValidUUID());
Metadata metadata = new MetadataImpl();
softwareFacet.setMetadata(metadata);
List<EService> eServices = resourceRegistryClient.getResourcesFromReferenceFacet(EService.class, IsIdentifiedBy.class, softwareFacet, true);
for(EService eService : eServices) {
logger.trace("{}", ElementMapper.marshal(eService));
}
}
@Test
public void testGetAllContexts() throws Exception {
List<Context> contexts = resourceRegistryClient.getAllContext();
logger.debug("{}", contexts);
ContextCache contextCache = ContextCache.getInstance();
Map<UUID, String> uuidToContextFullName = contextCache.getUUIDToContextFullNameAssociation();
logger.debug("{}", uuidToContextFullName);
for(Context c : contexts) {
UUID uuid = c.getUUID();
if(c.getParent()!=null) {
IsParentOf isParentOf = c.getParent();
Context parentContext = isParentOf.getSource();
UUID parentUUID = parentContext.getUUID();
Assert.assertEquals(parentContext, contextCache.getContextByUUID(parentUUID));
List<IsParentOf> children = parentContext.getChildren();
boolean found = false;
for(IsParentOf ipo : children) {
if(ipo.equals(isParentOf)) {
found = true;
break;
}
}
Assert.assertTrue(found);
logger.debug("{} : {} (parent {} : {})", c.getUUID(), contextCache.getContextFullNameByUUID(uuid), parentUUID, contextCache.getContextFullNameByUUID(parentUUID));
}else {
logger.debug("{} : {}", c.getUUID(), contextCache.getContextFullNameByUUID(uuid));
}
}
Context currentContext = resourceRegistryClient.getCurrentContext();
logger.debug("Current context : {}", currentContext);
for(Context c : contexts) {
UUID uuid = c.getUUID();
Context context = resourceRegistryClient.getContext(uuid);
String fullName = ContextCache.getInstance().getContextFullNameByUUID(uuid);
logger.debug("{} - {} : {}", uuid, fullName, context);
}
}
}