Skip to content

Commit

Permalink
add decision point registry for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ahouseholder committed Oct 12, 2023
1 parent 004c587 commit ffdbd31
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/ssvc/decision_points/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@
created_at: 9/20/23 10:07 AM
"""

from dataclasses import dataclass
from typing import Tuple
import logging
from dataclasses import dataclass, field
from typing import ClassVar, Dict, Tuple

from dataclasses_json import dataclass_json
from dataclasses_json import config, dataclass_json

from ssvc._mixins import _Base, _Keyed, _Namespaced, _Versioned

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class _DecisionPoints:
"""
A collection of SSVC decision points.
"""

registry: ClassVar[Dict[str, "SsvcDecisionPoint"]] = {}

def __iter__(self):
return iter(self.registry.values())


REGISTERED_DECISION_POINTS = _DecisionPoints()


@dataclass_json
@dataclass(kw_only=True)
Expand All @@ -32,6 +50,16 @@ class SsvcDecisionPoint(_Base, _Keyed, _Versioned, _Namespaced):

values: Tuple[SsvcDecisionPointValue]

# this is only for our own use in Python land, exclude it from serialization
_fullname: str = field(
init=False, repr=False, default=None, metadata=config(exclude=lambda x: True)
)

def __post_init__(self):
self._fullname = f"{self.namespace} {self.name} v{self.version}"
logging.debug(f"Add {self._fullname} to registry")
REGISTERED_DECISION_POINTS.registry[self._fullname] = self

def to_table(self):
rows = []
rows.append(f"{self.description}")
Expand Down
45 changes: 45 additions & 0 deletions src/test/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
import unittest

import jsonschema

import ssvc.decision_points # noqa F401
from ssvc.decision_points.base import REGISTERED_DECISION_POINTS


class MyTestCase(unittest.TestCase):
def test_validation(self):
schema = json.load(open("../../data/schema/Decision_Point.schema.json"))
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
hdlr = logging.StreamHandler()
logger.addHandler(hdlr)

# importing these causes the decision points to register themselves
from ssvc.dp_groups.v1 import SSVCv1 # noqa
from ssvc.dp_groups.v2 import SSVCv2 # noqa
from ssvc.dp_groups.v2_1 import SSVCv2_1 # noqa
from ssvc.dp_groups.cvss.v1 import CVSSv1 # noqa
from ssvc.dp_groups.cvss.v2 import CVSSv2 # noqa
from ssvc.dp_groups.cvss.v3 import CVSSv3 # noqa

for dp in REGISTERED_DECISION_POINTS:
exp = None
as_json = dp.to_json()
loaded = json.loads(as_json)

try:
jsonschema.validate(loaded, schema)
except jsonschema.exceptions.ValidationError as e:
exp = e

self.assertIsNone(exp, f"Validation failed for {dp.name} {dp.version}")
logger.debug(
f"Validation passed for ({dp.namespace}) {dp.name} v{dp.version}"
)


if __name__ == "__main__":
unittest.main()

0 comments on commit ffdbd31

Please sign in to comment.