# Path: data_catalog/dataset_delete.py
# class DataSetRemover(DataCatalogModel):
# 
#     """
#     Framework-agnostic object for removing data sets.
#     """
# 
#     def delete(self, entry_id, token):
#         """
#         Deletes data set information from ElasticSearch and requests deleting from other services.
#         :param entry_id: elastic search id
#         :param token: authorization token
#         :raises NotFoundError: entry not found in Elastic Search
#         :raises ConnectionError: problem with connecting to Elastic Search
#         """
#         elastic_data = self._get_entry(entry_id)
#         metadata = elastic_data["_source"]
#         target_uri = elastic_data["_source"]["targetUri"]
# 
#         self._delete_entry(entry_id)
# 
#         return {
#             "deleted_from_downloader": self._delete_from_downloader(target_uri, token),
#             "deleted_from_publisher": self._delete_from_dataset_publisher(metadata, token)
#         }
# 
#     def delete_public_from_hive(self, entry_id, token):
#         """
#         Attempts to remove a public data set from dataset-publisher.
#         Doesn't do anything with non-public data sets.
#         :param str entry_id:
#         :param token:
#         :return: True if something was deleted, False otherwise.
#         """
#         elastic_data = self._get_entry(entry_id)
#         metadata = elastic_data["_source"]
#         if metadata["isPublic"]:
#             delete_url = self._config.services_url.dataset_publisher_url
#             params = {"scope": "public"}
#             return self._external_delete('Dataset Publisher', token, delete_url, metadata, params)
# 
#     def _delete_from_downloader(self, target_uri, token):
#         delete_url = self._create_downloader_delete_url(target_uri)
#         return self._external_delete('Downloader', token, delete_url)
# 
#     def _delete_from_dataset_publisher(self, metadata, token):
#         delete_url = self._config.services_url.dataset_publisher_url
#         return self._external_delete('Dataset Publisher', token, delete_url, metadata)
# 
#     def _external_delete(self, service_name, token, url, data=None, parameters=None):
#         """
#         Deletes a data set from an external service.
#         :param str service_name: Service name. Only used in logging.
#         :param str token: Security token that will be sent with the request.
#         :param str url: URL to which to send DELETE message.
#         :param dict data: Data to send in the DELETE request.
#         :param dict parameters: parameters to send in the DELETE request
#         :return: True if delete was successful, false otherwise
#         :rtype: bool
#         """
#         self._log.info('Sending delete request to: %s', url)
#         response = requests.delete(url,
#                                    headers={'Authorization': token},
#                                    json=data,
#                                    params=parameters)
#         if response.status_code == 200:
#             return True
#         else:
#             self._log.warning('Failed to delete data set from %s.', service_name)
#             return False
# 
#     def _create_downloader_delete_url(self, target_uri):
#         database_id = self._get_database_id_from_uri(target_uri)
#         return self._config.services_url.downloader_url_pattern.format(database_id)
# 
#     @staticmethod
#     def _get_database_id_from_uri(target_uri):
#         target_uris = target_uri.split("/")
#         database_id = target_uris[-2]
#         return database_id
# 
# Path: tests/base_test.py
# class DataCatalogTestCase(unittest.TestCase):
#     def setUp(self):
#         setup_fake_env()
#         self._config = DCConfig()
# 
#         self.app = data_catalog.app._create_app(self._config)
#         self.app.config['TESTING'] = True
#         self.client = self.app.test_client()
# 
#         self.mocked_authenticate = disable_authentication(self.client)
# 
#     def tearDown(self):
#         clean_fake_env()


import requests
from mock import MagicMock, call
from ddt import ddt, data, unpack
from elasticsearch.exceptions import NotFoundError, ConnectionError
from data_catalog.dataset_delete import DataSetRemover
from tests.base_test import DataCatalogTestCase
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#



@ddt
class DataSetDeleteTest(DataCatalogTestCase):
    DATA_SET_ID = 'test-entry-id'
    AUTH_TOKEN = 'authorization-token'
    DATABASE_ID = 'database_id'
    TARGET_URI = 'hdfs://URI/DATA/{}/000000_1'.format(DATABASE_ID)
    MOCK_GET = {'_source': {'targetUri': TARGET_URI}}

    def setUp(self):
        super(DataSetDeleteTest, self).setUp()

        self._delete_obj = DataSetRemover()
        self._delete_obj._elastic_search.delete = self._mock_es_delete = MagicMock()
        self._delete_obj._elastic_search.get = self._mock_es_get = MagicMock()
        self._delete_obj._elastic_search.indices.flush = self._mock_es_flush = MagicMock()
        self._mock_es_get.return_value = self.MOCK_GET
        requests.delete = self._mock_req_delete = MagicMock()

    @data(NotFoundError, ConnectionError)
    def test_delete_elasticDeleteErroneous(self, error):
        self._mock_es_delete.side_effect = error
        with self.assertRaises(error):

