def test_files_list(self):
        files = self.imaging_api.get_files_list(
            requests.GetFilesListRequest(self.original_data_folder,
                                         self.test_storage)).value
        self.assertEqual(3, len(files))
        folder1 = next(x for x in files if x.name == 'Folder1')
        self.assertIsNotNone(folder1)
        self.assertTrue(folder1.is_folder)
        self.assertTrue(folder1.path.strip('/').endswith(folder1.name))
        folder2 = next(x for x in files if x.name == 'Folder2')
        self.assertIsNotNone(folder2)
        self.assertTrue(folder2.is_folder)
        self.assertTrue(folder2.path.strip('/').endswith(folder2.name))
        storage_file = next(x for x in files if x.name == 'Storage.txt')
        self.assertIsNotNone(storage_file)
        self.assertFalse(storage_file.is_folder)
        self.assertTrue(
            storage_file.path.strip('/').endswith(storage_file.name))
        self.assertEqual(storage_file.size, len(storage_file.path.strip('/')))

        files = self.imaging_api.get_files_list(
            requests.GetFilesListRequest(
                self.original_data_folder + '/' + folder1.name,
                self.test_storage)).value
        self.assertEqual(1, len(files))
        folder1_file = next(x for x in files if x.name == 'Folder1.txt')
        self.assertIsNotNone(folder1_file)
        self.assertFalse(folder1_file.is_folder)
        self.assertTrue(
            folder1_file.path.strip('/').endswith(folder1_file.name))
        self.assertEqual(folder1_file.size, len(folder1_file.path.strip('/')))

        files = self.imaging_api.get_files_list(
            requests.GetFilesListRequest(
                self.original_data_folder + '/' + folder2.name,
                self.test_storage)).value
        self.assertEqual(2, len(files))
        folder2_file = next(x for x in files if x.name == 'Folder2.txt')
        self.assertIsNotNone(folder2_file)
        self.assertFalse(folder2_file.is_folder)
        self.assertTrue(
            folder2_file.path.strip('/').endswith(folder2_file.name))
        self.assertEqual(folder2_file.size, len(folder2_file.path.strip('/')))
        folder3 = next(x for x in files if x.name == 'Folder3')
        self.assertIsNotNone(folder3)
        self.assertTrue(folder3.is_folder)
        self.assertTrue(folder3.path.strip('/').endswith(folder3.name))

        files = self.imaging_api.get_files_list(
            requests.GetFilesListRequest(
                self.original_data_folder + '/' + folder2.name + '/' +
                folder3.name, self.test_storage)).value
        self.assertEqual(1, len(files))
        folder3_file = next(x for x in files if x.name == 'Folder3.txt')
        self.assertIsNotNone(folder3_file)
        self.assertFalse(folder3_file.is_folder)
        self.assertTrue(
            folder3_file.path.strip('/').endswith(folder3_file.name))
        self.assertEqual(folder3_file.size, len(folder3_file.path.strip('/')))
    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 _get_storage_file_info(self, folder, file_name, storage):
        file_list_response = self.imaging_api.get_files_list(
            requests.GetFilesListRequest(folder, storage)).value

        for storage_file_info in file_list_response:
            if storage_file_info.name == file_name:
                return storage_file_info
        return None
    def __create_api_instance(self):
        print('Trying to obtain configuration from environment variables.')
        on_premise = True if os.environ.get('OnPremise') and \
                             bool(strtobool(os.environ.get('OnPremise'))) \
            else False
        client_secret = None if on_premise else os.environ.get('ClientSecret')
        client_id = None if on_premise else os.environ.get('ClientId')
        base_url = os.environ.get('ApiEndpoint')
        api_version = os.environ.get('ApiVersion')

        if (not on_premise and
            (not client_secret
             or not client_id)) or not base_url or not api_version:
            print('Access data isn\'t set completely by environment variables.'
                  ' Filling unset data with default values.')

        if not api_version:
            api_version = self._api_version
            print('Set default API version')

        server_access_info = os.path.join(self._local_test_folder,
                                          self._server_access_file)
        with open(server_access_info) as f:
            server_file_info = json.load(f)

        if server_file_info:
            if not client_secret and not on_premise:
                client_secret = server_file_info['ClientSecret']
                print('Set default Client Secret')

            if not client_id and not on_premise:
                client_id = server_file_info['ClientId']
                print('Set default Client Id')

            if not base_url:
                base_url = server_file_info['BaseURL']
                print('Set default Base URL')

        if (not on_premise and
            (not client_secret
             or not client_id)) or not base_url or not api_version:
            raise ValueError(
                'Please, specify valid access data (ClientSecret, ClientId, Base URL)'
            )

        print('On Premise: ' + str(on_premise))
        if not on_premise:
            print('Client Secret: ' + client_secret)
            print('Client Id: ' + client_id)

        print('Storage: ' + self.test_storage)
        print('Base URL: ' + base_url)
        print('API version: ' + api_version)

        self.imaging_api = ImagingApi(client_secret, client_id, base_url,
                                      api_version)

        self._input_test_files = self.imaging_api.get_files_list(
            requests.GetFilesListRequest(self.original_data_folder,
                                         self.test_storage)).value

        self.basic_input_test_files = list(
            filter(lambda x: not str.startswith(x.name, 'multipage_'),
                   self._input_test_files))

        self.multipage_input_test_files = list(
            filter(lambda x: str.startswith(x.name, 'multipage_'),
                   self._input_test_files))
    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_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))