From f7b3f9f230d9b80da420c41cc20a27e88784fa04 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 9 May 2017 13:41:59 +0000 Subject: [PATCH] Renamed Project by adding -se-plugin suffix as convention git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/is-exporter-se-plugin@148403 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 36 ++ .project | 23 ++ .settings/org.eclipse.core.resources.prefs | 6 + .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 208 ++++++++++ .../exporter/ISExporterPlugin.java | 55 +++ .../exporter/ISExporterPluginDeclaration.java | 69 ++++ .../exporter/mapper/GCoreResourceMapper.java | 367 ++++++++++++++++++ .../mapper/GenericResourceExporter.java | 134 +++++++ .../mapper/ServiceEndpointExporter.java | 303 +++++++++++++++ .../mapper/exception/CreateException.java | 23 ++ .../mapper/exception/UpdateException.java | 24 ++ .../exporter/ISExporterPluginTest.java | 53 +++ .../exporter/ScopedTest.java | 120 ++++++ .../mapper/GCoreResourceMapperTest.java | 59 +++ .../mapper/GenericResourceExporterTest.java | 111 ++++++ .../mapper/ServiceEndpointExporterTest.java | 86 ++++ .../ResourceRegistryClientFactorySetter.java | 9 + ...esourceRegistryPublisherFactorySetter.java | 9 + src/test/resources/logback-test.xml | 42 ++ src/test/resources/registry.properties.empty | 0 src/test/resources/registry.properties.test | 1 + 23 files changed, 1747 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/informationsystem/exporter/ISExporterPlugin.java create mode 100644 src/main/java/org/gcube/informationsystem/exporter/ISExporterPluginDeclaration.java create mode 100644 src/main/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapper.java create mode 100644 src/main/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporter.java create mode 100644 src/main/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporter.java create mode 100644 src/main/java/org/gcube/informationsystem/exporter/mapper/exception/CreateException.java create mode 100644 src/main/java/org/gcube/informationsystem/exporter/mapper/exception/UpdateException.java create mode 100644 src/test/java/org/gcube/informationsystem/exporter/ISExporterPluginTest.java create mode 100644 src/test/java/org/gcube/informationsystem/exporter/ScopedTest.java create mode 100644 src/test/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapperTest.java create mode 100644 src/test/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporterTest.java create mode 100644 src/test/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporterTest.java create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactorySetter.java create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherFactorySetter.java create mode 100644 src/test/resources/logback-test.xml create mode 100644 src/test/resources/registry.properties.empty create mode 100644 src/test/resources/registry.properties.test diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e43402f --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..89157e4 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + is-exporter-se-plugin + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..39a5e34 --- /dev/null +++ b/pom.xml @@ -0,0 +1,208 @@ + + 4.0.0 + + org.gcube.tools + maven-parent + 1.0.0 + + org.gcube.information-system + is-exporter-se-plugin + 1.0.0-SNAPSHOT + IS Exporter + Exports GenericReosurce and ServiceEndpoint from gCoreIS to Resource Registry + + UTF-8 + ${project.basedir}/distro + InformationSystem + + + + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + + + + + + org.gcube.distribution + gcube-bom + LATEST + pom + import + + + org.gcube.distribution + gcube-smartgears-bom + LATEST + pom + import + + + org.gcube.information-system + information-system-bom + LATEST + pom + import + + + + + + + org.gcube.vremanagement + smart-executor-api + [1.5.0-SNAPSHOT, 2.0.0-SNAPSHOT) + provided + + + + org.gcube.core + common-scope + provided + + + org.gcube.resources + common-gcore-resources + provided + + + + org.gcube.resources.discovery + ic-client + provided + + + + org.gcube.common + authorization-client + + + org.gcube.information-system + information-system-model + + + org.gcube.information-system + gcube-resources + + + + org.gcube.information-system + resource-registry-client + + + + org.gcube.information-system + resource-registry-publisher + + + + org.gcube.common + home-library-jcr + [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) + + + org.slf4j + slf4j-log4j12 + + + + + + org.gcube.common + home-library + [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) + + + + + org.json + json + 20160810 + + + + + org.slf4j + slf4j-api + provided + + + + + junit + junit + 4.11 + test + + + ch.qos.logback + logback-classic + 1.0.13 + test + + + + + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + ${distroDirectory}/descriptor.xml + + + jar-with-dependencies + + + + + servicearchive + install + + single + + + + package + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/exporter/ISExporterPlugin.java b/src/main/java/org/gcube/informationsystem/exporter/ISExporterPlugin.java new file mode 100644 index 0000000..9083185 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/ISExporterPlugin.java @@ -0,0 +1,55 @@ +package org.gcube.informationsystem.exporter; + +import java.util.Map; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.informationsystem.exporter.mapper.GenericResourceExporter; +import org.gcube.informationsystem.exporter.mapper.ServiceEndpointExporter; +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ISExporterPlugin extends Plugin { + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(ISExporterPlugin.class); + + public ISExporterPlugin(ISExporterPluginDeclaration pluginDeclaration) { + super(pluginDeclaration); + logger.debug("contructor"); + } + + /**{@inheritDoc}*/ + @Override + public void launch(Map inputs) throws Exception { + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + String scope = authorizationEntry.getContext(); + logger.debug("Launching {} execution on scope {}", + ISExporterPluginDeclaration.NAME, scope); + + GenericResourceExporter genericResourceExporter = new GenericResourceExporter(); + genericResourceExporter.export(); + + ServiceEndpointExporter serviceEndpointExporter = new ServiceEndpointExporter(); + serviceEndpointExporter.export(); + + logger.debug("{} execution finished", ISExporterPluginDeclaration.NAME); + } + + /**{@inheritDoc}*/ + @Override + protected void onStop() throws Exception { + logger.debug("onStop()"); + + Thread.currentThread().interrupt(); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/exporter/ISExporterPluginDeclaration.java b/src/main/java/org/gcube/informationsystem/exporter/ISExporterPluginDeclaration.java new file mode 100644 index 0000000..9b047dc --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/ISExporterPluginDeclaration.java @@ -0,0 +1,69 @@ +/** + * + */ +package org.gcube.informationsystem.exporter; + +import java.util.HashMap; +import java.util.Map; + +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ISExporterPluginDeclaration implements PluginDeclaration { + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(ISExporterPlugin.class); + + /** + * Plugin name used by the Executor to retrieve this class + */ + public static final String NAME = "ISExporter"; + public static final String DESCRIPTION = "IS Exporter"; + public static final String VERSION = "1.0.0"; + + /**{@inheritDoc}*/ + @Override + public void init() { + logger.debug(String.format("%s initialized", ISExporterPlugin.class.getSimpleName())); + } + + /**{@inheritDoc}*/ + @Override + public String getName() { + return NAME; + } + + /**{@inheritDoc}*/ + @Override + public String getDescription() { + return DESCRIPTION; + } + + /**{@inheritDoc}*/ + @Override + public String getVersion() { + return VERSION; + } + + /**{@inheritDoc}*/ + @Override + public Map getSupportedCapabilities() { + Map discoveredCapabilities = new HashMap(); + // No capabilities to discover + return discoveredCapabilities; + } + + /**{@inheritDoc}*/ + @Override + public Class> getPluginImplementation() { + return ISExporterPlugin.class; + } + +} diff --git a/src/main/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapper.java b/src/main/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapper.java new file mode 100644 index 0000000..3372177 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapper.java @@ -0,0 +1,367 @@ +package org.gcube.informationsystem.exporter.mapper; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.homelibrary.home.Home; +import org.gcube.common.homelibrary.home.HomeLibrary; +import org.gcube.common.homelibrary.home.HomeManager; +import org.gcube.common.homelibrary.home.HomeManagerFactory; +import org.gcube.common.homelibrary.home.workspace.Workspace; +import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; +import org.gcube.common.homelibrary.home.workspace.folder.FolderItem; +import org.gcube.common.homelibrary.util.WorkspaceUtil; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.informationsystem.exporter.ISExporterPluginDeclaration; +import org.gcube.informationsystem.exporter.mapper.exception.CreateException; +import org.gcube.informationsystem.exporter.mapper.exception.UpdateException; +import org.gcube.informationsystem.impl.utils.ISMapper; +import org.gcube.informationsystem.model.ISConstants; +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.Resource; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public abstract class GCoreResourceMapper { + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(GCoreResourceMapper.class); + + public static final String EXPORTED = "EXPORTED"; + public static final String EXPORTED_FROM_OLD_GCORE_IS = "EXPORTED_FROM_OLD_GCORE_IS"; + + public static final String MAPPING_ERROR = "mapping"; + public static final String PUBLISHING_ERROR = "publishing"; + public static final String TYPE = "type"; + public static final String ID = "id"; + public static final String ERROR = "error"; + public static final String DATE = "date"; + + public static final String EXCEPTION_TYPE = "exceptionType"; + public static final String CREATE = "create"; + public static final String UPDATE = "update"; + + protected Class grClass; + protected Class rClass; + + public static final String UTF8 = "UTF-8"; + + protected ResourceRegistryPublisher resourceRegistryPublisher; + protected ResourceRegistryClient resourceRegistryClient; + + public static String getCurrentContextName(){ + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry = null; + try { + authorizationEntry = Constants.authorizationService().get(token); + } catch (Exception e) { + return ScopeProvider.instance.get(); + } + return authorizationEntry.getContext(); + } + + public static String getDateString(Calendar calendar){ + Date date = calendar.getTime(); + SimpleDateFormat format = new SimpleDateFormat(ISConstants.DATETIME_PATTERN); + return format.format(date); + } + + public File getFile(Class grClass, String contextFullName, String dateString){ + return new File(contextFullName.replace("/", "_") + "-" + grClass.getSimpleName() + "-" + dateString + "-exporter.json"); + } + + protected GCoreResourceMapper(Class grClass, Class rClass){ + this.grClass = grClass; + this.rClass = rClass; + this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); + this.resourceRegistryClient = ResourceRegistryClientFactory.create(); + } + + protected List getAll() { + SimpleQuery query = ICFactory.queryFor(grClass); + DiscoveryClient client = ICFactory.clientFor(grClass); + return client.submit(query); + } + + protected String getStringAsUTF8(String s) throws UnsupportedEncodingException{ + byte bytes[] = s.getBytes("ISO-8859-1"); + return new String(bytes, UTF8); + } + + protected R create(R r) throws ResourceRegistryException { + return resourceRegistryPublisher.createResource(rClass, r); + } + + protected R update(R r) throws ResourceRegistryException { + return resourceRegistryPublisher.updateResource(rClass, r); + } + + protected R read(UUID uuid) throws ResourceRegistryException { + return resourceRegistryClient.getInstance(rClass, uuid); + } + + protected R createOrUpdate(R r) throws ResourceRegistryException { + + UUID uuid = r.getHeader().getUUID(); + boolean update = false; + + try { + resourceRegistryClient.exists(rClass, uuid); + update = true; + }catch (ResourceNotFoundException e) { + update = false; + } catch (ResourceAvailableInAnotherContextException e) { + // This code should never be reached because this should be fixed in map function + resourceRegistryPublisher.addResourceToContext(uuid); + try{ + Thread.sleep(100); + }catch (Exception ee) {} + update= true; + } + + if(update){ + logger.debug("Resource with UUID {} exist. It will be updated", uuid); + try{ + return update(r); + }catch (ResourceRegistryException e) { + throw new UpdateException(e); + } + }else{ + logger.debug("Resource with UUID {} does not exist. It will be created", uuid); + try{ + return create(r); + }catch (ResourceRegistryException e) { + throw new CreateException(e); + } + } + + } + + private ObjectNode addNodeToArray(ArrayNode arrayNode, ObjectMapper objectMapper, GR gr, Exception e){ + ObjectNode objectNode = objectMapper.createObjectNode(); + objectNode.put(TYPE, grClass.getSimpleName()); + objectNode.put(ID, gr.id()); + objectNode.put(ERROR, e.getMessage()); + arrayNode.add(objectNode); + return objectNode; + } + + protected String getUsername() throws Exception { + String token = SecurityTokenProvider.instance.get(); + return Constants.authorizationService().get(token).getClientInfo().getId(); + } + + @SuppressWarnings("deprecation") + protected Home getHome(HomeManager manager) throws Exception{ + Home home = null; + String scope = ScopeProvider.instance.get(); + if(scope!=null){ + home = manager.getHome(getUsername()); + }else{ + home = manager.getHome(); + } + return home; + } + + protected WorkspaceFolder getExporterFolder(Workspace ws) throws Exception { + WorkspaceFolder root = ws.getRoot(); + + WorkspaceFolder exporterFolder = null; + if(!ws.exists(ISExporterPluginDeclaration.NAME, root.getId())){ + String folderDescription = String.format("The folder is used by %s plugin to store informations regarding failures exporting old GCore Resource to new Resource Registry", ISExporterPluginDeclaration.NAME); + exporterFolder = ws.createFolder(ISExporterPluginDeclaration.NAME, folderDescription, root.getId()); + }else{ + exporterFolder = (WorkspaceFolder) ws.find(ISExporterPluginDeclaration.NAME, root.getId()); + } + return exporterFolder; + } + + public static final String APPLICATION_JSON_MIMETYPE = "application/json"; + + private static final String FOLDER_DESCRIPTION = "Failures Report Folder for " + ISExporterPluginDeclaration.NAME; + + protected FolderItem publishFileToWorkspace(File file) throws Exception { + HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory(); + HomeManager manager = factory.getHomeManager(); + Home home = getHome(manager); + Workspace ws = home.getWorkspace(); + WorkspaceFolder exporterFolder = getExporterFolder(ws); + FileInputStream fileInputStream = new FileInputStream(file); + + FolderItem folderItem = WorkspaceUtil.createExternalFile( + exporterFolder, file.getName(), + FOLDER_DESCRIPTION, + APPLICATION_JSON_MIMETYPE, + fileInputStream); + return folderItem; + } + + protected abstract R map(GR gr) throws Exception; + + protected void notifyFailures(int allSize, List failed){ + String contextName = getCurrentContextName(); + + if(failed.size()==0){ + logger.debug("No needs to create an empty report besause there were no exporting failures on {}", contextName); + return; + } + + logger.warn("-------------------------------------------------------"); + logger.warn("{} : {} of {} ({} failures) {}s were exported as {}s", contextName, + allSize-failed.size(), allSize, failed.size(), grClass.getSimpleName(), rClass.getSimpleName()); + logger.warn("-------------------------------------------------------\n\n"); + + ObjectMapper objectMapper = new ObjectMapper(); + + ObjectNode objectNode = objectMapper.createObjectNode(); + + Calendar calendar = Calendar.getInstance(); + String dateString = getDateString(calendar); + objectNode.put(DATE, dateString); + ObjectNode context = objectNode.putObject(contextName); + ArrayNode mappingArrayNode = context.putArray(MAPPING_ERROR); + ArrayNode publishingArrayNode = context.putArray(PUBLISHING_ERROR); + + for(GR gr : failed){ + logger.trace("-------------------------------------------------------"); + R r = null; + try { + r = map(gr); + } catch (Exception e) { + addNodeToArray(mappingArrayNode, objectMapper, gr, e); + logger.trace("Error exporting {}. The problem was on mapping {} with UUID {}", gr, grClass.getSimpleName(), gr.id()); + logger.trace("-------------------------------------------------------\n"); + continue; + } + + try { + createOrUpdate(r); + }catch (Exception e) { + if(e.getCause() instanceof NullPointerException){ + logger.error("This MUST BE A BUG. Please Investigate"); + } + + if(e.getMessage().contains("com.orientechnologies.orient.server.distributed.task.ODistributedOperationException")){ + logger.error("This is an OrientDB distributed Issue"); + } + + ObjectNode node = addNodeToArray(publishingArrayNode, objectMapper, gr, e); + if(e instanceof CreateException) { + node.put(EXCEPTION_TYPE, CREATE); + } + + if(e instanceof UpdateException){ + node.put(EXCEPTION_TYPE, UPDATE); + } + + try { + logger.trace("Error exporting {}. The problem was on publishing {} as {}", gr, rClass.getSimpleName(), ISMapper.marshal(r)); + } catch (JsonProcessingException e1) { + logger.trace("", e1); + } + } + logger.trace("-------------------------------------------------------\n"); + } + + try { + String json = objectMapper.writeValueAsString(objectNode); + + File file = getFile(rClass, contextName, dateString); + synchronized (file) { + try(FileWriter fw = new FileWriter(file, true); + BufferedWriter bw = new BufferedWriter(fw); + PrintWriter out = new PrintWriter(bw)){ + out.println(json); + out.flush(); + } catch( IOException e ){ + throw e; + } + } + + try { + publishFileToWorkspace(file); + file.delete(); + }catch (Exception e) { + logger.error("Error while saving file {} on Workspace", file.getName(), e); + // TODO Use the Social Notification + } + + } catch (Exception e) { + logger.error("Error exporting JSON error result", e); + } + + logger.trace("-------------------------------------------------------\n\n\n\n\n"); + } + + protected R mapAndPublish(GR gr) throws Exception { + R r = map(gr); + + List facets = r.getIdentificationFacets(); + for(Facet f : facets){ + f.setAdditionalProperty(EXPORTED, EXPORTED_FROM_OLD_GCORE_IS); + } + + return createOrUpdate(r); + } + + public void export() { + + List all = getAll(); + + logger.debug("-------------------------------------------------------"); + logger.debug("Going to export {} {}s as {}s", + all.size(), grClass.getSimpleName(), rClass.getSimpleName()); + logger.debug("-------------------------------------------------------"); + + + List failed = new ArrayList<>(); + + for(GR gr : all){ + try { + Thread.sleep(300); + mapAndPublish(gr); + }catch (Exception e) { + failed.add(gr); + } + } + + notifyFailures(all.size(), failed); + + } + + + + + +} diff --git a/src/main/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporter.java b/src/main/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporter.java new file mode 100644 index 0000000..eff99a2 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporter.java @@ -0,0 +1,134 @@ +package org.gcube.informationsystem.exporter.mapper; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.GenericResource.Profile; +import org.gcube.informationsystem.impl.embedded.HeaderImpl; +import org.gcube.informationsystem.impl.entity.facet.SimpleFacetImpl; +import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl; +import org.gcube.informationsystem.impl.entity.resource.ConfigurationImpl; +import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl; +import org.gcube.informationsystem.model.embedded.Header; +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.Resource; +import org.gcube.informationsystem.model.entity.facet.SimpleFacet; +import org.gcube.informationsystem.model.entity.facet.SoftwareFacet; +import org.gcube.informationsystem.model.entity.resource.Configuration; +import org.gcube.informationsystem.model.relation.ConsistsOf; +import org.gcube.informationsystem.model.relation.IsIdentifiedBy; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; +import org.json.JSONObject; +import org.json.XML; + +public class GenericResourceExporter extends GCoreResourceMapper { + + public static final String FIXED_VERSION = "1.0.0"; + public static final String FULL_BODY = "FULL_BODY"; + + public GenericResourceExporter(){ + super(GenericResource.class, Configuration.class); + } + + @Override + protected Configuration map(GenericResource gr) throws Exception { + Profile profile = gr.profile(); + UUID uuid = UUID.fromString(gr.id()); + boolean readFromIS = false; + + Configuration configuration = null; + SoftwareFacet softwareFacet = null; + SimpleFacet simpleFacet = null; + + try { + resourceRegistryClient.exists(rClass, uuid); + readFromIS = true; + }catch (ResourceNotFoundException e) { + readFromIS = false; + } catch (ResourceAvailableInAnotherContextException e) { + resourceRegistryPublisher.addResourceToContext(uuid); + Thread.sleep(100); + readFromIS = true; + } + + + if(readFromIS){ + configuration = read(uuid); + //softwareFacet = (SoftwareFacet) configuration.getIdentificationFacets().get(0); + + List> consistsOfs = configuration.getConsistsOf(); + + for(ConsistsOf c : consistsOfs){ + Facet target = c.getTarget(); + if(c instanceof IsIdentifiedBy){ + if(target instanceof SoftwareFacet) { + softwareFacet = (SoftwareFacet) target; + continue; + } + } + + if(c instanceof ConsistsOf){ + if(target instanceof SimpleFacet){ + simpleFacet = (SimpleFacet) target; + continue; + } + } + + } + }else{ + configuration = new ConfigurationImpl(); + Header header = new HeaderImpl(uuid); + configuration.setHeader(header); + } + + /* ----------------------------------------- */ + if(softwareFacet==null){ + softwareFacet = new SoftwareFacetImpl(); + IsIdentifiedBy identifiedBy = + new IsIdentifiedByImpl(configuration, softwareFacet, null); + configuration.addFacet(identifiedBy); + } + + + softwareFacet.setGroup(profile.type()); + softwareFacet.setName(profile.name()); + softwareFacet.setVersion(FIXED_VERSION); + String description = profile.description(); + if(description!=null && description.compareTo("")!=0){ + softwareFacet.setDescription(getStringAsUTF8(description)); + } + /* ----------------------------------------- */ + + /* ----------------------------------------- */ + String xmlBody = profile.bodyAsString(); + JSONObject jsonBody = XML.toJSONObject(getStringAsUTF8(xmlBody)); + Map map = jsonBody.toMap(); + if(simpleFacet==null){ + simpleFacet = new SimpleFacetImpl(); + configuration.addFacet(simpleFacet); + + try { + SimpleFacet testSimpleFacet = new SimpleFacetImpl(); + testSimpleFacet.setAdditionalProperties(map); + SimpleFacet created = resourceRegistryPublisher.createFacet(SimpleFacet.class, testSimpleFacet); + resourceRegistryPublisher.deleteFacet(created); + simpleFacet.setAdditionalProperties(map); + }catch (Exception e) { + simpleFacet.setAdditionalProperty(FULL_BODY, getStringAsUTF8(xmlBody)); + } + }else{ + if(simpleFacet.getAdditionalProperty(FULL_BODY)!=null){ + simpleFacet.setAdditionalProperty(FULL_BODY, getStringAsUTF8(xmlBody)); + }else{ + simpleFacet.setAdditionalProperties(map); + } + } + /* ----------------------------------------- */ + + return configuration; + } + +} diff --git a/src/main/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporter.java b/src/main/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporter.java new file mode 100644 index 0000000..0cd4957 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporter.java @@ -0,0 +1,303 @@ +package org.gcube.informationsystem.exporter.mapper; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Profile; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime; +import org.gcube.common.resources.gcore.common.Platform; +import org.gcube.common.resources.gcore.utils.Group; +import org.gcube.informationsystem.impl.embedded.HeaderImpl; +import org.gcube.informationsystem.impl.embedded.ValueSchemaImpl; +import org.gcube.informationsystem.impl.entity.facet.AccessPointFacetImpl; +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.relation.ConsistsOfImpl; +import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl; +import org.gcube.informationsystem.model.embedded.Header; +import org.gcube.informationsystem.model.embedded.ValueSchema; +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.Resource; +import org.gcube.informationsystem.model.entity.facet.AccessPointFacet; +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.relation.ConsistsOf; +import org.gcube.informationsystem.model.relation.IsIdentifiedBy; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServiceEndpointExporter extends GCoreResourceMapper { + + private static Logger logger = LoggerFactory.getLogger(ServiceEndpointExporter.class); + + public static final String FIXED_VERSION = "1.0.0"; + + public static final String PLATFORM = "PLATFORM"; + public static final String POSITION = "POSITION"; + + public static final String GHN_ID = "ghnID"; + + public static final String USERNAME = "USERNAME"; + public static final String PASSWORD = "PASSWORD"; + public static final String USERNAME_PASSWORD_SCHEMA_STRING; + //public static final URI USERNAME_PASSWORD_SCHEMA; + + public static final String NAME = "NAME"; + public static final String ENCRYPTED = "ENCRYPTED"; + public static final String VALUE = "VALUE"; + public static final String PROPERTY_SCHEMA_STRING; + public static final URI PROPERTY_SCHEMA; + + public static final String NOT_URI_ENDPOINT = "NOT_URI_ENDPOINT"; + + static { + USERNAME_PASSWORD_SCHEMA_STRING = String.format("%s:%s", USERNAME, PASSWORD); + //USERNAME_PASSWORD_SCHEMA = URI.create(USERNAME_PASSWORD_SCHEMA_STRING); + PROPERTY_SCHEMA_STRING = String.format("%s:%s:%s", NAME, ENCRYPTED, VALUE); + PROPERTY_SCHEMA = URI.create(PROPERTY_SCHEMA_STRING); + } + + public ServiceEndpointExporter(){ + super(ServiceEndpoint.class, EService.class); + } + + @Override + protected EService map(ServiceEndpoint gr) throws Exception { + Profile profile = gr.profile(); + UUID uuid = UUID.fromString(gr.id()); + boolean readFromIS = false; + + EService eService = null; + AccessPointFacet[] accessPointFacets = null; + SoftwareFacet softwareFacet = null; + SoftwareFacet platformSoftwareFacet = null; + ServiceStateFacet serviceStateFacet = null; + NetworkingFacet networkingFacet = null; + + try { + resourceRegistryClient.exists(rClass, uuid); + readFromIS = true; + }catch (ResourceNotFoundException e) { + readFromIS = false; + }catch (ResourceAvailableInAnotherContextException e) { + resourceRegistryPublisher.addResourceToContext(uuid); + Thread.sleep(100); + readFromIS = true; + } + + Group accessPoints = profile.accessPoints(); + accessPointFacets = new AccessPointFacet[accessPoints.size()]; + + if(readFromIS){ + eService = read(uuid); + //softwareFacet = (SoftwareFacet) eService.getIdentificationFacets().get(0); + + List> consistsOfs = eService.getConsistsOf(); + + for(ConsistsOf c : consistsOfs){ + Facet target = c.getTarget(); + if(c instanceof IsIdentifiedBy){ + if(target instanceof SoftwareFacet) { + softwareFacet = (SoftwareFacet) target; + continue; + } + } else { + if(target instanceof AccessPointFacet){ + try { + Object positionObject = c.getAdditionalProperty(POSITION); + Integer position = Integer.valueOf(positionObject.toString()); + if(position!=null){ + accessPointFacets[position] = (AccessPointFacet) target; + } + }catch (Exception e) { + // Position is used on relation to match the AccessPoint on ServiceEndpoint + logger.error("No POSITION found", e); + } + continue; + } + + if(target instanceof SoftwareFacet){ + SoftwareFacet targetSoftwareFacet = (SoftwareFacet) target; + if(targetSoftwareFacet.getGroup().compareTo(PLATFORM)==0){ + platformSoftwareFacet = targetSoftwareFacet; + } + continue; + } + + if(target instanceof ServiceStateFacet){ + serviceStateFacet = (ServiceStateFacet) target; + continue; + } + + if(target instanceof NetworkingFacet){ + networkingFacet = (NetworkingFacet) target; + continue; + } + + } + + } + }else{ + eService = new EServiceImpl(); + Header header = new HeaderImpl(uuid); + eService.setHeader(header); + } + + /* ----------------------------------------- */ + if(softwareFacet==null){ + softwareFacet = new SoftwareFacetImpl(); + IsIdentifiedBy identifiedBy = + new IsIdentifiedByImpl(eService, softwareFacet, null); + eService.addFacet(identifiedBy); + } + + softwareFacet.setGroup(profile.category()); + softwareFacet.setName(profile.name()); + softwareFacet.setVersion(FIXED_VERSION); + String description = profile.description(); + if(description!=null && description.compareTo("")!=0){ + softwareFacet.setDescription(getStringAsUTF8(description)); + } + /* ----------------------------------------- */ + + + /* ----------------------------------------- */ + Platform platform = profile.platform(); + + if(platformSoftwareFacet==null){ + platformSoftwareFacet = new SoftwareFacetImpl(); + eService.addFacet(platformSoftwareFacet); + } + + platformSoftwareFacet.setGroup(PLATFORM); + platformSoftwareFacet.setName(platform.name()); + String platformVersion = String.format("%d.%d.%d-%d", + platform.version(), platform.minorVersion(), + platform.revisionVersion(), platform.buildVersion()); + softwareFacet.setVersion(platformVersion); + /* ----------------------------------------- */ + + + /* ----------------------------------------- */ + Runtime runTime = profile.runtime(); + + if(serviceStateFacet==null){ + serviceStateFacet = new ServiceStateFacetImpl(); + eService.addFacet(serviceStateFacet); + } + serviceStateFacet.setValue(runTime.status()); + + if(networkingFacet==null){ + networkingFacet = new NetworkingFacetImpl(); + eService.addFacet(networkingFacet); + } + networkingFacet.setHostName(runTime.hostedOn()); + String ghnID = runTime.ghnId(); + if(ghnID!=null && ghnID.compareTo("")!=0){ + networkingFacet.setAdditionalProperty(GHN_ID, ghnID); + } + + /* ----------------------------------------- */ + + + /* ----------------------------------------- */ + int i=0; + for(AccessPoint accessPoint : accessPoints){ + if(accessPointFacets[i] == null){ + accessPointFacets[i] = new AccessPointFacetImpl(); + ConsistsOf consistsOf = new ConsistsOfImpl(eService, accessPointFacets[i], null); + consistsOf.setAdditionalProperty(POSITION, i); + eService.addFacet(consistsOf); + } + + accessPointFacets[i].setEntryName(accessPoint.name()); + String address = accessPoint.address(); + if(address!=null && address.compareTo("")!=0){ + try { + URI uri = URI.create(address); + accessPointFacets[i].setEndpoint(uri); + }catch (IllegalArgumentException e) { + accessPointFacets[i].setAdditionalProperty(NOT_URI_ENDPOINT, address); + } + } + + String accessPointDescription = accessPoint.description(); + if(accessPointDescription!=null && accessPointDescription.compareTo("")!=0){ + accessPointFacets[i].setDescription(getStringAsUTF8(accessPointDescription)); + } + + /* ---------- */ + ValueSchema authorization = new ValueSchemaImpl(); + + String value = USERNAME_PASSWORD_SCHEMA_STRING; + String schema = USERNAME_PASSWORD_SCHEMA_STRING; + + boolean replaceColon = false; + + try { + value = value.replace(USERNAME, accessPoint.username()); + }catch (NullPointerException e) { + value = value.replace(USERNAME, ""); + schema = schema.replace(USERNAME, ""); + replaceColon = true; + } + + try { + value = value.replace(PASSWORD, accessPoint.password()); + }catch (NullPointerException e) { + value = value.replace(PASSWORD, ""); + schema = schema.replace(PASSWORD, ""); + replaceColon = true; + } + + if(replaceColon){ + value = value.replace(":", ""); + schema = schema.replace(":", ""); + } + + if(value.compareTo("")!=0){ + authorization.setValue(value); + authorization.setSchema(URI.create(schema)); + + accessPointFacets[i].setAuthorization(authorization); + } + /* ----- */ + + + List properties = new ArrayList<>(); + + for(Property property : accessPoint.properties()){ + ValueSchema valueSchema = new ValueSchemaImpl(); + String propertyValue = PROPERTY_SCHEMA.toString(); + propertyValue = propertyValue.replace(NAME, property.name()); + propertyValue = propertyValue.replace(ENCRYPTED, Boolean.toString(property.isEncrypted())); + propertyValue = propertyValue.replace(VALUE, property.value()); + + valueSchema.setValue(propertyValue); + valueSchema.setSchema(PROPERTY_SCHEMA); + + properties.add(valueSchema); + + } + accessPointFacets[i].setProperties(properties); + + i++; + } + /* ----------------------------------------- */ + + + return eService; + } + +} diff --git a/src/main/java/org/gcube/informationsystem/exporter/mapper/exception/CreateException.java b/src/main/java/org/gcube/informationsystem/exporter/mapper/exception/CreateException.java new file mode 100644 index 0000000..7883ccb --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/mapper/exception/CreateException.java @@ -0,0 +1,23 @@ +package org.gcube.informationsystem.exporter.mapper.exception; + +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; + +public class CreateException extends ResourceRegistryException { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = 7815403701115692343L; + + public CreateException(String message) { + super(message); + } + + public CreateException(Throwable cause) { + super(cause); + } + + public CreateException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/org/gcube/informationsystem/exporter/mapper/exception/UpdateException.java b/src/main/java/org/gcube/informationsystem/exporter/mapper/exception/UpdateException.java new file mode 100644 index 0000000..7abe4ca --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/exporter/mapper/exception/UpdateException.java @@ -0,0 +1,24 @@ +package org.gcube.informationsystem.exporter.mapper.exception; + +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; + +public class UpdateException extends ResourceRegistryException { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = -1473775004888760485L; + + + public UpdateException(String message) { + super(message); + } + + public UpdateException(Throwable cause) { + super(cause); + } + + public UpdateException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/test/java/org/gcube/informationsystem/exporter/ISExporterPluginTest.java b/src/test/java/org/gcube/informationsystem/exporter/ISExporterPluginTest.java new file mode 100644 index 0000000..0b6a532 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/exporter/ISExporterPluginTest.java @@ -0,0 +1,53 @@ +package org.gcube.informationsystem.exporter; + +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.informationsystem.exporter.mapper.GenericResourceExporterTest; +import org.gcube.informationsystem.exporter.mapper.ServiceEndpointExporterTest; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ISExporterPluginTest { + + private static Logger logger = LoggerFactory.getLogger(ISExporterPluginTest.class); + + @Test + public void testLaunch() throws ObjectNotFound, Exception{ + String[] tokens = { + ScopedTest.GCUBE, + ScopedTest.GCUBE_DEVSEC, + ScopedTest.GCUBE_DEVSEC_DEVVRE, + ScopedTest.GCUBE_DEVNEXT, + ScopedTest.GCUBE_DEVNEXT_NEXTNEXT + }; + + for(String token : tokens){ + logger.info("\n\n\n-------------------------------------------------------------------------"); + ScopedTest.setContext(token); + ISExporterPlugin isExporterPlugin = new ISExporterPlugin(new ISExporterPluginDeclaration()); + isExporterPlugin.launch(null); + logger.info("\n\n\n"); + } + } + + //@Test + public void delAllExported() throws ObjectNotFound, Exception{ + String[] tokens = { + ScopedTest.GCUBE, + ScopedTest.GCUBE_DEVSEC, + ScopedTest.GCUBE_DEVSEC_DEVVRE, + ScopedTest.GCUBE_DEVNEXT, + ScopedTest.GCUBE_DEVNEXT_NEXTNEXT + }; + + for(String token : tokens){ + ScopedTest.setContext(token); + + GenericResourceExporterTest genericResourceExporterTest = new GenericResourceExporterTest(); + genericResourceExporterTest.del(); + + ServiceEndpointExporterTest serviceEndpointExporterTest = new ServiceEndpointExporterTest(); + serviceEndpointExporterTest.del(); + } + } +} diff --git a/src/test/java/org/gcube/informationsystem/exporter/ScopedTest.java b/src/test/java/org/gcube/informationsystem/exporter/ScopedTest.java new file mode 100644 index 0000000..7c4efbd --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/exporter/ScopedTest.java @@ -0,0 +1,120 @@ +/** + * + */ +package org.gcube.informationsystem.exporter; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactorySetter; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactorySetter; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + * + */ +public class ScopedTest { + + private static final Logger logger = LoggerFactory.getLogger(ScopedTest.class); + + protected static final String PROPERTIES_FILENAME = "token.properties"; + + private static final String GCUBE_DEVNEXT_VARNAME = "GCUBE_DEVNEXT"; + public static final String GCUBE_DEVNEXT; + + private static final String GCUBE_DEVNEXT_NEXTNEXT_VARNAME = "GCUBE_DEVNEXT_NEXTNEXT"; + public static final String GCUBE_DEVNEXT_NEXTNEXT; + + public static final String GCUBE_DEVSEC_VARNAME = "GCUBE_DEVSEC"; + public static final String GCUBE_DEVSEC; + + public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE"; + public static final String GCUBE_DEVSEC_DEVVRE; + + public static final String GCUBE_VARNAME = "GCUBE"; + public static final String GCUBE; + + + public static final String DEFAULT_TEST_SCOPE; + public static final String ALTERNATIVE_TEST_SCOPE; + + + protected static final String REGISTRY_PROPERTIES_FILENAME = "registry.properties"; + public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL"; + public static final String RESOURCE_REGISTRY_URL; + + static { + Properties properties = new Properties(); + InputStream input = ScopedTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME); + + try { + // load the properties file + properties.load(input); + } catch (IOException e) { + throw new RuntimeException(e); + } + + GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME); + GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME); + + GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME); + GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME); + + GCUBE = properties.getProperty(GCUBE_VARNAME); + + DEFAULT_TEST_SCOPE = GCUBE_DEVSEC; + ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC_DEVVRE; + + + properties = new Properties(); + input = ScopedTest.class.getClassLoader().getResourceAsStream(REGISTRY_PROPERTIES_FILENAME); + + try { + // load the properties file + properties.load(input); + } catch (IOException e) { + throw new RuntimeException(e); + } + + RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY); + if(RESOURCE_REGISTRY_URL!=null){ + ResourceRegistryPublisherFactorySetter.forceToURL(RESOURCE_REGISTRY_URL); + ResourceRegistryClientFactorySetter.forceToURL(RESOURCE_REGISTRY_URL); + } + } + + 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); + 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{ + setContext(DEFAULT_TEST_SCOPE); + } + + @AfterClass + public static void afterClass() throws Exception{ + SecurityTokenProvider.instance.reset(); + ScopeProvider.instance.reset(); + } + +} diff --git a/src/test/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapperTest.java b/src/test/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapperTest.java new file mode 100644 index 0000000..25638a6 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/exporter/mapper/GCoreResourceMapperTest.java @@ -0,0 +1,59 @@ +package org.gcube.informationsystem.exporter.mapper; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Calendar; + +import org.gcube.informationsystem.exporter.ScopedTest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GCoreResourceMapperTest extends ScopedTest { + + private static Logger logger = LoggerFactory.getLogger(GenericResourceExporterTest.class); + + protected File getFile(GCoreResourceMapper grm) throws Exception { + String contextName = GCoreResourceMapper.getCurrentContextName(); + String dateString = GCoreResourceMapper.getDateString(Calendar.getInstance()); + File file = grm.getFile(GenericResourceExporterTest.class, contextName, dateString); + String json = "{}"; + synchronized (file) { + try(FileWriter fw = new FileWriter(file, true); + BufferedWriter bw = new BufferedWriter(fw); + PrintWriter out = new PrintWriter(bw)){ + out.println(json); + out.flush(); + } catch( IOException e ){ + throw e; + } + } + return file; + } + + //@Test + public void testWorkspace() throws Exception { + + String[] tokens = { + ScopedTest.GCUBE, + ScopedTest.GCUBE_DEVSEC, + ScopedTest.GCUBE_DEVSEC_DEVVRE, + ScopedTest.GCUBE_DEVNEXT, + ScopedTest.GCUBE_DEVNEXT_NEXTNEXT + }; + + for(String token : tokens){ + logger.info("\n\n\n-------------------------------------------------------------------------"); + ScopedTest.setContext(token); + GenericResourceExporter gre = new GenericResourceExporter(); + File file = getFile(gre); + gre.publishFileToWorkspace(file); + logger.info("\n\n\n"); + file.delete(); + } + + } + +} diff --git a/src/test/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporterTest.java b/src/test/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporterTest.java new file mode 100644 index 0000000..8910df2 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/exporter/mapper/GenericResourceExporterTest.java @@ -0,0 +1,111 @@ +package org.gcube.informationsystem.exporter.mapper; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.informationsystem.exporter.ScopedTest; +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.resource.Configuration; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class GenericResourceExporterTest extends ScopedTest{ + + private static Logger logger = LoggerFactory.getLogger(GenericResourceExporterTest.class); + + @Test + public void export(){ + GenericResourceExporter gre = new GenericResourceExporter(); + gre.export(); + } + + + public void del() throws ResourceRegistryException{ + ResourceRegistryClient client = ResourceRegistryClientFactory.create(); + ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(); + + @SuppressWarnings("unchecked") + List configurations = (List) client.getInstances(Configuration.NAME, false); + List failed = new ArrayList<>(); + + logger.debug("Going to delete {} {}s", + configurations.size(), Configuration.NAME); + + int excluded = 0; + + for(Configuration configuration : configurations){ + try { + Facet facet = configuration.getIdentificationFacets().get(0); + String string = (String) facet.getAdditionalProperty(GCoreResourceMapper.EXPORTED); + publisher.deleteResource(configuration); + if(string!=null && string.compareTo(GCoreResourceMapper.EXPORTED_FROM_OLD_GCORE_IS)==0){ + publisher.deleteResource(configuration); + }else{ + excluded++; + } + }catch (Exception e) { + failed.add(configuration); + } + } + + logger.debug("{} of {} ({} failures) {}s were deleted. {} {}s were excluded because there was not exported from gCore IS.", + configurations.size()-failed.size()-excluded, configurations.size(), failed.size(), Configuration.NAME, excluded, Configuration.NAME); + } + + //@Test + public void investigateSingleResource() throws Exception { + ScopedTest.setContext(ScopedTest.GCUBE); + + UUID uuid = UUID.fromString(""); + + //ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(); + //publisher.deleteResource(uuid); + + //ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + + SimpleQuery query = ICFactory.queryFor(GenericResource.class) + .addCondition(String.format("$resource/ID/text() eq '%1s'", uuid.toString())); + DiscoveryClient client = ICFactory.clientFor(GenericResource.class); + List seList = client.submit(query); + + GenericResourceExporter see = new GenericResourceExporter(); + see.mapAndPublish(seList.get(0)); + + } + + //@Test + public void addToAllScopes() throws Exception { + + UUID uuid = UUID.fromString(""); + ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(); + + String[] tokens = { + ScopedTest.GCUBE, + ScopedTest.GCUBE_DEVSEC, + ScopedTest.GCUBE_DEVSEC_DEVVRE, + ScopedTest.GCUBE_DEVNEXT, + ScopedTest.GCUBE_DEVNEXT_NEXTNEXT + }; + + for(String token : tokens){ + ScopedTest.setContext(token); + publisher.addResourceToContext(uuid); + } + + } + +} diff --git a/src/test/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporterTest.java b/src/test/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporterTest.java new file mode 100644 index 0000000..6fabe4e --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/exporter/mapper/ServiceEndpointExporterTest.java @@ -0,0 +1,86 @@ +package org.gcube.informationsystem.exporter.mapper; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.informationsystem.exporter.ScopedTest; +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.resource.EService; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ServiceEndpointExporterTest extends ScopedTest{ + + private static Logger logger = LoggerFactory.getLogger(ServiceEndpointExporterTest.class); + + @Test + public void export(){ + ServiceEndpointExporter see = new ServiceEndpointExporter(); + see.export(); + } + + public void del() throws ResourceRegistryException{ + ResourceRegistryClient client = ResourceRegistryClientFactory.create(); + ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(); + + + @SuppressWarnings("unchecked") + List eServices = (List) client.getInstances(EService.NAME, false); + List failed = new ArrayList<>(); + + logger.debug("Going to delete {} {}s", + eServices.size(), EService.NAME); + + int excluded = 0; + + for(EService eService : eServices){ + try { + Facet facet = eService.getIdentificationFacets().get(0); + String string = (String) facet.getAdditionalProperty(GCoreResourceMapper.EXPORTED); + if(string!=null && string.compareTo(GCoreResourceMapper.EXPORTED_FROM_OLD_GCORE_IS)==0){ + publisher.deleteResource(eService); + }else{ + excluded++; + } + }catch (Exception e) { + failed.add(eService); + } + } + + logger.debug("{} of {} ({} failures) {}s were deleted. {} {}s were excluded because there was not exported from gCore IS.", + eServices.size()-failed.size()-excluded, eServices.size(), failed.size(), EService.NAME, excluded, EService.NAME); + } + + @Test + public void investigateSingleResource() throws Exception { + UUID uuid = UUID.fromString("66a674a2-8319-43b4-86c1-bd092bc7c490"); + ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create(); + publisher.addResourceToContext(uuid); + //ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + + + SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) + .addCondition(String.format("$resource/ID/text() eq '%1s'", uuid.toString())); + DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); + List seList = client.submit(query); + + ServiceEndpointExporter see = new ServiceEndpointExporter(); + see.notifyFailures(seList.size(), seList); + + } + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactorySetter.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactorySetter.java new file mode 100644 index 0000000..3b9d7e4 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientFactorySetter.java @@ -0,0 +1,9 @@ +package org.gcube.informationsystem.resourceregistry.client; + +public class ResourceRegistryClientFactorySetter { + + public static void forceToURL(String url) { + ResourceRegistryClientFactory.forceToURL(url); + } + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherFactorySetter.java b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherFactorySetter.java new file mode 100644 index 0000000..6cb4751 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/publisher/ResourceRegistryPublisherFactorySetter.java @@ -0,0 +1,9 @@ +package org.gcube.informationsystem.resourceregistry.publisher; + +public class ResourceRegistryPublisherFactorySetter { + + public static void forceToURL(String url) { + ResourceRegistryPublisherFactory.forceToURL(url); + } + +} diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..42fc238 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,42 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/registry.properties.empty b/src/test/resources/registry.properties.empty new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/registry.properties.test b/src/test/resources/registry.properties.test new file mode 100644 index 0000000..cd70085 --- /dev/null +++ b/src/test/resources/registry.properties.test @@ -0,0 +1 @@ +RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry \ No newline at end of file