commit 4d184c64baa5f9373c6a04aa665e13ec8c1defdd Author: roberto.cirillo Date: Wed Jun 20 09:02:56 2018 +0000 branch from trunk git-svn-id: https://svn.research-infrastructures.eu/public/d4science/gcube/branches/content-management/storage-manager-wrapper/2.5@169342 82a268e6-3cf1-43bd-a215-b396298e98cf diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..8374d7b --- /dev/null +++ b/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..a14c1bc --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + storage-manager-wrapper + + + + + + 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..6249222 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +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..4ff5639 --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,5 @@ +#Wed May 02 14:50:59 CEST 2012 +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/CHANGELOG b/distro/CHANGELOG new file mode 100644 index 0000000..49759c5 --- /dev/null +++ b/distro/CHANGELOG @@ -0,0 +1,16 @@ +v 2.2.0 (21-04-2015) + * change smp protocol +v 2.1.0 (01-04-2014) + * support MongoDB backend with authentication +v 2.0.1 (29-05-2013) + * bug fix incident: connection pending #606 +v 2.0.0 (19-05-2013) + * added move, copy and link operations + *FWS integration +v 1.0.2 (15-01-2013) + * Integration with new gCube release 2.12.0 version system +v 1.0.1 (11-10-2012) + * added VOLATILE area +v. 1.0.0 (04-05-2012) + * First release + diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..7bca8c8 --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1,2 @@ +${gcube.license} + diff --git a/distro/MAINTAINERS b/distro/MAINTAINERS new file mode 100644 index 0000000..05abbd7 --- /dev/null +++ b/distro/MAINTAINERS @@ -0,0 +1 @@ +* Roberto Cirillo (roberto.cirillo@isti.cnr.it), CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..0136490 Binary files /dev/null and b/distro/README differ diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..1181b78 --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,47 @@ + + + Added new JUnit Test classes + update JUnit Test Classes + delete old method for retrieving remote phrase used by old smp protocol classes + + + + dinamically load "write concern"" and ""read preference" options + add method for decrypt an id + + + dinamically load smpConnection handler + add method for decrypt an id + + + update JUnit test for smp uri without payload + + + change smp protocol + + + support MongoDB backend with authentication + + + Integration with new gCube release 3.0.0 version system + Java7 refactoring + + + Integration with new gCube release 2.15.0 version system + bugfix on pending connections + + + Integration with new gCube release 2.14.0 version system + FWS integration + added move, copy and link operations + + + Integration with new gCube release 2.12.0 version system + + + Added VOLATILE area + + + First Release + + diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..e395580 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,32 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + changelog.xml + profile.xml + + 755 + true + + + + + target/${build.finalName}.${project.packaging} + /${artifactId} + + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..83f8c3d --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,25 @@ + + + + Service + + ${description} + ContentManagement + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + diff --git a/distro/svnpath.txt b/distro/svnpath.txt new file mode 100644 index 0000000..edacb04 --- /dev/null +++ b/distro/svnpath.txt @@ -0,0 +1 @@ +${scm.url} \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4aaa974 --- /dev/null +++ b/pom.xml @@ -0,0 +1,130 @@ + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + + org.gcube.contentmanagement + storage-manager-wrapper + 2.5.0-SNAPSHOT + + scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/content-management/${project.artifactId} + scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/content-management/${project.artifactId} + http://svn.d4science.research-infrastructures.eu/public/d4science/gcube/trunk/content-management/${project.artifactId} + + + ${project.basedir}/distro + + + + + org.gcube.distribution + gcube-bom + LATEST + pom + import + + + + + + org.gcube.contentmanagement + storage-manager-core + [2.2.0-SNAPSHOT, 3.0.0-SNAPSHOT) + provided + + + org.gcube.resources + common-gcore-resources + + + + + org.gcube.core + common-gcore-stubs + + + + org.gcube.resources.discovery + ic-client + + + + org.gcube.core + common-encryption + + + + org.gcube.core + common-scope-maps + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + junit + junit + 4.11 + test + + + org.slf4j + slf4j-log4j12 + 1.6.4 + provided + + + commons-codec + commons-codec + 1.8 + + + + + + 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 + + + + + servicearchive + install + + single + + + + + + + \ No newline at end of file diff --git a/src/main/java/log4j.properties b/src/main/java/log4j.properties new file mode 100644 index 0000000..2a7b243 --- /dev/null +++ b/src/main/java/log4j.properties @@ -0,0 +1,15 @@ +log4j.rootLogger=DEBUG, A1, stdout +log4j.appender.A1=org.apache.log4j.RollingFileAppender +log4j.appender.A1.File=log.txt +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n +# ***** Max file size is set to 100KB +log4j.appender.A1.MaxFileSize=100MB +# ***** Keep one backup file +log4j.appender.A1.MaxBackupIndex=1 + +#CONSOLE +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Threshold=INFO +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=[%t] %-5p %c %d{dd MMM yyyy ;HH:mm:ss.SSS} - %m%n diff --git a/src/main/java/org/gcube/common/scope/impl/ServiceMapScannerMediator.java b/src/main/java/org/gcube/common/scope/impl/ServiceMapScannerMediator.java new file mode 100644 index 0000000..db25f91 --- /dev/null +++ b/src/main/java/org/gcube/common/scope/impl/ServiceMapScannerMediator.java @@ -0,0 +1,40 @@ +package org.gcube.common.scope.impl; + +import java.util.Set; + +import org.gcube.common.scope.api.ScopeProvider; + +/** + * Check the validity of a given scope + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public class ServiceMapScannerMediator { + + /** + * The validation has been removed + * @param scope + * @return + */ + @Deprecated + public static boolean isValid(String scope){ +// log.debug("validating scope "+scope); + String currentScope=ScopeProvider.instance.get(); + ScopeProvider.instance.set(scope); + try{ + new ScopedServiceMap().scope(); + + }catch(Exception e){ + return false; + }finally{ + ScopeProvider.instance.set(currentScope); + } + return true; + } + + + public static Set getScopeKeySet(){ + return ServiceMapScanner.maps().keySet(); + } + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/URIs.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/URIs.java new file mode 100644 index 0000000..ed835fc --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/URIs.java @@ -0,0 +1,155 @@ +package org.gcube.contentmanager.storageclient.model.protocol; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; +//import org.gcube.common.core.scope.GCUBEScope; +//import org.gcube.common.core.scope.GCUBEScopeManager; +import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler; + +/** + * Utility methods for content URI creation and manipulation. + * @author Fabio Simeoni (University of Strathclyde) + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public class URIs { + + static { + Handler.activateProtocol(); + } + + /**Scheme of cms URIs.*/ + public static final String PROTOCOL="smp"; + + /** + * Indicates whether a URI is a valid content URI. + * @param uri the URI. + * @throws URISyntaxException if the URI fails validation. + */ + public static void validate(URI uri) throws URISyntaxException { + if (!PROTOCOL.equals(uri.getScheme()) || + uri.getAuthority()==null || + uri.getPath()==null || + uri.getPath().length()<2) + throw new IllegalArgumentException(new URISyntaxException(uri.toString(),"uri is not a well-formed content URI")); + } + + /** + * Constructs a content URI from a collection identifiers and one or more node identifiers. + * @param collectionID the collection identifier. + * @param identifiers the node identifiers. + * @return the URI. + * @throws IllegalArgumentException if the input is null or empty. + */ + public static URI make(String collectionID, String ... identifiers) throws IllegalArgumentException { + + if (collectionID==null || identifiers==null || identifiers.length==0) + throw new IllegalArgumentException("null or empty input"); + + StringBuilder path = new StringBuilder(); + for (Object id : identifiers) + path.append("/"+id.toString()); + + URI uri = null; + try { + uri = new URI(PROTOCOL,collectionID,path.toString(),null); + } + catch(URISyntaxException e) { + throw new RuntimeException("error in generation uri with "+PROTOCOL+","+collectionID+","+path,e); + } + return uri; + } + + /** + * Returns the collection identifier in a sm URI. + * @param uri the URI. + * @return the identifier. + * @throws URISyntaxException if the URI is not a content URI. + */ + public static String collectionID(URI uri) throws URISyntaxException { + validate(uri); + return uri.getAuthority(); + } + + /** + * Returns the document identifier in a content URI. + * @param uri the URI. + * @return the identifier. + * @throws URISyntaxException if the URI is not a content URI. + */ + public static String documentID(URI uri) throws URISyntaxException { + validate(uri); + String p = uri.getPath().substring(1); + if (p.endsWith("/")) + p = p.substring(0,p.length()-1); + int index = p.indexOf("/"); + return p.substring(0,index>0?index:p.length()); + } + + /** + * Returns the identifier of the node identified by a content URI. + * @param uri the URI. + * @return the identifier. + * @throws URISyntaxException if the URI is not a content URI. + */ + public static String nodeID(URI uri) throws URISyntaxException { + validate(uri); + return uri.getPath().substring(uri.getPath().lastIndexOf("/")+1); + } + + /** + * Returns the identifiers in a content URI. + * @param uri the URI. + * @return the identifiers. + * @throws URISyntaxException if the URI is not a content URI. + */ + public static String[] nodeIDs(URI uri) throws URISyntaxException { + validate(uri); + List ids = new ArrayList(); + for (String s : uri.getPath().substring(1).split("/")) //will be validated here + ids.add(s); + return ids.toArray(new String[0]); + } + + /** + * Returns a content URI for the parent of the node identified by another content URI. + * @param uri the input URI. + * @return the parent URI. + * @throws URISyntaxException if the input URI is not a content URL. + */ + public static URI parentURI(URI uri) throws URISyntaxException { + validate(uri); + String u = uri.getPath(); + return make(uri.getAuthority(),u.substring(1,u.lastIndexOf("/")).split("/")); + } + + /** + * Returns a content URI for the document of the node identified by another content URI. + * @param uri the input URI. + * @return the document URI. + * @throws URISyntaxException if the input URI is not a content URI. + */ + public static URI documentURI(URI uri) throws URISyntaxException { + validate(uri); + return make(uri.getAuthority(),documentID(uri)); + } + + /** + * Returns a URL connection in a given scope. + * @param uri a content URI. + * @param scope the scope. + * @return the connection. + * @throws IOException if the connections could not be established. + * @throws URISyntaxException if the URI is not a content URI or if the protocol handler for the smp scheme is not active. + * @deprecated since 2.3.1. Use {@link URLConnection} normally in current scope. + */ + public static URLConnection connection(URI uri, String scope) throws IOException, URISyntaxException { + validate(uri); + URLConnection connection = uri.toURL().openConnection(); + return connection; + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/Handler.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/Handler.java new file mode 100644 index 0000000..c4d9b77 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/Handler.java @@ -0,0 +1,39 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * A handler for the smp protocol. + * @author Fabio Simeoni (University of Strathclyde) + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public class Handler extends URLStreamHandler { + + /**{@inheritDoc}*/ + @Override + protected URLConnection openConnection(URL u) throws IOException { + return SMPURLConnectionFactory.getSmp(u); + } + + /** + * Registers the protocol with the JVM. + */ + public static void activateProtocol() { + + String pkgs_name="java.protocol.handler.pkgs"; + String pkgs = System.getProperty(pkgs_name); + String pkg = "org.gcube.contentmanager.storageclient.model.protocol"; + if (pkgs==null) + pkgs = pkg ; + else if (!pkgs.contains(pkg)) + pkgs = pkgs+"|"+pkg; + System.setProperty(pkgs_name, pkgs); + } + + + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPConnection.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPConnection.java new file mode 100644 index 0000000..fe7cef5 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPConnection.java @@ -0,0 +1,60 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanager.storageclient.wrapper.ISClientConnector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An extension of URLConnection used for smp url + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public abstract class SMPConnection extends URLConnection { + + Logger logger= LoggerFactory.getLogger(SMPURLConnection.class); + + public SMPConnection(URL url) { + super(url); + } + + public abstract URLConnection init(URL url); + + /**{@inheritDoc}*/ + @Override + public synchronized void connect() throws IOException { + connected=true; + } + + @Override + public abstract InputStream getInputStream() throws IOException; + + + protected abstract InputStream storageClient(String url) throws Exception; + + /** + * This method has been moved in Configuration class + * @param rootScope + * @return + * @throws Exception + * @see org.gcube.contentmanager.storageclient.wrapper.Configuration + * + */ + @Deprecated + protected String retrieveEncryptionPhrase() throws Exception { + String currentScope=ScopeProvider.instance.get(); + logger.debug("retrieve encryption prhase on scope: "+currentScope); + String encryptedKey=null; + ISClientConnector isclient=new ISClientConnector(); + encryptedKey=isclient.retrievePropertyValue("PassPhrase", currentScope); + String decryptString=org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey); + return decryptString; + } + + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnection.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnection.java new file mode 100644 index 0000000..51f93de --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnection.java @@ -0,0 +1,47 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** +* +* This is invoked by the platform with a URL of the right protocol. +* @author Fabio Simeoni (University of Strathclyde), @author Roberto Cirillo (ISTI-CNR) +* @deprecated this class will be deleted and will be changed with the factory class invocation: SMPURLConnectionFactory +* @see SMPURLConnectionFactory +*/ +@Deprecated +public class SMPURLConnection extends URLConnection { + + SMPConnection smp; + Logger logger= LoggerFactory.getLogger(SMPURLConnection.class); + /** + * Constructs a new instance for a given sm URL. + * @deprecated reason this method is deprecated
+ * {will be removed in next version}
+ * use {@link #SMPURLConnectionFactory.getSmp(url)} + * @param url the URL. + */ + @Deprecated + public SMPURLConnection(URL url) { + super(url); + this.smp=SMPURLConnectionFactory.getSmp(url); + } + + /**{@inheritDoc}*/ + @Override + public synchronized void connect() throws IOException { + connected=true; + } + + /**{@inheritDoc}*/ + @Override + public synchronized InputStream getInputStream() throws IOException { + return smp.getInputStream(); + } +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionById.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionById.java new file mode 100644 index 0000000..56bea42 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionById.java @@ -0,0 +1,115 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.apache.commons.codec.binary.Base64; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.service.operation.GetHttpUrl; +import org.gcube.contentmanagement.blobstorage.service.operation.GetUrl; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.Configuration; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.ISClientConnector; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is invoked by the platform with a URL of the smp protocol with new format (>= 2.2.0 version) . +* @author Roberto Cirillo (ISTI-CNR) + * + * Example: smp://data.gcube.org?uhdnfounhcfnshfnrhbvyaeegytf6dfawiuawgcyg + */ +public class SMPURLConnectionById extends SMPConnection { + + private Logger logger= LoggerFactory.getLogger(SMPURLConnectionOld.class); + private String serviceClass="Storage-manager"; + private String serviceName="resolver-uri"; + private String owner="storage-manager"; + + /** + * Constructs a new instance for a given sm URL. + * @param url the URL. + */ + public SMPURLConnectionById(URL url) { + super(url); + } + + public SMPConnection init(URL url){ + return new SMPURLConnectionById(url); + } + + /**{@inheritDoc} + * internal handler implementation + * */ + @Override + public synchronized InputStream getInputStream() throws IOException { + if (!connected) + this.connect(); + try { + return storageClient(this.url.toString()); + } + catch(Exception e) { + IOException ioe = new IOException(); + ioe.initCause(e); + throw ioe; + } + } + + + /** + * Get a StorageManager instance from url + */ + + protected InputStream storageClient(String url) throws Exception { + logger.info("url :" + url); + String [] urlParam=url.split(GetUrl.URL_SEPARATOR); + String protocol=urlParam[0]; + protocol=protocol.substring(0, protocol.length()-1); + logger.debug("protocol is "+protocol); + if(ScopeProvider.instance.get() == null){ + throw new RuntimeException("Scope not set"); + } + String encrypted=retrieveStringEncrypted(urlParam); + MemoryType memory=null; + if(encrypted.contains(GetHttpUrl.VOLATILE_URL_IDENTIFICATOR)){ + memory=MemoryType.VOLATILE; + encrypted=encrypted.replace(GetHttpUrl.VOLATILE_URL_IDENTIFICATOR, ""); + } + logger.debug("String encrypted "+encrypted); + String phrase=retrieveEncryptionPhrase(); + String location=null; + if(Base64.isBase64(encrypted) && (protocol.equalsIgnoreCase("http"))){ + byte[] valueDecoded= Base64.decodeBase64(encrypted); + String encryptedID = new String(valueDecoded); + location=new StringDecrypter("DES", phrase).decrypt(encryptedID); + }else{ + location=new StringDecrypter("DES", phrase).decrypt(encrypted); + } + IClient client=null; + if(memory!=null) + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, memory).getClient(); + else + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED).getClient(); + InputStream is=null; + is=client.get().RFileAsInputStream(location); + return is; + } + + private String retrieveStringEncrypted(String[] urlParam) { + String encrypted=urlParam[3]; + int i=4; + while(i < urlParam.length){ + encrypted=encrypted+GetUrl.URL_SEPARATOR+urlParam[i]; + i++; + } + return encrypted; + } + + + + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionFactory.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionFactory.java new file mode 100644 index 0000000..8dbec52 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionFactory.java @@ -0,0 +1,118 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.contentmanagement.blobstorage.service.operation.GetUrl; +import org.gcube.contentmanager.storageclient.protocol.utils.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Factory class used to determine the right smp url protocol: old format(<2.2.0 version), new format (>= 2.2.0-SNAPSHOT) + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public class SMPURLConnectionFactory { + + static Logger logger=LoggerFactory.getLogger(SMPURLConnectionFactory.class); + + public static SMPConnection getSmp(URL url) { + String urlString=url.toString(); + Utils.checkScopeProvider(); + if(isNewSmpFormat(urlString)|| isIntermediateType(urlString)){ + logger.info("detected new smp format "+url); + SMPConnection connection=load(url); + if (connection!= null) + return connection; + return new SMPURLConnectionById(url); + }else{ + logger.info("detected old smp format "+url); + return new SMPURLConnectionOld(url); + } + } + + + /** + * patch method for a particular smp uri: smp://data.gcube.org/8TFLhJ991DF1M/Ae8rGamC3CgCjAXlnVGmbP5+HKCzc= + * @param urlString + * @return + */ + private static boolean isIntermediateType(String urlString) { + String [] urlParam=urlString.split(GetUrl.URL_SEPARATOR); + String infraHost=urlParam[2]; + if(Utils.isScopeProviderMatch(infraHost)) + return true; + String infra=Utils.getInfraFromResolverHost(infraHost); + String rootScope="/"+infra; + ScopeBean scope=new ScopeBean(rootScope); + if(scope.is(Type.INFRASTRUCTURE)){ + return Utils.validationScope2(rootScope); + }else{ + return false; + } + } + + private static boolean isNewSmpFormat(String urlString) { + logger.debug("check format: "+urlString); + String httpString=urlString; + httpString=httpString.replace("smp://", "http://"); + logger.debug("httpUrl conversion: "+httpString); + URL httpUrl=null; + try { + httpUrl=new URL(httpString); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List services=Utils.queryServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME); + String host=null; + if(services != null && services.size()>0){ + host=Utils.getResolverHost(services.get(0)); + } + logger.debug("uri-resolver host: "+host+" in scope: "+ScopeProvider.instance.get()); + if((host!=null) && (host.equals(httpUrl.getHost()))){ + return true; + }else{ + return false; + } + } + + + /** + * Load a backend driver from classpath + * @param url + * @return + */ + private static SMPConnection load(URL url){ + ServiceLoader loader = ServiceLoader.load(SMPConnection.class); + Iterator iterator = loader.iterator(); + List impls = new ArrayList(); + while(iterator.hasNext()) + impls.add(iterator.next()); + int implementationCounted=impls.size(); +// System.out.println("size: "+implementationCounted); + if(implementationCounted==0){ + logger.info(" 0 implementation found. Load default implementation of SMPConnection"); + return null; + }else if(implementationCounted>0){ + SMPConnection connection = impls.get(0); + logger.info("1 implementation of TransportManager found. "); + connection.init(url); + return connection; + }else{ + return null; + } + } + + + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionOld.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionOld.java new file mode 100644 index 0000000..9888ce9 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPURLConnectionOld.java @@ -0,0 +1,147 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.Configuration; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** +* +* This is invoked by the platform with a URL of the right protocol. +* @author Fabio Simeoni (University of Strathclyde), @author Roberto Cirillo (ISTI-CNR) +* +*/ +public class SMPURLConnectionOld extends SMPConnection{ + + + Logger logger= LoggerFactory.getLogger(SMPURLConnectionOld.class); + /** + * Constructs a new instance for a given sm URL. + * @param url the URL. + */ + public SMPURLConnectionOld(URL url) { + super(url); + } + + public SMPConnection init(URL url){ + return new SMPURLConnectionOld(url); + } + + /**{@inheritDoc}*/ + @Override + public synchronized InputStream getInputStream() throws IOException { + if (!connected) + this.connect(); + try { + return storageClient(this.url.toString()); + } + catch(Exception e) { + IOException ioe = new IOException(); + ioe.initCause(e); + throw ioe; + } + } + + + protected InputStream storageClient(String url) throws Exception { + logger.info("url :" + url); + String [] urlParam=url.split("\\?"); + logger.info("String encrypted "+urlParam[1]); + String param=new StringDecrypter("DES", retrieveEncryptionPhrase()).decrypt(urlParam[1]); + logger.info("String decrypted: "+param); + String [] getParam=param.split("\\&"); + String serviceClass=null; + String serviceName=null; + String owner=null; + String accessType=null; + String memoryType=null; + String scopeType=null; + AccessType type = null; + MemoryType mType = null; + String server= null; + String [] par1; + for(String par : getParam){ + if(par.contains("ServiceClass")){ + par1=par.split("="); + serviceClass=par1[1]; + }else if(par.contains("ServiceName")){ + par1=par.split("="); + serviceName=par1[1]; + }else if(par.contains("owner")){ + par1=par.split("="); + owner=par1[1]; + }else if(par.contains("scope")){ + par1=par.split("="); + scopeType=par1[1]; + }else if(par.contains("server")){ + par1=par.split("="); + server=par1[1]; + }else if(par.contains("AccessType")){ + par1=par.split("="); + accessType=par1[1]; + if(accessType.equalsIgnoreCase("public")){ + type=AccessType.PUBLIC; + }else if(accessType.equalsIgnoreCase("shared")){ + type=AccessType.SHARED; + } + }else if(par.contains("MemoryType")){ + par1=par.split("="); + memoryType=par1[1]; + if(memoryType.equalsIgnoreCase("VOLATILE")){ + mType=MemoryType.VOLATILE; + }else{ + mType=MemoryType.PERSISTENT; + } + } + } + // throw an exception if one or more uri parameters are invalid + if((serviceName==null) || (serviceClass==null) || (owner == null) || (scopeType==null) || (type == null)){ + logger.error("Bad Parameter in URI"); + if (type == null){ + logger.error("URI generated from a private file"); + throw new MalformedURLException("The uri is generated from a private file. It cannot be accessed"); + } + if (serviceName==null) throw new MalformedURLException("The uri generated has an invalid serviceName"); + if (serviceClass==null) throw new MalformedURLException("The uri generated has an invalid serviceClass"); + if (owner==null) throw new MalformedURLException("The uri generated has an invalid owner"); + if (scopeType==null) throw new MalformedURLException("The uri is generated has an invalid scopeType."); + } + String location=extractLocation(urlParam[0]); + logger.info("Parameters from URI "+serviceClass+" "+serviceName+" "+owner+" "+type+" "+mType +" location: "+urlParam[0]+" scope: "+scopeType); + IClient client=null; + String currentScope=ScopeProvider.instance.get(); + logger.info("current scope used: "+currentScope+". scope found on url is: "+ scopeType); +// ScopeProvider.instance.set(scopeType); + if(mType != null){ + if(server!=null) + client=new StorageClient(serviceClass, serviceName, owner, type, mType, server).getClient(); + else + client=new StorageClient(serviceClass, serviceName, owner, type, mType).getClient(); + }else{ + if (server != null) + client=new StorageClient(serviceClass, serviceName, owner, server, type ).getClient(); + else + client=new StorageClient(serviceClass, serviceName, owner, type).getClient(); + } + InputStream is=null; + is=client.get().RFileAsInputStream(location); +// ScopeProvider.instance.set(currentScope); + return is; + } + + private String extractLocation(String url) { + String [] loc=url.split("//"); + logger.info("url extracted: "+loc[1]); + return loc[1]; + } + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPUrl.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPUrl.java new file mode 100644 index 0000000..e674dda --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/SMPUrl.java @@ -0,0 +1,137 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URLConnection; +import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPURLConnectionFactory; + +/** + * Allow to the end user to manage a smp url as an url with a configured stream handler + * @author Roberto Cirillo (ISTI-CNR) + * + */ +@Deprecated +public class SMPUrl extends java.net.URLStreamHandler{ + + private java.net.URL url; + + /** + * map a url string as a normal url + * @param u url string + * @throws MalformedURLException + */ + public SMPUrl(String u) throws MalformedURLException{ + this.url=new java.net.URL(null, u, this); + } + + public URLConnection openConnection() throws IOException{ + return this.openConnection(url); + } + + @Override + protected URLConnection openConnection(java.net.URL u) throws IOException { + if(u.getProtocol().equalsIgnoreCase("smp")){ + return SMPURLConnectionFactory.getSmp(u); + }else{ + return new java.net.URL(null, u.toString()).openConnection(); + } + + } + + public String getHost(){ + return this.url.getHost(); + } + + /** + * Gets the path part of this URL. + * + * @return the path part of this URL, or an + * empty string if one does not exist + * @since 1.3 + */ + public String getPath() { + return url.getPath(); + } + + /** + * Gets the userInfo part of this URL. + * + * @return the userInfo part of this URL, or + * null if one does not exist + * @since 1.3 + */ + public String getUserInfo() { + return url.getUserInfo(); + } + + /** + * Gets the authority part of this URL. + * + * @return the authority part of this URL + * @since 1.3 + */ + public String getAuthority() { + return url.getAuthority(); + } + + /** + * Gets the port number of this URL. + * + * @return the port number, or -1 if the port is not set + */ + public int getPort() { + return url.getPort(); + } + + /** + * Gets the default port number of the protocol associated + * with this URL. If the URL scheme or the URLStreamHandler + * for the URL do not define a default port number, + * then -1 is returned. + * + * @return the port number + * @since 1.4 + */ + public int getDefaultPort() { + return url.getDefaultPort(); + } + + /** + * Gets the protocol name of this URL. + * + * @return the protocol of this URL. + */ + public String getProtocol() { + return url.getProtocol(); + } + + + + /** + * Gets the file name of this URL. + * The returned file portion will be + * the same as getPath(), plus the concatenation of + * the value of getQuery(), if any. If there is + * no query portion, this method and getPath() will + * return identical results. + * + * @return the file name of this URL, + * or an empty string if one does not exist + */ + public String getFile() { + return url.getFile(); + } + + /** + * Gets the anchor (also known as the "reference") of this + * URL. + * + * @return the anchor (also known as the "reference") of this + * URL, or null if one does not exist + */ + public String getRef() { + return url.getRef(); + } + + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringDecrypter.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringDecrypter.java new file mode 100644 index 0000000..603338a --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringDecrypter.java @@ -0,0 +1,118 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.io.UnsupportedEncodingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.KeySpec; +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.DESedeKeySpec; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class can be used to encrypt and decrypt using DES and a given key + * + * @author Javid Jamae @author Roberto Cirillo + * + * + */ +public class StringDecrypter { + + public static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; + public static final String DES_ENCRYPTION_SCHEME = "DES"; + private KeySpec keySpec; + private SecretKeyFactory keyFactory; + private Cipher cipher; + private static final String UNICODE_FORMAT = "UTF8"; + private static final Logger logger = LoggerFactory.getLogger(StringDecrypter.class); + + public StringDecrypter( String encryptionScheme , String phrase) throws EncryptionException{ + if(phrase!=null) + buildEncryption(encryptionScheme, phrase); + else + throw new RuntimeException("No valid encripted phrase discovered on IS"); +// buildEncryption(encryptionScheme, DEFAULT_ENCRYPTION_KEY); + } + + private void buildEncryption(String encryptionScheme, String encryptionKey) + throws EncryptionException { + if ( encryptionKey == null ) + throw new IllegalArgumentException( "encryption key was null" ); + if ( encryptionKey.trim().length() < 24 ) + throw new IllegalArgumentException( + "encryption key was less than 24 characters" ); + + try + { + byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT ); + + if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) ) + { + keySpec = new DESedeKeySpec( keyAsBytes ); + } + else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) ) + { + keySpec = new DESKeySpec( keyAsBytes ); + } + else + { + throw new IllegalArgumentException( "Encryption scheme not supported: " + + encryptionScheme ); + } + + keyFactory = SecretKeyFactory.getInstance( encryptionScheme ); + cipher = Cipher.getInstance( encryptionScheme ); + + } + catch (InvalidKeyException e) + { + throw new EncryptionException( e ); + } + catch (UnsupportedEncodingException e) + { + throw new EncryptionException( e ); + } + catch (NoSuchAlgorithmException e) + { + throw new EncryptionException( e ); + } + catch (NoSuchPaddingException e) + { + throw new EncryptionException( e ); + } + } + + public String decrypt( String encryptedString ) throws EncryptionException + { + logger.trace("decrypting string..."); + if ( encryptedString == null || encryptedString.trim().length() <= 0 ) + throw new IllegalArgumentException( "encrypted string was null or empty" ); + + try + { + SecretKey key = keyFactory.generateSecret( keySpec ); + cipher.init( Cipher.DECRYPT_MODE, key ); + return org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedString, key); + } + catch (Exception e) + { + throw new EncryptionException( e ); + } + } + + + @SuppressWarnings("serial") + public static class EncryptionException extends Exception + { + public EncryptionException( Throwable t ) + { + super( t ); + } + } + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringEncrypter.java b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringEncrypter.java new file mode 100644 index 0000000..3642180 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/model/protocol/smp/StringEncrypter.java @@ -0,0 +1,120 @@ +package org.gcube.contentmanager.storageclient.model.protocol.smp; + +import java.io.UnsupportedEncodingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.KeySpec; +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.DESedeKeySpec; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class can be used to encrypt and decrypt using DES and a given key + * + * @author Javid Jamae @author Roberto Cirillo + * + * + */ +@Deprecated +public class StringEncrypter { + + public static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; + public static final String DES_ENCRYPTION_SCHEME = "DES"; + private KeySpec keySpec; + private SecretKeyFactory keyFactory; + private Cipher cipher; + private static final String UNICODE_FORMAT = "UTF8"; + private static final Logger logger = LoggerFactory.getLogger(StringDecrypter.class); + + public StringEncrypter( String encryptionScheme , String phrase) throws EncryptionException{ + buildEncryption(encryptionScheme, phrase); + } + + public StringEncrypter( String encryptionScheme) throws EncryptionException{ + buildEncryption(encryptionScheme, null); + } + + private void buildEncryption(String encryptionScheme, String encryptionKey) + throws EncryptionException { + if ( encryptionKey == null ) + throw new IllegalArgumentException( "encryption key was null" ); + if ( encryptionKey.trim().length() < 24 ) + throw new IllegalArgumentException( + "encryption key was less than 24 characters" ); + + try + { + byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT ); + + if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) ) + { + keySpec = new DESedeKeySpec( keyAsBytes ); + } + else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) ) + { + keySpec = new DESKeySpec( keyAsBytes ); + } + else + { + throw new IllegalArgumentException( "Encryption scheme not supported: " + + encryptionScheme ); + } + + keyFactory = SecretKeyFactory.getInstance( encryptionScheme ); + cipher = Cipher.getInstance( encryptionScheme ); + + } + catch (InvalidKeyException e) + { + throw new EncryptionException( e ); + } + catch (UnsupportedEncodingException e) + { + throw new EncryptionException( e ); + } + catch (NoSuchAlgorithmException e) + { + throw new EncryptionException( e ); + } + catch (NoSuchPaddingException e) + { + throw new EncryptionException( e ); + } + } + + public String decrypt( String unencryptedString ) throws EncryptionException + { + logger.trace("decrypting string..."); + if ( unencryptedString == null || unencryptedString.trim().length() <= 0 ) + throw new IllegalArgumentException( "encrypted string was null or empty" ); + + try + { + SecretKey key = keyFactory.generateSecret( keySpec ); + cipher.init( Cipher.ENCRYPT_MODE, key ); + return org.gcube.common.encryption.StringEncrypter.getEncrypter().encrypt(unencryptedString, key); + } + catch (Exception e) + { + throw new EncryptionException( e ); + } + } + + + @SuppressWarnings("serial") + public static class EncryptionException extends Exception + { + public EncryptionException( Throwable t ) + { + super( t ); + } + } + +} + diff --git a/src/main/java/org/gcube/contentmanager/storageclient/protocol/utils/Utils.java b/src/main/java/org/gcube/contentmanager/storageclient/protocol/utils/Utils.java new file mode 100644 index 0000000..b7bae6a --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/protocol/utils/Utils.java @@ -0,0 +1,164 @@ +package org.gcube.contentmanager.storageclient.protocol.utils; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.common.scope.impl.ServiceMapScannerMediator; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * + * Utility class for scope identity + * + * @author Roberto Cirillo (ISTI-CNR) + + */ +public class Utils { + + // Temporary CONSTANTS + private static final String GCUBE_RESOLVER_HOST = "data.gcube.org"; + private static final String D4SCIENCE_RESOLVER_HOST = "data.d4science.org"; + private static final String GCUBE_INFRA = "gcube"; + private static final String D4SCIENCE_INFRA = "d4science.research-infrastructures.eu"; + public static final String INFRASTRUCTURE_ENV_VARIABLE_NAME="infrastructure"; + public static final String URI_RESOLVER_RESOURCE_CATEGORY="Service"; + public static final String URI_RESOLVER_RESOURCE_NAME="HTTP-URI-Resolver"; + + private static final Logger logger = LoggerFactory.getLogger(Utils.class); + + + public static boolean validationScope2(String scope){ + ScopeBean scopeBean=new ScopeBean(scope); + if((scopeBean.is(Type.VRE))) + scope=scopeBean.enclosingScope().toString(); + return ServiceMapScannerMediator.isValid(scope); + } + + public static ArrayList getVOScopes(String scope){ + ArrayList vos=new ArrayList(); + ScopeBean scopeBean=new ScopeBean(scope); + //retrieve INFRA scope + while(!scopeBean.is(Type.INFRASTRUCTURE)){ + logger.debug("the scope "+scope+" is not an INFRA scope "); + scopeBean=new ScopeBean(scopeBean.enclosingScope().toString()); + } + scope=scopeBean.toString(); + if(scopeBean.is(Type.INFRASTRUCTURE)){ + Set scopeSet=ServiceMapScannerMediator.getScopeKeySet(); + for(String scopeItem : scopeSet){ + //retrieve all Vo scopes + logger.debug("scope scanned: "+scopeItem); + if(scopeItem.contains(scope) && (new ScopeBean(scopeItem).is(Type.VO))){ + logger.debug("found vo scope: "+scopeItem); + vos.add(scopeItem); + } + } + } + return vos; + } + + public static String extractInfrastructureNewVersion(String urlParam){ + logger.debug("urlParam: "+urlParam); + String infra= checkVarEnv(INFRASTRUCTURE_ENV_VARIABLE_NAME); + if (infra != null) + return infra; + else + //only for test return the infra from the uri. In prod will be throws a runtime exception + return getInfraFromResolverHost(urlParam); + } + + public static String getInfraFromResolverHost(String resolverHost) { + if(resolverHost.equals(GCUBE_RESOLVER_HOST)){ + return GCUBE_INFRA; + }else if(resolverHost.equals(D4SCIENCE_RESOLVER_HOST)){ + return D4SCIENCE_INFRA; + }else return resolverHost; + } + + public static String checkVarEnv(String name){ + Map env = System.getenv(); + TreeSet keys = new TreeSet(env.keySet()); + Iterator iter = keys.iterator(); + String value=null; + while(iter.hasNext()) + { + String key = iter.next(); + if(key.equalsIgnoreCase(name)){ + value=env.get(key); + break; + } + } + return value; + } + + public static boolean isScopeProviderMatch(String infraHost) { + String currentScope=ScopeProvider.instance.get(); + if(currentScope != null){ + //get vo scope + String voScope=Utils.getVOScope(currentScope); + // get the uri resolver host + List services=queryServiceEndpoint(URI_RESOLVER_RESOURCE_CATEGORY, URI_RESOLVER_RESOURCE_NAME); + String host=null; + if(services != null && services.size()>0){ + host=getResolverHost(services.get(0)); + if(host!=null){ + if(infraHost.equalsIgnoreCase(host)){ + return true; + } + } + } + } + return false; + } + + private static String getVOScope(String currentScope) { + ScopeBean scopeBean=new ScopeBean(currentScope); + if((scopeBean.is(Type.VRE))) + currentScope=scopeBean.enclosingScope().toString(); + return currentScope; + } + + public static List queryServiceEndpoint(String category, String name){ + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq '"+category+"' and $resource/Profile/Name eq '"+name+"' "); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + return resources; + } + + /** + * if the scope provider is not set then check if the env variable: SCOPE is set and set the scopeProvider + */ + public static void checkScopeProvider(){ + String scopeProvided=ScopeProvider.instance.get(); + logger.info("check scope: scope found "+scopeProvided); + if ((scopeProvided==null) || (scopeProvided.isEmpty())){ + logger.debug("scope not correctly set on ScopeProvider"); + logger.info("scope: check variable"); + scopeProvided=Utils.checkVarEnv("SCOPE"); + if (scopeProvided != null){ + ScopeProvider.instance.set(scopeProvided); + }else{ + throw new RuntimeException("Scope not set "); + } + } + } + + + public static String getResolverHost(ServiceEndpoint serviceEndpoint) { + return serviceEndpoint.profile().runtime().hostedOn(); + + } + +} diff --git a/src/main/java/org/gcube/contentmanager/storageclient/wrapper/AccessType.java b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/AccessType.java new file mode 100644 index 0000000..0ac48d4 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/AccessType.java @@ -0,0 +1,14 @@ +package org.gcube.contentmanager.storageclient.wrapper; +/** + * define the kind of access to storage manager + * private: The file uploaded are visibility limited at the owner + * shared: the visibility is limited for all user that have the same serviceClass and serviceName + * public: the visibility is limited to all the infrastructured area + * + * @author Roberto Cirillo (ISTI-CNR) + * + */ + +public enum AccessType { + PUBLIC, SHARED, PRIVATE + } \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageclient/wrapper/Configuration.java b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/Configuration.java new file mode 100644 index 0000000..64a005d --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/Configuration.java @@ -0,0 +1,464 @@ +package org.gcube.contentmanager.storageclient.wrapper; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeSet; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.contentmanagement.blobstorage.service.impl.ServiceEngine; +import org.gcube.contentmanager.storageclient.protocol.utils.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Configuration { + + private String clientID; +// public | private | shared. If shared the rwx permits are extended to all services of the same type + private String typeAccess; + private String memoryType; + private String owner; + private String scopeString; + private String server; + private String environment; + private String sc; + private String sn; + private String user; + private String password; + private String passPhrase; + private ISClientConnector isclient; +// the scope used for discovering the runtimeResource + private String RRScope; + private String backendType; + private String[] volatileHost; + private String[] persistentHosts; + private String resolverHost; + private static final Logger logger = LoggerFactory.getLogger(Configuration.class); + private static final String DEFAULT_BACKEND_TYPE="MongoDB"; + private static final String WRITE_CONCERN_PROPERTY_NAME="write_concern"; + private static final String READ_PREFERENCE_PROPERTY_NAME="read_preference"; + private static final String DB_PROPERTY_NAME="collection"; + private static final String DB_STRING_SEPARATOR=";"; + /** + * home library need a special configuration + */ + private static final String HOME_LIBRARY_SERVICE_CLASS="org.gcube.portlets.user"; + private static final String HOME_LIBRARY_SERVICE_NAME_DEV="test-home-library"; + private static final String HOME_LIBRARY_SERVICE_NAME_PROD="home-library"; + private static final String HL_CONTEXT = "/d4science.research-infrastructures.eu"; + private static final String HL_CONTEXT_DEV = "/gcube"; + + + /** + * Loads all the configuration parameters in a java object + * @param sc: service class of the remote resource + * @param sn service name of the remote resource + * @param scopeString string that identifies the scope + * @param owner user of the client library + * @param clientID + * @param accessType indicates the type of access to the storage + * @param memory indicates the type of memory used by the storage: Persistent or Volatile + */ + public Configuration(String sc, String sn, String scopeString, String owner, String clientID, String accessType, String memory){ + this.sc=sc; + this.sn=sn; + this.owner=owner; + this.clientID=clientID; + this.typeAccess=accessType; + this.memoryType=memory; + setScopeString(scopeString); + } + + /** + * Retrieve a valid configuration from IS for instantiating the engine + */ + public void getConfiguration(){ + String[] newServer=null; +// ISClientConnector isclient=getISClient(); + String currentScope=ScopeProvider.instance.get(); + logger.debug("Scope found on ScopeProvider instance is "+currentScope); + if(RRScope == null){ + if(new ScopeBean(currentScope).is(Type.VRE)){ + logger.debug("If ScopeProvider scope is VRE scope RR scope became VO scope"); + RRScope=new ScopeBean(currentScope).enclosingScope().toString(); + }else{ + logger.debug("If ScopeProvider scope is not a VRE scope RR scope is ScopeProvider scope"); + RRScope=currentScope; + } + } + logger.debug("RuntimeResource scope "+RRScope); + ServiceEndpoint resource=getISClient().getStorageEndpoint(RRScope); + if(resource ==null ) + throw new IllegalStateException("the storage resource is not present on IS in scope: "+RRScope); + List resolverResource =getISClient().getServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME); + if(resolverResource !=null && resolverResource.size()> 0) + setResolverHost(getISClient().getResolverHost(resolverResource.get(0))); + else + throw new IllegalStateException("the uri resolver resource is not present on IS in scope: "+currentScope); + // old method for retrieve hostedOn field in storage ServiceEndpoint resource + if(server==null){ + logger.debug("server not set. Try to query IS in scope: "+scopeString); + String[] serverFound=checkVarEnvMongo(); + if(serverFound==null){ + serverFound=getISClient().retrieveConnectionInfo(resource); + setUser(getISClient().getUsername()); + setPassword(getISClient().password); + setBackendType(getISClient().getBackendType(resource)); + }else{ + setBackendType(checkVarEnvBackendType()); + if(getBackendType() == null) setBackendType(DEFAULT_BACKEND_TYPE); + setUser(checkVarEnvUser()); + setPassword(checkVarEnvPassword()); + } + newServer=serverFound; + }else{ + logger.debug("server found: "+server); + String[] serverPassed={server}; + newServer=serverPassed; + if(getBackendType() == null) setBackendType(DEFAULT_BACKEND_TYPE); + } + if(newServer==null){ + throw new IllegalStateException("Resource not found on Information System"); + }else{ + setEnvironment(setAreaStorage(getSc(), getSn())); + setServerHosts(newServer, isclient, resource); + try { + setPassPhrase(retrieveEncryptionPhrase()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private void setServerHosts(String[] newServer, ISClientConnector isclient, ServiceEndpoint resource) { + if((getMemoryType() != null) && ((getMemoryType().equals(MemoryType.VOLATILE.toString()) || (getMemoryType().equals(MemoryType.BOTH.toString()))))){ + setVolatileHost(isclient.getVolatileHost(resource)); + logger.info("backend host is the volatile server"+volatileHost[0]); + } + setPersistentHosts(newServer); + } + + + private String setAreaStorage(String sc, String sn) { + String area=null; + if(isHomeLibrary(sc, sn)) + return getHomeLibraryContext(); + if((memoryType != null) && (memoryType.equals(MemoryType.VOLATILE.toString()))){ + area="VOLATILE"+scopeString+clientID; + }else{ + area=scopeString+clientID; + } + return area; + } + + private boolean isHomeLibrary(String sc, String sn){ + if(((sc.equals(HOME_LIBRARY_SERVICE_CLASS) && sn.equals(HOME_LIBRARY_SERVICE_NAME_DEV)))||((sc.equals(HOME_LIBRARY_SERVICE_CLASS) && sn.equals(HOME_LIBRARY_SERVICE_NAME_PROD)))) + return true; + return false; + } + + private String getHomeLibraryContext(){ + String area=null; + String scope=ScopeProvider.instance.get(); + String context=null; + if (scope.startsWith("/gcube")) + context= HL_CONTEXT_DEV; + else if(scope.startsWith("/d4science.research-infrastructures.eu")) + context=HL_CONTEXT; + else{ + throw new RuntimeException("Unrecognized scope: "+scope); + } + area=context+clientID; + if((getMemoryType() != null) && (getMemoryType().equals(MemoryType.VOLATILE.toString()))) + area="VOLATILE"+area; + return area; + } + + + protected void getOptionalPropertiesFromRR( String currentScope, ServiceEngine engine) { + String write=null; + String read=null; + String [] dbNames=null; + String dbString=null; + // check optional properties only if it is not a volatile storage instance + if((getMemoryType() != null) && (!(getMemoryType().equals(MemoryType.VOLATILE.toString())))){ + write=getISClient().retrievePropertyValue(WRITE_CONCERN_PROPERTY_NAME, currentScope); + logger.debug("read preference: read from service endpoint"); + read=getISClient().retrievePropertyValue(READ_PREFERENCE_PROPERTY_NAME, currentScope); + logger.debug(" write preference: read from service endpoint"); + dbString=getISClient().retrievePropertyValue(DB_PROPERTY_NAME, currentScope); + if((write!=null) && (read!=null)){ + engine.setWriteConcern(write); + engine.setReadConcern(read); + } + if(dbString!=null){ + if (dbString.contains(DB_STRING_SEPARATOR)){ + logger.debug("more than one collection read from ServiceEnpoint"); + dbNames=dbString.split(DB_STRING_SEPARATOR); + }else{ + dbNames= new String[]{dbString}; + } + if (dbNames.length > 1) + logger.debug("multiple collection discovered"); + engine.setDbNames(dbNames); + } + } + } + + /** + * Check environmental variable called : "STORAGE_MANAGER_MONGO_SERVER" for retrieving server list + * @return + */ + private static String[] checkVarEnvMongo(){ + Map env = System.getenv(); + TreeSet keys = new TreeSet(env.keySet()); + + Iterator iter = keys.iterator(); + String server=null; + while(iter.hasNext()) + { + String key = iter.next(); + if(key.equalsIgnoreCase("STORAGE_MANAGER_MONGO_SERVER")){ + server=env.get(key); + break; + } + } + if(server!=null){ + String [] servers={server}; + return servers; + } + return null; + } + + /** + * Check environmental variable called : "STORAGE_MANAGER_BACKEND_TYPE" for retrieving server list + * @return + */ + private static String checkVarEnvBackendType(){ + Map env = System.getenv(); + TreeSet keys = new TreeSet(env.keySet()); + + Iterator iter = keys.iterator(); + String type=null; + while(iter.hasNext()) + { + String key = iter.next(); + if(key.equalsIgnoreCase("STORAGE_MANAGER_BACKEND_TYPE")){ + type=env.get(key); + break; + } + } + if(type!=null){ + return type; + } + return null; + } + + /** + * Check environmental variable called : "STORAGE_MANAGER_USER" for retrieving server list + * @return + */ + private static String checkVarEnvUser(){ + Map env = System.getenv(); + TreeSet keys = new TreeSet(env.keySet()); + + Iterator iter = keys.iterator(); + String type=null; + while(iter.hasNext()) + { + String key = iter.next(); + if(key.equalsIgnoreCase("STORAGE_MANAGER_USER")){ + type=env.get(key); + break; + } + } + if(type!=null){ + return type; + } + return null; + } + + /** + * Check environmental variable called : "STORAGE_MANAGER_PASSWORD" for retrieving server list + * @return + */ + private static String checkVarEnvPassword(){ + Map env = System.getenv(); + TreeSet keys = new TreeSet(env.keySet()); + + Iterator iter = keys.iterator(); + String type=null; + while(iter.hasNext()) + { + String key = iter.next(); + if(key.equalsIgnoreCase("STORAGE_MANAGER_PASSWORD")){ + type=env.get(key); + break; + } + } + if(type!=null){ + return type; + } + return null; + } + + + public String retrieveEncryptionPhrase() throws Exception { + String currentScope=ScopeProvider.instance.get(); + logger.debug("retrieve encryption prhase on scope: "+currentScope); + String encryptedKey=null; +// ISClientConnector isclient=getISClient(); + logger.info("retrieve encryption phrase from scope "+currentScope); + encryptedKey=getISClient().retrievePropertyValue("PassPhrase", currentScope); + logger.info("encrypted prhase is "+encryptedKey); + String decryptString=org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey); + return decryptString; + } + + + public String getClientID() { + return clientID; + } + + public void setClientID(String clientID) { + this.clientID = clientID; + } + + public String getTypeAccess() { + return typeAccess.toString(); + } + + public void setTypeAccess(String typeAccess) { + this.typeAccess = typeAccess; + } + + public String getMemoryType() { + return memoryType.toString(); + } + + public void setMemoryType(String memoryType) { + this.memoryType = memoryType; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public String getServer() { + return server; + } + + public void setServer(String server) { + this.server = server; + } + + public String getEnvironment() { + return environment; + } + + public void setEnvironment(String server) { + this.environment = server; + } + + public String getScopeString() { + return scopeString; + } + + public void setScopeString(String scopeString) { + this.scopeString = scopeString; + } + + public String getRRScope() { + return RRScope; + } + + public void setRRScope(String rRScope) { + RRScope = rRScope; + } + + public String getBackendType() { + return backendType; + } + + public void setBackendType(String backendType) { + this.backendType = backendType; + } + + public String[] getVolatileHost() { + return volatileHost; + } + + public void setVolatileHost(String[] volatileHost) { + this.volatileHost = volatileHost; + } + + public String[] getPersistentHosts() { + return persistentHosts; + } + + public void setPersistentHosts(String[] hosts) { + this.persistentHosts = hosts; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPassPhrase() { + return passPhrase; + } + + public void setPassPhrase(String passPhrase) { + this.passPhrase = passPhrase; + } + + public String getResolverHost() { + return resolverHost; + } + + public void setResolverHost(String resolverHost) { + this.resolverHost = resolverHost; + } + + public ISClientConnector getISClient(){ + if (isclient == null) + isclient=new ISClientConnector(); + return isclient; + } + + public String getSc() { + return sc; + } + + public void setSc(String sc) { + this.sc = sc; + } + + public String getSn() { + return sn; + } + + public void setSn(String sn) { + this.sn = sn; + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageclient/wrapper/ISClientConnector.java b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/ISClientConnector.java new file mode 100644 index 0000000..1625ce2 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/ISClientConnector.java @@ -0,0 +1,234 @@ +package org.gcube.contentmanager.storageclient.wrapper; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Define the queries for IS-Collector service + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public class ISClientConnector { + /** + * Logger for this class + */ + private static final Logger logger = LoggerFactory.getLogger(ISClientConnector.class); + private String[] server; + private String backendType; + private String[] volatileHost; + protected String username; + protected String password; + protected ServiceEndpoint storageResource; + private static HashMap isCache; + + public ISClientConnector(){ + } + + public String[] retrieveConnectionInfo(ServiceEndpoint resource){ + return fillConnectionFields(resource); + } + + public List getServiceEndpoint(String category, String name){ + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq '"+category+"' and $resource/Profile/Name eq '"+name+"' "); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + return resources; + } + + public ServiceEndpoint getStorageEndpoint(String scope) { + //if the serviceEndpoint has been already discovered and selected, I'm going to use that + // otherwise I'm going to discovered it from IS + if(getStorageResource() == null){ + logger.debug("discovering service endpoint"); + String savedScope=null; + if(scope!=null){ + savedScope=ScopeProvider.instance.get(); + logger.debug("set scopeProvider to scope "+scope+" scope provider scope is "+savedScope); + ScopeProvider.instance.set(scope); + } + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'DataStorage' and $resource/Profile/Name eq 'StorageManager' "); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + if(scope!=null){ + logger.debug("reset scopeProvider to scope "+scope); + ScopeProvider.instance.set(savedScope); + } + if (resources.size()>0){ + ServiceEndpoint storageResource = getPriorityResource(resources); + setStorageResource(storageResource); + return storageResource; + }else + throw new RuntimeException("Storage ServiceEndpoint not found under scope: "+scope); + }else{ + logger.debug("service endpoint already discovered"); + return getStorageResource(); + } + } + + + private String[] fillConnectionFields(ServiceEndpoint resource) { + if(resource!=null){ + String [] server=new String[resource.profile().accessPoints().size()]; + int i=0; + for (AccessPoint ap:resource.profile().accessPoints()) { + if (ap.name().equals("server"+(i+1))) { + server[i] = ap.address(); + // if presents, try to get user and password + setUsername(ap.username()); + // set password default value to empty string + setPassword(""); + if(getUsername() != null && getUsername().length() > 0){ + try { + setPassword(StringEncrypter.getEncrypter().decrypt(ap.password())); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + i++; + } + } + setBackendType(retrievePropertyValue(resource, "type")); + String [] volatileHost= new String [1]; + volatileHost[0]=retrievePropertyValue(resource, "volatile"); + setVolatileHost(volatileHost); + logger.info("Type of backend found in RR is "+backendType); + return server; + + }else{ + throw new IllegalStateException("Runtime Resource found are more than 1 but all without default priority setted"); + } + } + + private ServiceEndpoint getPriorityResource(List resources) { + ServiceEndpoint defaultResource=null; + logger.info("search RR with priority "); + // search RR with property DEFAULT + for (ServiceEndpoint res : resources){ + String priority=retrievePropertyValue(res, "priority"); + if (priority!=null){ + defaultResource=res; + setStorageResource(res); + logger.info("found a RR with priority: "); + break; + } + } + return defaultResource; + } + + public String getBackendType(ServiceEndpoint resource) { + if(getBackendType() !=null) return getBackendType(); + setBackendType(retrievePropertyValue(resource, "type")); + return getBackendType(); + } + + public String[] getVolatileHost(ServiceEndpoint resource) { + if(getVolatileHost() !=null) return getVolatileHost(); + String [] volatileHost= new String[1]; + volatileHost[0] = retrievePropertyValue(resource, "volatile"); + setVolatileHost(volatileHost); + return volatileHost; + } + + + public String retrievePropertyValue(String name, String scope) { + ServiceEndpoint res = getStorageEndpoint(scope); + Iterator it= res.profile().accessPoints().iterator(); + String value=null; + while(it.hasNext()){ + AccessPoint ap=(AccessPoint)it.next(); + Mapmap= ap.propertyMap(); + Property type=map.get(name); + if(type!=null){ + value=type.value(); + if(value!= null) break; + } + + } + return value; + } + + private String retrievePropertyValue(ServiceEndpoint res, String name) { + Iterator it= res.profile().accessPoints().iterator(); + AccessPoint ap=(AccessPoint)it.next(); + Mapmap= ap.propertyMap(); + Property type=map.get(name); + if (type!=null) + return type.value(); + else + return null; + } + + public String getResolverHost(ServiceEndpoint serviceEndpoint) { + return serviceEndpoint.profile().runtime().hostedOn(); + + } + + public String[] getServer() { + return server; + } + + public void setServer(String[] server) { + this.server = server; + } + + public String getBackendType() { + return backendType; + } + + public void setBackendType(String backendType) { + this.backendType = backendType; + } + + public String[] getVolatileHost() { + return volatileHost; + } + + public void setVolatileHost(String[] volatileHost) { + this.volatileHost = volatileHost; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public ServiceEndpoint getStorageResource() { + return storageResource; + } + + public void setStorageResource(ServiceEndpoint storageResource) { + this.storageResource = storageResource; + } + + + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/contentmanager/storageclient/wrapper/MemoryType.java b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/MemoryType.java new file mode 100644 index 0000000..478d46f --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/MemoryType.java @@ -0,0 +1,12 @@ +package org.gcube.contentmanager.storageclient.wrapper; + +/** + * Define the Memory type used for storage backend + * @author Roberto Cirillo (ISTI-CNR) + * + */ + +public enum MemoryType { + PERSISTENT, VOLATILE, BOTH +} + diff --git a/src/main/java/org/gcube/contentmanager/storageclient/wrapper/StorageClient.java b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/StorageClient.java new file mode 100644 index 0000000..2939654 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageclient/wrapper/StorageClient.java @@ -0,0 +1,406 @@ +package org.gcube.contentmanager.storageclient.wrapper; + +//import org.gcube.contentmanagement.blobstorage.resource.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +//import org.gcube.contentmanagement.blobstorage.resource.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.service.impl.ServiceEngine; +import org.gcube.contentmanager.storageclient.protocol.utils.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Define the parameters for invoke the storage-manager-core library + * @author Roberto Cirillo (ISTI-CNR) + * + */ +public class StorageClient { + + private String clientID; +// public | private | shared. If shared the rwx permits are extended to all services of the same type + private AccessType typeAccess; + private MemoryType memoryType; + private String serviceClass; + private String serviceName; + private String owner; + private String server; + private String scopeString; + private String currentScope; + private String backendType; + private String volatileHost; + private String RRScope; + + private static final Logger logger = LoggerFactory.getLogger(StorageClient.class); + private static final String DEFAULT_SERVICE_CLASS="ExternalApplication"; + private static final String DEFAULT_SERVICE_NAME="Default"; + private static final MemoryType DEFAULT_MEMORY_TYPE=MemoryType.PERSISTENT; + + /** + * Constructor without optional argument created for gcube infrastructure internal use + * @param ServiceClass + * @param ServiceName + * @param owner + * @param typeAccess + * @param scope scope identifier + * @param forceScope if true the scope used is the scope specified in scope parameter, else the scope specified in the parameter scope is used only for discovering the RuntimeResource + */ + @Deprecated + public StorageClient(String serviceClass, String serviceName, String owner, AccessType accessType, String scope, boolean forceScope){ + checkScopeProvider(); + if(forceScope){ + this.currentScope=ScopeProvider.instance.get(); + ScopeProvider.instance.set(scope); + this.scopeString=ScopeProvider.instance.get(); + }else{ + this.RRScope=scope; + this.scopeString=ScopeProvider.instance.get(); + } + String id=owner; + this.owner=owner; + if(accessType!=null) + this.typeAccess=accessType; + else throw new RuntimeException("AccessType parameter must be not null"); + this.memoryType=MemoryType.BOTH; + this.serviceClass=serviceClass; + this.serviceName=serviceName; + setClientId(serviceClass, serviceName, id); + + } + + + + /** + * Constructor without optional argument created for gcube infrastructure internal use + * @param ServiceClass + * @param ServiceName + * @param owner + * @param typeAccess + * @param scope + */ + public StorageClient(String serviceClass, String serviceName, String owner, AccessType accessType){ + checkScopeProvider(); + String id=owner; + this.owner=owner; + this.scopeString=ScopeProvider.instance.get(); + if(accessType!=null) + this.typeAccess=accessType; + else throw new RuntimeException("AccessType parameter must be not null"); + this.memoryType=MemoryType.BOTH; + this.serviceClass=serviceClass; + this.serviceName=serviceName; + setClientId(serviceClass, serviceName, id); + + } + + + /** + * Constructor created for external use + * @param owner + * @param typeAccess + * @param memory defines the kind of memory: VOLATILE or PERSISTENT + * @param scope + */ + public StorageClient(String owner, AccessType accessType, MemoryType memory){ + checkScopeProvider(); + String id=owner; + this.owner=owner; + this.scopeString=ScopeProvider.instance.get(); + if(accessType!=null) + this.typeAccess=accessType; + else throw new RuntimeException("AccessType parameter must be not null"); + if(memory!=null) + this.memoryType=memory; + else throw new RuntimeException("MemoryType parameter must be not null"); + this.serviceClass=DEFAULT_SERVICE_CLASS; + this.serviceName=DEFAULT_SERVICE_NAME; + setClientId(serviceClass, serviceName, id); + } + + + /** + * Constructor with optional argument server + * @param ServiceClass + * @param ServiceName + * @param owner + * @param typeAccess + * @param scope + * @param server: define the mongoDBserver + */ + public StorageClient(String serviceClass, String serviceName, String owner, String server, AccessType accessType){ + checkScopeProvider(); + String id=owner; + this.owner=owner; + this.scopeString=ScopeProvider.instance.get(); + if(accessType!=null) + this.typeAccess=accessType; + else throw new RuntimeException("AccessType parameter must be not null"); + this.memoryType=MemoryType.BOTH; + this.serviceClass=serviceClass; + this.serviceName=serviceName; + this.server=server; + this.memoryType=DEFAULT_MEMORY_TYPE; + setClientId(serviceClass, serviceName, id); + } + + + /** + * Constructor with optional argument memoryType + * @param ServiceClass + * @param ServiceName + * @param owner + * @param typeAccess + * @param memory defines the kind of memory: VOLATILE or PERSISTENT + * @param scope + */ + public StorageClient(String serviceClass, String serviceName, String owner, AccessType accessType, MemoryType memory){ + checkScopeProvider(); + String id=owner; + this.owner=owner; + this.scopeString=ScopeProvider.instance.get(); + if(accessType!=null) + this.typeAccess=accessType; + else throw new RuntimeException("AccessType parameter must be not null"); + if(memory!=null) + this.memoryType=memory; + else throw new RuntimeException("MemoryType parameter must be not null"); + this.serviceClass=serviceClass; + this.serviceName=serviceName; + setClientId(serviceClass, serviceName, id); + } + + + + /** + * Constructor with optional arguments server and memory + * @param ServiceClass + * @param ServiceName + * @param owner + * @param typeAccess + * @param memory defines the kind of memory: VOLATILE or PERSISTENT + * @param server: define the mongoDBserver + * @param scope + */ + public StorageClient(String serviceClass, String serviceName, String owner, AccessType accessType, MemoryType memory, String server){ + checkScopeProvider(); + String id=owner; + this.owner=owner; + this.scopeString=ScopeProvider.instance.get(); + if(accessType!=null) + this.typeAccess=accessType; + else throw new RuntimeException("AccessType parameter must be not null"); + if(memoryType!=null) + this.memoryType=memory; + else throw new RuntimeException("MemoryType parameter must be not null"); + this.serviceClass=serviceClass; + this.serviceName=serviceName; + this.server=server; + setClientId(serviceClass, serviceName, id); + } + + + /** + * Get Instance remote client - storage-manager-core + * @return + * @throws IllegalStateException if the resource is not found on the IS + */ + public IClient getClient(){ + Configuration cfg= new Configuration(serviceClass, serviceName, scopeString, owner, clientID, typeAccess.toString(), memoryType.toString()); + cfg.getConfiguration(); + ServiceEngine engine= new ServiceEngine(cfg.getPersistentHosts(), cfg.getVolatileHost(), cfg.getEnvironment(), cfg.getTypeAccess(), cfg.getOwner(), cfg.getMemoryType()); + // set additional fields for the new engine object + engine.setServiceClass(getServiceClass()); + engine.setServiceName(getServiceName()); + engine.setGcubeAccessType(getTypeAccess().toString()); + engine.setBackendType(getBackendType()); + engine.setBackendUser(cfg.getUser()); + engine.setBackendPassword(cfg.getPassword()); + engine.setResolverHost(cfg.getResolverHost()); + if(cfg.getPassPhrase()!=null) + engine.setPassPhrase(cfg.getPassPhrase()); + if(getMemoryType() !=null) + engine.setGcubeMemoryType(getMemoryType().toString()); + engine.setGcubeScope(ScopeProvider.instance.get()); + engine.setOwnerGcube(owner); + cfg.getOptionalPropertiesFromRR( getCurrentScope(), engine); + if(getCurrentScope()!=null) + ScopeProvider.instance.set(getCurrentScope()); + return engine; + } + + + private void setClientId(String serviceClass, String serviceName, String id) { + if(typeAccess == AccessType.PUBLIC){ + clientID=""; + }else if(typeAccess == AccessType.PRIVATE){ + clientID=ServiceEngine.FILE_SEPARATOR+"home"+ServiceEngine.FILE_SEPARATOR+serviceClass+ServiceEngine.FILE_SEPARATOR+serviceName+ServiceEngine.FILE_SEPARATOR+id; + }else if(typeAccess==AccessType.SHARED){ + clientID=ServiceEngine.FILE_SEPARATOR+"home"+ServiceEngine.FILE_SEPARATOR+serviceClass+ServiceEngine.FILE_SEPARATOR+serviceName; + }else{ + throw new IllegalArgumentException("type is not correctly: public, private or shared"); + } + } + + + /** + * if the scope provider is not set then check if the env variable: SCOPE is set and set the scopeProvider + */ + private void checkScopeProvider(){ + String scopeProvided=ScopeProvider.instance.get(); + if (scopeProvided==null){ + scopeProvided=Utils.checkVarEnv("SCOPE"); + if (scopeProvided != null){ + ScopeProvider.instance.set(scopeProvided); + }else{ + throw new RuntimeException("Scope not set "); + } + }else setCurrentScope(scopeProvided); + } + + + + public String getClientID() { + return clientID; + } + + + + public void setClientID(String clientID) { + this.clientID = clientID; + } + + + + public AccessType getTypeAccess() { + return typeAccess; + } + + + + public void setTypeAccess(AccessType typeAccess) { + this.typeAccess = typeAccess; + } + + + + public MemoryType getMemoryType() { + return memoryType; + } + + + + public void setMemoryType(MemoryType memoryType) { + this.memoryType = memoryType; + } + + + + public String getServiceClass() { + return serviceClass; + } + + + + public void setServiceClass(String serviceClass) { + this.serviceClass = serviceClass; + } + + + + public String getServiceName() { + return serviceName; + } + + + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + + + public String getOwner() { + return owner; + } + + + + public void setOwner(String owner) { + this.owner = owner; + } + + + + public String getServer() { + return server; + } + + + + public void setServer(String server) { + this.server = server; + } + + + + public String getScopeString() { + return scopeString; + } + + + + public void setScopeString(String scopeString) { + this.scopeString = scopeString; + } + + + + public String getCurrentScope() { + return currentScope; + } + + + + public void setCurrentScope(String currentScope) { + this.currentScope = currentScope; + } + + + + public String getBackendType() { + return backendType; + } + + + + public void setBackendType(String backendType) { + this.backendType = backendType; + } + + + + public String getVolatileHost() { + return volatileHost; + } + + + + public void setVolatileHost(String volatileHost) { + this.volatileHost = volatileHost; + } + + + + public String getRRScope() { + return RRScope; + } + + + + public void setRRScope(String rRScope) { + RRScope = rRScope; + } + + +} \ No newline at end of file diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/CompressTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/CompressTest.java new file mode 100644 index 0000000..edcfeb5 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/CompressTest.java @@ -0,0 +1,219 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.zip.Deflater; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; +import java.util.zip.Inflater; + +import org.junit.Test; + +public class CompressTest { + + String original="pippo pluto paperino e topolino"; + String compressed; +// private static final String HEX_DIGITS = "0123456789abcdef"; + private static final byte[] HEX_CHAR = new byte[] { '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + + @Test + public void gZip() throws IOException{ + String orig = "550c2612e4b0269cbdf3a53e";//"z7PSlllN48hNMXmVpJK81As3reUmPlPZGmbP5+HKCzc="; + + // compress it + ByteArrayOutputStream baostream = new ByteArrayOutputStream(); + OutputStream outStream = new GZIPOutputStream(baostream); + outStream.write(orig.getBytes("UTF-8")); + outStream.close(); + byte[] compressedBytes = baostream.toByteArray(); // toString not always possible + // uncompress it + InputStream inStream = new GZIPInputStream( + new ByteArrayInputStream(compressedBytes)); + ByteArrayOutputStream baoStream2 = new ByteArrayOutputStream(); + byte[] buffer = new byte[8192]; + int len; + while ((len = inStream.read(buffer)) > 0) { + baoStream2.write(buffer, 0, len); + } + String uncompressedStr = baoStream2.toString("UTF-8"); + + System.out.println("orig: " + orig); + System.out.println("unc: " + uncompressedStr); + } + + + public static final String dumpBytes(byte[] buffer) { + if (buffer == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + sb.setLength(0); + for (int i = 0; i < buffer.length; i++) { + sb.append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4])) + .append((char) (HEX_CHAR[buffer[i] & 0x000F])).append(' '); + } + return sb.toString(); + } + + public static String humanReadableByteCount(long bytes, boolean si) { + int unit = si ? 1000 : 1024; + if (bytes < unit) return bytes + " B"; + int exp = (int) (Math.log(bytes) / Math.log(unit)); + String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); + return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); + } + +// @Test + public void gzip() throws IOException{ + System.out.println("original: "+original); + byte[] compressed=compressToByte(original, "UTF8"); + String fromBytes=new String(compressed, "UTF8"); + System.out.println(" l: "+fromBytes.length()); + System.out.println(" fromBytes: "+fromBytes); + String decompressed= unCompressString(compressed, "UTF8"); + System.out.println("decompressed: "+decompressed); + } + public static byte[] compressToByte(final String data, final String encoding) + throws IOException + { + if (data == null || data.length() == 0) + { + return null; + } + else + { + byte[] bytes = data.getBytes(encoding); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream os = new GZIPOutputStream(baos); + os.write(bytes, 0, bytes.length); + os.close(); + byte[] result = baos.toByteArray(); + return result; + } + } + public static String unCompressString(final byte[] data, final String encoding) + throws IOException + { + if (data == null || data.length == 0) + { + return null; + } + else + { + ByteArrayInputStream bais = new ByteArrayInputStream(data); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + GZIPInputStream is = new GZIPInputStream(bais); + byte[] tmp = new byte[256]; + while (true) + { + int r = is.read(tmp); + if (r < 0) + { + break; + } + buffer.write(tmp, 0, r); + } + is.close(); + + byte[] content = buffer.toByteArray(); + return new String(content, 0, content.length, encoding); + } + } + +// @Test + public void deflatertest(){ + try { + // Encode a String into bytes + String inputString = "blahblahblah"; + System.out.println("inputString: "+inputString+ " length: "+inputString.length()); + byte[] input = inputString.getBytes("UTF8"); + System.out.println("inputBytes "+input.length); + // Compress the bytes + byte[] output = new byte[100]; + Deflater compresser = new Deflater(); + compresser.setStrategy(Deflater.HUFFMAN_ONLY); + compresser.setInput(input); + compresser.finish(); + int compressedDataLength = compresser.deflate(output); + compresser.end(); + System.out.println("compressed "+compresser); + System.out.println("length: "+compresser.toString().length()+" length "+compressedDataLength); + String outString = new String(output); + System.out.println("length: "+outString.length()+" string "+outString); + // Decompress the bytes + Inflater decompresser = new Inflater(); + decompresser.setInput(output, 0, compressedDataLength); + byte[] result = new byte[100]; + int resultLength = decompresser.inflate(result); + decompresser.end(); + + // Decode the bytes into a String + String outputString = new String(result, 0, resultLength, "UTF8"); + System.out.println("output: "+outputString); + } catch(java.io.UnsupportedEncodingException ex) { + // handle + } catch (java.util.zip.DataFormatException ex) { + // handle + } + + } + + +// @Test + public void compressgZipTest() { + try { + System.out.println("original: "+original); + String compressed=compress(original); + System.out.println("compressed: "+compressed); + String decompressed= decompress(compressed); + System.out.println("decompressed: "+decompressed); + } catch (IOException e) { + fail("Not yet implemented"); + } + + } + + public static String compress(String str) throws IOException { + if (str == null || str.length() == 0) { + return str; + } + System.out.println("String length : " + str.length()); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(out); + gzip.write(str.getBytes()); + gzip.close(); + String outStr = out.toString("ISO-8859-1"); +// String outStr = out.toString("UTF-8"); + System.out.println("Output String lenght : " + outStr.length()); + return outStr; + } + + public static String decompress(String str) throws IOException { + if (str == null || str.length() == 0) { + return str; + } + System.out.println("Input String length : " + str.length()); + GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("ISO-8859-1"))); +// GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("UTF-8"))); + BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "ISO-8859-1")); +// BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8")); + String outStr = ""; + String line; + while ((line=bf.readLine())!=null) { + outStr += line; + } + System.out.println("Output String lenght : " + outStr.length()); + return outStr; + } + + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/CopyDirTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/CopyDirTest.java new file mode 100644 index 0000000..3559959 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/CopyDirTest.java @@ -0,0 +1,140 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class CopyDirTest { + + private String owner="rcirillo"; +// local file + private String localPath="src/test/resources/CostaRica1.jpg"; +//remote files + private String remoteOriginalFilePath="/test/img/original.jpg"; + private String remoteOriginalFilePath2="/test/img/original2.jpg"; + private String remoteCopyFilePath="/test/copyImg/img/original.jpg"; + private String remoteCopyFilePath2="/test/copyImg/img/original2.jpg"; +//remote directories + private String remoteOriginalDirPath="/test/img/"; + private String remoteCopyDirPath="/test/copyImg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; +// private String scope="/gcube/devsec";//"/d4science.research-infrastructures.eu/FARM";//"/CNR.it/ISTI";//"/gcube"; // "/d4science.research-infrastructures.eu/FARM/VTI";// + private String serviceClass="JUnitTest-CopyDir"; + private String serviceName="StorageManager"; + + + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + String id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath); + assertNotNull(id); + id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath2); + assertNotNull(id); + client.copyDir().from(remoteOriginalDirPath).to(remoteCopyDirPath); + } + + /** + * Check the integrity of the remote copy folder after the original copy folder's cancellation + * @throws RemoteBackendException + */ + @Test + public void checkOriginalFolderTest() throws RemoteBackendException { + client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.get().LFile(newFilePath).RFile(remoteOriginalFilePath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.removeDir().RDir(remoteOriginalDirPath); + client.get().LFile(newFilePath).RFile(remoteCopyFilePath); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.get().LFile(newFilePath).RFile(remoteCopyFilePath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.copyDir().from(remoteCopyDirPath).to(remoteOriginalDirPath); + + } + + /** + * Check the integrity of the original copy folder after the remote copy folder's cancellation + * @throws RemoteBackendException + */ + @Test + public void checkCopiedFolderTest() throws RemoteBackendException { + client.get().LFile(newFilePath).RFile(remoteCopyFilePath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.get().LFile(newFilePath).RFile(remoteCopyFilePath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.removeDir().RDir(remoteCopyDirPath); + client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.get().LFile(newFilePath).RFile(remoteOriginalFilePath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.copyDir().from(remoteOriginalDirPath).to(remoteCopyDirPath); + } + + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunitLink.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + @After + public void deleteRemoteDir(){ + client.removeDir().RDir(remoteCopyDirPath); + client.removeDir().RDir(remoteOriginalDirPath); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/CopyTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/CopyTest.java new file mode 100644 index 0000000..88a9278 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/CopyTest.java @@ -0,0 +1,224 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class CopyTest { + + + private String owner="rcirillo"; + private String localFrog="src/test/resources/CostaRica1.jpg"; + private String localDog="src/test/resources/dog.jpg"; + private String remoteOriginalFilePath="/test/img/original2.jpg"; + private String remoteCopyPath="/test/copyDir/link.jpg"; + private String absoluteLocalFrog; + private String absoluteLocalDog; + private String newFilePath="src/test/resources"; + private IClient client; + private String serviceClass="CopyTest"; + private String serviceName="StorageManager"; + private String newFilePath2; + private long frogSize; + private long dogSize; + + + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); +// String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); +// System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +// assertNotNull(id); + + } + + /** + * + * @throws RemoteBackendException + */ + @Test + public void removeCopiedFileTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +//first copy + client.copyFile().from(remoteOriginalFilePath).to(remoteCopyPath); + client.get().LFile(newFilePath).RFile(remoteCopyPath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkOriginalFileIsAlive(); +//remove original file + removeRemoteOriginalFile(); + } + + @Test + public void removeOriginalFileTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +//first copy + client.copyFile().from(remoteOriginalFilePath).to(remoteCopyPath); + client.get().LFile(newFilePath).RFile(remoteCopyPath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); +// remove original file + removeRemoteOriginalFile(); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkFileIsAlive(remoteCopyPath); +//remove original file + client.remove().RFile(remoteCopyPath); + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + } + + @Test + public void destinationAlreadyPresentTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); + // put dog in destination location + String previousDestinationId=client.put(false).LFile(absoluteLocalDog).RFile(remoteCopyPath); + System.out.println("id loaded on destination place: "+previousDestinationId); +//first copy (frog) + client.copyFile(true).from(remoteOriginalFilePath).to(remoteCopyPath); +// get new id + String newDestinationId=client.get().LFile(newFilePath).RFile(remoteCopyPath); + System.out.println("new destination id "+newDestinationId); +// check if the id is persisted + assertEquals(previousDestinationId, newDestinationId); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + assertEquals(frogSize, f.length()); + removeLocalFile(newFilePath); + removeLocalFile(newFilePath2); +// remove original file + removeRemoteOriginalFile(); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkFileIsAlive(remoteCopyPath); +//remove original file + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + } + + /** + * Replace a file previously copied by softCopy operation. In this case the file hasn't a payload associated but only a link field + * @throws RemoteBackendException + */ + @Test + public void replaceDestinationTest() throws RemoteBackendException { + // put orignal frog file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +// copy (frog) + client.copyFile(true).from(remoteOriginalFilePath).to(remoteCopyPath); +// get destination id + String newDestinationId=client.get().LFile(newFilePath).RFile(remoteCopyPath); + System.out.println("destination id "+newDestinationId); +// replace destination file with dog + String newId=client.put(true).LFile(absoluteLocalDog).RFile(remoteCopyPath); +// get new destinationid + String newDestinationId2=client.get().LFile(newFilePath2).RFile(remoteCopyPath); + assertEquals(newId, newDestinationId2); + System.out.println("new destination id "+newDestinationId2); + assertEquals(newDestinationId, newDestinationId2); + File f =new File(newFilePath2); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + // check if the remote file is a dog + assertEquals(dogSize, f.length()); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkFileIsAlive(remoteOriginalFilePath); +//remove original file + removeRemoteOriginalFile(); + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + removeLocalFile(newFilePath); + removeLocalFile(newFilePath2); + + } + + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + private void removeLocalFile(String newFilePath){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + File f1=new File(localFrog); + absoluteLocalFrog=f1.getAbsolutePath(); + frogSize=f1.length(); + File f2=new File(localDog); + absoluteLocalDog=f2.getAbsolutePath(); + dogSize=f2.length(); + String dir=new File(absoluteLocalFrog).getParent(); + newFilePath=dir+"/testJunitLink.jpg"; + newFilePath2= dir+"/testJunitLink2.jpg"; + } + + private void checkOriginalFileIsAlive() { + String id; + id=client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + private void checkFileIsAlive(String remotePath) { + String id; + id=client.get().LFile(newFilePath).RFile(remotePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + + private void removeRemoteOriginalFile() { + client.remove().RFile(remoteOriginalFilePath); + List linkList=client.showDir().RDir("test/img"); + assertTrue(linkList.isEmpty()); + } + +// @After + private void removeCopiedFile(String link, String dir) { + System.out.println("remove file at: "+link); + client.remove().RFile(link); + System.out.println("show dir: "+dir); + client.removeDir().RDir(dir); + List list=client.showDir().RDir(dir); + assertTrue(list.isEmpty()); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/CountingTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/CountingTest.java new file mode 100644 index 0000000..0a24c52 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/CountingTest.java @@ -0,0 +1,75 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.Before; +import org.junit.Test; + +public class CountingTest { + + private String owner="daniel.williams";//"scarponi";//"alessandro.pieve";//"roberto.cirillo";//"valentina.marioli";//"roberto.cirillo"; + private String remotePath="/test/img/CostaRica1.jpg"; + private String remoteDir="/test/img/"; +// private String absoluteLocalPath; + private IClient client; +// private String scope="/gcube/devsec";//"/d4science.research-infrastructures.eu"; //"/gcube/devsec/devVRE"; //" "/d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; //"/CNR.it/ISTI";// + private String serviceClass="JUnitTest";//"org.gcube.portlets.user";// + private String serviceName="StorageManager";//"test-home-library";// +// private String id; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.PUBLIC, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void getFolderCount(){ + String folderCount=client.getFolderTotalItems().RDir(remoteDir); + assertNotNull(folderCount); + } + + @Test + public void getFolderVolume(){ + String folderVolume=client.getFolderTotalVolume().RDir(remotePath); + assertNotNull(folderVolume); + } + + @Test + public void getUserCount(){ + String userItems=client.getUserTotalItems(); + assertNotNull(userItems); + System.out.println(owner+" items: "+userItems); + } + + @Test + public void getUserVolume(){ + String userVolume=client.getTotalUserVolume(); + assertNotNull(userVolume); + System.out.println(owner+" volume: "+userVolume); + } + +// @After +// public void sleep(){ +// try { +// Thread.sleep(10000); +// } catch (InterruptedException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/DownloadsTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/DownloadsTest.java new file mode 100644 index 0000000..6c3eea2 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/DownloadsTest.java @@ -0,0 +1,191 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import org.bson.types.ObjectId; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DownloadsTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remotePath="/test/img/CostaRica1.jpg"; + private String remotePath1="/test/img/CostaRica2.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; + private String scope="/d4science.research-infrastructures.eu"; //"/gcube";//"/gcube";//"/d4science.research-infrastructures.eu"; //"/gcube/devsec";//"/d4science.research-infrastructures.eu";//"/d4science.research-infrastructures.eu";//"/d4science.research-infrastructures.eu";// //"/CNR.it";// ///gcube/devsec/devVRE"; //"/CNR.it/ISTI";//"/gcube/devsec/devVRE"; // /d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; //"/CNR.it/ISTI";// + private String serviceClass="JUnitTest"; + private String serviceName="StorageManager"; + private String NotExistingId="5a5c7d1d1b9b060285bbe2bd"; + private String id; + private String id1; + private String encryptedId="OE5tOCtuTUt5eXVNd3JkRDlnRFpDY1h1MVVWaTg0cUtHbWJQNStIS0N6Yz0";//"SG1EN2JFNXIELzZ1czdsSkRIa0Evd3VzcGFHU3J6L2RHbWJQNStIS0N6Yz0";//"OE5tOCtuTUt5eXVNd3JkRDlnRFpDY1h1MVVWaTg0cUtHbWJQNStIS0N6Yz0";//"SThtL0xRU281M2UzY29ldE44SkhkbzVkMlBWVmM4aEJHbWJQNStIS0N6Yz0";//"dExaYzNKelNyQVZMemxpcXplVXYzZGN4OGZTQ2w4aU5HbWJQNStIS0N6Yz0";//"Mm9nc0tZbXR1TVI2cVRxL3pVUElrRXJkbk9vVDY2eEJHbWJQNStIS0N6Yz0";//"FemRmUEFtRGVZMnozcEdBekVHU3E4Skt5dkh2OXJObHFHbWJQNStIS0N6Yz0";//"L0p3OGJjUHhFaEJoTmppdjlsK0l0Z0h1b3VpVlloUzVHbWJQNStIS0N6Yz0";//"NWJTRFdxQkQxclJHV05FbExrRDJjL0g4QTBwSnV1TVdHbWJQNStIS0N6Yz0";//"M2JIM2hqNUNyRkxBdG00cnRaWDBpUGxRTmtVQmtEdXBHbWJQNStIS0N6Yz0";//"lfV6BqnBWUbN5dUiQ6xpkMgI69wEwcm6Ygh60bFzaL3h2Run5e9uZMoTix+ykL5H";//"huivj74/QCHnj376YGe/FicgYHSHcwph7SoMty7FBmAh+80AzGQtOdanne6zJBd5";//"lfV6BqnBWUbN5dUiQ6xpkMgI69wEwcm6Ygh60bFzaL3h2Run5e9uZMoTix+ykL5H";//"bksxMGVWTlZ3WjM5Z1ZXYXlUOUtMZzVSNlBhZXFGb05HbWJQNStIS0N6Yz0";//"bEVydmtsOHhCclZMZGd4cEtnTVQzZXQ5UVNxWHZURGJHbWJQNStIS0N6Yz0";//"bEVydmtsOHhCclZMZGd4cEtnTVQz";//"cHEvek1sbjdBaWJkc0s4SzZpSUJpU0c2ZEgyOEVyUGJHbWJQNStIS0N6Yz0";//"RnpoMy9ZaVRoRkZjbk8ybGx0QnlRR";//"L1pWTlV3ZWxPbHRyMloxZ0JnWUVHdHYvUnZDVHJiMTBHbWJQNStIS0N6Yz0"; + + @Before + public void getClient() throws RemoteBackendException{ +// ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + ScopeProvider.instance.set(scope); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("upload done "); + id1=client.put(true).LFile(absoluteLocalPath).RFile(remotePath1); + System.out.println("upload done "); + assertNotNull(id); + + } + + + @Test + public void getId(){ + String id= client.getId(encryptedId); + System.out.println("id decrypted: "+id); + assertNotNull(id); + assertTrue(ObjectId.isValid(id)); + } + + @Test + public void isPresent(){ + boolean isPresent = client.exist().RFile(remotePath); + assertTrue(isPresent); + } + + + @Test + public void downloadByPath() throws RemoteBackendException { + String idFound=client.get().LFile(newFilePath).RFile(remotePath); + System.out.println("downloadByPath id: "+idFound+" id orig "+id ); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + @Test + public void downloadByPathWithBackendTypeReturned() throws RemoteBackendException { + String id=client.get().LFile(newFilePath).RFile(remotePath, true); + System.out.println("downloadByPath id+backendType: "+id); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + + + @Test + public void downloadByInputStream() throws IOException, InterruptedException { + downloadByIS(id); + downloadByIS(id1); + + } + + + private void downloadByIS(String id) throws FileNotFoundException, IOException, + InterruptedException { + System.out.println("download by InputStream with id: "+id); + InputStream is=client.get().RFileAsInputStream(id); + System.out.println("store in: "+newFilePath); + File file=new File(newFilePath); + FileOutputStream out=new FileOutputStream(file); + byte buf[]=new byte[1024]; + int len=0; + while((len=is.read(buf))>0){ + out.write(buf,0,len); + } + out.close(); + is.close(); + System.out.println("path new File downloaded: "+file.getAbsolutePath()); + assertTrue(file.exists()); + removeLocalFile(); + } + + @Test + public void downloadById() throws RemoteBackendException{ + String idReturned=client.get().LFile(newFilePath).RFileById(id); +// String idReturned=client.get().LFile(newFilePath).RFile("5a54a2cb5aacf53a48803437"); + System.out.println("downloadById id: "+idReturned); +// File f =new File(newFilePath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + @Test + public void downloadByIdNotExistingId() throws RemoteBackendException{ + String idReturned=client.get().LFile(newFilePath).RFileById(NotExistingId); + assertNull(idReturned); + } + + + @Test + public void downloadByPathNotExistingId() throws RemoteBackendException{ + String idReturned=null; + idReturned=client.get().LFile(newFilePath).RFile(NotExistingId); + assertNull(idReturned); + } + + + + + @Test + public void downloadByIdWithBackendTypeReturned() throws RemoteBackendException{ + assertNotNull(id); + String idReturned=client.get().LFile(newFilePath).RFileById(id, true); +// String idReturned=client.get().LFile(newFilePath).RFile(id, true); + System.out.println("downloadById id+backendType: "+idReturned); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + + @After + public void removeRemoteFile() throws RemoteBackendException{ + String id=client.remove().RFile(remotePath); + System.out.println("removeRemotePath id: "+id); + String id1=client.remove().RFile(remotePath1); + System.out.println("removeRemotePath id: "+id1); + List list=client.showDir().RDir("test/img"); + assertTrue(list.isEmpty()); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/Junit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/DuplicateTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/DuplicateTest.java new file mode 100644 index 0000000..81b37dc --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/DuplicateTest.java @@ -0,0 +1,159 @@ +/** + * + */ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.Before; +import org.junit.After; +import org.junit.Test; + +/** + * @author Roberto Cirillo (ISTI-CNR) 2018 + * + */ +public class DuplicateTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remoteOriginalFilePath="/test/img/original2.jpg"; + private String remoteCopyPath="/test/duplicateDir/link.jpg"; +// private String remoteCopyPath2="/test/duplicateDirCopy/link.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; +// private String scope="/gcube/devsec"; //"/d4science.research-infrastructures.eu/gCubeApps";//"/d4science.research-infrastructures.eu/FARM";// "/d4science.research-infrastructures.eu/FARM/VTI";// + private String serviceClass="DuplicateTest"; + private String serviceName="StorageManager"; + private String originalId; + + + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + originalId=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath); + System.out.println("original id "+ originalId); + assertNotNull(originalId); + + } + + /** + * + * @throws RemoteBackendException + */ + @Test + public void duplicateFilePathTest() throws RemoteBackendException { +//first copy + String id=client.duplicateFile().RFile(remoteOriginalFilePath); + System.out.println("new id: "+id); + client.get().LFile(newFilePath).RFileById(id); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + removeDuplicatedFile(id); + checkOriginalFileIsAlive(); +//remove original file +// removeRemoteOriginalFile(); + } + + + @Test + public void duplicateFileIdTest() throws RemoteBackendException { +//first copy + String id=client.duplicateFile().RFileById(originalId); + System.out.println("new id: "+id); + client.get().LFile(newFilePath).RFileById(id); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + removeDuplicatedFile(id); + checkOriginalFileIsAlive(); +//remove original file +// removeRemoteOriginalFile(); + } + +// @Test + public void removeOriginalFileTest() throws RemoteBackendException { +//first copy + client.copyFile().from(remoteOriginalFilePath).to(remoteCopyPath); + client.get().LFile(newFilePath).RFile(remoteCopyPath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); +// remove original file + removeRemoteOriginalFile(); + checkFileIsAlive(remoteCopyPath); +//remove original file + client.remove().RFile(remoteCopyPath); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunitLink.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + private void checkOriginalFileIsAlive() { + String id; + id=client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + private void checkFileIsAlive(String remotePath) { + String id; + id=client.get().LFile(newFilePath).RFile(remotePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + @After + public void removeRemoteOriginalFile() { + client.remove().RFile(remoteOriginalFilePath); +// client.removeDir().RDir("test/img"); + List linkList=client.showDir().RDir("test/img"); + assertTrue(linkList.isEmpty()); + } + + private void removeDuplicatedFile(String link) { + System.out.println("remove file at: "+link); + client.remove().RFile(link); + } + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/FwsQueryTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/FwsQueryTest.java new file mode 100644 index 0000000..3736da5 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/FwsQueryTest.java @@ -0,0 +1,108 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import static org.gcube.resources.discovery.icclient.ICFactory.*; +import java.util.ArrayList; +import java.util.List; +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanager.storageclient.wrapper.ISClientConnector; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class FwsQueryTest { + + String scope="/gcube/devsec"; + + @Before + public void setscope(){ + ScopeProvider.instance.set(scope); + } + +// @Test + public void getServerFws(){ + SimpleQuery query = queryFor(GenericResource.class); + DiscoveryClient client = clientFor(GenericResource.class); + List resources = client.submit(query); + List filteredResources = new ArrayList(); + for (GenericResource gr:resources) { + String serviceName = gr.profile().name(); + if (serviceName.equalsIgnoreCase("MongoDBServer")) { + XPathHelper helper = new XPathHelper(gr.profile().body()); + List list= helper.evaluate("server_list/server/@ip"); + String[] server=new String[list.size()]; + int i=0; + for(String s : list){ + System.out.println("string founded: "+s); + server[i]=s; + i++; + } + break; + } + } + } + + @Test + public void getServerFwsWithoutXPATh(){ + SimpleQuery query = queryFor(GenericResource.class); + DiscoveryClient client = clientFor(GenericResource.class); + List resources = client.submit(query); + List filteredResources = new ArrayList(); + String[] server=new String[3]; + for (GenericResource gr:resources) { + String serviceName = gr.profile().name(); + if (serviceName.equalsIgnoreCase("MongoDBServer")) { + + Element body=gr.profile().body(); + NodeList nodes=body.getChildNodes(); + Node node=nodes.item(0); + NodeList serversNode = node.getChildNodes(); + int i=0; + Node s=serversNode.item(i); + while(s!=null){ + NamedNodeMap maps=s.getAttributes(); + if(maps==null) + break; + Node ip1=maps.getNamedItem("ip"); + server[i]=ip1.getNodeValue(); + i++; + s=serversNode.item(i); + } + + + + for(int j=0; j list=client.showDir().RDir("test/img2"); + assertTrue(list.isEmpty()); + } + + private void print(MyFile f) { + System.out.println("\t name "+f.getName()); + System.out.println("\t size "+f.getSize()); + System.out.println("\t owner "+f.getOwner()); + System.out.println("\t id "+f.getId()); + System.out.println("\t absolute remote path "+f.getAbsoluteRemotePath()); + System.out.println("\t remote path: "+f.getRemotePath()); + System.out.println("\t mimetype: "+f.getMimeType()); + } + + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/HLcheckTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/HLcheckTest.java new file mode 100644 index 0000000..0c44edf --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/HLcheckTest.java @@ -0,0 +1,140 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HLcheckTest { + + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remotePath="/test/imgs/CostaRica1.jpg"; + private String remotePath1="/test/imgs/CostaRica2.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; + private String scope=Costants.DEFAULT_SCOPE_STRING;//"/gcube/devsec";//"/d4science.research-infrastructures.eu";//"/d4science.research-infrastructures.eu";//"/d4science.research-infrastructures.eu";//"/d4science.research-infrastructures.eu";// //"/CNR.it";// ///gcube/devsec/devVRE"; //"/CNR.it/ISTI";//"/gcube/devsec/devVRE"; // /d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; //"/CNR.it/ISTI";// + private String serviceClass="org.gcube.portlets.user";//"JUnitTest"; + private String serviceName="test-home-library";//"StorageManager"; + private String id; + private String id1; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, scope, false).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("upload done "); + id1=client.put(true).LFile(absoluteLocalPath).RFile(remotePath1); + System.out.println("upload done "); + assertNotNull(id); + + } + + @Test + public void downloadByPath() throws RemoteBackendException { + String idFound=client.get().LFile(newFilePath).RFile(remotePath); + System.out.println("downloadByPath id: "+idFound+" id orig "+id ); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + @Test + public void downloadByInputStream() throws IOException, InterruptedException { + downloadByIS(id); + downloadByIS(id1); + + } + + @Test + public void downloadById() throws RemoteBackendException{ +// String idReturned=client.get().LFile(newFilePath).RFileById(id); + String idReturned=client.get().LFile(newFilePath).RFile(id); + System.out.println("downloadById id: "+idReturned); +// File f =new File(newFilePath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + + + + @After + public void removeRemoteFile() throws RemoteBackendException{ + String id=client.remove().RFile(remotePath); + System.out.println("removeRemotePath id: "+id); + String id1=client.remove().RFile(remotePath1); + System.out.println("removeRemotePath id: "+id1); + List list=client.showDir().RDir("test/imgs"); + for(StorageObject obj : list){ + System.out.println("found "+obj.getName()); + } + assertTrue(list.isEmpty()); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/Junit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + private void downloadByIS(String id) throws FileNotFoundException, IOException, + InterruptedException { + System.out.println("download by InputStream with id: "+id); + InputStream is=client.get().RFileAsInputStream(id); + System.out.println("store in: "+newFilePath); + File file=new File(newFilePath); + FileOutputStream out=new FileOutputStream(file); + byte buf[]=new byte[1024]; + int len=0; + while((len=is.read(buf))>0){ + out.write(buf,0,len); + } + out.close(); + is.close(); + System.out.println("path new File downloaded: "+file.getAbsolutePath()); + assertTrue(file.exists()); + removeLocalFile(); + } +} + + + + + diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/LinkTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/LinkTest.java new file mode 100644 index 0000000..a45f960 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/LinkTest.java @@ -0,0 +1,152 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.Before; +import org.junit.Test; + +public class LinkTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remoteOriginalFilePath="/test/img3/original.jpg"; + private String remoteOriginalDirPath="/test/img3"; + private String remoteLinkPath="/test/linkDir2/link.jpg"; + private String remoteLinkPath2="/test/linkDirCopy2/link.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; +// private String scope="/gcube/devsec";//"/CNR.it/ISTI";//"/gcube"; // "/d4science.research-infrastructures.eu/FARM/VTI";// + private String serviceClass="JUnitTest-linkTest"; + private String serviceName="StorageManager"; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + String id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath); + assertNotNull(id); + + } + + /** + * 2 links creation from a file + * @throws RemoteBackendException + */ + @Test + public void linkTest() throws RemoteBackendException { +//first Link creation + client.linkFile().from(remoteOriginalFilePath).to(remoteLinkPath); + client.get().LFile(newFilePath).RFile(remoteLinkPath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(f.getAbsolutePath()); + //second link creation + client.linkFile().from(remoteLinkPath).to(remoteLinkPath2); + client.get().LFile(newFilePath).RFile(remoteLinkPath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLinkFiles(); +// removeOriginalFiles(); + removeLocalFile(f.getAbsolutePath()); + } + + +// @Test + public void removeLinkFiles() throws RemoteBackendException{ +// remove first link + removeLink(remoteLinkPath, "test/linkDir2/"); + checkOriginalFileIsAlive(); +// remove second link + removeLink(remoteLinkPath2, "test/linkDirCopy/"); + checkOriginalFileIsAlive(); +//remove original file + removeRemoteOriginalFile(); + } + + +// @Test + public void removeOriginalFiles() throws RemoteBackendException{ +// creation files + linkTest(); +// remove original file +// removeRemoteOriginalFile(); + removeRemoteOriginalDir(); + checkLinkFileIsAlive(); +// remove first link + removeLink(remoteLinkPath, "test/linkDir/"); +// remove second link + removeLink(remoteLinkPath2, "test/linkDirCopy/"); + } + + + + private void removeLocalFile(String filePath){ + File f=new File(filePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunitLink.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + private void checkOriginalFileIsAlive() { + String id; + id=client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(newFilePath); + } + + private void checkLinkFileIsAlive() { + String id=client.get().LFile(newFilePath).RFile(remoteLinkPath); + System.out.println("id link is alive: "+id); + assertNotNull(id); + removeLocalFile(newFilePath); + } + + private void removeRemoteOriginalFile() { + client.remove().RFile(remoteOriginalFilePath); + List linkList=client.showDir().RDir("test/img3"); + assertTrue(linkList.isEmpty()); + } + + private void removeRemoteOriginalDir() { + client.removeDir().RDir(remoteOriginalDirPath); + } + + private void removeLink(String link, String dir) { + System.out.println("remove link file at: "+link); + client.remove().RFile(link); + System.out.println("show dir: "+dir); + List list=client.showDir().RDir(dir); + assertTrue(list.isEmpty()); + } + + +} \ No newline at end of file diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/LockTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/LockTest.java new file mode 100644 index 0000000..a2cbbaf --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/LockTest.java @@ -0,0 +1,110 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LockTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica.jpg"; + private String localPath2="src/test/resources/dog.jpg"; + private String localPathDownload="src/test/resources/download.png"; + private String remotePath="/test/test4-resources/CostaRica1.jpg"; + private String absoluteLocalPath; + private String absoluteLocalPath2; + private String absoluteLocalPathDownload; + private String newFilePath="src/test/resources"; + private String remoteDirPath= "test/test4-resources"; + private IClient client; +// private String scope="/gcube/devsec";//"/CNR.it/ISTI";///gcube/devsec"; + private String serviceClass="JUnitTest-LockTest"; + private String serviceName="StorageManager"; + + @Before + public void getClient(){ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + //upload costaRica + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("UploadByPath id: "+id); + assertNotNull(id); + } + + @Test + public void lockTest(){ + //download & lock + String idLock=client.lock().LFile(absoluteLocalPathDownload).RFile(remotePath); + System.out.println("locked "+remotePath+" with id : "+idLock); + try{ + client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + }catch(Exception e){ + assertNotNull(e); + } + //upload & unlock + client.unlock(idLock).LFile(absoluteLocalPath2).RFile(remotePath); + //download + client.get().LFile(absoluteLocalPathDownload).RFile(remotePath); + // delete + removeRemoteFile(); + + } + + @After + public void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + + private void removeRemoteFile() throws RemoteBackendException{ + + List list = printDir(remoteDirPath); + client.remove().RFile(remotePath); + list = printDir(remoteDirPath); + assertTrue(list.isEmpty()); + } + + private List printDir(String dir) { + List list=client.showDir().RDir(dir); + for(StorageObject obj : list){ + System.out.println("found "+obj.getName()+" and id " +obj.getId()); + } + return list; + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + absoluteLocalPath2=new File(localPath2).getAbsolutePath(); + absoluteLocalPathDownload=new File(localPathDownload).getAbsolutePath(); + } + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/MoveDirTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/MoveDirTest.java new file mode 100644 index 0000000..f061dd4 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/MoveDirTest.java @@ -0,0 +1,118 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class MoveDirTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remoteOriginalFilePath="/test/move/img/original.jpg"; + private String remoteOriginalFilePath2="/test/move/img/original2.jpg"; + private String remoteMovePath="/test/move/"; + private String remoteMovePath2="/test/trash/"; + private String remoteMoveFilePath="/test/trash/move/img/original.jpg"; + private String remoteMoveFilePath2="/test/trash/move/img/original2.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; +// private String scope="/gcube/devsec";//"/d4science.research-infrastructures.eu";//"/CNR.it/ISTI";//"/gcube"; // "/d4science.research-infrastructures.eu/FARM/VTI";// + private String serviceClass="JUnitTest-MoveDir"; + private String serviceName="StorageManager"; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + String id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath); + assertNotNull(id); + id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath2); + assertNotNull(id); + id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath2+"_1"); + assertNotNull(id); + id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath2+"_2"); + assertNotNull(id); + id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath2+"_3"); + assertNotNull(id); + id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath2+"_4"); + assertNotNull(id); + } + + + @Test + public void moveTest() throws RemoteBackendException { + System.out.println("start move"); + long time=System.currentTimeMillis(); +//first moved operation + client.moveDir().from(remoteMovePath).to(remoteMovePath2); + long totalTime=System.currentTimeMillis()-time; + System.out.println("total Time "+totalTime); + checkOriginalFileIsAlive(remoteOriginalFilePath); + checkOriginalFileIsAlive(remoteOriginalFilePath2); + client.get().LFile(newFilePath).RFile(remoteMoveFilePath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + client.get().LFile(newFilePath).RFile(remoteMoveFilePath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunitMoveOp.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + private void checkOriginalFileIsAlive(String remoteOriginalFilePath) { + String id=null; + try{ + id=client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + }catch(RemoteBackendException e ){} + assertNull(id); + } + + private void checkMoveFileIsAlive() { + String id=client.get().LFile(newFilePath).RFile(remoteMovePath); + System.out.println("id link is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + @After + public void removeRemoteDirs(){ + client.removeDir().RDir(remoteMovePath2); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/MoveTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/MoveTest.java new file mode 100644 index 0000000..c2d83ed --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/MoveTest.java @@ -0,0 +1,108 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.Before; +import org.junit.Test; + +public class MoveTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remoteOriginalFilePath="/test/new/original.jpg"; + private String remoteMovePath="/moveDirFailTest/link.jpg"; + private String remoteMovePath2="/moveDirCopy/link.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources/test"; + private IClient client; +// private String scope="/gcube/devsec"; //"/d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; ////"/CNR.it/ISTI";//"/gcube"; + private String serviceClass="JUnitTest-Move"; + private String serviceName="StorageManager"; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + String id=client.put(true).LFile(absoluteLocalPath).RFile(remoteOriginalFilePath); + assertNotNull(id); + } + + + @Test + public void moveTest() throws RemoteBackendException { +//first moved operation +// client.copyFile().from(remoteOriginalFilePath).to(remoteMovePath); + client.moveFile().from(remoteOriginalFilePath).to(remoteMovePath); +// client.moveFile().from("/test/img/").to(remoteMovePath); + client.get().LFile(newFilePath).RFile(remoteMovePath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + checkOriginalFileIsAlive(); + removeLocalFile(); + //second file moved + client.moveFile().from(remoteMovePath).to(remoteMovePath2); + client.get().LFile(newFilePath).RFile(remoteMovePath2); + f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + checkMoveFileIsAlive(remoteMovePath2); + removeLocalFile(); + removeRemoteFile(); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunitMoveOp.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + private void checkOriginalFileIsAlive() { + String id=null; + try{ + id=client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + System.out.println("id: "+id); + }catch(Exception e ){} + assertNull(id); + } + + private void removeRemoteFile() { + client.remove().RFile(remoteMovePath2); + + + } + + private void checkMoveFileIsAlive(String remoteMovePath) { + String id=client.get().LFile(newFilePath).RFile(remoteMovePath); + System.out.println("id link is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/PropertiesTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/PropertiesTest.java new file mode 100644 index 0000000..b4557f7 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/PropertiesTest.java @@ -0,0 +1,129 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.MyFile; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class PropertiesTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remotePath="/test/mimeTest/CostaRica1.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; + private String scope="/gcube/devsec"; //"/d4science.research-infrastructures.eu"; ///gcube/devsec/devVRE"; //"/CNR.it/ISTI";//"/gcube/devsec/devVRE"; // /d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; //"/CNR.it/ISTI";// + private String serviceClass="JUnitTest"; + private String serviceName="StorageManager"; + private String id; + private String field="mimetype"; + private String value="image/png"; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.PUBLIC, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + assertNotNull(id); + } + + + @Test + public void getPropertyByPath() throws RemoteBackendException { + String valueFound= client.getMetaInfo(field).RFile(remotePath); + System.out.println("value found for property: "+field+" is "+valueFound); + MyFile f= client.getMetaFile().RFile(remotePath); + assertNotNull(f); + print(f); + String result= client.setMetaInfo(field, value).RFile(remotePath); + System.out.println("new property set "); + MyFile f1= client.getMetaFile().RFile(remotePath); + print(f1); + assertNotNull(f1); + assertEquals(f1.getMimeType(), value); + + + } + + @Test + public void getPropertyById() throws RemoteBackendException { + String valueFound= client.getMetaInfo(field).RFile(id); + System.out.println("value found for property: "+field+" is "+valueFound); + MyFile f= client.getMetaFile().RFile(id); + assertNotNull(f); + print(f); + String result= client.setMetaInfo(field, value).RFile(id); + System.out.println("new property set "); + MyFile f1= client.getMetaFile().RFile(id); + print(f1); + assertNotNull(f1); + assertEquals(f1.getMimeType(), value); + + } + +// @Test + public void setProperty() throws RemoteBackendException { + String result= client.setMetaInfo(field, value).RFile(id); + MyFile f= client.getMetaFile().RFile(remotePath); + assertNotNull(f); + print(f); + + } + + @After + public void removeRemoteFile() throws RemoteBackendException{ + String id=client.remove().RFile(remotePath); + List list=client.showDir().RDir("test/mimeTest"); + assertTrue(list.isEmpty()); + removeLocalFile(); + } + + private void print(MyFile f) { + System.out.println("\t name "+f.getName()); + System.out.println("\t size "+f.getSize()); + System.out.println("\t owner "+f.getOwner()); + System.out.println("\t id "+f.getId()); + System.out.println("\t absolute remote path "+f.getAbsoluteRemotePath()); + System.out.println("\t remote path: "+f.getRemotePath()); + System.out.println("\t mimetype: "+f.getMimeType()); + System.out.println("\t lastOperation: "+f.getOperation()); + } + + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/RemoveTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/RemoveTest.java new file mode 100644 index 0000000..9b81766 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/RemoveTest.java @@ -0,0 +1,102 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class RemoveTest { + + private String owner="roberto.cirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remotePath="/test/CostaRica1.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; + private String scope="/gcube"; // /d4science.research-infrastructures.eu"; //"/CNR.it/ISTI";// + private String serviceClass="JUnitTest";//"org.gcube.portlets.user";// + private String serviceName="StorageManager";//"home-library"; + private String id; + private String dirToRemove="";//"/Share/fe5ed634-94f1-48a3-be1c-230920399b57/";//"/Home/fabio.sinibaldi/Workspace/Thredds main catalog/";//"/Home/andrea.rossi/Workspace/";//"/Home/gianpaolo.coro/Workspace/Trash/"; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + assertNotNull(id); + + } + +// @Test + public void removeDirTest(){ + client.removeDir().RDir(dirToRemove); + } + + +// @Test + public void removeMultipleFileTest(){ + removeMultipleFile(); + } + + + + /** + * + */ + private void removeMultipleFile() { + client.remove().RFile("5aa16dfe02cadc50bff0eaf1"); +// client.remove().RFile(remotePath2); +// client.remove().RFile(remotePath3); +// client.remove().RFile(remotePath4); +// client.remove().RFile(remotePath5); +// client.remove().RFile(remotePath6); +// client.remove().RFile(remotePath7); +// client.remove().RFile(remotePath8); +// client.remove().RFile(remotePath9); +// client.remove().RFile(remotePath10); + + } + + @After + public void checkRemoveFile() throws RemoteBackendException{ + List list=client.showDir().RDir(dirToRemove); + System.out.println("#files "+list.size()); + assertTrue(list.isEmpty()); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/RemoveVolatileArea.java b/src/test/java/org/gcube/contentmanager/storageclient/test/RemoveVolatileArea.java new file mode 100644 index 0000000..a2bf3e9 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/RemoveVolatileArea.java @@ -0,0 +1,80 @@ +package org.gcube.contentmanager.storageclient.test; + + + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class RemoveVolatileArea { + private String owner="roberto.cirillo"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remotePath="/test/CostaRica1.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private IClient client; + private String scope="/gcube"; //"/d4science.research-infrastructures.eu"; //"/gcube"; // /d4science.research-infrastructures.eu"; //"/CNR.it/ISTI";// + private String serviceClass="org.gcube.portlets.user";//"JUnitTest"; + private String serviceName="test-home-library";//"StorageManager"; + private String id; + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + assertNotNull(id); + + } + + @Test + public void removeVolatile(){ + client.removeDir().RDir("/Home/roberto.cirillo/Workspace/Trash/"); + } + + + + @After + public void checkRemoveFile() throws RemoteBackendException{ + List list=client.showDir().RDir("test/remove"); + System.out.println("#files "+list.size()); + assertTrue(list.isEmpty()); + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + + +} + diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/ReplaceFileTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/ReplaceFileTest.java new file mode 100644 index 0000000..2bbe6c8 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/ReplaceFileTest.java @@ -0,0 +1,153 @@ +/** + * + */ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Roberto Cirillo (ISTI-CNR) 2018 + * + */ +public class ReplaceFileTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica.jpg";//"src/test/resources/empty.txt";//"src/test/resources/CostaRica1.jpg"; + private String localPath1="src/test/resources/dog.jpg"; + private String localPath2="src/test/resources/download.jpg"; + private String remotePath="/tests/CostaRica.jpg";//"/tests/img/CostaRica1.jpg";//"/tests/img/empty.txt";// + private String absoluteLocalPath; + private String absoluteLocalPath1; + private String absoluteLocalPath2; +// private String newFilePath="src/test/resources"; + private IClient client; + private String scope="/gcube";//"/d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; //"/gcube/devsec";//"/d4science.research-infrastructures.eu"; //"/CNR.it";////"/gcube/devsec";//""/CNR.it/ISTI";//"/gcube";//"/gcube/devNext/NextNext";// + private String serviceClass="JUnitTest"; + private String serviceName="StorageManager"; + private String id; + + @Before + public void getClient(){ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + setLocalResources(); + } + + + @Test + public void replaceByPath() throws RemoteBackendException { + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("UploadByPath id: "+id); + client.get().LFile(absoluteLocalPath).RFile(id); + File f=new File(absoluteLocalPath); + assertTrue(f.exists()); + long size=f.length(); + String newId=client.put(true).LFile(absoluteLocalPath1).RFile(remotePath); + System.out.println("UploadByPath again id: "+newId); + System.out.println("download file test by id"); + assertEquals(id, newId); + client.get().LFile(absoluteLocalPath2).RFile(newId); + File f1= new File(absoluteLocalPath2); + assertTrue(f1.exists()); + long size1=f1.length(); + assertNotNull(newId); + assertNotEquals(size, size1); + removeRemoteFile(); + } + + + @Test + public void replaceById1() throws RemoteBackendException { + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("UploadByPath id: "+id); + client.get().LFile(absoluteLocalPath).RFileById(id); + File f=new File(absoluteLocalPath); + assertTrue(f.exists()); + long size=f.length(); + String newId=client.put(true).LFile(absoluteLocalPath1).RFileById(id); + System.out.println("UploadByPath again id: "+newId); + System.out.println("download file test by id"); + assertEquals(id, newId); + client.get().LFile(absoluteLocalPath2).RFile(newId); + File f1= new File(absoluteLocalPath2); + assertTrue(f1.exists()); + long size1=f1.length(); + assertNotNull(newId); + assertNotEquals(size, size1); + removeRemoteFile(); + } + +// +// @Test +// public void replaceById2() throws RemoteBackendException { +// String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); +// System.out.println("UploadByPath id: "+id); +// client.get().LFile(absoluteLocalPath).RFile(id); +// assertTrue(new File(absoluteLocalPath).exists()); +// String newId=client.put(true).LFile(absoluteLocalPath1).RFileById(id); +// System.out.println("UploadByPath again id: "+newId); +// System.out.println("download file test by id"); +// assertEquals(id, newId); +// client.get().LFile(absoluteLocalPath2).RFile(newId); +// assertTrue(new File(absoluteLocalPath2).exists()); +// assertNotNull(id); +// removeRemoteFile(); +// } + + @Test + public void notReplaceById() throws RemoteBackendException { + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("UploadByPath id: "+id); + client.get().LFile(absoluteLocalPath).RFileById(id); + File f=new File(absoluteLocalPath); + assertTrue(f.exists()); + long size=f.length(); + String newId=client.put(false).LFile(absoluteLocalPath1).RFileById(id); + System.out.println("UploadByPath again id: "+newId); + System.out.println("download file test by id"); + assertEquals(id, newId); + client.get().LFile(absoluteLocalPath2).RFile(newId); + File f1= new File(absoluteLocalPath2); + assertTrue(f1.exists()); + long size1=f1.length(); + assertNotNull(newId); + assertEquals(size, size1); + removeRemoteFile(); + } + + + private void removeRemoteFile() throws RemoteBackendException{ + client.removeDir().RDir("tests"); + List list=client.showDir().RDir("tests"); + assertTrue(list.isEmpty()); + } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + absoluteLocalPath1=new File(localPath1).getAbsolutePath(); + absoluteLocalPath2=new File(localPath2).getAbsolutePath(); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/SoftCopyTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/SoftCopyTest.java new file mode 100644 index 0000000..de290cf --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/SoftCopyTest.java @@ -0,0 +1,320 @@ +/** + * + */ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Roberto Cirillo (ISTI-CNR) 2018 + * + */ +public class SoftCopyTest { + + private String owner="rcirillo"; + private String localFrog="src/test/resources/CostaRica1.jpg";//"src/test/resources/testFile.txt";//"src/test/resources/CostaRica1.jpg"; + private String localDog="src/test/resources/dog.jpg"; + private String remoteOriginalFilePath="/test/frog.jpg"; + private String remoteCopyPath="/test/SoftCopy1/frog1.jpg"; + private String remoteCopyPath2="/test/SoftCopy2/frog2.jpg"; + private String remoteCopyPath3="/test/SoftCopy3/frog3.jpg"; + private String remoteCopyDir="/test/copyDir/"; + private String absoluteLocalFrog; + private String absoluteLocalDog; + private String newFilePath="src/test/resources"; + private IClient client; + private String serviceClass="CopyTest"; + private String serviceName="StorageManager"; + private String newFilePath2; + private long frogSize; + private long dogSize; + + + + @Before + public void getClient() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); +// String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); +// System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +// assertNotNull(id); + + } + + /**After a copy the file copied is deleted and a check is performed on the original file + * + * @throws RemoteBackendException + */ + @Test + public void removeCopiedFileTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +//first copy + client.softCopy().from(remoteOriginalFilePath).to(remoteCopyPath); + client.get().LFile(newFilePath).RFile(remoteCopyPath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); + removeCopiedFile(remoteCopyPath, remoteCopyDir); + checkOriginalFileIsAlive(); +//remove original file + removeRemoteOriginalFile(); + } + + + /**After a copy the original file is deleted and a check is performed on the copied file + * + * @throws RemoteBackendException + */ + @Test + public void removeOriginalFileTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +//first copy + client.softCopy().from(remoteOriginalFilePath).to(remoteCopyPath); + client.get().LFile(newFilePath).RFile(remoteCopyPath); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); +// remove original file + removeRemoteOriginalFile(); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkFileIsAlive(remoteCopyPath); +//remove original file + client.remove().RFile(remoteCopyPath); + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + } + + + /**After a copy the original file is deleted and a check is performed on the copied file + * + * @throws RemoteBackendException + */ + @Test + public void copyByIdTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the original path "+remoteOriginalFilePath+" is: "+id ); + assertNotNull(id); +//first copy +// String id2=client.softCopy().from(remoteOriginalFilePath).to(null); + String id2=client.softCopy().from(id).to(null); + System.out.println("id of the copied file without path is: "+id2 ); + assertNotNull(id2); + client.get().LFile(newFilePath).RFile(id2); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + removeLocalFile(); +// remove original file + removeRemoteOriginalFile(); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); +// checkFileIsAlive(remoteCopyPath); + checkFileIsAlive(id2); +//remove original file +// client.remove().RFile(remoteCopyPath); +// client.remove().RFile(id2); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + removeCopiedFile(id2, "test/copyDir/"); + } + + + /** + * Copy operation when the destination file is already present. + * In this test there is a check on the id of the destination file. It should be the same of the previous file located there + * @throws RemoteBackendException + */ + @Test + public void destinationAlreadyPresentTest() throws RemoteBackendException { + // put orignal file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); + // put dog in destination location + String previousDestinationId=client.put(true).LFile(absoluteLocalDog).RFile(remoteCopyPath); + System.out.println("id loaded on destination place: "+previousDestinationId); +//first copy (frog) + client.softCopy(true).from(remoteOriginalFilePath).to(remoteCopyPath); +// get new id + String newDestinationId=client.get().LFile(newFilePath).RFile(remoteCopyPath); + System.out.println("new destination id "+newDestinationId); +// check if the id is persisted + assertEquals(previousDestinationId, newDestinationId); + File f =new File(newFilePath); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + assertEquals(frogSize, f.length()); + removeLocalFile(newFilePath); + removeLocalFile(newFilePath2); +// remove original file + removeRemoteOriginalFile(); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkFileIsAlive(remoteCopyPath); +//remove original file + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + } + + + /** + * Replace a file previously copied by softCopy operation. In this case the file hasn't a payload associated but only a link field + * @throws RemoteBackendException + */ + @Test + public void replaceDestinationTest() throws RemoteBackendException { + // put orignal frog file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +// copy (frog) + client.softCopy(true).from(remoteOriginalFilePath).to(remoteCopyPath); +// get destination id + String newDestinationId=client.get().LFile(newFilePath).RFile(remoteCopyPath); + System.out.println("destination id "+newDestinationId); +// replace destination file with dog + String newId=client.put(true).LFile(absoluteLocalDog).RFile(remoteCopyPath); +// get new destinationid + String newDestinationId2=client.get().LFile(newFilePath2).RFile(remoteCopyPath); + assertEquals(newId, newDestinationId2); + System.out.println("new destination id "+newDestinationId2); + assertEquals(newDestinationId, newDestinationId2); + File f =new File(newFilePath2); + System.out.println("path new File downloaded: "+f.getAbsolutePath()); + assertTrue(f.exists()); + // check if the remote file is a dog + assertEquals(dogSize, f.length()); +// removeCopiedFile(remoteCopyPath, "test/copyDir/"); + checkFileIsAlive(remoteOriginalFilePath); +//remove original file + removeRemoteOriginalFile(); + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + removeLocalFile(newFilePath); + removeLocalFile(newFilePath2); + + } + + @Test + public void copyAnAlreadyCopiedFile() throws RemoteBackendException { + // put orignal frog file + String id=client.put(true).LFile(absoluteLocalFrog).RFile(remoteOriginalFilePath); + System.out.println("id of the following path "+remoteOriginalFilePath+" is: "+id ); +// copy (frog) + String idCopy=client.softCopy(true).from(remoteOriginalFilePath).to(remoteCopyPath); + System.out.println("copied file id: "+idCopy); +// new copy on frog + String idCopy2=client.softCopy(true).from(remoteCopyPath).to(remoteCopyPath2); + System.out.println("second copy file id: "+idCopy2); +// new copy on frog + String idCopy3=client.softCopy(true).from(remoteOriginalFilePath).to(remoteCopyPath3); + System.out.println("second copy file id: "+idCopy2); +// check files + checkFileIsAlive(remoteOriginalFilePath); + checkFileIsAlive(remoteCopyPath); + checkFileIsAlive(remoteCopyPath2); + checkFileIsAlive(remoteCopyPath3); + //remove files + removeRemoteOriginalFile(); + removeCopiedFile(remoteCopyPath, "test/SoftCopy1/"); + removeCopiedFile(remoteCopyPath2, "test/SoftCopy2/"); + removeCopiedFile(remoteCopyPath3, "test/SoftCopy3/"); + removeLocalFile(newFilePath); + removeLocalFile(newFilePath2); + + } + + + /** + * Utility test for cleaning the remote file from backend + * @throws RemoteBackendException + */ + @Test + public void removeRemoteTemporaryFiles() throws RemoteBackendException { + removeLocalFile(newFilePath); + removeLocalFile(newFilePath2); + removeRemoteOriginalFile(); + removeCopiedFile(remoteCopyPath, "test/copyDir/"); + + } + + private void removeLocalFile(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + private void removeLocalFile(String newFilePath){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + } + + + private void setLocalResources() { + File f1=new File(localFrog); + absoluteLocalFrog=f1.getAbsolutePath(); + frogSize=f1.length(); + File f2=new File(localDog); + absoluteLocalDog=f2.getAbsolutePath(); + dogSize=f2.length(); + String dir=new File(absoluteLocalFrog).getParent(); + newFilePath=dir+"/testJunitLink.jpg"; + newFilePath2= dir+"/testJunitLink2.jpg"; + } + + private void checkOriginalFileIsAlive() { + String id; + id=client.get().LFile(newFilePath).RFile(remoteOriginalFilePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + private void checkFileIsAlive(String remotePath) { + String id; + id=client.get().LFile(newFilePath).RFile(remotePath); + System.out.println("id orig is alive: "+id); + assertNotNull(id); + removeLocalFile(); + } + + + private void removeRemoteOriginalFile() { + client.remove().RFile(remoteOriginalFilePath); +// client.removeDir().RDir("test/img"); + List linkList=client.showDir().RDir("test/img"); + assertTrue(linkList.isEmpty()); + } + + private void removeCopiedFile(String link, String dir) { + System.out.println("remove file at: "+link); + client.remove().RFile(link); + System.out.println("show dir: "+dir); +// client.removeDir().RDir(dir); + List list=client.showDir().RDir(dir); + assertTrue(list.isEmpty()); + } + +} \ No newline at end of file diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/StringEncrypterTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/StringEncrypterTest.java new file mode 100644 index 0000000..60bd815 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/StringEncrypterTest.java @@ -0,0 +1,60 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import org.gcube.contentmanagement.blobstorage.service.directoryOperation.Encrypter; +import org.gcube.contentmanager.storageclient.model.protocol.smp.StringDecrypter.EncryptionException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.junit.BeforeClass; +import org.junit.Test; + +public class StringEncrypterTest { + +// private static StringEncrypter encrypter; + private static Encrypter encrypter; + private String phrase="yfvjAEFu5UuhW9vsQ9E8MQSglmEKAPkBG7AbD75ZKLZWnh3DQferKg=="; + private String phrase1="smp://Wikipedia_logo_silver.png?"; +// private static final String passPhrase="this is a phrasethis is a phrase"; + private String encryption=""; + private String decryption=""; + + @BeforeClass + public static void setEncrypter() throws EncryptionException, org.gcube.contentmanagement.blobstorage.service.directoryOperation.Encrypter.EncryptionException{ +// encrypter=new StringEncrypter("DES"); + encrypter=new Encrypter("DES", Costants.DEFAULT_PASS_PHRASE); + } + + @Test + public void decrypt() { +// try { + try { + decryption=encrypter.decrypt(phrase); + } catch (org.gcube.contentmanagement.blobstorage.service.directoryOperation.Encrypter.EncryptionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// } catch (EncryptionException e) { +// +// } + System.out.println("decryption: "+decryption); + assertNotNull(decryption); + } + + @Test + public void encrypt() { +// try { + try { + encryption=encrypter.encrypt(phrase1); + } catch (org.gcube.contentmanagement.blobstorage.service.directoryOperation.Encrypter.EncryptionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// } catch (EncryptionException e) { +// +// } + System.out.println("encryption: "+encryption); + assertNotNull(encryption); + } + + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/UploadsTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/UploadsTest.java new file mode 100644 index 0000000..93ca5ba --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/UploadsTest.java @@ -0,0 +1,145 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class UploadsTest { + + private String owner="rcirillo"; + private String localPath="src/test/resources/CostaRica.jpg";//"src/test/resources/empty.txt";//"src/test/resources/CostaRica1.jpg"; + private String localPath1="src/test/resources/CostaRica1.jpg"; + private String remotePath="/tests/img/tabulardatadump.tar.gz";//"/tests/img/CostaRica1.jpg";//"/tests/img/empty.txt";// + private String absoluteLocalPath; + private String absoluteLocalPath1; +// private String newFilePath="src/test/resources"; + private IClient client; +// private String scope="/gcube";//"/d4science.research-infrastructures.eu"; //"/d4science.research-infrastructures.eu"; //"/gcube/devsec";//"/d4science.research-infrastructures.eu"; //"/CNR.it";////"/gcube/devsec";//""/CNR.it/ISTI";//"/gcube";//"/gcube/devNext/NextNext";// + private String serviceClass="JUnitTest"; + private String serviceName="StorageManager"; + + @Before + public void getClient(){ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client=new StorageClient(serviceClass, serviceName, owner, AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + assertNotNull(client); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + } + + + @Test + public void uploadByPath() throws RemoteBackendException { + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("UploadByPath id: "+id); + client.get().LFile(absoluteLocalPath).RFile(id); + assertTrue(new File(absoluteLocalPath).exists()); + id=client.put(true).LFile(absoluteLocalPath1).RFile(remotePath); + System.out.println("UploadByPath again id: "+id); + System.out.println("download file test by id"); + client.get().LFile("/home/rcirillo/workspaceGCUBE/storage-manager-wrapper_HEAD/src/test/resources/download.jpg").RFile(id); + assertTrue(new File(absoluteLocalPath).exists()); + assertNotNull(id); + removeRemoteFile(); + } + + @Test + public void uploadwithMimeType() throws RemoteBackendException { + String id=client.put(true, "image/jpeg").LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("UploadByPath id: "+id); + assertNotNull(id); + removeRemoteFile(); + } + + @Test + public void uploadByPathWithBAckendReturnedTest() throws RemoteBackendException { + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath, true); + System.out.println("UploadByPath id+backendType: "+id); + assertNotNull(id); + removeRemoteFile(); + } + + @Test + public void uploadByInputStream(){ + InputStream is=null; + try { + is=new FileInputStream(new File(absoluteLocalPath)); + client.put(true).LFile(is).RFile(remotePath); + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + removeRemoteFile(); + } + + + @Test + public void uploadByOutputStream() throws IOException{ + OutputStream out=client.put(true).RFileAsOutputStream(remotePath); + assertNotNull(out); + System.out.println("outstream returned: "+out); + FileInputStream fin=null; + try { + fin=new FileInputStream(new File(absoluteLocalPath)); + int c; + while ((c = fin.read()) != -1) { + out.write(c); } + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } finally { + if (fin != null) { + fin.close(); + } + } + removeRemoteFile(); + } + + + + + private void removeRemoteFile() throws RemoteBackendException{ + client.removeDir().RDir("tests/img"); + List list=client.showDir().RDir("tests/img"); + assertTrue(list.isEmpty()); + } + + + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + absoluteLocalPath1=new File(localPath1).getAbsolutePath(); + String dir1=new File(absoluteLocalPath1).getParent(); + } +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverByIdTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverByIdTest.java new file mode 100644 index 0000000..d349b5f --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverByIdTest.java @@ -0,0 +1,191 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Test the uri creation without payload + * @author Roberto Cirillo + * + */ +public class UrlResolverByIdTest { + + private IClient client; + private static String serviceName="TestStorage"; + private static String serviceClass="UrlTest"; + private static String owner="cirillo"; +// private static String scope="/gcube/devsec";//"/d4science.research-infrastructures.eu";//"/gcube/devsec";//"/CNR.it/ISTI";//"/gcube/devsec"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String localNewPath="src/test/resources/CostaRicaMove.jpg"; + private String remotePath="/Uritest/img/CostaRicaine.jpg"; + private String newPath="/Uritest/img5/CostaMove.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + private String id; + + @Before + public void init() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client = new StorageClient(serviceClass, serviceName, owner , AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); +// remove the following line for running getUrlbyIdNotPaylloadCheck test + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + } + + +// @Test + public void getUrlbyIdNotPaylloadCheck() throws RemoteBackendException { + String url=client.getUrl(true).RFile(remotePath); + System.out.println("url generated: "+url); + assertNotNull(url); + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + System.out.println("id retrieved: "+id); + client.moveFile().from(remotePath).to(newPath); + verifyUrl(url); + } + + + @Test + public void getUrlbyId() throws RemoteBackendException { +// id="5aa16dfe02cadc50bff0eaf1";//"5aa16dfe02cadc50bff0eaf7";//"5a0c1cddf1d47d0c2fea0c1c";//"5a0c1cddf1d47d0c2fea0c1c";//"5a056737f1d47d0c2fe1ccb8";//"5a048c4cf1d47d0c2fe10537";//client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + String url=client.getHttpUrl(true).RFile(id); +// String url=client.getUrl().RFileById(id); + System.out.println("httpUrl generated: "+url); + assertNotNull(url); +// id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + + +// System.out.println("id retrieved: "+id); +// client.moveFile().from(remotePath).to(newPath); +// verifyUrl(url); + } + + +// @Test +// public void getUrlbyId() throws RemoteBackendException { +//// id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); +// String url=client.getHttpUrl().RFileById("5aa16dfe02cadc50bff0eaf7"); +// System.out.println("url generated: "+url); +// assertNotNull(url); +//// url=url+"-VLT"; +// System.out.println("id retrieved: "+id); +// client.moveFile().from(remotePath).to(newPath); +// verifyUrl(url); +// } + + @Test + public void getUrlbyPath() throws RemoteBackendException { + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + String url=client.getUrl(true).RFile(remotePath); + System.out.println("url generated: "+url); + assertNotNull(url); + System.out.println("id retrieved: "+id); + client.moveFile().from(remotePath).to(newPath); + verifyUrl(url); + client.moveFile().from(newPath).to(remotePath); + } + + @Test + public void getFilebyEncryptedId() throws RemoteBackendException { + id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + String url=client.getUrl(true).RFile(remotePath); + System.out.println("url generated: "+url); + assertNotNull(url); + String idEncrypted=url.substring(url.lastIndexOf(".org/")+5); + System.out.println("id encrypted: "+id); + client.get().RFileAsInputStream(remotePath); + client.moveFile().from(remotePath).to(newPath); + verifyUrl(url); + client.moveFile().from(newPath).to(remotePath); + } + + + /** + * download the file and verify if the file exist + * @param url + */ + + private void verifyUrl(String url) { + Handler.activateProtocol(); + URL smsHome = null; + try { + smsHome = new URL(url); + } catch (MalformedURLException e1) { + e1.printStackTrace(); + } + URLConnection uc = null; + File f=null; + try { + uc = ( URLConnection ) smsHome.openConnection ( ); + InputStream is=uc.getInputStream(); + f=new File(localNewPath); + FileOutputStream out=new FileOutputStream(f); + byte buf[]=new byte[1024]; + int len=0; + System.out.println("InputStream "+is); + while((len=is.read(buf))>0){ + out.write(buf,0,len); + } + out.close(); + is.close(); + }catch(Exception e ){ + e.printStackTrace(); + assertTrue(false); + } + assertTrue(f.exists()); + } + + @After + public void removeRemoteFile() throws RemoteBackendException{ + String id=client.remove().RFile(remotePath); + List list=client.showDir().RDir("Uritest/img"); + assertTrue(list.isEmpty()); +// String id2=client.remove().RFile(newPath); +// List list2=client.showDir().RDir("Uritest/img5"); +// assertTrue(list.isEmpty()); + removeLocalFiles(); + } + + private void removeLocalFiles(){ + File f=new File(newFilePath); + f.delete(); + assertFalse(f.exists()); + f=new File(localNewPath); + f.delete(); + assertFalse(f.exists()); + + } + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + } + +} + diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverTest.java new file mode 100644 index 0000000..192cd36 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/UrlResolverTest.java @@ -0,0 +1,73 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanagement.blobstorage.resource.StorageObject; +import org.gcube.contentmanagement.blobstorage.service.IClient; +import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.gcube.contentmanager.storageclient.wrapper.AccessType; +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; +import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class UrlResolverTest { + + private IClient client; + private static String serviceName="TestStorage"; + private static String serviceClass="UrlTest"; + private static String owner="cirillo"; +// private static String scope="/gcube/devsec";//"/CNR.it/ISTI";//"/gcube/devsec"; + private String localPath="src/test/resources/CostaRica1.jpg"; + private String remotePath="/test/img4/CostaRicaine.jpg"; + private String absoluteLocalPath; + private String newFilePath="src/test/resources"; + + @Before + public void init() throws RemoteBackendException{ + ScopeProvider.instance.set(Costants.DEFAULT_SCOPE_STRING); + try { + client = new StorageClient(serviceClass, serviceName, owner , AccessType.SHARED, Costants.DEFAULT_MEMORY_TYPE).getClient(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + setLocalResources(); + String id=client.put(true).LFile(absoluteLocalPath).RFile(remotePath); + } + + + @Test + public void getUrl() throws RemoteBackendException { + String url=client.getUrl().RFile(remotePath); + System.out.println("url generated: "+url); + } + + @Test + public void getHttpUrl() throws RemoteBackendException { + String url=client.getHttpUrl().RFile(remotePath); + System.out.println("HttpUrl generated: "+url); + } + +// @After +// public void removeRemoteFile() throws RemoteBackendException{ +// String id=client.remove().RFile(remotePath); +// List list=client.showDir().RDir("test/img4"); +// assertTrue(list.isEmpty()); +// } + + + private void setLocalResources() { + absoluteLocalPath=new File(localPath).getAbsolutePath(); + String dir=new File(absoluteLocalPath).getParent(); + newFilePath=dir+"/testJunit.jpg"; + absoluteLocalPath=new File(localPath).getAbsolutePath(); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/scopeValidationTest.java b/src/test/java/org/gcube/contentmanager/storageclient/test/scopeValidationTest.java new file mode 100644 index 0000000..934baa9 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/scopeValidationTest.java @@ -0,0 +1,26 @@ +package org.gcube.contentmanager.storageclient.test; + +import static org.junit.Assert.*; + +import org.gcube.common.scope.impl.ServiceMapScannerMediator; +import org.gcube.contentmanager.storageclient.test.utils.Costants; +import org.junit.BeforeClass; +import org.junit.Test; + +public class scopeValidationTest { + + static ServiceMapScannerMediator scanner; +// String scope="/gcube"; + + @BeforeClass + public static void init(){ + scanner=new ServiceMapScannerMediator(); + } + + + @Test + public void test() { + assertTrue(scanner.isValid(Costants.DEFAULT_SCOPE_STRING)); + } + +} diff --git a/src/test/java/org/gcube/contentmanager/storageclient/test/utils/Costants.java b/src/test/java/org/gcube/contentmanager/storageclient/test/utils/Costants.java new file mode 100644 index 0000000..bf48794 --- /dev/null +++ b/src/test/java/org/gcube/contentmanager/storageclient/test/utils/Costants.java @@ -0,0 +1,20 @@ +/** + * + */ +package org.gcube.contentmanager.storageclient.test.utils; + + +import org.gcube.contentmanager.storageclient.wrapper.MemoryType; + + +/** + * @author Roberto Cirillo (ISTI-CNR) 2018 + * + */ +public final class Costants { + + public static final MemoryType DEFAULT_MEMORY_TYPE = MemoryType.PERSISTENT; + public static final String DEFAULT_SCOPE_STRING="/gcube/devsec"; + public static final String DEFAULT_PASS_PHRASE="this is a phrasethis is a phrase"; + +} diff --git a/src/test/resources/CostaRica.jpg b/src/test/resources/CostaRica.jpg new file mode 100644 index 0000000..26bfd6f Binary files /dev/null and b/src/test/resources/CostaRica.jpg differ diff --git a/src/test/resources/CostaRica1.jpg b/src/test/resources/CostaRica1.jpg new file mode 100644 index 0000000..7a0428e Binary files /dev/null and b/src/test/resources/CostaRica1.jpg differ diff --git a/src/test/resources/dog.jpg b/src/test/resources/dog.jpg new file mode 100644 index 0000000..7a0428e Binary files /dev/null and b/src/test/resources/dog.jpg differ diff --git a/src/test/resources/download.png b/src/test/resources/download.png new file mode 100644 index 0000000..7a0428e Binary files /dev/null and b/src/test/resources/download.png differ