def register_and_trigger_handler(matchers):
    register_event_handlers(
        {orthanc.ChangeType.STABLE_STUDY: forward_dicom(matchers)},
        orthanc,
        client,
        logging_configuration=python_logging)
    orthanc.on_change(orthanc.ChangeType.STABLE_STUDY, '', 'study-uuid')
def test_no_registered_callbacks_should_be_reported_in_on_change_event(caplog):
    args = {}
    register_event_handlers(args,
                            orthanc,
                            httpx,
                            logging_configuration=python_logging)
    caplog.set_level(logging.DEBUG)
    orthanc.on_change(orthanc.ChangeType.ORTHANC_STARTED, '', '')
    assert 'no handler registered for ORTHANC_STARTED' in caplog.text
def test_registered_callback_should_be_triggered_on_change_event():
    event = ChangeEvent()
    register_event_handlers({orthanc.ChangeType.STABLE_STUDY: capture(event)},
                            orthanc, httpx)
    orthanc.on_change(orthanc.ChangeType.STABLE_STUDY,
                      orthanc.ResourceType.STUDY, 'resource-uuid')
    assert event.resource_id == 'resource-uuid'
    assert event.resource_type == orthanc.ResourceType.STUDY
    assert event.orthanc is not None
def test_all_registered_callbacks_should_be_triggered_on_change_event():
    event1 = ChangeEvent()
    event2 = ChangeEvent()
    register_event_handlers(
        {orthanc.ChangeType.STABLE_STUDY: [capture(event1),
                                           capture(event2)]}, orthanc, httpx)
    orthanc.on_change(orthanc.ChangeType.STABLE_STUDY,
                      orthanc.ResourceType.STUDY, 'resource-uuid')
    assert event1.resource_id is not None
    assert event2.resource_id is not None
def test_event_shall_have_human_readable_representation(caplog):
    caplog.set_level(logging.INFO)

    def log_event(evt, _):
        logging.info(evt)

    register_event_handlers({orthanc.ChangeType.STABLE_STUDY: log_event},
                            orthanc, httpx)
    orthanc.on_change(orthanc.ChangeType.STABLE_STUDY,
                      orthanc.ResourceType.STUDY, 'uuid')
    assert 'change_type=STABLE_STUDY' in caplog.text
    assert 'resource_type=STUDY' in caplog.text
def test_shall_return_values_from_executed_handlers():
    system = respx.get('/system').respond(200, json={'Version': '1.9.0'})

    def get_system_info(_, client):
        return client.get('http://localhost:8042/system').json()

    register_event_handlers(
        {orthanc.ChangeType.ORTHANC_STARTED: get_system_info}, orthanc, httpx)
    (system_info, ) = orthanc.on_change(orthanc.ChangeType.ORTHANC_STARTED,
                                        orthanc.ResourceType.NONE, '')
    assert system.called
    assert system_info.get('Version') == '1.9.0'
def test_should_not_resubmit_other_job_types(caplog):
    job = respx.get('/jobs/job-uuid').respond(200,
                                              json={
                                                  'CreationTime':
                                                  '20210210T084350.430751',
                                                  'CompletionTime':
                                                  '20210210T224350.430751',
                                                  'Type': 'CreateDicomZip'
                                              })
    event_dispatcher.register_event_handlers(
        {orthanc.ChangeType.JOB_FAILURE: handle_failed_forwarding_job(0.1)},
        orthanc,
        client,
        logging_configuration=python_logging)
    caplog.set_level(logging.DEBUG)
    orthanc.on_change(orthanc.ChangeType.JOB_FAILURE, '', 'job-uuid')
    assert job.called
    assert caplog.messages[
        -1] == 'not retrying "CreateDicomZip" job "job-uuid"'
def test_on_failure_should_resubmit_job(caplog):
    # TODO: figure out why expectation not met in Timer thread.
    job = respx.get('/jobs/job-uuid').respond(200,
                                              json={
                                                  'CreationTime':
                                                  '20210210T084350.430751',
                                                  'CompletionTime':
                                                  '20210210T084351.430751',
                                                  'Type': 'DicomModalityStore'
                                              })
    resubmit = respx.post('/jobs/job-uuid/resubmit').respond(200)  # NOQA
    event_dispatcher.register_event_handlers(
        {orthanc.ChangeType.JOB_FAILURE: handle_failed_forwarding_job(0.1)},
        orthanc,
        client,
        logging_configuration=python_logging)
    caplog.set_level(logging.DEBUG)
    orthanc.on_change(orthanc.ChangeType.JOB_FAILURE, '', 'job-uuid')
    assert job.called
    # XXX
    # assert resubmit.called
    assert caplog.messages[-1] == 'resubmitting job "job-uuid" after 2 seconds'
import logging

import orthanc  # NOQA provided by the plugin runtime.

from orthanc_ext import event_dispatcher


def log_event(param):

    def log_event_impl(event, _):
        logging.info(f'orthanc "{event}" event handled with param "{param}"')

    return log_event_impl


def start_maintenance_cycle(event, _):
    logging.info(f'do something special on "{event}"')


def show_system_info(_, client):
    version = client.get('/system').json().get('Version')
    logging.warning(f'orthanc version retrieved: "{version}"', )


event_dispatcher.register_event_handlers({
    orthanc.ChangeType.ORTHANC_STARTED:
        [log_event('started'), start_maintenance_cycle, show_system_info],
    orthanc.ChangeType.ORTHANC_STOPPED: log_event('stopped')
}, orthanc, event_dispatcher.create_client(orthanc))