refs #5748: Add "Add To Context API" in Resource Registry Publisher

https://support.d4science.org/issues/5748

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-publisher@134121 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-14 15:33:35 +00:00
parent 72f5aa25e7
commit 9385457e9e
3 changed files with 204 additions and 12 deletions

View File

@ -195,12 +195,10 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method)
throws Exception {
/*
* if(method!=HTTPMETHOD.POST &&
* httpInputs.getUrlParameters()!=null){
*/
url = new URL(url + "?" + httpInputs.getUrlParameters());
// }
if(httpInputs.getUrlParameters()!=null){
url = new URL(url + "?" + httpInputs.getUrlParameters());
}
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
@ -576,6 +574,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ADD_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.RESOURCE_PATH_PART);
@ -615,6 +615,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
StringWriter stringWriter = new StringWriter();
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ENTITY_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.ADD_PATH_PART);
stringWriter.append(PATH_SEPARATOR);
stringWriter.append(EntityPath.FACET_PATH_PART);

View File

@ -0,0 +1,180 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.publisher;
import java.net.URI;
import java.net.URL;
import java.util.Calendar;
import java.util.UUID;
import org.gcube.informationsystem.impl.entity.facet.AccessPointFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.EventFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.LicenseFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.MemoryFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.NetworkingFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.ServiceStateFacetImpl;
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.consistsof.HasPersistentMemoryImpl;
import org.gcube.informationsystem.impl.relation.consistsof.HasVolatileMemoryImpl;
import org.gcube.informationsystem.impl.relation.isrelatedto.HostsImpl;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.facet.AccessPointFacet;
import org.gcube.informationsystem.model.entity.facet.CPUFacet;
import org.gcube.informationsystem.model.entity.facet.EventFacet;
import org.gcube.informationsystem.model.entity.facet.LicenseFacet;
import org.gcube.informationsystem.model.entity.facet.MemoryFacet;
import org.gcube.informationsystem.model.entity.facet.MemoryFacet.MemoryUnit;
import org.gcube.informationsystem.model.entity.facet.NetworkingFacet;
import org.gcube.informationsystem.model.entity.facet.ServiceStateFacet;
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.IsIdentifiedBy;
import org.gcube.informationsystem.model.relation.consistsof.HasPersistentMemory;
import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory;
import org.gcube.informationsystem.model.relation.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.proxy.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.proxy.ResourceRegistryPublisherFactory;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class MultiContextTest {
private static Logger logger = LoggerFactory
.getLogger(MultiContextTest.class);
protected ResourceRegistryPublisher resourceRegistryPublisher;
protected ResourceRegistryClient resourceRegistryClient;
@BeforeClass
public static void beforeClass() throws Exception {
ScopedTest.beforeClass();
}
public MultiContextTest(){
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
resourceRegistryClient = ResourceRegistryClientFactory.create();
}
@Test
public void testCreateEServiceHostingNode() throws Exception {
EService eService = new EServiceImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("InformationSystem");
softwareFacet.setName("resource-registry");
softwareFacet.setVersion("1.1.0");
IsIdentifiedBy<EService, Facet> isIdentifiedBy = new IsIdentifiedByImpl<EService, Facet>(eService, softwareFacet, null);
eService.addFacet(isIdentifiedBy);
AccessPointFacet accessPointFacet = new AccessPointFacetImpl();
accessPointFacet.setEndpoint(new URI("http://localhost"));
accessPointFacet.setEntryName("port1");
eService.addFacet(accessPointFacet);
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(Calendar.getInstance().getTime());
eventFacet.setValue("Created");
eService.addFacet(eventFacet);
ServiceStateFacet serviceStateFacet = new ServiceStateFacetImpl();
serviceStateFacet.setValue("ready");
eService.addFacet(serviceStateFacet);
LicenseFacet licenseFacet = new LicenseFacetImpl();
licenseFacet.setName("EUPL");
licenseFacet.setTextURL(new URL("https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11"));
eService.addFacet(licenseFacet);
EService createEService = resourceRegistryPublisher.createResource(EService.class, eService);
logger.debug("Created : {}", createEService);
/* ----- */
HostingNode hostingNode = new HostingNodeImpl();
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");
IsIdentifiedBy<HostingNode, NetworkingFacet> isIdentifiedByHNNF = new IsIdentifiedByImpl<HostingNode, NetworkingFacet>(hostingNode, networkingFacet, null);
hostingNode.addFacet(isIdentifiedByHNNF);
CPUFacet cpuFacet = new CPUFacetImpl();
cpuFacet.setClockSpeed("1 GHz");
cpuFacet.setModel("Opteron");
cpuFacet.setVendor("AMD");
hostingNode.addFacet(cpuFacet);
MemoryFacet ram = new MemoryFacetImpl();
ram.setSize(8);
ram.setUnit(MemoryUnit.GB);
ram.setUsed(2);
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileMemory =
new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(hostingNode, ram, null);
hostingNode.addFacet(hasVolatileMemory);
MemoryFacet disk = new MemoryFacetImpl();
disk.setSize(256);
disk.setUnit(MemoryUnit.GB);
disk.setUsed(120);
HasPersistentMemory<HostingNode, MemoryFacet> hasPersistentMemory =
new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(hostingNode, disk, null);
hostingNode.addFacet(hasPersistentMemory);
Hosts<HostingNode, EService> hosts = new HostsImpl<HostingNode, EService>(hostingNode, createEService, null);
hostingNode.attachResource(hosts);
HostingNode createdHN = resourceRegistryPublisher.createResource(HostingNode.class, hostingNode);
logger.debug("Created : {}", createdHN);
logger.debug("Changing token to test add to scope");
ScopedTest.setContext(ScopedTest.SECOND_TOKEN);
UUID uuid = createdHN.getHeader().getUUID();
boolean addedToContext = resourceRegistryPublisher.addResourceToContext(uuid);
Assert.assertTrue(addedToContext);
UUID eServiceUUID = createEService.getHeader().getUUID();
try {
resourceRegistryClient.getResource(eServiceUUID);
}catch(ResourceRegistryException e){
logger.debug("Resource with {} Not Found as Expected", uuid.toString());
}
/*
boolean deleted = resourceRegistryPublisher.deleteResource(createdHN);
Assert.assertTrue(deleted);
deleted = resourceRegistryPublisher.deleteResource(createEService);
Assert.assertTrue(deleted);
*/
}
}

View File

@ -28,7 +28,11 @@ public class ScopedTest {
protected static final String PROPERTIES_FILENAME = "config.properties";
private static final String TOKEN_VARNAME = "TOKEN";
private static final String TOKEN;
public static final String TOKEN;
private static final String SECOND_TOKEN_VARNAME = "SECOND_TOKEN";
public static final String SECOND_TOKEN;
static {
Properties properties = new Properties();
@ -42,19 +46,25 @@ public class ScopedTest {
}
TOKEN = properties.getProperty(TOKEN_VARNAME);
SECOND_TOKEN = properties.getProperty(SECOND_TOKEN_VARNAME);
}
private static String getCurrentScope() throws ObjectNotFound, Exception{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(TOKEN);
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", TOKEN, context);
logger.info("Context of token {} is {}", token, context);
return context;
}
public static void setContext(String token) throws ObjectNotFound, Exception{
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(getCurrentScope(token));
}
@BeforeClass
public static void beforeClass() throws Exception{
SecurityTokenProvider.instance.set(TOKEN);
ScopeProvider.instance.set(getCurrentScope());
setContext(TOKEN);
}
@AfterClass