implementing Publisher

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@131451 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-09-16 10:36:06 +00:00
parent a677cb07a0
commit 806d689504
3 changed files with 241 additions and 41 deletions

View File

@ -27,11 +27,11 @@ public interface ResourceRegistryPublisher {
public <C extends ConsistsOf<Resource, Facet>> boolean deleteConsistsOf(C consistsOf); public <C extends ConsistsOf<Resource, Facet>> boolean deleteConsistsOf(C consistsOf);
public <I extends IsRelatedTo<Resource, Resource>> I create(Class<I> isRelatedToClass, I isRelatedTo); public <I extends IsRelatedTo<Resource, Resource>> I createIsRelatedTo(Class<I> isRelatedToClass, I isRelatedTo);
public <I extends IsRelatedTo<Resource, Resource>> I update(I isRelatedTo); public <I extends IsRelatedTo<Resource, Resource>> I updateIsRelatedTo(I isRelatedTo);
public <I extends IsRelatedTo<Resource, Resource>> boolean delete(I isRelatedTo); public <I extends IsRelatedTo<Resource, Resource>> boolean deleteIsRelatedTo(I isRelatedTo);
} }

View File

@ -10,7 +10,8 @@ import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.HashMap; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import javax.xml.ws.EndpointReference; import javax.xml.ws.EndpointReference;
@ -34,12 +35,40 @@ import org.slf4j.LoggerFactory;
public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher { public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(ResourceRegistryPublisher.class); .getLogger(ResourceRegistryPublisherImpl.class);
private final AsyncProxyDelegate<EndpointReference> delegate; private final AsyncProxyDelegate<EndpointReference> delegate;
public static final String PATH_SEPARATOR = "/"; public static final String PATH_SEPARATOR = "/";
public final class RREntry<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public RREntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
public ResourceRegistryPublisherImpl(ProxyDelegate<EndpointReference> config) { public ResourceRegistryPublisherImpl(ProxyDelegate<EndpointReference> config) {
this.delegate = new AsyncProxyDelegate<EndpointReference>(config); this.delegate = new AsyncProxyDelegate<EndpointReference>(config);
} }
@ -65,14 +94,14 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
protected final HTTPMETHOD method; protected final HTTPMETHOD method;
protected final String urlParameters; protected final String urlParameters;
protected String getParametersDataString(Map<String, String> parameters) throws UnsupportedEncodingException { protected String getParametersDataString(List<Map.Entry<String, String>> parameters) throws UnsupportedEncodingException {
if(parameters==null){ if(parameters==null){
return null; return null;
} }
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
boolean first = true; boolean first = true;
for(Map.Entry<String, String> entry : parameters.entrySet()){ for(Map.Entry<String, String> entry : parameters){
if (first) { if (first) {
first = false; first = false;
} else { } else {
@ -80,8 +109,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
} }
result.append(URLEncoder.encode(entry.getKey(), UTF8)); result.append(URLEncoder.encode(entry.getKey(), UTF8));
result.append(PARAM_EQUALS); result.append(PARAM_EQUALS);
result.append(URLEncoder.encode(entry.getValue(), UTF8)); result.append(URLEncoder.encode(entry.getValue(), UTF8));
} }
return result.toString(); return result.toString();
@ -94,7 +124,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException
*/ */
public HTTPInputs(String path, HTTPMETHOD method, public HTTPInputs(String path, HTTPMETHOD method,
Map<String, String> parameters) throws UnsupportedEncodingException { List<Map.Entry<String, String>> parameters) throws UnsupportedEncodingException {
super(); super();
this.path = path; this.path = path;
this.method = method; this.method = method;
@ -206,7 +236,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
} }
} }
return Entities.unmarshal(clazz, result.toString()); String res = result.toString();
logger.trace("Server returned content : {}", res);
return Entities.unmarshal(clazz, res);
} }
} }
@ -223,8 +256,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(facetClass.getSimpleName()); stringWriter.append(facetClass.getSimpleName());
Map<String, String> parameters = new HashMap<>(); List<Map.Entry<String, String>> parameters = new ArrayList<>();
parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(facet)); parameters.add(new RREntry<String, String>(EntityPath.DEFINITION_PARAM, Entities.marshal(facet)));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
@ -248,8 +281,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(facet.getHeader().getUUID().toString()); stringWriter.append(facet.getHeader().getUUID().toString());
Map<String, String> parameters = new HashMap<>(); List<Map.Entry<String, String>> parameters = new ArrayList<>();
parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(facet)); parameters.add(new RREntry<String, String>(EntityPath.DEFINITION_PARAM, Entities.marshal(facet)));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, parameters); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.POST, parameters);
@ -296,8 +329,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(resourceClass.getSimpleName()); stringWriter.append(resourceClass.getSimpleName());
Map<String, String> parameters = new HashMap<>(); List<Map.Entry<String, String>> parameters = new ArrayList<>();
parameters.put(EntityPath.DEFINITION_PARAM, Entities.marshal(resource)); parameters.add(new RREntry<String, String>(EntityPath.DEFINITION_PARAM, Entities.marshal(resource)));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
@ -351,9 +384,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(consistsOf.getTarget().getHeader().getUUID().toString()); stringWriter.append(consistsOf.getTarget().getHeader().getUUID().toString());
Map<String, String> parameters = new HashMap<>(); List<Map.Entry<String, String>> parameters = new ArrayList<>();
parameters.put(EntityPath.TYPE_PARAM, consistsOfClass.getSimpleName()); parameters.add(new RREntry<String, String>(EntityPath.TYPE_PARAM, consistsOfClass.getSimpleName()));
parameters.put(EntityPath.PROPERTIES_PARAM, Entities.marshal(consistsOf)); parameters.add(new RREntry<String, String>(EntityPath.PROPERTIES_PARAM, Entities.marshal(consistsOf)));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
@ -397,7 +430,7 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
} }
@Override @Override
public <I extends IsRelatedTo<Resource, Resource>> I create( public <I extends IsRelatedTo<Resource, Resource>> I createIsRelatedTo(
Class<I> isRelatedToClass, I isRelatedTo) { Class<I> isRelatedToClass, I isRelatedTo) {
try { try {
@ -415,9 +448,9 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);
stringWriter.append(isRelatedTo.getTarget().getHeader().getUUID().toString()); stringWriter.append(isRelatedTo.getTarget().getHeader().getUUID().toString());
Map<String, String> parameters = new HashMap<>(); List<Map.Entry<String, String>> parameters = new ArrayList<>();
parameters.put(EntityPath.TYPE_PARAM, isRelatedToClass.getSimpleName()); parameters.add(new RREntry<String, String>(EntityPath.TYPE_PARAM, isRelatedToClass.getSimpleName()));
parameters.put(EntityPath.PROPERTIES_PARAM, Entities.marshal(isRelatedTo)); parameters.add(new RREntry<String, String>(EntityPath.PROPERTIES_PARAM, Entities.marshal(isRelatedTo)));
HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters); HTTPInputs httpInputs = new HTTPInputs(stringWriter.toString(), HTTPMETHOD.PUT, parameters);
@ -432,13 +465,13 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
} }
@Override @Override
public <I extends IsRelatedTo<Resource, Resource>> I update(I isRelatedTo) { public <I extends IsRelatedTo<Resource, Resource>> I updateIsRelatedTo(I isRelatedTo) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
@Override @Override
public <I extends IsRelatedTo<Resource, Resource>> boolean delete(I isRelatedTo) { public <I extends IsRelatedTo<Resource, Resource>> boolean deleteIsRelatedTo(I isRelatedTo) {
try { try {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR); stringWriter.append(PATH_SEPARATOR);

View File

@ -3,12 +3,30 @@
*/ */
package org.gcube.informationsystem.resourceregistry.publisher; package org.gcube.informationsystem.resourceregistry.publisher;
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.impl.entity.facet.ContactFacetImpl; import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.NetworkingFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl;
import org.gcube.informationsystem.impl.entity.resource.EServiceImpl;
import org.gcube.informationsystem.impl.entity.resource.HostingNodeImpl;
import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl;
import org.gcube.informationsystem.impl.relation.isrelatedto.HostsImpl;
import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.facet.ContactFacet; import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.entity.facet.CPUFacet;
import org.gcube.informationsystem.model.entity.facet.NetworkingFacet;
import org.gcube.informationsystem.model.entity.facet.SoftwareFacet;
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.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisher; import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisherFactory; import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisherFactory;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -32,22 +50,171 @@ public class ResourceRegistryPublisherTest {
} }
@Test @Test
public void testCreateUpdateDeleteFacet(){ public void testCreateUpdateDeleteFacet() throws Exception {
ContactFacet contactFacet = new ContactFacetImpl(); CPUFacet cpuFacet = new CPUFacetImpl();
contactFacet.setName("Luca"); cpuFacet.setClockSpeed("1 GHz");
contactFacet.setSurname("Frosini"); cpuFacet.setModel("Opteron");
contactFacet.setEMail("info@lucafrosini.com"); cpuFacet.setVendor("AMD");
logger.debug("Going to created {}", contactFacet);
ContactFacet created = resourceRegistryPublisher.createFacet(ContactFacet.class, contactFacet); logger.debug("Going to create: {}", cpuFacet);
logger.trace("Created {}", created); CPUFacet createdCpuFacet = resourceRegistryPublisher.createFacet(CPUFacet.class, cpuFacet);
logger.debug("Created: {}", createdCpuFacet);
created.setTitle("Dott. Ing."); Assert.assertTrue(cpuFacet.getClockSpeed().compareTo(createdCpuFacet.getClockSpeed())==0);
ContactFacet updated = resourceRegistryPublisher.updateFacet(ContactFacet.class, created); Assert.assertTrue(cpuFacet.getModel().compareTo(createdCpuFacet.getModel())==0);
logger.trace("Updated {}", updated); Assert.assertTrue(cpuFacet.getVendor().compareTo(createdCpuFacet.getVendor())==0);
UUID uuid = createdCpuFacet.getHeader().getUUID();
String newVendor = "Intel";
String newClockSpeed = "2 GHz";
createdCpuFacet.setVendor(newVendor);
createdCpuFacet.setClockSpeed(newClockSpeed);
String additionPropertyKey = "My";
String additionPropertyValue = "Test";
createdCpuFacet.setAdditionalProperty(additionPropertyKey, additionPropertyValue);
logger.debug("Going to update: {}", cpuFacet);
CPUFacet updatedCpuFacet = resourceRegistryPublisher.updateFacet(CPUFacet.class, createdCpuFacet);
logger.debug("Updated: {}", updatedCpuFacet);
Assert.assertTrue(createdCpuFacet.getClockSpeed().compareTo(updatedCpuFacet.getClockSpeed())==0);
Assert.assertTrue(createdCpuFacet.getModel().compareTo(updatedCpuFacet.getModel())==0);
Assert.assertTrue(createdCpuFacet.getVendor().compareTo(updatedCpuFacet.getVendor())==0);
Assert.assertTrue(((String) updatedCpuFacet.getAdditionalProperty(additionPropertyKey)).compareTo((String) createdCpuFacet.getAdditionalProperty(additionPropertyKey))==0);
Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getHeader().getUUID())==0);
boolean deleted = resourceRegistryPublisher.deleteFacet(updatedCpuFacet);
Assert.assertTrue(deleted);
}
@Test
public void testCreateDeleteResources() throws Exception {
EService eService = new EServiceImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("InformationSystem");
softwareFacet.setName("resource-registry");
softwareFacet.setVersion("1.1.0");
IsIdentifiedBy<Resource, Facet> isIdentifiedBy = new IsIdentifiedByImpl<Resource, Facet>(eService, softwareFacet, null);
eService.addFacet(isIdentifiedBy);
logger.debug("Going to create : {}", eService);
EService createdEService = resourceRegistryPublisher.createResource(EService.class, eService);
logger.debug("Created : {}", createdEService);
NetworkingFacet networkingFacet = new NetworkingFacetImpl();
networkingFacet.setIPAddress("146.48.87.183");
networkingFacet.setHostName("pc-frosini.isti.cnr.it");
networkingFacet.setDomainName("isti.cnr.it");
networkingFacet.setMask("255.255.248.0");
networkingFacet.setBroadcastAddress("146.48.87.255");
logger.debug("Going to create : {}", networkingFacet);
NetworkingFacet createdNetworkingFacet = resourceRegistryPublisher.createFacet(NetworkingFacet.class, networkingFacet);
logger.debug("Created : {}", createdNetworkingFacet);
HostingNode hostingNode = new HostingNodeImpl();
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
hostingNode.addFacet(cpuFacet);
isIdentifiedBy = new IsIdentifiedByImpl<Resource, Facet>(hostingNode, networkingFacet, null);
hostingNode.attachFacet(isIdentifiedBy);
Hosts<HostingNode, EService> hosts = new HostsImpl<HostingNode, EService>(hostingNode, eService, null);
hostingNode.attachResource(hosts);
logger.debug("Going to create : {}", hostingNode);
HostingNode createdHostingNode = resourceRegistryPublisher.createResource(HostingNode.class, hostingNode);
logger.debug("Created : {}", createdHostingNode);
logger.debug("Going to delete : {}", hostingNode);
boolean deleted = resourceRegistryPublisher.deleteResource(hostingNode);
Assert.assertTrue(deleted);
boolean deleted = resourceRegistryPublisher.deleteFacet(updated);
logger.trace("{} {} deleted : {}", Facet.NAME, updated, deleted);
} }
@Test
public void testCreateAndDeleteEntitiesAndRelations() throws Exception {
EService eService = new EServiceImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("InformationSystem");
softwareFacet.setName("resource-registry");
softwareFacet.setVersion("1.1.0");
IsIdentifiedBy<Resource, Facet> isIdentifiedBy = new IsIdentifiedByImpl<Resource, Facet>(eService, softwareFacet, null);
eService.addFacet(isIdentifiedBy);
logger.debug("Going to create : {}", eService);
EService createdEService = resourceRegistryPublisher.createResource(EService.class, eService);
logger.debug("Created : {}", createdEService);
HostingNode hostingNode = new HostingNodeImpl();
logger.debug("Going to create : {}", hostingNode);
HostingNode createdHostingNode = resourceRegistryPublisher.createResource(HostingNode.class, hostingNode);
logger.debug("Created : {}", createdHostingNode);
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
logger.debug("Going to create: {}", cpuFacet);
CPUFacet createdCpuFacet = resourceRegistryPublisher.createFacet(CPUFacet.class, cpuFacet);
logger.debug("Created: {}", createdCpuFacet);
IsIdentifiedBy<HostingNode, CPUFacet> isIdentifiedByCPUFacet = new IsIdentifiedByImpl<>(createdHostingNode, createdCpuFacet, null);
logger.debug("Going to create : {}", isIdentifiedByCPUFacet);
@SuppressWarnings("unchecked")
IsIdentifiedBy<HostingNode, CPUFacet> createdIsIdentifiedByCPUFacet = resourceRegistryPublisher.createConsistsOf(IsIdentifiedBy.class, isIdentifiedByCPUFacet);
logger.debug("Created : {}", createdIsIdentifiedByCPUFacet);
Hosts<HostingNode, EService> hosts = new HostsImpl<>(createdHostingNode, createdEService, null);
logger.debug("Going to create : {}", hosts);
@SuppressWarnings("unchecked")
Hosts<HostingNode, EService> createdHosts = resourceRegistryPublisher.createIsRelatedTo(Hosts.class, hosts);
logger.debug("Created : {}", createdHosts);
logger.debug("Going to delete : {}", createdIsIdentifiedByCPUFacet);
@SuppressWarnings("unchecked")
boolean deleted = resourceRegistryPublisher.deleteConsistsOf((ConsistsOf) createdIsIdentifiedByCPUFacet);
Assert.assertTrue(deleted);
logger.debug("Going to delete : {}", createdCpuFacet);
deleted = resourceRegistryPublisher.deleteFacet(createdCpuFacet);
Assert.assertTrue(deleted);
logger.debug("Going to delete : {}", createdHosts);
deleted = resourceRegistryPublisher.deleteIsRelatedTo((IsRelatedTo) createdHosts);
Assert.assertTrue(deleted);
logger.debug("Going to delete : {}", createdHostingNode);
deleted = resourceRegistryPublisher.deleteResource(createdHostingNode);
Assert.assertTrue(deleted);
}
} }