def test_create_folder(self):
        folder = self.temp_folder + '/DummyFolder'

        try:
            if self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(folder, self.test_storage,
                                                 True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists)

            self.imaging_api.create_folder(
                requests.CreateFolderRequest(folder, self.test_storage))
            self.assertTrue(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists)

        finally:
            if self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(folder, self.test_storage,
                                                 True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists)
    def test_file_versions_download(self):        
        if self.imaging_api.api_client.configuration.on_premise:
            return

        folder = self.temp_folder + '/Storage'
        file1 = 'Storage.txt'
        file2 = 'Folder1/Folder1.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file1,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file2,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(
                    folder + '/' + file1, self.test_storage)).value
            recent_version = next(x for x in versions if x.is_latest is True)
            old_version = next(x for x in versions if x.is_latest is not True)

            with open(self.imaging_api.download_file(
                    requests.DownloadFileRequest(folder + '/' + file1, self.test_storage,
                                                 old_version.version_id)), 'r') as old_file:
                self.assertEqual(old_version.size, len(old_file.read()))

            with open(self.imaging_api.download_file(
                    requests.DownloadFileRequest(folder + '/' + file1, self.test_storage,
                                                 recent_version.version_id)), 'r') as old_file:
                self.assertEqual(recent_version.size, len(old_file.read()))

        finally:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))
    def test_copy_folder(self):
        folder = self.temp_folder + '/Storage'

        try:
            if self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(folder, self.test_storage,
                                                 True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists)

            self.imaging_api.copy_folder(
                requests.CopyFolderRequest(self.original_data_folder, folder,
                                           self.test_storage,
                                           self.test_storage))
            self.assertTrue(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists)

            original_files = self.imaging_api.get_files_list(
                requests.GetFilesListRequest(self.original_data_folder,
                                             self.test_storage)).value
            copied_files = self.imaging_api.get_files_list(
                requests.GetFilesListRequest(folder, self.test_storage)).value
            self.assertGreater(len(original_files), 0)
            self.assertGreater(len(copied_files), 0)
            self.assertEqual(len(original_files), len(copied_files))
            for original_file in original_files:
                self.assertIsNotNone(
                    next(copied_file for copied_file in copied_files
                         if copied_file.is_folder == original_file.is_folder
                         and copied_file.size == original_file.size
                         and copied_file.name == original_file.name))

        finally:
            if self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(folder, self.test_storage,
                                                 True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(folder,
                                                 self.test_storage)).exists)
 def tearDown(self):
     if not self.failed_any_test and self.remove_result and self.imaging_api.object_exists(
             requests.ObjectExistsRequest(self.temp_folder,
                                          self.test_storage)).exists:
         self.imaging_api.delete_folder(
             requests.DeleteFolderRequest(self.temp_folder,
                                          self.test_storage, True))