# Path: data_catalog/dataset_delete.py
# class DataSetRemover(DataCatalogModel):
# 
#     """
#     Framework-agnostic object for removing data sets.
#     """
# 
#     def delete(self, entry_id, token):
#         """
#         Deletes data set information from ElasticSearch and requests deleting from other services.
#         :param entry_id: elastic search id
#         :param token: authorization token
#         :raises NotFoundError: entry not found in Elastic Search
#         :raises ConnectionError: problem with connecting to Elastic Search
#         """
#         elastic_data = self._get_entry(entry_id)
#         metadata = elastic_data["_source"]
#         target_uri = elastic_data["_source"]["targetUri"]
# 
#         self._delete_entry(entry_id)
# 
#         return {
#             "deleted_from_downloader": self._delete_from_downloader(target_uri, token),
#             "deleted_from_publisher": self._delete_from_dataset_publisher(metadata, token)
#         }
# 
#     def delete_public_from_hive(self, entry_id, token):
#         """
#         Attempts to remove a public data set from dataset-publisher.
#         Doesn't do anything with non-public data sets.
#         :param str entry_id:
#         :param token:
#         :return: True if something was deleted, False otherwise.
#         """
#         elastic_data = self._get_entry(entry_id)
#         metadata = elastic_data["_source"]
#         if metadata["isPublic"]:
#             delete_url = self._config.services_url.dataset_publisher_url
#             params = {"scope": "public"}
#             return self._external_delete('Dataset Publisher', token, delete_url, metadata, params)
# 
#     def _delete_from_downloader(self, target_uri, token):
#         delete_url = self._create_downloader_delete_url(target_uri)
#         return self._external_delete('Downloader', token, delete_url)
# 
#     def _delete_from_dataset_publisher(self, metadata, token):
#         delete_url = self._config.services_url.dataset_publisher_url
#         return self._external_delete('Dataset Publisher', token, delete_url, metadata)
# 
#     def _external_delete(self, service_name, token, url, data=None, parameters=None):
#         """
#         Deletes a data set from an external service.
#         :param str service_name: Service name. Only used in logging.
#         :param str token: Security token that will be sent with the request.
#         :param str url: URL to which to send DELETE message.
#         :param dict data: Data to send in the DELETE request.
#         :param dict parameters: parameters to send in the DELETE request
#         :return: True if delete was successful, false otherwise
#         :rtype: bool
#         """
#         self._log.info('Sending delete request to: %s', url)
#         response = requests.delete(url,
#                                    headers={'Authorization': token},
#                                    json=data,
#                                    params=parameters)
#         if response.status_code == 200:
#             return True
#         else:
#             self._log.warning('Failed to delete data set from %s.', service_name)
#             return False
# 
#     def _create_downloader_delete_url(self, target_uri):
#         database_id = self._get_database_id_from_uri(target_uri)
#         return self._config.services_url.downloader_url_pattern.format(database_id)
# 
#     @staticmethod
#     def _get_database_id_from_uri(target_uri):
#         target_uris = target_uri.split("/")
#         database_id = target_uris[-2]
#         return database_id
# 
# Path: tests/base_test.py
# class DataCatalogTestCase(unittest.TestCase):
#     def setUp(self):
#         setup_fake_env()
#         self._config = DCConfig()
# 
#         self.app = data_catalog.app._create_app(self._config)
#         self.app.config['TESTING'] = True
#         self.client = self.app.test_client()
# 
#         self.mocked_authenticate = disable_authentication(self.client)
# 
#     def tearDown(self):
#         clean_fake_env()


# Path: tests/test_dataset_delete.py
import requests
from mock import MagicMock, call
from ddt import ddt, data, unpack
from elasticsearch.exceptions import NotFoundError, ConnectionError
from data_catalog.dataset_delete import DataSetRemover
from tests.base_test import DataCatalogTestCase

# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#



@ddt
class DataSetDeleteTest(DataCatalogTestCase):
    DATA_SET_ID = 'test-entry-id'
    AUTH_TOKEN = 'authorization-token'
    DATABASE_ID = 'database_id'
    TARGET_URI = 'hdfs://URI/DATA/{}/000000_1'.format(DATABASE_ID)
    MOCK_GET = {'_source': {'targetUri': TARGET_URI}}

    def setUp(self):
        super(DataSetDeleteTest, self).setUp()

        self._delete_obj = DataSetRemover()
        self._delete_obj._elastic_search.delete = self._mock_es_delete = MagicMock()
        self._delete_obj._elastic_search.get = self._mock_es_get = MagicMock()
        self._delete_obj._elastic_search.indices.flush = self._mock_es_flush = MagicMock()
        self._mock_es_get.return_value = self.MOCK_GET
        requests.delete = self._mock_req_delete = MagicMock()

    @data(NotFoundError, ConnectionError)
    def test_delete_elasticDeleteErroneous(self, error):
        self._mock_es_delete.side_effect = error
        with self.assertRaises(error):

            self._delete_obj.delete(self.DATA_SET_ID, self.AUTH_TOKEN)