Commit 5087b3c9 authored by George Papathanail's avatar George Papathanail
Browse files

test: add FakeQodCallbackDeliveryPort wire status consumer in tests

parent 4b7afb76
Loading
Loading
Loading
Loading
+39 −8
Original line number Diff line number Diff line
@@ -55,7 +55,9 @@ from open_exposure_gateway.domain.models import (
    Operation,
    QodSession,
)
from open_exposure_gateway.domain.quality_on_demand import QosStatusChangedCloudEvent
from open_exposure_gateway.domain.quality_on_demand import Subject as QodSubject
from open_exposure_gateway.domain.srm_events import SRMOperationStatus
from open_exposure_gateway.ports.callback_delivery_port import CallbackDeliveryPort
from open_exposure_gateway.ports.database.callbacks import (
    CallbackDeliveryRepository,
@@ -65,6 +67,7 @@ from open_exposure_gateway.ports.database.instances import AppInstanceRepository
from open_exposure_gateway.ports.database.operations import OperationRepository
from open_exposure_gateway.ports.database.qod_sessions import QodSessionRepository
from open_exposure_gateway.ports.database.registration import AppRegistrationRepository
from open_exposure_gateway.ports.qod_callback_port import QodCallbackDeliveryPort

Handler = Callable[[dict[str, Any]], Awaitable[None]]

@@ -270,26 +273,46 @@ def wire_qod_operation_consumer(
    bus: FakeDataBus,
    operation_repo: OperationRepository,
    qod_session_repo: QodSessionRepository,
    callback_registration_repo: CallbackRegistrationRepository | None = None,
    callback_delivery_repo: CallbackDeliveryRepository | None = None,
    callback_delivery_port: QodCallbackDeliveryPort | None = None,
) -> NatsOperationConsumer:
    """Wires QualityOnDemandService.handle_completed behind the fake bus -- pass the
    same repo instances used to build the service under test so a completion event
    updates the rows the test can see."""
    """Wires QualityOnDemandService.handle_completed and handle_status_changed behind
    the fake bus -- pass the same repo instances used to build the service under test
    so a completion/status event updates the rows the test can see."""
    service = QualityOnDemandService(
        srm_client=AsyncMock(),
        operation_repo=operation_repo,
        qod_session_repo=qod_session_repo,
        callback_registration_repo=callback_registration_repo,
        callback_delivery_repo=callback_delivery_repo,
        callback_delivery_port=callback_delivery_port,
    )
    consumer = NatsOperationConsumer(
    completed_consumer = NatsOperationConsumer(
        client=AsyncMock(), subject=QodSubject.OPERATION_COMPLETED, handler=service.handle_completed
    )

    async def deliver(payload: dict[str, Any]) -> None:
        await consumer._handle_message(
    async def deliver_completed(payload: dict[str, Any]) -> None:
        await completed_consumer._handle_message(
            FakeMsg(data=json.dumps(payload).encode(), subject=str(QodSubject.OPERATION_COMPLETED))
        )

    bus.subscribe(QodSubject.OPERATION_COMPLETED, deliver)
    return consumer
    bus.subscribe(QodSubject.OPERATION_COMPLETED, deliver_completed)

    status_consumer = NatsOperationConsumer(
        client=AsyncMock(),
        subject=QodSubject.OPERATION_STATUS,
        event_model=SRMOperationStatus,
        handler=service.handle_status_changed,
    )

    async def deliver_status(payload: dict[str, Any]) -> None:
        await status_consumer._handle_message(
            FakeMsg(data=json.dumps(payload).encode(), subject=str(QodSubject.OPERATION_STATUS))
        )

    bus.subscribe(QodSubject.OPERATION_STATUS, deliver_status)
    return completed_consumer


class FakeAppRegistrationRepository(AppRegistrationRepository):
@@ -455,3 +478,11 @@ class FakeCallbackDeliveryPort:

    async def deliver(self, sink: str, event: AppInstanceStatusChangeCloudEvent) -> None:
        self.delivered.append((sink, event))


class FakeQodCallbackDeliveryPort:
    def __init__(self) -> None:
        self.delivered: list[tuple[str, QosStatusChangedCloudEvent]] = []

    async def deliver(self, sink: str, event: QosStatusChangedCloudEvent) -> None:
        self.delivered.append((sink, event))