Skip to content

Commit

Permalink
Merge pull request #1 from repo-2017/abacus-parser-tests
Browse files Browse the repository at this point in the history
Abacus parser tests
  • Loading branch information
NullChefo authored Feb 8, 2024
2 parents 9595165 + f0d392d commit 6edad49
Show file tree
Hide file tree
Showing 20 changed files with 799 additions and 600 deletions.
61 changes: 38 additions & 23 deletions slp_abacus/resources/schemas/abacus_mapping_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,42 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Mapping File",
"type": "object",
"required": ["trustzones", "components"],
"required": [
"trustzones",
"components"
],
"properties": {
"trustzones": {
"type": "array",
"items": {
"type": "object",
"required": ["label"],
"required": [
"label"
],
"properties": {
"label": {"$ref": "#/definitions/LabelUnion"},
"type": {"$ref": "#/definitions/query"}
"label": {
"$ref": "#/definitions/LabelUnion"
},
"type": {
"$ref": "#/definitions/query"
}
}
}
},
"components": {
"type": "array",
"items": {
"type": "object",
"required": ["label"],
"required": [
"label"
],
"properties": {
"label": {"$ref": "#/definitions/LabelUnion"},
"type": {"$ref": "#/definitions/query"}
"label": {
"$ref": "#/definitions/LabelUnion"
},
"type": {
"$ref": "#/definitions/query"
}
}
}
}
Expand All @@ -35,35 +50,35 @@
}
]
},
"LabelUnion":{
"anyOf":[
"LabelUnion": {
"anyOf": [
{
"type":"array",
"items":{
"type":"string"
"type": "array",
"items": {
"type": "string"
}
},
{
"$ref":"#/definitions/RegExClass"
"$ref": "#/definitions/RegExClass"
},
{
"type":"string"
"type": "string"
}
],
"title":"LabelUnion"
"title": "LabelUnion"
},
"RegExClass":{
"type":"object",
"additionalProperties":false,
"properties":{
"$regex":{
"type":"string"
"RegExClass": {
"type": "object",
"additionalProperties": false,
"properties": {
"$regex": {
"type": "string"
}
},
"required":[
"required": [
"$regex"
],
"title":"RegExClass"
"title": "RegExClass"
}
}
}
4 changes: 2 additions & 2 deletions slp_abacus/slp_abacus/abacus_processor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from starlette.datastructures import UploadFile

from sl_util.sl_util.file_utils import get_byte_data, get_byte_data_from_upload_file, read_byte_data
from sl_util.sl_util.file_utils import get_byte_data, get_byte_data_from_upload_file
from slp_abacus.slp_abacus.load.abacus_loader import AbacusLoader
from slp_abacus.slp_abacus.load.abacus_mapping_file_loader import AbacusMappingFileLoader
from slp_abacus.slp_abacus.parse.abacus_parser import AbacusParser
Expand All @@ -14,7 +14,7 @@ class AbacusProcessor(OTMProcessor):
Abacus implementation of OTMProcessor
"""

def __init__(self, project_id: str, project_name: str, source, mappings: [bytes], diag_type=None):
def __init__(self, project_id: str, project_name: str, source, mappings: [bytes], diag_type=None):
self.project_id = project_id
self.project_name = project_name
self.source: bytes = \
Expand Down
1 change: 0 additions & 1 deletion slp_abacus/slp_abacus/load/abacus_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
import yaml

from sl_util.sl_util.file_utils import read_byte_data
from slp_abacus.slp_abacus.objects.diagram_objects import Diagram, DiagramComponent, DiagramRepresentation
Expand Down
8 changes: 8 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,12 @@ 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
6 changes: 3 additions & 3 deletions slp_abacus/slp_abacus/parse/abacus_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from otm.otm.entity.otm import OTM
from otm.otm.otm_builder import OTMBuilder
from slp_base import OTMBuildingError
from slp_base.slp_base.provider_parser import ProviderParser
from slp_base.slp_base.provider_type import DiagramType
from slp_abacus.slp_abacus.load.abacus_mapping_file_loader import AbacusMapping
from slp_abacus.slp_abacus.objects.diagram_objects import Diagram
from slp_abacus.slp_abacus.parse.diagram_mapper import DiagramMapper
from slp_abacus.slp_abacus.parse.transformers.default_trustzone_transformer import DefaultTrustZoneTransformer
from slp_base import OTMBuildingError
from slp_base.slp_base.provider_parser import ProviderParser
from slp_base.slp_base.provider_type import DiagramType

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from otm.otm.trustzone_representation_calculator import \
TrustZoneRepresentationCalculator
from sl_util.sl_util.iterations_utils import append_if_not_exists
from slp_base import OTMBuildingError
from slp_abacus.slp_abacus.objects.diagram_objects import Diagram, DiagramTrustZone, DiagramComponent
from slp_abacus.slp_abacus.parse.transformers.transformer import Transformer
from slp_base import OTMBuildingError


def _find_orphan_components(components: List[DiagramComponent]) -> List[DiagramComponent]:
Expand All @@ -25,7 +25,8 @@ def transform(self):
return

if not self.default_trustzone:
raise OTMBuildingError(title='Invalid configuration', message='A default trust zone is required with orphan components')
raise OTMBuildingError(title='Invalid configuration',
message='A default trust zone is required with orphan components')

self.__use_default_trustzone_as_global_parent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from otm.otm.entity.parent_type import ParentType
from slp_abacus.slp_abacus.objects.diagram_objects import DiagramComponent, DiagramTrustZone
from slp_abacus.slp_abacus.parse.transformers.transformer import Transformer

PARENT_TYPES = {
DiagramComponent: ParentType.COMPONENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ class AbacusMappingFileValidator(MultipleMappingFileValidator):
def __init__(self, mappings_data: [bytes]):
super(AbacusMappingFileValidator, self).__init__(
Schema.from_package('slp_abacus', self.schema_filename), mappings_data)

# def validate(self):
# logger.debug('Validating mapping files')
33 changes: 24 additions & 9 deletions slp_abacus/slp_abacus/validate/abacus_validator.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import logging
import os

import xmlschema

from sl_util.sl_util.file_utils import get_file_type_by_content
from slp_base import ProviderValidator, DiagramFileNotValidError, DiagramType
from slp_base.slp_base.provider_validator import generate_size_error, generate_content_type_error, generate_schema_error
from slp_base.slp_base.provider_validator import generate_size_error, generate_content_type_error

logger = logging.getLogger(__name__)
path = os.path.dirname(__file__)

class AbacusValidator(ProviderValidator):
MAX_SIZE = 10 * 1024 * 1024
MIN_SIZE = 10

def __init__(self):
super(AbacusValidator, self).__init__()
path = os.path.dirname(__file__)


# def validate(self):
# logger.info('Validating Abacus file')
class AbacusValidator(ProviderValidator):

def __init__(self, data):
super(AbacusValidator, self).__init__()
self.data = data
self.provider = DiagramType.ABACUS

def validate(self):
logger.info('Validating Abacus file')
self.__validate_size()
self.__validate_content_type()

def __validate_size(self):
size = len(self.data)
if size > MAX_SIZE or size < MIN_SIZE:
raise generate_size_error(self.provider, 'diag_file', DiagramFileNotValidError)

def __validate_content_type(self):
mime = get_file_type_by_content(self.data)
if mime not in self.provider.valid_mime:
raise generate_content_type_error(self.provider, 'diag_file', DiagramFileNotValidError)
Loading

0 comments on commit 6edad49

Please sign in to comment.