diff --git a/core/esmf-aspect-meta-model-python/scripts/download_test_models.py b/core/esmf-aspect-meta-model-python/scripts/download_test_models.py new file mode 100644 index 0000000..883495f --- /dev/null +++ b/core/esmf-aspect-meta-model-python/scripts/download_test_models.py @@ -0,0 +1,67 @@ +# Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH +# +# See the AUTHORS file(s) distributed with this work for additional +# information regarding authorship. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 + +from os import mkdir, remove +from os.path import exists, join +from pathlib import Path +from zipfile import ZipFile + +import requests + + +FOLDER_TO_EXTRACT = "valid" +TEST_MODELS_PATH = join("tests", "integration", "java_models", "resources") +VERSION = "2.7.0" + + +def get_model_files_path(version: str) -> str: + """Get a path for storing test models.""" + base_path = Path(__file__).parents[1].absolute() + models_path = join(base_path, TEST_MODELS_PATH, f"esmf-test-aspect-models-{version}.jar") + + return models_path + + +def download_test_models(version: str = VERSION): + """Downloads and extract the esmf-test-aspect-models.""" + model_files_path = get_model_files_path(version) + + print(f"Start downloading esmf-test-aspect-models version {version}") + url = ( + f"https://repo1.maven.org/maven2/org/eclipse/esmf/esmf-test-aspect-models/{version}/" + f"esmf-test-aspect-models-{version}.jar" + ) + response = requests.get(url, allow_redirects=True) + + resource_folder = Path(model_files_path).parent.absolute() + if not exists(resource_folder): + mkdir(resource_folder) + + with open(model_files_path, "wb") as f: + f.write(response.content) + print("JAR-File Downloaded") + + print(f"Start extracting files from {model_files_path}") + extracted_file_path = Path(model_files_path).parents[0].absolute() + archive = ZipFile(model_files_path) + for file_name in archive.namelist(): + if file_name.startswith(FOLDER_TO_EXTRACT): + archive.extract(file_name, extracted_file_path) + + archive.close() + print("Done extracting files.") + + print("Deleting esmf-test-aspect-models JAR file.") + remove(model_files_path) + + +if __name__ == "__main__": + download_test_models() diff --git a/core/esmf-aspect-meta-model-python/tests/integration/java_models/__init__.py b/core/esmf-aspect-meta-model-python/tests/integration/java_models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py b/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py new file mode 100644 index 0000000..fbdc0fb --- /dev/null +++ b/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py @@ -0,0 +1,78 @@ +"""Collect statistics about loading test Aspect models.""" + +import csv + +from glob import glob +from os.path import join +from pathlib import Path + +from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader + +SAMM_VERSION = "2.0.0" + + +def get_test_files(): + """Get ttl models for testing.""" + base_path = Path(__file__).parent.absolute() + samm_folder_name = f"samm_{SAMM_VERSION.replace('.', '_')}" + search_pattern = join(base_path, "resources", "**", samm_folder_name, "**", "*.ttl") + test_model_files = glob(search_pattern, recursive=True) + + return test_model_files + + +def load_test_models(): + """Test for loading Aspect models.""" + test_files = get_test_files() + result = [] + all_test_files = len(test_files) + i = 0 + step = 10 + print("Loading test Aspect models...") + + for test_file in test_files: + i += 1 + if i % step == 0: + print(f"{i}/{all_test_files}") + + test_file_path = Path(test_file) + data = { + "file_name": test_file_path.name, + "folder_name": join(test_file_path.parents[1].name, test_file_path.parents[0].name), + "status": "initializing", + "error": None, + } + + try: + loader = AspectLoader() + model_elements = loader.load_aspect_model(test_file) + if not model_elements: + raise Exception("No elements loaded") + except Exception as error: + data["error"] = str(error) + data["status"] = "exception" + else: + data["status"] = "success" + + result.append(data) + + print(f"{i}/{all_test_files}") + return result + + +def run_test_loading(): + """Run loading of all test Aspect models.""" + report = load_test_models() + + base_path = Path(__file__).parent.absolute() + with open(join(base_path, "loading_models_test_report.csv"), "w", newline="") as csvfile: + fieldnames = ["folder_name", "file_name", "status", "error"] + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + + writer.writeheader() + for row in report: + writer.writerow(row) + + +if __name__ == "__main__": + run_test_loading()