From d14741ce95e1680e0336d5cdcc549d422b12cdf8 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Tue, 7 Aug 2018 16:56:50 +0300 Subject: [PATCH 1/9] Moved and renamed DropboxClient to DropboxCloudService. --- .../cloud/dropbox/DropboxCloudService.groovy} | 8 ++++---- .../commands/DownloadFromDropboxCommand.groovy | 4 ++-- .../uicomparison/commands/UploadToDropboxCommand.groovy | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) rename src/main/groovy/com/sysgears/seleniumbundle/core/{dropbox/DropboxClient.groovy => data/cloud/dropbox/DropboxCloudService.groovy} (96%) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/dropbox/DropboxClient.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy similarity index 96% rename from src/main/groovy/com/sysgears/seleniumbundle/core/dropbox/DropboxClient.groovy rename to src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy index 5081086..3c7e5a3 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/dropbox/DropboxClient.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy @@ -1,4 +1,4 @@ -package com.sysgears.seleniumbundle.core.dropbox +package com.sysgears.seleniumbundle.core.data.cloud.dropbox import com.dropbox.core.BadRequestException import com.dropbox.core.DbxException @@ -13,7 +13,7 @@ import groovy.util.logging.Slf4j * Client for Dropbox. Provides methods to work with Dropbox API. */ @Slf4j -class DropboxClient { +class DropboxCloudService { /** * Project properties. @@ -32,9 +32,9 @@ class DropboxClient { private final DbxClientV2 client /** - * Creates an instance of a custom DropboxClient. + * Creates an instance of a custom DropboxCloudService. */ - DropboxClient() { + DropboxCloudService() { client = new DbxClientV2(config, conf.dropbox.accessToken as String) } diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy index af9e1d7..f62b66b 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy @@ -2,7 +2,7 @@ package com.sysgears.seleniumbundle.core.uicomparison.commands import com.sysgears.seleniumbundle.core.command.AbstractCommand import com.sysgears.seleniumbundle.core.conf.Config -import com.sysgears.seleniumbundle.core.dropbox.DropboxClient +import com.sysgears.seleniumbundle.core.data.cloud.dropbox.DropboxCloudService import com.sysgears.seleniumbundle.core.implicitinit.annotations.ImplicitInit import groovy.util.logging.Slf4j import org.apache.commons.io.FilenameUtils @@ -22,7 +22,7 @@ class DownloadFromDropboxCommand extends AbstractCommand { /** * Instance of Dropbox Client to be used by the command. */ - private DropboxClient client = new DropboxClient() + private DropboxCloudService client = new DropboxCloudService() /** * Creates an instance of DownloadFromDropboxCommand. diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy index 0055ddf..f4859a0 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy @@ -2,7 +2,7 @@ package com.sysgears.seleniumbundle.core.uicomparison.commands import com.sysgears.seleniumbundle.core.command.AbstractCommand import com.sysgears.seleniumbundle.core.conf.Config -import com.sysgears.seleniumbundle.core.dropbox.DropboxClient +import com.sysgears.seleniumbundle.core.data.cloud.dropbox.DropboxCloudService import com.sysgears.seleniumbundle.core.implicitinit.annotations.ImplicitInit import com.sysgears.seleniumbundle.core.utils.FileHelper import groovy.util.logging.Slf4j @@ -23,7 +23,7 @@ class UploadToDropboxCommand extends AbstractCommand { /** * Instance of Dropbox Client to be used by the command. */ - private DropboxClient client = new DropboxClient() + private DropboxCloudService client = new DropboxCloudService() /** * Creates an instance of UploadToDropboxCommand. From 468fd9e020ef1fb067933625268574161ac5b378 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Tue, 7 Aug 2018 16:58:57 +0300 Subject: [PATCH 2/9] Added common files for cloud services. --- .../core/data/cloud/AbstractCloudService.groovy | 5 +++++ .../core/data/cloud/ICloudService.groovy | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/AbstractCloudService.groovy create mode 100644 src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/ICloudService.groovy diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/AbstractCloudService.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/AbstractCloudService.groovy new file mode 100644 index 0000000..5c41b3e --- /dev/null +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/AbstractCloudService.groovy @@ -0,0 +1,5 @@ +package com.sysgears.seleniumbundle.core.data.cloud + +abstract class AbstractCloudService implements ICloudService { + +} diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/ICloudService.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/ICloudService.groovy new file mode 100644 index 0000000..c64f0b6 --- /dev/null +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/ICloudService.groovy @@ -0,0 +1,12 @@ +package com.sysgears.seleniumbundle.core.data.cloud + +interface ICloudService { + + void downloadFile(String remotePath, String localPath) + + void downloadFiles(String remotePath, String localPath) + + void uploadFile(String localPath, String remotePath) + + void uploadFiles(String localPath, String remotePath) +} \ No newline at end of file From 8d5761c12c8f3f43898900669950152df3bb9c40 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Tue, 7 Aug 2018 16:59:12 +0300 Subject: [PATCH 3/9] Added ClassFinder. --- .../core/utils/ClassFinder.groovy | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/main/groovy/com/sysgears/seleniumbundle/core/utils/ClassFinder.groovy diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/utils/ClassFinder.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/utils/ClassFinder.groovy new file mode 100644 index 0000000..e025333 --- /dev/null +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/utils/ClassFinder.groovy @@ -0,0 +1,104 @@ +package com.sysgears.seleniumbundle.core.utils + +import com.sysgears.seleniumbundle.core.command.AbstractCommand +import com.sysgears.seleniumbundle.core.command.CommandArgs +import com.sysgears.seleniumbundle.core.conf.Config +import com.sysgears.seleniumbundle.core.data.cloud.AbstractCloudService +import groovy.util.logging.Slf4j +import org.apache.commons.io.FilenameUtils + +/** + * Provides methods to find classes by criteria. + */ +@Slf4j +class ClassFinder { + + /** + * The path to main sources. + */ + private final static String GROOVY_SOURCE_PATH = "src/main/groovy/" + + /** + * The root package to start searching from. + */ + private final static String ROOT_DIR = "${GROOVY_SOURCE_PATH}com/sysgears/seleniumbundle" + + /** + * Finds a command by a given name which extends Abstract command. + * + * @param args parsed command arguments which have command name and arguments + * @param instance of config + * + * @return an instance of the found command initialized with the given arguments and the config + */ + static T findCommand(CommandArgs args, Config conf) { + find(args.name, AbstractCommand, "Command", "commands") + .newInstance(args.arguments, conf) as T + } + + /** + * Finds a cloud service by a given name which extends AbstractCloudService. + * + * @param className name of a class + * @param conf instance of config + * + * @return an instance of the found cloud service initialized with the config + */ + static T findCloudService(String className, Config conf) { + find(className, AbstractCloudService, "Service") + .newInstance(conf) as T + } + + /** + * Finds a class by the given name which has the given sub name and is stored in the given sub folder. + * + * @param className name of a class + * @param parentClass class which the sought class extends + * @param subName sub name of the class + * @param subFolder name of the sub folder to search for the class in + * + * @return class of the found groovy class + * + * @throws IllegalArgumentException if command has not been found + */ + static Class find(String className, Class parentClass, String subName, String subFolder = "") + throws IllegalArgumentException { + def clazz = FileHelper.getFiles(ROOT_DIR, "groovy").findAll { + it.path.matches(/^(\w*${File.separator})*$subFolder(${File.separator}\w*)*$subName\.groovy$/) + }.findAll { File file -> + getClassName(file, subName).equalsIgnoreCase(className) + }.findResult { + def clazz = Class.forName(getClassPath(it.path)) + + (hasParent(clazz, parentClass)) ? clazz : null + } + + clazz ?: { + log.error("Class [$className] wasn't found.") + throw new IllegalArgumentException("Class [$className] wasn't found.") + }() + } + + private static String getClassName(File file, String subName) { + file.name - "${subName}.groovy" + } + + private static String getClassPath(String filePath) { + (filePath - FilenameUtils.separatorsToSystem(GROOVY_SOURCE_PATH) - ".groovy") + .split(File.separator).join(".") + } + + /** + * Checks if a class or any of its superclasses has a target class as parent. + * + * @param clazz class to start the check from + * @param targetClass expected parent + * + * @return true if class has a target class as one of the superclasses, false otherwise + */ + private static Boolean hasParent(Class clazz, Class targetClass) { + def parent = clazz.getSuperclass() + + parent ? parent == targetClass ?: hasParent(parent, targetClass) : false + } +} From bdac58fd84961d85d7e826df28cbf03d0aee7887 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Tue, 7 Aug 2018 17:52:37 +0300 Subject: [PATCH 4/9] DropboxCloudService implements ICloudService. Implemented methods. --- .../cloud/dropbox/DropboxCloudService.groovy | 73 ++++++++++++++----- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy index 3c7e5a3..13e0a61 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy @@ -6,6 +6,8 @@ import com.dropbox.core.DbxRequestConfig import com.dropbox.core.v2.DbxClientV2 import com.dropbox.core.v2.files.DeleteErrorException import com.sysgears.seleniumbundle.core.conf.Config +import com.sysgears.seleniumbundle.core.data.cloud.AbstractCloudService +import com.sysgears.seleniumbundle.core.utils.FileHelper import com.sysgears.seleniumbundle.core.utils.PathHelper import groovy.util.logging.Slf4j @@ -13,7 +15,7 @@ import groovy.util.logging.Slf4j * Client for Dropbox. Provides methods to work with Dropbox API. */ @Slf4j -class DropboxCloudService { +class DropboxCloudService extends AbstractCloudService { /** * Project properties. @@ -41,44 +43,75 @@ class DropboxCloudService { /** * Downloads a file from Dropbox. * - * @param dropboxPath path to a file on Dropbox - * @param downloadPath path where to download the file + * @param remotePath path to a file on Dropbox + * @param localPath path to download the file to * * @throws IOException if any error occurs while downloading file from Dropbox */ - void downloadFile(String dropboxPath, String downloadPath) throws IOException { - File file = new File(downloadPath) + @Override + void downloadFile(String remotePath, String localPath) throws IOException { + File file = new File(localPath) + file.getParentFile().mkdirs() try { - client.files().download(dropboxPath).download(new FileOutputStream(file)) + client.files().download(remotePath).download(new FileOutputStream(file)) } catch (BadRequestException e) { - log.error("Unable to download a file from $dropboxPath.", e) - throw new IOException("Unable to download a file from $dropboxPath, invalid request", e) + log.error("Unable to download a file from $remotePath.", e) + throw new IOException("Unable to download a file from $remotePath, invalid request", e) } catch (IOException | DbxException e) { - log.error("Unable to download a file from $dropboxPath.", e) - throw new IOException("Unable to download a file from $dropboxPath.", e) + log.error("Unable to download a file from $remotePath.", e) + throw new IOException("Unable to download a file from $remotePath.", e) + } + } + + /** + * Downloads all files stored in remote path on Dropbox. + * + * @param remotePath path to files on Dropbox + * @param localPath local path to download the files to + */ + @Override + void downloadFiles(String remotePath, String localPath) { + + getDropboxPaths().each { + downloadFile(it, localPath + it - remotePath) } } /** * Uploads a file to Dropbox. * - * @param dropboxPath path for saving a file on Dropbox - * @param uploadPath local path to a file + * @param remotePath path for saving a file on Dropbox + * @param localPath local path to a file * * @throws IOException if any error occurs while uploading file to Dropbox */ - void uploadFile(String dropboxPath, String uploadPath) throws IOException { - dropboxPath = PathHelper.convertToUnixLike(dropboxPath) - log.info("uploading $dropboxPath") + @Override + void uploadFile(String localPath, String remotePath) throws IOException { + remotePath = PathHelper.convertToUnixLike(remotePath) + try { - client.files().uploadBuilder("$dropboxPath").uploadAndFinish(new FileInputStream(uploadPath)) + client.files().uploadBuilder("$remotePath").uploadAndFinish(new FileInputStream(localPath)) } catch (BadRequestException e) { - log.error("Unable to upload a file from $uploadPath.", e) - throw new IOException("Unable to upload a file from $uploadPath, invalid request", e) + log.error("Unable to upload a file from $localPath.", e) + throw new IOException("Unable to upload a file from $localPath, invalid request", e) } catch (IOException | DbxException e) { - log.error("Unable to upload a file from $uploadPath.", e) - throw new IOException("Unable to upload a file from $uploadPath.", e) + log.error("Unable to upload a file from $localPath.", e) + throw new IOException("Unable to upload a file from $localPath.", e) + } + } + + /** + * Uploads all files stored in local path to Dropbox. + * + * @param localPath path to local directory + * @param remotePath path for saving files on Dropbox + */ + @Override + void uploadFiles(String localPath, String remotePath) { + + FileHelper.getFiles(localPath).each { + uploadFile(it.path, remotePath + it.path - localPath) } } From 4567cd76aaeb45ae11a88f4f9cc109a2ac2b21a0 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Wed, 8 Aug 2018 15:59:09 +0300 Subject: [PATCH 5/9] Renamed commands for uploading/downloading files. --- ...omDropboxCommand.groovy => DownloadFromCommand.groovy} | 6 +++--- ...loadToDropboxCommand.groovy => UploadToCommand.groovy} | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) rename src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/{DownloadFromDropboxCommand.groovy => DownloadFromCommand.groovy} (89%) rename src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/{UploadToDropboxCommand.groovy => UploadToCommand.groovy} (87%) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy similarity index 89% rename from src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy rename to src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy index f62b66b..fb9c7fa 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromDropboxCommand.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy @@ -11,7 +11,7 @@ import org.apache.commons.io.FilenameUtils * Class which provides the method to download screenshots from Dropbox. */ @Slf4j -class DownloadFromDropboxCommand extends AbstractCommand { +class DownloadFromCommand extends AbstractCommand { /** * Category of the downloaded screenshots. @@ -25,7 +25,7 @@ class DownloadFromDropboxCommand extends AbstractCommand { private DropboxCloudService client = new DropboxCloudService() /** - * Creates an instance of DownloadFromDropboxCommand. + * Creates an instance of DownloadFromCommand. * * @param arguments map that contains command arguments * @param conf project properties @@ -33,7 +33,7 @@ class DownloadFromDropboxCommand extends AbstractCommand { * @throws IllegalArgumentException is thrown in case a value is missing for a mandatory parameter or * the value doesn't match the validation pattern */ - DownloadFromDropboxCommand(Map> arguments, Config conf) throws IllegalArgumentException { + DownloadFromCommand(Map> arguments, Config conf) throws IllegalArgumentException { super(arguments, conf) } diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy similarity index 87% rename from src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy rename to src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy index f4859a0..e519b0f 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToDropboxCommand.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy @@ -12,7 +12,7 @@ import org.apache.commons.io.FilenameUtils * Class which provides the method to upload screenshots to Dropbox. */ @Slf4j -class UploadToDropboxCommand extends AbstractCommand { +class UploadToCommand extends AbstractCommand { /** * Category of the uploaded screenshots. @@ -26,7 +26,7 @@ class UploadToDropboxCommand extends AbstractCommand { private DropboxCloudService client = new DropboxCloudService() /** - * Creates an instance of UploadToDropboxCommand. + * Creates an instance of UploadToCommand. * * @param @param arguments the map with arguments of the command * @param conf project properties @@ -34,14 +34,14 @@ class UploadToDropboxCommand extends AbstractCommand { * @throws IllegalArgumentException is thrown in case a value is missing for a mandatory parameter or * the value doesn't match the validation pattern */ - UploadToDropboxCommand(Map> arguments, Config conf) throws IllegalArgumentException { + UploadToCommand(Map> arguments, Config conf) throws IllegalArgumentException { super(arguments, conf) } /** * Executes the command. * - * @throws IOException in case Dropbox client operations produce an error. + * @throws IOException in case Dropbox serviceInstance operations produce an error. */ @Override void execute() throws IOException { From d31634f8591e344165dba08ea9b9962a13167ce1 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Wed, 8 Aug 2018 16:02:17 +0300 Subject: [PATCH 6/9] Modified DownloadFromCommand to use different services. --- .../commands/DownloadFromCommand.groovy | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy index fb9c7fa..793602d 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/DownloadFromCommand.groovy @@ -2,17 +2,22 @@ package com.sysgears.seleniumbundle.core.uicomparison.commands import com.sysgears.seleniumbundle.core.command.AbstractCommand import com.sysgears.seleniumbundle.core.conf.Config -import com.sysgears.seleniumbundle.core.data.cloud.dropbox.DropboxCloudService +import com.sysgears.seleniumbundle.core.data.cloud.ICloudService import com.sysgears.seleniumbundle.core.implicitinit.annotations.ImplicitInit -import groovy.util.logging.Slf4j +import com.sysgears.seleniumbundle.core.utils.ClassFinder import org.apache.commons.io.FilenameUtils /** * Class which provides the method to download screenshots from Dropbox. */ -@Slf4j class DownloadFromCommand extends AbstractCommand { + /** + * Name of cloud service to be used. + */ + @ImplicitInit(pattern = "googledrive|dropbox", isRequired = true) + private String service + /** * Category of the downloaded screenshots. */ @@ -20,9 +25,9 @@ class DownloadFromCommand extends AbstractCommand { private List categories /** - * Instance of Dropbox Client to be used by the command. + * Instance of Cloud Client to be used by the command. */ - private DropboxCloudService client = new DropboxCloudService() + private ICloudService serviceInstance /** * Creates an instance of DownloadFromCommand. @@ -35,31 +40,17 @@ class DownloadFromCommand extends AbstractCommand { */ DownloadFromCommand(Map> arguments, Config conf) throws IllegalArgumentException { super(arguments, conf) + + serviceInstance = ClassFinder.findCloudService(service, conf) } /** * Executes the command. - * - * @throws IOException in case Dropbox client operations produced an error */ @Override - void execute() throws IOException { + void execute() { categories.each { category -> - def categoryPath = FilenameUtils.separatorsToSystem(conf.ui.path."$category") - def remotePaths = client.dropboxPaths.findAll { - it.matches(/^\/$category\/(.*).png\$/) - } - - if (!remotePaths) { - log.error("No $category files found on Dropbox.") - throw new IOException("No $category files found on Dropbox.") - } - - remotePaths.each { remotePath -> - def localPath = "${categoryPath.substring(0, categoryPath.lastIndexOf('/'))}${remotePath}" - - client.downloadFile(remotePath, localPath) - } + serviceInstance.downloadFiles(category, FilenameUtils.separatorsToSystem(conf.ui.path."$category")) } } } \ No newline at end of file From 78f4e7e319b2c090279b347f30d3ff21de0e279b Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Wed, 8 Aug 2018 16:14:10 +0300 Subject: [PATCH 7/9] Minor changes to DropboxCloudService. --- .../cloud/dropbox/DropboxCloudService.groovy | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy index 13e0a61..b43e0e0 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy @@ -81,8 +81,8 @@ class DropboxCloudService extends AbstractCloudService { /** * Uploads a file to Dropbox. * - * @param remotePath path for saving a file on Dropbox * @param localPath local path to a file + * @param remotePath path for saving a file on Dropbox * * @throws IOException if any error occurs while uploading file to Dropbox */ @@ -90,6 +90,9 @@ class DropboxCloudService extends AbstractCloudService { void uploadFile(String localPath, String remotePath) throws IOException { remotePath = PathHelper.convertToUnixLike(remotePath) + // delete is a workaround due to issues with "withMode(WriteMode.OVERWRITE)" + deleteFile(remotePath) + try { client.files().uploadBuilder("$remotePath").uploadAndFinish(new FileInputStream(localPath)) } catch (BadRequestException e) { @@ -118,22 +121,22 @@ class DropboxCloudService extends AbstractCloudService { /** * Deletes file saved in Dropbox path. * - * @param dropboxPath path to the file to be deleted + * @param remotePath path to the file to be deleted * * @throws IOException if any error occurs while deleting file on Dropbox */ - void deleteFile(String dropboxPath) throws IOException { - dropboxPath = PathHelper.convertToUnixLike(dropboxPath) + void deleteFile(String remotePath) throws IOException { + remotePath = PathHelper.convertToUnixLike(remotePath) try { - client.files().deleteV2("$dropboxPath") + client.files().deleteV2("$remotePath") } catch (DeleteErrorException ignored) { // exception is thrown if there is no file of given path } catch (BadRequestException e) { - log.error("Unable to delete a file from $dropboxPath.", e) - throw new IOException("Unable to delete a file from $dropboxPath, invalid request", e) + log.error("Unable to delete a file from $remotePath.", e) + throw new IOException("Unable to delete a file from $remotePath, invalid request", e) } catch (DbxException e) { - log.error("Unable to delete a file from $dropboxPath.", e) - throw new IOException("Unable to delete a file from $dropboxPath.", e) + log.error("Unable to delete a file from $remotePath.", e) + throw new IOException("Unable to delete a file from $remotePath.", e) } } From e63df3155e66b00b11d88812573c414cb9455a32 Mon Sep 17 00:00:00 2001 From: Nikita Riabchenko Date: Wed, 8 Aug 2018 16:16:23 +0300 Subject: [PATCH 8/9] Modified UploadToCommand to use different services. --- .../commands/UploadToCommand.groovy | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy index e519b0f..eac202d 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/uicomparison/commands/UploadToCommand.groovy @@ -2,18 +2,22 @@ package com.sysgears.seleniumbundle.core.uicomparison.commands import com.sysgears.seleniumbundle.core.command.AbstractCommand import com.sysgears.seleniumbundle.core.conf.Config -import com.sysgears.seleniumbundle.core.data.cloud.dropbox.DropboxCloudService +import com.sysgears.seleniumbundle.core.data.cloud.ICloudService import com.sysgears.seleniumbundle.core.implicitinit.annotations.ImplicitInit -import com.sysgears.seleniumbundle.core.utils.FileHelper -import groovy.util.logging.Slf4j +import com.sysgears.seleniumbundle.core.utils.ClassFinder import org.apache.commons.io.FilenameUtils /** * Class which provides the method to upload screenshots to Dropbox. */ -@Slf4j class UploadToCommand extends AbstractCommand { + /** + * Name of cloud service to be used. + */ + @ImplicitInit(pattern = "googledrive|dropbox", isRequired = true) + String service + /** * Category of the uploaded screenshots. */ @@ -21,9 +25,9 @@ class UploadToCommand extends AbstractCommand { private List categories /** - * Instance of Dropbox Client to be used by the command. + * Instance of Cloud Client to be used by the command. */ - private DropboxCloudService client = new DropboxCloudService() + private ICloudService serviceInstance /** * Creates an instance of UploadToCommand. @@ -36,31 +40,18 @@ class UploadToCommand extends AbstractCommand { */ UploadToCommand(Map> arguments, Config conf) throws IllegalArgumentException { super(arguments, conf) + + serviceInstance = ClassFinder.findCloudService(service, conf) } /** * Executes the command. - * - * @throws IOException in case Dropbox serviceInstance operations produce an error. */ @Override void execute() throws IOException { categories.each { category -> - def categoryPath = FilenameUtils.separatorsToSystem(conf.ui.path."$category") - def localPaths = FileHelper.getFiles(categoryPath).collect { it.path } - - if (!localPaths) { - log.error("No $category files found.") - throw new IOException("No $category files found.") - } - - localPaths.each { localPath -> - def remotePath = localPath - categoryPath.substring(0, categoryPath.lastIndexOf(File.separator)) - // delete is a workaround due to issues with "withMode(WriteMode.OVERWRITE)" - client.deleteFile(remotePath) - client.uploadFile(remotePath, localPath) - } + serviceInstance.uploadFiles(category, FilenameUtils.separatorsToSystem(conf.ui.path."$category")) } } } From cd35bb55ea9c313d1258274cb9ab20a0af1e0d90 Mon Sep 17 00:00:00 2001 From: Nikita Ryabchenko Date: Tue, 14 Aug 2018 17:05:52 +0300 Subject: [PATCH 9/9] Added "/" in the beginning of remote paths as it is required by Dropbox. --- .../core/data/cloud/dropbox/DropboxCloudService.groovy | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy index b43e0e0..9dcc489 100644 --- a/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy +++ b/src/main/groovy/com/sysgears/seleniumbundle/core/data/cloud/dropbox/DropboxCloudService.groovy @@ -72,7 +72,6 @@ class DropboxCloudService extends AbstractCloudService { */ @Override void downloadFiles(String remotePath, String localPath) { - getDropboxPaths().each { downloadFile(it, localPath + it - remotePath) } @@ -94,7 +93,7 @@ class DropboxCloudService extends AbstractCloudService { deleteFile(remotePath) try { - client.files().uploadBuilder("$remotePath").uploadAndFinish(new FileInputStream(localPath)) + client.files().uploadBuilder("/$remotePath").uploadAndFinish(new FileInputStream(localPath)) } catch (BadRequestException e) { log.error("Unable to upload a file from $localPath.", e) throw new IOException("Unable to upload a file from $localPath, invalid request", e) @@ -127,8 +126,9 @@ class DropboxCloudService extends AbstractCloudService { */ void deleteFile(String remotePath) throws IOException { remotePath = PathHelper.convertToUnixLike(remotePath) + try { - client.files().deleteV2("$remotePath") + client.files().deleteV2("/$remotePath") } catch (DeleteErrorException ignored) { // exception is thrown if there is no file of given path } catch (BadRequestException e) { @@ -148,6 +148,7 @@ class DropboxCloudService extends AbstractCloudService { * @throws IOException if any error occurs while getting Dropbox paths */ List getDropboxPaths() throws IOException { + try { client.files().listFolderBuilder("").withRecursive(true).start().getEntries().collect { it.getPathLower()