Skip to content

Commit

Permalink
fix: updates ssp add-props to replace existing properties
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jan 19, 2025
1 parent bdb6444 commit d133a6b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
15 changes: 10 additions & 5 deletions tests/trestle/core/commands/author/ssp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,14 +1160,19 @@ def test_ssp_gen_and_assemble_add_props(tmp_trestle_dir: pathlib.Path) -> None:
impl_reqs = assem_ssp.control_implementation.implemented_requirements
impl_req = next((i_req for i_req in impl_reqs if i_req.control_id == 'ac-1'), None)
assert len(impl_req.props) == 1
assert impl_req.props[0].name == 'prop_with_ns'
assert impl_req.props[0].value == 'prop with ns'
assert impl_req.props[0].ns == 'https://my_new_namespace'
assert impl_req.props[0].name == 'prop_with_ns' # type: ignore
assert impl_req.props[0].value == 'prop with ns' # type: ignore
assert impl_req.props[0].ns == 'https://my_new_namespace' # type: ignore

smt_a = next((smt for smt in impl_req.statements if smt.statement_id == 'ac-1_smt.a'), None)
assert len(smt_a.props) == 1
assert smt_a.props[0].name == 'smt_prop'
assert smt_a.props[0].value == 'smt prop'
assert smt_a.props[0].name == 'smt_prop' # type: ignore
assert smt_a.props[0].value == 'smt prop' # type: ignore

# Run again and check that there is no change
assert ssp_assemble._run(args) == 0
assem_ssp_2, _ = ModelUtils.load_model_for_class(tmp_trestle_dir, ssp_name, ossp.SystemSecurityPlan)
assert assem_ssp_2.metadata.last_modified == assem_ssp.metadata.last_modified


def test_ssp_gen_and_assemble_implementation_parts(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
Expand Down
4 changes: 2 additions & 2 deletions trestle/core/catalog/catalog_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _add_props_to_imp_req(
# add the props at control level
if props:
imp_req.props = as_list(imp_req.props)
imp_req.props.extend(props)
ControlInterface.reconcile_props(imp_req, props)

# add the props at the part level
for label, part_id in control_part_id_map.items():
Expand All @@ -359,7 +359,7 @@ def _add_props_to_imp_req(
for statement in as_list(imp_req.statements):
if statement.statement_id == part_id:
statement.props = as_list(statement.props)
statement.props.extend(props)
ControlInterface.reconcile_props(statement, props)

@staticmethod
def _update_ssp_with_md_header(
Expand Down
12 changes: 12 additions & 0 deletions trestle/core/control_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,18 @@ def insert_status_in_props(item: TypeWithProps, status: common.ImplementationSta
prop = ControlInterface._status_as_prop(status)
ControlInterface._replace_prop(item, prop)

@staticmethod
def reconcile_props(item: TypeWithProps, props: List[common.Property]) -> None:
"""Add properties to an item with properties while replacing existing."""
names = [prop.name for prop in as_list(item.props)]
item.props = as_list(item.props)
for prop in props:
if prop.name in names:
index = names.index(prop.name)
item.props[index] = prop
else:
item.props.append(prop)

@staticmethod
def _copy_status_in_props(dest: TypeWithProps, src: TypeWithProps) -> None:
"""Copy status in props from one object to another."""
Expand Down

0 comments on commit d133a6b

Please sign in to comment.