From f0d392d26a0403a8ea35022c241ca877d5cd0d48 Mon Sep 17 00:00:00 2001 From: Stefan Kehayov Date: Thu, 8 Feb 2024 13:20:35 +0200 Subject: [PATCH] reformat code files with IDE reformat and added abacus test parse and abacus test load --- .../load/abacus_mapping_file_loader.py | 9 +++ .../tests/unit/load/test_abacus_load.py | 28 +++++++++ .../tests/unit/parse/test_abacus_parse.py | 62 +++++++++++++++++++ .../tests/validate/test_abacus_validator.py | 3 +- 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 slp_abacus/tests/unit/load/test_abacus_load.py create mode 100644 slp_abacus/tests/unit/parse/test_abacus_parse.py diff --git a/slp_abacus/slp_abacus/load/abacus_mapping_file_loader.py b/slp_abacus/slp_abacus/load/abacus_mapping_file_loader.py index 84dae66b..471d4858 100644 --- a/slp_abacus/slp_abacus/load/abacus_mapping_file_loader.py +++ b/slp_abacus/slp_abacus/load/abacus_mapping_file_loader.py @@ -1,6 +1,8 @@ import logging from typing import List, Dict +import yaml + from slp_base.slp_base.mapping_file_loader import MappingFileLoader logger = logging.getLogger(__name__) @@ -11,6 +13,13 @@ def __init__(self, trustzones: List[Dict], components: List[Dict]): self.trustzones: List[Dict] = trustzones self.components: List[Dict] = components + @classmethod + def from_yaml(cls, yaml_content: str): + yaml_data = yaml.safe_load(yaml_content) + trustzones = yaml_data.get('trustzones', []) + components = yaml_data.get('components', []) + return cls(trustzones, components) + class AbacusMappingFileLoader(MappingFileLoader): diff --git a/slp_abacus/tests/unit/load/test_abacus_load.py b/slp_abacus/tests/unit/load/test_abacus_load.py new file mode 100644 index 00000000..25003bf5 --- /dev/null +++ b/slp_abacus/tests/unit/load/test_abacus_load.py @@ -0,0 +1,28 @@ +from unittest.mock import patch + +import pytest + +from slp_abacus.slp_abacus.load.abacus_loader import AbacusLoader +from slp_base import LoadingDiagramFileError + +# Assuming your test data is stored in a variable named 'test_data' +test_data = { + "OutConnections": [ + {"EEID": 1, "SinkComponentName": "Component1", "ConnectionTypeName": "Type1"}, + {"EEID": 2, "SinkComponentName": "Component2", "ConnectionTypeName": "Type2"}, + ] +} + + +class TestAbacusLoader: + + @pytest.fixture + def abacus_loader_instance(self): + return AbacusLoader(project_id='test_project', abacus_source='abacus_merged', mapping_files=[]) + + @patch('sl_util.sl_util.file_utils.read_byte_data') + def test_load_failure(self, mock_read_byte_data, abacus_loader_instance): + mock_read_byte_data.side_effect = Exception('Test Exception') + + with pytest.raises(LoadingDiagramFileError): + abacus_loader_instance.load() diff --git a/slp_abacus/tests/unit/parse/test_abacus_parse.py b/slp_abacus/tests/unit/parse/test_abacus_parse.py new file mode 100644 index 00000000..3610a346 --- /dev/null +++ b/slp_abacus/tests/unit/parse/test_abacus_parse.py @@ -0,0 +1,62 @@ +from unittest.mock import MagicMock, patch + +import pytest +import yaml + +from slp_abacus.slp_abacus.abacus_processor import AbacusParser +from slp_abacus.slp_abacus.load.abacus_mapping_file_loader import AbacusMapping +from slp_abacus.slp_abacus.objects.diagram_objects import Diagram, DiagramRepresentation + +mapping_config = """ +trustzones: + - label: Public Cloud + type: b61d6911-338d-46a8-9f39-8dcd24abfe91 + default: true + +components: + - label: SST PoC Integrator + type: CD-MSG-BROKER + - label: SST PoC Webpage + type: compact-server-side-web-application + - label: SST PoC Database + type: other-database + - label: SST PoC Static Content + type: CD-CONTENT-DELIVERY-NETWORK + - label: SST PoC Backend + type: back-end-server + - label: Angular v12.0.0 + type: web-client + - label: SST PoC Webpage + type: compact-server-side-web-application +""" + + +class TestAbacusParser: + + @pytest.fixture + def abacus_parser_instance(self): + project_id = 'test_project_id' + project_name = 'Test Project' + diagram = Diagram(DiagramRepresentation(project_id, {'width': 1000, 'height': 1000}), [], [], []) + mapping_data = yaml.safe_load(mapping_config) + abacus_mapping = AbacusMapping(**mapping_data) + return AbacusParser(project_id, project_name, diagram, abacus_mapping) + + @patch('slp_abacus.slp_abacus.parse.diagram_mapper.DiagramMapper') + @patch('slp_abacus.slp_abacus.parse.transformers.default_trustzone_transformer.DefaultTrustZoneTransformer') + @patch('otm.otm.otm_builder.OTMBuilder') + def test_build_otm_success(self, mock_otm_builder, mock_default_trustzone_transformer, mock_diagram_mapper, + abacus_parser_instance): + expected_project_id = 'test_project_id' + expected_project_name = 'Test Project' + + mock_otm_instance = MagicMock() + mock_otm_builder.return_value.build.return_value = mock_otm_instance + + otm = abacus_parser_instance.build_otm() + + assert otm.project_id == expected_project_id + assert otm.project_name == expected_project_name + + mock_diagram_mapper(abacus_parser_instance.diagram, abacus_parser_instance.mapping) + mock_default_trustzone_transformer(abacus_parser_instance.diagram) diff --git a/slp_abacus/tests/validate/test_abacus_validator.py b/slp_abacus/tests/validate/test_abacus_validator.py index a353c734..5f830276 100644 --- a/slp_abacus/tests/validate/test_abacus_validator.py +++ b/slp_abacus/tests/validate/test_abacus_validator.py @@ -5,8 +5,7 @@ from sl_util.sl_util import secure_regex as re from sl_util.sl_util.file_utils import get_byte_data from slp_abacus.slp_abacus.validate.abacus_validator import AbacusValidator -from slp_abacus.tests.resources.test_resource_paths import wrong_root_abacus, wrong_mxcell_abacus, wrong_mxfile_abacus, \ - wrong_mxgraphmodel_abacus, not_xml, abacus_merged +from slp_abacus.tests.resources.test_resource_paths import abacus_merged from slp_base import DiagramFileNotValidError, CommonError filename_pattern = re.compile('^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}\\.[abacus|xml]')