Exemplo n.º 5
0
 def _copy_input_file_to_test_folder(self, input_file_name, folder,
                                     storage):
     if not self.imaging_api.object_exists(
             requests.ObjectExistsRequest(
                 os.path.join(folder, input_file_name), storage)).exists:
         self.imaging_api.copy_file(
             requests.CopyFileRequest(
                 os.path.join(self.original_data_folder + '/UseCases',
                              input_file_name),
                 os.path.join(folder, input_file_name), storage, storage))
    def _copy_input_file_to_test_folder(self, input_file_name, folder,
                                        storage):
        if not self._check_input_file_exists(input_file_name):
            raise ValueError(
                "Input file {0} doesn't exist in the specified storage folder: {1}. Please, upload it first."
                .format(input_file_name, folder))

        if not self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    os.path.join(folder, input_file_name), storage)).exists:
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    os.path.join(self.original_data_folder, input_file_name),
                    os.path.join(folder, input_file_name), storage, storage))
    def setUp(self):
        self.failed_any_test = False
        self.default_storage = 'Imaging-CI'
        self.cloud_test_folder_prefix = 'ImagingCloudTestPython'
        self.original_data_folder = 'ImagingIntegrationTestData'
        self._server_access_file = 'serverAccess.json'
        self._api_version = 'v3.0'
        self._base_url = 'https://api.aspose.cloud/'
        self._local_test_folder = os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
            'TestData/')
        self.remove_result = True

        self.temp_folder = '{0}_{1}'.format(
            self.cloud_test_folder_prefix,
            os.environ.get('BUILD_NUMBER') or getpass.getuser())

        self.basic_export_formats = [
            'bmp', 'gif', 'jpg', 'png', 'psd', 'tiff', 'webp'
        ]

        self.test_storage = os.environ.get('StorageName')
        if not self.test_storage:
            print(
                'Storage name is not set by environment variable. Using the default one.'
            )
            self.test_storage = self.default_storage

        self.__create_api_instance()

        if not self.failed_any_test and self.remove_result and self.imaging_api.object_exists(
                requests.ObjectExistsRequest(self.temp_folder,
                                             self.test_storage)).exists:
            self.imaging_api.delete_folder(
                requests.DeleteFolderRequest(self.temp_folder,
                                             self.test_storage, True))
            self.imaging_api.create_folder(
                requests.CreateFolderRequest(self.temp_folder,
                                             self.test_storage))
    def __object_detection_request_tester(self,
                                          test_method_name,
                                          save_result_to_storage,
                                          parameters_line,
                                          input_file_name,
                                          result_file_name,
                                          invoke_request_action,
                                          response_tester,
                                          folder,
                                          storage=None):
        if not storage:
            storage = self.default_storage

        print(test_method_name)

        if not self._check_input_file_exists(input_file_name):
            raise ValueError(
                "Input file {0} doesn't exist in the specified storage folder: {1}. Please, upload it first."
                .format(input_file_name, folder))

        if not self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    os.path.join(folder, input_file_name), storage)).exists:
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    os.path.join(self.original_data_folder, input_file_name),
                    os.path.join(folder, input_file_name), storage, storage))
        passed = False
        out_path = str(None)

        try:
            print(parameters_line)

            if save_result_to_storage:
                out_path = os.path.join(folder, result_file_name)

                if self.imaging_api.object_exists(
                        requests.ObjectExistsRequest(out_path,
                                                     storage)).exists:
                    self.imaging_api.delete_file(
                        requests.DeleteFileRequest(out_path, storage))

            response = invoke_request_action()
            if save_result_to_storage:

                result_info = self._get_storage_file_info(
                    folder, result_file_name, storage)
                if not result_info:
                    raise ValueError(
                        "Result file {0} doesn't exist in the specified storage folder: {1}. "
                        "Result isn't present in the storage by an unknown reason."
                        .format(result_file_name, folder))

            response_tester(response)

            passed = True

        except Exception as e:
            self.failed_any_test = True
            print(str(e))
            raise
        finally:
            if passed and save_result_to_storage and self.remove_result \
                    and self.imaging_api.object_exists(
                requests.ObjectExistsRequest(out_path, storage)).exists:
                self.imaging_api.delete_file(
                    requests.DeleteFileRequest(out_path, storage))
            print("Test passed: " + str(passed) + os.linesep)
    def __request_tester(self,
                         test_method_name,
                         save_result_to_storage,
                         parameters_line,
                         input_file_name,
                         result_file_name,
                         invoke_request_action,
                         properties_tester,
                         folder,
                         storage=None):
        if not storage:
            storage = self.default_storage

        print(test_method_name)

        self._copy_input_file_to_test_folder(input_file_name, folder, storage)
        passed = False
        out_path = str(None)

        try:
            print(parameters_line)

            if save_result_to_storage:
                out_path = os.path.join(folder, result_file_name)

                if self.imaging_api.object_exists(
                        requests.ObjectExistsRequest(out_path,
                                                     storage)).exists:
                    self.imaging_api.delete_file(
                        requests.DeleteFileRequest(out_path, storage))

            result_properties = None
            response = invoke_request_action()
            if save_result_to_storage:

                result_info = self._get_storage_file_info(
                    folder, result_file_name, storage)
                if not result_info:
                    raise ValueError(
                        "Result file {0} doesn't exist in the specified storage folder: {1}. "
                        "Result isn't present in the storage by an unknown reason."
                        .format(result_file_name, folder))
                if not (str.endswith(result_file_name, '.pdf')
                        or str.endswith(result_file_name, '.pdf;')):
                    result_properties = self.imaging_api.get_image_properties(
                        requests.GetImagePropertiesRequest(
                            result_file_name, folder, storage))
                    self.assertIsNotNone(result_properties)
            elif not (str.endswith(str(response), '.pdf')
                      or str.endswith(str(response), '.pdf;')):
                result_properties = self.imaging_api.extract_image_properties(
                    requests.ExtractImagePropertiesRequest(response))
                self.assertIsNotNone(result_properties)

            original_properties = self.imaging_api.get_image_properties(
                requests.GetImagePropertiesRequest(input_file_name, folder,
                                                   storage))
            self.assertIsNotNone(original_properties)

            if result_properties:
                properties_tester(original_properties, result_properties,
                                  response)

            passed = True

        except Exception as e:
            self.failed_any_test = True
            print(str(e))
            raise
        finally:
            if passed and save_result_to_storage and self.remove_result \
                    and self.imaging_api.object_exists(
                requests.ObjectExistsRequest(out_path, storage)).exists:
                self.imaging_api.delete_file(
                    requests.DeleteFileRequest(out_path, storage))
            print("Test passed: " + str(passed) + os.linesep)
    def test_upload_file(self):
        folder = self.temp_folder + '/Storage'
        file = 'Storage.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            original_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    self.original_data_folder + '/' + file,
                    self.test_storage))

            result = self.imaging_api.upload_file(
                requests.UploadFileRequest(
                    folder + '/' + file,
                    original_file,
                    self.test_storage))
            self.assertIsNotNone(result)
            self.assertTrue(not result.errors or len(result) == 0)
            self.assertIsNotNone(result.uploaded)
            self.assertEqual(1, len(result.uploaded))
            self.assertEqual(file, result.uploaded[0])

            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file, self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            original_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        self.original_data_folder,
                        self.test_storage)).value if x.name == file)

            uploaded_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        folder, self.test_storage)).value if x.name == file)
            self.assertEqual(original_file_info.size, uploaded_file_info.size)

            original_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    self.original_data_folder + '/' + file,
                    self.test_storage))

            uploaded_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    folder + '/' + file, self.test_storage))

            with open(original_file, 'r') as original_reader, open(uploaded_file, 'r') as uploaded_reader:
                original_string = original_reader.read()
                copied_string = uploaded_reader.read()
                self.assertEqual(len(original_string), len(copied_string))
                self.assertEqual(len(original_string), original_file_info.size)
                self.assertEqual(original_string, copied_string)
                self.assertEqual(
                    original_string,
                    original_file_info.path.strip('/'))
                self.assertEqual(
                    original_string.replace(
                        self.original_data_folder,
                        folder),
                    uploaded_file_info.path.strip('/'))

        finally:
            self.imaging_api.delete_file(
                requests.DeleteFileRequest(
                    folder + '/' + file,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file,
                        self.test_storage)).exists)

            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)
    def test_move_file(self):
        folder = self.temp_folder + '/Storage'
        file = 'Storage.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file,
                    folder + '/' + file + '.copied',
                    self.test_storage,
                    self.test_storage))
            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file + '.copied',
                    self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            self.imaging_api.move_file(
                requests.MoveFileRequest(
                    folder + '/' + file + '.copied',
                    folder + '/' + file,
                    self.test_storage,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file + '.copied',
                        self.test_storage)).exists)
            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file, self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            original_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        self.original_data_folder,
                        self.test_storage)).value if x.name == file)
            moved_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        folder, self.test_storage)).value if x.name == file)
            self.assertEqual(original_file_info.size, moved_file_info.size)

            with open(self.imaging_api.download_file(
                    requests.DownloadFileRequest(self.original_data_folder + '/' + file,
                                                 self.test_storage)), 'r') as original_file, \
                    open(self.imaging_api.download_file(requests.DownloadFileRequest(folder + '/' + file,
                                                                                     self.test_storage)),
                         'r') as moved_file:
                original_string = original_file.read()
                moved_string = moved_file.read()
                self.assertEqual(len(original_string), len(moved_string))
                self.assertEqual(len(original_string), original_file_info.size)
                self.assertEqual(original_string, moved_string)
                self.assertEqual(
                    original_string,
                    original_file_info.path.strip('/'))
                self.assertEqual(
                    original_string.replace(
                        self.original_data_folder,
                        folder),
                    moved_file_info.path.strip('/'))

        finally:
            self.imaging_api.delete_file(
                requests.DeleteFileRequest(
                    folder + '/' + file,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file,
                        self.test_storage)).exists)

            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)
    def test_file_versions_move(self):        
        if self.imaging_api.api_client.configuration.on_premise:
            return

        folder = self.temp_folder + '/Storage'
        file1 = 'Storage.txt'
        file2 = 'Folder1/Folder1.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file1,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file2,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(
                    folder + '/' + file1, self.test_storage)).value
            recent_version = next(x for x in versions if x.is_latest is True)

            self.imaging_api.move_file(
                requests.MoveFileRequest(
                    folder + '/' + file1,
                    folder + '/' + file1 + '.recent',
                    self.test_storage,
                    self.test_storage,
                    recent_version.version_id))
            copied_versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(
                    folder + '/' + file1 + '.recent',
                    self.test_storage)).value
            self.assertEqual(1, len(copied_versions))
            self.assertTrue(copied_versions[0].is_latest)
            self.assertEqual(recent_version.size, copied_versions[0].size)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file1,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file2,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(
                    folder + '/' + file1, self.test_storage)).value
            old_version = next(x for x in versions if x.is_latest is not True)

            self.imaging_api.move_file(
                requests.MoveFileRequest(
                    folder + '/' + file1,
                    folder + '/' + file1 + '.old',
                    self.test_storage,
                    self.test_storage,
                    old_version.version_id))
            copied_versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(folder + '/' + file1 + '.old',
                                                self.test_storage)).value
            self.assertEqual(1, len(copied_versions))
            self.assertTrue(copied_versions[0].is_latest)
            self.assertEqual(old_version.size, copied_versions[0].size)

        finally:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))
    def test_file_versions_create(self):        
        if self.imaging_api.api_client.configuration.on_premise:
            return

        folder = self.temp_folder + '/Storage'
        file1 = 'Storage.txt'
        file2 = 'Folder1/Folder1.txt'

        file1_size = next(
            x for x in self.imaging_api.get_files_list(
                requests.GetFilesListRequest(
                    self.original_data_folder,
                    self.test_storage)).value if x.name == file1).size

        file2_size = next(
            x for x in self.imaging_api.get_files_list(
                requests.GetFilesListRequest(
                    self.original_data_folder + '/Folder1',
                    self.test_storage)).value if x.name == 'Folder1.txt').size

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file1,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file2,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(
                    folder + '/' + file1, self.test_storage)).value

            self.assertEqual(2, len(versions))
            recent_version_size = next(
                x for x in versions if x.is_latest is True).size
            old_version_size = next(
                x for x in versions if x.is_latest is not True).size
            self.assertNotEqual(recent_version_size, old_version_size)
            self.assertEqual(old_version_size, file1_size)
            self.assertEqual(recent_version_size, file2_size)

        finally:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))