def test_callback_send_n_event_report(self): """Check callback for sending DIMSE N-EVENT-REPORT messages.""" # N-EVENT-REPORT-RQ primitive = N_EVENT_REPORT() primitive.MessageID = 1 primitive.AffectedSOPClassUID = '1.1.1' primitive.AffectedSOPInstanceUID = '1.1.1' primitive.EventTypeID = 2 self.dimse.send_msg(primitive, 1) # User defined primitive.EventInformation = BytesIO(n_er_rq_ds) self.dimse.send_msg(primitive, 1) # N-EVENT-REPORT-RSP primitive = N_EVENT_REPORT() primitive.MessageIDBeingRespondedTo = 1 primitive.Status = 0x0000 self.dimse.send_msg(primitive, 1) # User defined primitive.AffectedSOPClassUID = '1.2' primitive.AffectedSOPInstanceUID = '1.2.3' primitive.EventTypeID = 4 primitive.EventReply = BytesIO(n_er_rsp_ds) self.dimse.send_msg(primitive, 1)
def test_assignment(self): """ Check assignment works correctly """ primitive = N_EVENT_REPORT() # AffectedSOPClassUID primitive.AffectedSOPClassUID = '1.1.1' assert primitive.AffectedSOPClassUID == UID('1.1.1') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPClassUID = UID('1.1.2') assert primitive.AffectedSOPClassUID == UID('1.1.2') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPClassUID = b'1.1.3' assert primitive.AffectedSOPClassUID == UID('1.1.3') assert isinstance(primitive.AffectedSOPClassUID, UID) # AffectedSOPInstanceUID primitive.AffectedSOPInstanceUID = b'1.2.1' assert primitive.AffectedSOPInstanceUID == UID('1.2.1') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPInstanceUID = UID('1.2.2') assert primitive.AffectedSOPInstanceUID == UID('1.2.2') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPInstanceUID = '1.2.3' assert primitive.AffectedSOPInstanceUID == UID('1.2.3') assert isinstance(primitive.AffectedSOPClassUID, UID) # Event Information ds = Dataset() ds.PatientID = '1234567' primitive.EventInformation = BytesIO(encode(ds, True, True)) ds = decode(primitive.EventInformation, True, True) assert ds.PatientID == '1234567' # Event Reply ds = Dataset() ds.PatientID = '123456' primitive.EventReply = BytesIO(encode(ds, True, True)) ds = decode(primitive.EventReply, True, True) assert ds.PatientID == '123456' # Event Type ID primitive.EventTypeID = 0x0000 assert primitive.EventTypeID == 0x0000 # MessageID primitive.MessageID = 11 assert 11 == primitive.MessageID # MessageIDBeingRespondedTo primitive.MessageIDBeingRespondedTo = 13 assert 13 == primitive.MessageIDBeingRespondedTo # Status primitive.Status = 0x0000 assert primitive.Status == 0x0000
def test_conversion_rsp(self): """ Check conversion to a -RSP PDU produces the correct output """ primitive = N_EVENT_REPORT() primitive.MessageIDBeingRespondedTo = 5 primitive.AffectedSOPClassUID = '1.2.4.10' primitive.AffectedSOPInstanceUID = '1.2.4.5.7.8' primitive.Status = 0x0000 primitive.EventTypeID = 2 ds = Dataset() ds.PatientID = 'Test1101' ds.PatientName = "Tube HeNe" primitive.EventReply = BytesIO(encode(ds, True, True)) dimse_msg = N_EVENT_REPORT_RSP() dimse_msg.primitive_to_message(primitive) pdvs = [] for fragment in dimse_msg.encode_msg(1, 16382): pdvs.append(fragment) assert len(pdvs) == 2 cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] assert cs_pdv == n_er_rsp_cmd assert ds_pdv == n_er_rsp_ds
def test_conversion_rq(self): """ Check conversion to a -RQ PDU produces the correct output """ primitive = N_EVENT_REPORT() primitive.MessageID = 7 primitive.AffectedSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' primitive.AffectedSOPInstanceUID = '1.2.392.200036.9116.2.6.1.48' primitive.EventTypeID = 2 ds = Dataset() ds.PatientID = 'Test1101' ds.PatientName = "Tube HeNe" primitive.EventInformation = BytesIO(encode(ds, True, True)) dimse_msg = N_EVENT_REPORT_RQ() dimse_msg.primitive_to_message(primitive) pdvs = [] for fragment in dimse_msg.encode_msg(1, 16382): pdvs.append(fragment) assert len(pdvs) == 2 cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] assert cs_pdv == n_er_rq_cmd assert ds_pdv == n_er_rq_ds
def test_is_valid_request(self): """Test N_EVENT_REPORT.is_valid_request""" primitive = N_EVENT_REPORT() assert not primitive.is_valid_request primitive.MessageID = 1 assert not primitive.is_valid_request primitive.AffectedSOPClassUID = '1.2' assert not primitive.is_valid_request primitive.EventTypeID = 1 assert not primitive.is_valid_request primitive.AffectedSOPInstanceUID = '1.2.1' assert primitive.is_valid_request
def test_callback_receive_n_event_report(self): """Check callback for receiving DIMSE N-EVENT-REPORT messages.""" # N-EVENT-REPORT-RQ primitive = N_EVENT_REPORT() primitive.MessageID = 1 primitive.AffectedSOPClassUID = '1.1.1' primitive.AffectedSOPInstanceUID = '1.1.1.1' primitive.EventTypeID = 5 msg = N_EVENT_REPORT_RQ() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_event_report_rq(msg) # User defined primitive.EventInformation = BytesIO(n_er_rq_ds) msg = N_EVENT_REPORT_RQ() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_event_report_rq(msg) # N-EVENT-REPORT-RSP primitive = N_EVENT_REPORT() primitive.MessageIDBeingRespondedTo = 1 primitive.Status = 5 msg = N_EVENT_REPORT_RSP() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_event_report_rsp(msg) # User defined primitive.AffectedSOPClassUID = '1.2.3' primitive.AffectedSOPInstanceUID = '1.2.3.4' primitive.EventTypeID = 4 primitive.EventReply = BytesIO(n_er_rsp_ds) msg = N_EVENT_REPORT_RSP() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_event_report_rsp(msg)
def test_send_n_event_report_rsp(self): """Test the handler for N-EVENT-REPORT rsp with Event Type ID.""" self.ae = ae = AE() ae.add_supported_context('1.2.840.10008.1.1') ae.add_requested_context('1.2.840.10008.1.1') scp = ae.start_server(('', 11112), block=False) assoc = ae.associate('localhost', 11112) assert assoc.is_established msg = N_EVENT_REPORT() msg.MessageIDBeingRespondedTo = 1 msg.AffectedSOPClassUID = '1.2.3' msg.AffectedSOPInstanceUID = '1.2.3.4' msg.EventTypeID = 1 # US msg.EventReply = BytesIO(b'\x00\x01') # Dataset msg.Status = 0x0000 assoc.dimse.send_msg(msg, 1) assoc.release() scp.shutdown()
def _event_report_scp(self, req, context): """Implementation of the N-EVENT-REPORT SCP. Parameters ---------- req : dimse_primitives.N_EVENT_REPORT The N-EVENT-REPORT request primitive sent by the peer. context : presentation.PresentationContext The presentation context that the SCP is operating under. Notes ----- **N-EVENT-REPORT Request** *Parameters* | (M) Message ID | (M) Affected SOP Class UID | (M) Affected SOP Instance UID | (M) Event Type ID | (U) Event Information **N-EVENT-REPORT Response** *Parameters* | (M) Message ID Being Responded To | (U=) Affected SOP Class UID | (U=) Affected SOP Instance UID | (C=) Event Type ID | (C) Event Reply | (M) Status *Status* Success | ``0x0000`` Success Failure | ``0x0110`` Processing failure | ``0x0112`` No such SOP Instance | ``0x0113`` No such event type | ``0x0114`` No such argument | ``0x0115`` Invalid argument value | ``0x0117`` Invalid object Instance | ``0x0118`` No such SOP Class | ``0x0119`` Class-Instance conflict | ``0x0210`` Duplicate invocation | ``0x0211`` Unrecognised operation | ``0x0212`` Mistyped argument | ``0x0213`` Resource limitation References ---------- * DICOM Standard, Part 4, `Annex F <http://dicom.nema.org/medical/dicom/current/output/html/part04.html#chapter_F>`_ * DICOM Standard, Part 7, Sections `10.1.1 <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#sect_10.1.1>`_, `10.3.1 <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#sect_10.3.1>`_ and `Annex C <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#chapter_C>`_ """ # Build N-EVENT-REPLY response primitive rsp = N_EVENT_REPORT() rsp.MessageIDBeingRespondedTo = req.MessageID rsp.AffectedSOPClassUID = req.AffectedSOPClassUID rsp.AffectedSOPInstanceUID = req.AffectedSOPInstanceUID rsp.EventTypeID = req.EventTypeID try: status, ds = evt.trigger( self.assoc, evt.EVT_N_EVENT_REPORT, {'request' : req, 'context' : context.as_tuple} ) except Exception as exc: LOGGER.error( "Exception in the handler bound to 'evt.EVT_N_EVENT_REPORT" ) LOGGER.exception(exc) rsp.Status = 0x0110 self.dimse.send_msg(rsp, context.context_id) return # Check Status validity # Validate rsp_status and set rsp.Status accordingly rsp = self.validate_status(status, rsp) if rsp.Status in self.statuses: status = self.statuses[rsp.Status] else: # Unknown status self.dimse.send_msg(rsp, context.context_id) return if status[0] in (STATUS_SUCCESS, STATUS_WARNING) and ds: # If success or warning then there may be a dataset transfer_syntax = context.transfer_syntax[0] bytestream = encode(ds, transfer_syntax.is_implicit_VR, transfer_syntax.is_little_endian) bytestream = BytesIO(bytestream) if bytestream.getvalue() == b'': LOGGER.error( "Failed to encode the N-EVENT-REPORT response's 'Event " "Reply' dataset" ) # Processing failure rsp.Status = 0x0110 else: rsp.EventReply = bytestream # Send response primitive self.dimse.send_msg(rsp, context.context_id)
def test_exceptions(self): """ Check incorrect types/values for properties raise exceptions """ primitive = N_EVENT_REPORT() # MessageID with pytest.raises(TypeError): primitive.MessageID = 'halp' with pytest.raises(TypeError): primitive.MessageID = 1.111 with pytest.raises(ValueError): primitive.MessageID = 65536 with pytest.raises(ValueError): primitive.MessageID = -1 # MessageIDBeingRespondedTo with pytest.raises(TypeError): primitive.MessageIDBeingRespondedTo = 'halp' with pytest.raises(TypeError): primitive.MessageIDBeingRespondedTo = 1.111 with pytest.raises(ValueError): primitive.MessageIDBeingRespondedTo = 65536 with pytest.raises(ValueError): primitive.MessageIDBeingRespondedTo = -1 # AffectedSOPClassUID with pytest.raises(TypeError): primitive.AffectedSOPClassUID = 45.2 with pytest.raises(TypeError): primitive.AffectedSOPClassUID = 100 with pytest.raises(ValueError): primitive.AffectedSOPClassUID = 'abc' # AffectedSOPInstanceUID with pytest.raises(TypeError): primitive.AffectedSOPInstanceUID = 45.2 with pytest.raises(TypeError): primitive.AffectedSOPInstanceUID = 100 with pytest.raises(ValueError): primitive.AffectedSOPInstanceUID = 'abc' # EventInformation msg = r"'EventInformation' parameter must be a BytesIO object" with pytest.raises(TypeError, match=msg): primitive.EventInformation = 'halp' with pytest.raises(TypeError): primitive.EventInformation = 1.111 with pytest.raises(TypeError): primitive.EventInformation = 50 with pytest.raises(TypeError): primitive.EventInformation = [30, 10] # EventReply msg = r"'EventReply' parameter must be a BytesIO object" with pytest.raises(TypeError, match=msg): primitive.EventReply = 'halp' with pytest.raises(TypeError): primitive.EventReply = 1.111 with pytest.raises(TypeError): primitive.EventReply = 50 with pytest.raises(TypeError): primitive.EventReply = [30, 10] # EventTypeID with pytest.raises(TypeError): primitive.EventTypeID = 19.4 # Status with pytest.raises(TypeError): primitive.Status = 19.4