Skip to content

Commit

Permalink
reformat code files with IDE reformat and added abacus test parse and…
Browse files Browse the repository at this point in the history
… abacus test load
  • Loading branch information
Stefan Kehayov committed Feb 8, 2024
1 parent 976b77c commit f0d392d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
9 changes: 9 additions & 0 deletions slp_abacus/slp_abacus/load/abacus_mapping_file_loader.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand All @@ -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):

Expand Down
28 changes: 28 additions & 0 deletions slp_abacus/tests/unit/load/test_abacus_load.py
Original file line number Diff line number Diff line change
@@ -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()
62 changes: 62 additions & 0 deletions slp_abacus/tests/unit/parse/test_abacus_parse.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions slp_abacus/tests/validate/test_abacus_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
Expand Down

0 comments on commit f0d392d

Please sign in to comment.