diff --git a/tests/data/yaml/test_complete_rule_no_params.yaml b/tests/data/yaml/test_complete_rule_no_params.yaml index f95d72f7..2df9d39f 100644 --- a/tests/data/yaml/test_complete_rule_no_params.yaml +++ b/tests/data/yaml/test_complete_rule_no_params.yaml @@ -7,6 +7,6 @@ x-trestle-rule-info: include-controls: - id: ac-1 x-trestle-component-info: - name: Component 1 - description: Component 1 description + name: Component 2 + description: Component 2 description type: service \ No newline at end of file diff --git a/tests/trestlebot/tasks/test_rule_transform_task.py b/tests/trestlebot/tasks/test_rule_transform_task.py index 9fb61200..c7d4dd2e 100644 --- a/tests/trestlebot/tasks/test_rule_transform_task.py +++ b/tests/trestlebot/tasks/test_rule_transform_task.py @@ -54,11 +54,22 @@ def test_rule_transform_task(tmp_trestle_dir: str) -> None: assert orig_comp is not None assert orig_comp.metadata.title == "Component definition for test_comp" assert orig_comp.components is not None - assert len(orig_comp.components) == 1 + assert len(orig_comp.components) == 2 component = orig_comp.components[0] assert component.props is not None + assert component.title == "Component 2" + assert len(component.props) == 2 + assert component.props[0].name == RULE_ID + assert component.props[0].value == "example_rule_2" + assert component.props[1].name == RULE_DESCRIPTION + assert component.props[1].value == "My rule description for example rule 2" + + component = orig_comp.components[1] + + assert component.props is not None + assert component.title == "Component 1" assert len(component.props) == 5 assert component.props[0].name == RULE_ID assert component.props[0].value == "example_rule_1" diff --git a/trestlebot/transformers/csv_to_yaml.py b/trestlebot/transformers/csv_to_yaml.py index f1d08172..38bb93d3 100644 --- a/trestlebot/transformers/csv_to_yaml.py +++ b/trestlebot/transformers/csv_to_yaml.py @@ -20,12 +20,7 @@ from typing import List from trestlebot.transformers.csv_transformer import ToRulesCSVTransformer -from trestlebot.transformers.trestle_rule import ( - ComponentInfo, - Control, - Profile, - TrestleRule, -) +from trestlebot.transformers.trestle_rule import TrestleRule, get_default_rule from trestlebot.transformers.yaml_transformer import FromRulesYAMLTransformer @@ -65,21 +60,8 @@ def write_to_yaml(self, filepath: pathlib.Path) -> None: def write_default_trestle_rule_keys(self, filepath: pathlib.Path) -> None: """Write default TrestleRule keys to a YAML file.""" try: - test_rule = TrestleRule( - name="example rule", - description="example description", - component=ComponentInfo( - name="example component", - type="service", - description="example description", - ), - profile=Profile( - description="example profile", - href="example href", - include_controls=[Control(id="example")], - ), - ) - self._yaml_transformer.write_to_file(test_rule, filepath) + default_rule: TrestleRule = get_default_rule() + self._yaml_transformer.write_to_file(default_rule, filepath) except Exception as e: raise YAMLWriteError( f"Failed to write empty TrestleRule keys to YAML file: {e}" diff --git a/trestlebot/transformers/csv_transformer.py b/trestlebot/transformers/csv_transformer.py index 3c7f6587..5fd004db 100644 --- a/trestlebot/transformers/csv_transformer.py +++ b/trestlebot/transformers/csv_transformer.py @@ -52,6 +52,7 @@ Parameter, Profile, TrestleRule, + get_default_rule, ) @@ -218,7 +219,13 @@ def write_to_file(self, filepath: pathlib.Path) -> None: fieldnames.extend(self._csv_columns.get_required_column_names()) fieldnames.extend(self._csv_columns.get_optional_column_names()) + # The trestle csv_to_oscal_cd task skips the header row and the + # first row which is meant to have descriptions. We will just write a default right now. + default_rule: TrestleRule = get_default_rule() + example_row = self._transformer.transform(default_rule) + writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() + writer.writerow(example_row) for row in self._rows: writer.writerow(row) diff --git a/trestlebot/transformers/trestle_rule.py b/trestlebot/transformers/trestle_rule.py index 23be279a..ae30c605 100644 --- a/trestlebot/transformers/trestle_rule.py +++ b/trestlebot/transformers/trestle_rule.py @@ -66,3 +66,21 @@ class TrestleRule(BaseModel): component: ComponentInfo parameter: Optional[Parameter] profile: Profile + + +def get_default_rule() -> TrestleRule: + """Create a default rule for template purposes.""" + return TrestleRule( + name="example rule", + description="example description", + component=ComponentInfo( + name="example component", + type="service", + description="example description", + ), + profile=Profile( + description="example profile", + href="example href", + include_controls=[Control(id="example")], + ), + )