Skip to content

Commit

Permalink
feat: Add layer custom field
Browse files Browse the repository at this point in the history
Added `layer` custom field to generic and diagram serializer.
  • Loading branch information
ewuerger committed Dec 12, 2024
1 parent 91afc12 commit 64d0692
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
19 changes: 19 additions & 0 deletions capella2polarion/converters/element_converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""Objects for serialization of capella objects to workitems."""

from __future__ import annotations

import collections
Expand Down Expand Up @@ -33,6 +34,14 @@
logger = logging.getLogger(__name__)
C2P_IMAGE_PREFIX = "__C2P__"
JINJA_RENDERED_IMG_CLS = "jinja-rendered-image"
ARCHITECTURE_LAYERS: dict[str, str] = {
"common": "Common",
"oa": "Operational Analysis",
"sa": "System Analysis",
"la": "Logical Architecture",
"pa": "Physical Architecture",
"epbs": "EPBS",
}


def resolve_element_type(type_: str) -> str:
Expand Down Expand Up @@ -424,6 +433,10 @@ def __generic_work_item(
obj, raw_description or markupsafe.Markup("")
)
converter_data.description_references = uuids
layer = polarion_api.TextContent(
type="string",
value=ARCHITECTURE_LAYERS.get(converter_data.layer, "UNKNOWN"),
)
requirement_types = self._get_requirement_types_text(obj)

converter_data.work_item = data_model.CapellaWorkItem(
Expand All @@ -433,6 +446,7 @@ def __generic_work_item(
uuid_capella=obj.uuid,
description=polarion_api.HtmlContent(value),
status="open",
layer=layer,
**requirement_types, # type:ignore[arg-type]
)
assert converter_data.work_item is not None
Expand All @@ -451,6 +465,10 @@ def _diagram(
assert converter_data.work_item is not None
assert isinstance(diagram, m.Diagram)
work_item_id = converter_data.work_item.id
layer = polarion_api.TextContent(
type="string",
value=ARCHITECTURE_LAYERS.get(converter_data.layer, "UNKNOWN"),
)

diagram_html, attachment = self._draw_diagram_svg(
diagram,
Expand All @@ -473,6 +491,7 @@ def _diagram(
uuid_capella=diagram.uuid,
description=polarion_api.HtmlContent(diagram_html),
status="open",
layer=layer,
)
if attachment:
self._add_attachment(converter_data.work_item, attachment)
Expand Down
31 changes: 30 additions & 1 deletion capella2polarion/converters/model_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import capellambse
import polarion_rest_api_client as polarion_api
from capellambse import model as m

from capella2polarion import data_model
from capella2polarion.connectors import polarion_repo
Expand Down Expand Up @@ -63,8 +64,9 @@ def read_model(

if config.diagram_config:
for d in self.model.diagrams:
layer = get_layer_name(d)
self.converter_session[d.uuid] = data_session.ConverterData(
"", config.diagram_config, d
layer, config.diagram_config, d
)

if missing_types:
Expand Down Expand Up @@ -176,3 +178,30 @@ def generate_work_item_links(
link_serializer.create_grouped_back_link_fields(
converter_data.work_item, local_back_links
)


def get_layer_name(diagram: m.Diagram) -> str:
"""Return the layer name for a diagram."""
match diagram.type.name:
case (
"OEBD"
| "OAIB"
| "OAB"
| "OABD"
| "ORB"
| "OES"
| "OAS"
| "OPD"
| "OCB"
):
return "oa"
case "CM" | "MB" | "CC" | "MCB" | "SFBD" | "SDFB" | "SAB" | "CSA":
return "sa"
case "LCBD" | "LFBD" | "LDFB" | "LAB" | "CRR":
return "la"
case "PFBD" | "PDFB" | "PCBD" | "PAB" | "PPD":
return "pa"
case "EAB" | "CIBD":
return "epbs"
case _:
return "common"
67 changes: 27 additions & 40 deletions tests/test_workitem_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,38 @@ def set_attachment_ids(attachments: list[polarion_api.WorkItemAttachment]):


def test_diagram_no_attachments(model: capellambse.MelodyModel):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(
polarion_repo.PolarionDataRepository(), False, False
)

work_item = converter.converter_session[TEST_DIAG_UUID].work_item
assert work_item is not None
assert work_item.attachments == []


def test_diagram_has_attachments(model: capellambse.MelodyModel):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(
polarion_repo.PolarionDataRepository(), False, True
)

work_item = converter.converter_session[TEST_DIAG_UUID].work_item
assert work_item is not None

assert len(work_item.attachments) == 2


Expand All @@ -134,49 +138,44 @@ def test_diagram_attachments_new(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)]
)

worker.project_client.work_items.get.return_value = (
data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)
)
worker.project_client.work_items.attachments = mock.MagicMock()
worker.project_client.work_items.attachments.create.side_effect = (
set_attachment_ids
)

converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)

worker.compare_and_update_work_item(
converter.converter_session[TEST_DIAG_UUID]
)

assert worker.project_client.work_items.update.call_count == 1
assert worker.project_client.work_items.attachments.create.call_count == 1
assert worker.project_client.work_items.attachments.get_all.call_count == 0

created_attachments: list[polarion_api.WorkItemAttachment] = (
worker.project_client.work_items.attachments.create.call_args.args[0]
)
work_item: data_model.CapellaWorkItem = (
worker.project_client.work_items.update.call_args.args[0]
)

assert len(created_attachments) == 2
assert created_attachments[0].title == created_attachments[1].title
assert (
created_attachments[0].file_name[:3]
== created_attachments[0].file_name[:3]
)

assert work_item.description.value == TEST_DIAG_DESCR.format(
title="Diagram",
attachment_id="1-__C2P__diagram.svg",
Expand All @@ -191,18 +190,16 @@ def test_new_diagram(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")

checksum = json.dumps({"__C2P__WORK_ITEM": DIAGRAM_WI_CHECKSUM})

worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
data_model.CapellaWorkItem(
WORKITEM_ID, uuid_capella=TEST_DIAG_UUID, checksum=checksum
)
]
)

worker.project_client.work_items.get.return_value = (
data_model.CapellaWorkItem(
WORKITEM_ID, uuid_capella=TEST_DIAG_UUID, checksum=checksum
Expand All @@ -212,15 +209,13 @@ def test_new_diagram(
worker.project_client.work_items.attachments.create.side_effect = (
set_attachment_ids
)

converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)

worker.compare_and_update_work_item(
converter.converter_session[TEST_DIAG_UUID]
)
Expand All @@ -241,6 +236,7 @@ def test_diagram_attachments_updated(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)]
Expand All @@ -259,28 +255,24 @@ def test_diagram_attachments_updated(
file_name="__C2P__diagram.png",
),
]

worker.project_client.work_items.get.return_value = (
data_model.CapellaWorkItem(
WORKITEM_ID,
uuid_capella=TEST_DIAG_UUID,
attachments=existing_attachments,
)
)

worker.project_client.work_items.attachments.get_all = mock.MagicMock()
worker.project_client.work_items.attachments.get_all.return_value = (
existing_attachments
)

converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)

worker.compare_and_update_work_item(
converter.converter_session[TEST_DIAG_UUID]
)
Expand All @@ -289,11 +281,9 @@ def test_diagram_attachments_updated(
assert worker.project_client.work_items.attachments.create.call_count == 0
assert worker.project_client.work_items.attachments.update.call_count == 2
assert worker.project_client.work_items.attachments.get_all.call_count == 1

work_item: data_model.CapellaWorkItem = (
worker.project_client.work_items.update.call_args.args[0]
)

assert work_item.description.value == TEST_DIAG_DESCR.format(
title="Diagram",
attachment_id="SVG-ATTACHMENT",
Expand All @@ -306,6 +296,7 @@ def test_diagram_attachments_unchanged_work_item_changed(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
diagram_work_item = data_model.CapellaWorkItem(
WORKITEM_ID,
Expand Down Expand Up @@ -339,15 +330,13 @@ def test_diagram_attachments_unchanged_work_item_changed(
worker.project_client.work_items.attachments.get_all.return_value = (
diagram_work_item.attachments
)

converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)

worker.compare_and_update_work_item(
converter.converter_session[TEST_DIAG_UUID]
)
Expand All @@ -357,11 +346,9 @@ def test_diagram_attachments_unchanged_work_item_changed(
assert worker.project_client.work_items.attachments.get_all.call_count == 1
assert worker.project_client.work_items.attachments.create.call_count == 0
assert worker.project_client.work_items.attachments.update.call_count == 0

work_item: data_model.CapellaWorkItem = (
worker.project_client.work_items.update.call_args.args[0]
)

assert work_item.description.value == TEST_DIAG_DESCR.format(
title="Diagram",
attachment_id="SVG-ATTACHMENT",
Expand All @@ -374,6 +361,7 @@ def test_diagram_attachments_fully_unchanged(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
Expand All @@ -384,15 +372,13 @@ def test_diagram_attachments_fully_unchanged(
)
]
)

converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)

worker.compare_and_update_work_item(
converter.converter_session[TEST_DIAG_UUID]
)
Expand Down Expand Up @@ -557,6 +543,7 @@ def test_diagram_delete_attachments(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
Expand Down Expand Up @@ -596,9 +583,9 @@ def test_diagram_delete_attachments(
]

converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
"",
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
model.diagrams.by_uuid(TEST_DIAG_UUID),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)
Expand Down

0 comments on commit 64d0692

Please sign in to comment.