diff --git a/ted_sws/data_manager/adapters/sparql_endpoint.py b/ted_sws/data_manager/adapters/sparql_endpoint.py index 88b43c1d8..580901929 100644 --- a/ted_sws/data_manager/adapters/sparql_endpoint.py +++ b/ted_sws/data_manager/adapters/sparql_endpoint.py @@ -35,13 +35,14 @@ class SPARQLClientPool(object): connection_pool = {} @staticmethod - def create_or_reuse_connection(endpoint_url: str): + def create_or_reuse_connection(endpoint_url: str, use_env_credentials: bool = True): if endpoint_url not in SPARQLClientPool.connection_pool: sparql_wrapper = SPARQLWrapper(endpoint_url) - sparql_wrapper.setCredentials( - user=config.AGRAPH_SUPER_USER, - passwd=config.AGRAPH_SUPER_PASSWORD - ) + if use_env_credentials: + sparql_wrapper.setCredentials( + user=config.AGRAPH_SUPER_USER, + passwd=config.AGRAPH_SUPER_PASSWORD + ) SPARQLClientPool.connection_pool[endpoint_url] = sparql_wrapper return SPARQLClientPool.connection_pool[endpoint_url] @@ -117,8 +118,8 @@ def add_data_to_repository(self, file_content, repository_name, mime_type): class SPARQLTripleStoreEndpoint(TripleStoreEndpointABC): - def __init__(self, endpoint_url: str): - self.endpoint = SPARQLClientPool.create_or_reuse_connection(endpoint_url) + def __init__(self, endpoint_url: str, use_env_credentials: bool = True): + self.endpoint = SPARQLClientPool.create_or_reuse_connection(endpoint_url, use_env_credentials) def _set_sparql_query(self, sparql_query: str): """ diff --git a/ted_sws/notice_validator/services/check_availability_of_notice_in_cellar.py b/ted_sws/notice_validator/services/check_availability_of_notice_in_cellar.py new file mode 100644 index 000000000..75ea75648 --- /dev/null +++ b/ted_sws/notice_validator/services/check_availability_of_notice_in_cellar.py @@ -0,0 +1,13 @@ +from ted_sws.data_manager.adapters.sparql_endpoint import SPARQLTripleStoreEndpoint + +WEBAPI_SPARQL_URL = "https://publications.europa.eu/webapi/rdf/sparql" +CELLAR_NOTICE_AVAILABILITY_QUERY = "ASK {{ VALUES ?instance {{<{notice_uri}>}} ?instance ?predicate [] . }}" +WEBAPI_SPARQL_RUN_FORMAT = "application/sparql-results+json" + + +def check_availability_of_notice_in_cellar(notice_uri: str, endpoint_url: str = WEBAPI_SPARQL_URL) -> bool: + query = CELLAR_NOTICE_AVAILABILITY_QUERY.format(notice_uri=notice_uri) + result = SPARQLTripleStoreEndpoint( + endpoint_url=endpoint_url, + use_env_credentials=False).with_query(sparql_query=query).fetch_tree() + return result['boolean'] diff --git a/tests/e2e/conftest.py b/tests/e2e/conftest.py index dfe1d6aae..931ece3e8 100644 --- a/tests/e2e/conftest.py +++ b/tests/e2e/conftest.py @@ -39,3 +39,8 @@ def fake_mapping_suite_id() -> str: @pytest.fixture def fuseki_triple_store(): return FusekiAdapter(host=config.FUSEKI_ADMIN_HOST, user=config.FUSEKI_ADMIN_USER, password=config.FUSEKI_ADMIN_PASSWORD) + + +@pytest.fixture +def cellar_sparql_endpoint(): + return "https://publications.europa.eu/webapi/rdf/sparql" diff --git a/tests/e2e/data_manager/conftest.py b/tests/e2e/data_manager/conftest.py index 5c541c165..946410dc8 100644 --- a/tests/e2e/data_manager/conftest.py +++ b/tests/e2e/data_manager/conftest.py @@ -6,6 +6,7 @@ SHACLTestSuite, TransformationTestData, MappingSuite from tests import TEST_DATA_PATH + @pytest.fixture def query_content(): return """# title: Official name @@ -213,7 +214,6 @@ def path_to_file_system_repository(): @pytest.fixture def notice_with_distilled_status(notice_2020, rdf_file_content): - notice_2020.update_status_to(new_status=NoticeStatus.NORMALISED_METADATA) notice_2020.update_status_to(new_status=NoticeStatus.ELIGIBLE_FOR_TRANSFORMATION) notice_2020.update_status_to(new_status=NoticeStatus.PREPROCESSED_FOR_TRANSFORMATION) @@ -234,7 +234,3 @@ def fake_repository_path(): def invalid_mapping_suite_id() -> str: return "test_invalid_package" - -@pytest.fixture -def cellar_sparql_endpoint(): - return "https://publications.europa.eu/webapi/rdf/sparql" \ No newline at end of file diff --git a/tests/e2e/notice_metadata_processor/conftest.py b/tests/e2e/notice_metadata_processor/conftest.py index 5c541c165..5c19fd48a 100644 --- a/tests/e2e/notice_metadata_processor/conftest.py +++ b/tests/e2e/notice_metadata_processor/conftest.py @@ -233,8 +233,3 @@ def fake_repository_path(): @pytest.fixture def invalid_mapping_suite_id() -> str: return "test_invalid_package" - - -@pytest.fixture -def cellar_sparql_endpoint(): - return "https://publications.europa.eu/webapi/rdf/sparql" \ No newline at end of file diff --git a/tests/e2e/notice_validator/conftest.py b/tests/e2e/notice_validator/conftest.py index 6fd55f2ce..4c38a5237 100644 --- a/tests/e2e/notice_validator/conftest.py +++ b/tests/e2e/notice_validator/conftest.py @@ -41,3 +41,13 @@ def fake_notice_F03_content(fake_repository_path, fake_mapping_suite_F03_id): def fake_notice_F03(fake_notice_F03_content, fake_notice_id): xml_manifestation = XMLManifestation(object_data=fake_notice_F03_content) return Notice(ted_id=fake_notice_id, xml_manifestation=xml_manifestation) + + +@pytest.fixture +def valid_cellar_uri(): + return 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' + + +@pytest.fixture +def invalid_cellar_uri(): + return 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type-invalid' diff --git a/tests/e2e/notice_validator/test_ check_availability_of_notice_in_cellar.py b/tests/e2e/notice_validator/test_ check_availability_of_notice_in_cellar.py new file mode 100644 index 000000000..19a6822de --- /dev/null +++ b/tests/e2e/notice_validator/test_ check_availability_of_notice_in_cellar.py @@ -0,0 +1,9 @@ +from ted_sws.notice_validator.services.check_availability_of_notice_in_cellar import \ + check_availability_of_notice_in_cellar + + +def test_check_availability_of_notice_in_cellar(cellar_sparql_endpoint, valid_cellar_uri, invalid_cellar_uri): + assert check_availability_of_notice_in_cellar(notice_uri=valid_cellar_uri, + endpoint_url=cellar_sparql_endpoint) + assert not check_availability_of_notice_in_cellar(notice_uri=invalid_cellar_uri, + endpoint_url=cellar_sparql_endpoint)