예제 #1
0
 def test_is_valid_resposne(self):
     """Test N_DELETE.is_valid_response."""
     primitive = N_DELETE()
     assert not primitive.is_valid_response
     primitive.MessageIDBeingRespondedTo = 1
     assert not primitive.is_valid_response
     primitive.Status = 0x0000
     assert primitive.is_valid_response
예제 #2
0
 def test_is_valid_request(self):
     """Test N_DELETE.is_valid_request"""
     primitive = N_DELETE()
     assert not primitive.is_valid_request
     primitive.MessageID = 1
     assert not primitive.is_valid_request
     primitive.RequestedSOPClassUID = '1.2'
     assert not primitive.is_valid_request
     primitive.RequestedSOPInstanceUID = '1.2.1'
     assert primitive.is_valid_request
예제 #3
0
    def test_send_n_delete(self):
        """Check sending DIMSE N-DELETE messages."""
        # N-DELETE-RQ
        primitive = N_DELETE()
        primitive.MessageID = 1
        primitive.AffectedSOPClassUID = '1.1.1'

        def test_callback(msg):
            """Callback"""
            self.assertEqual(msg.__class__.__name__, 'N_DELETE_RQ')

        self.dimse.on_send_dimse_message = test_callback
        self.dimse.send_msg(primitive, 1)

        # N-DELETE-RSP
        primitive = N_DELETE()
        primitive.MessageIDBeingRespondedTo = 1
        primitive.Status = 0x0000

        def test_callback(msg):
            """Callback"""
            self.assertEqual(msg.__class__.__name__, 'N_DELETE_RSP')

        self.dimse.on_send_dimse_message = test_callback
        self.dimse.send_msg(primitive, 1)
예제 #4
0
    def message_to_primitive(self):
        """Convert the DIMSEMessage class to a DIMSE primitive.

        Returns
        -------
        DIMSE message primitive
            One of the DIMSE message primitives from
            pynetdicom3.dimse_primitives generated from
            the current DIMSEMessage.
        """
        # pylint: disable=too-many-branches
        cls_type_name = self.__class__.__name__
        if 'C_ECHO' in cls_type_name:
            primitive = C_ECHO()
        elif 'C_STORE' in cls_type_name:
            primitive = C_STORE()
        elif 'C_FIND' in cls_type_name:
            primitive = C_FIND()
        elif 'C_GET' in cls_type_name:
            primitive = C_GET()
        elif 'C_MOVE' in cls_type_name:
            primitive = C_MOVE()
        elif 'C_CANCEL' in cls_type_name:
            primitive = C_CANCEL()
        elif 'N_EVENT' in cls_type_name:
            primitive = N_EVENT_REPORT()
        elif 'N_GET' in cls_type_name:
            primitive = N_GET()
        elif 'N_SET' in cls_type_name:
            primitive = N_SET()
        elif 'N_ACTION' in cls_type_name:
            primitive = N_ACTION()
        elif 'N_CREATE' in cls_type_name:
            primitive = N_CREATE()
        elif 'N_DELETE' in cls_type_name:
            primitive = N_DELETE()

        # Command Set
        # For each parameter in the primitive, set the appropriate value
        #   from the Message's Command Set elements
        for elem in self.command_set:
            if hasattr(primitive, elem.keyword):
                setattr(primitive, elem.keyword,
                        self.command_set.__getattr__(elem.keyword))

        # Datasets
        # Set the primitive's DataSet/Identifier/etc attribute
        try:
            dataset_keyword = _DATASET_KEYWORDS[cls_type_name]
            setattr(primitive, dataset_keyword, self.data_set)
        except KeyError:
            pass

        # Set the presentation context ID the message was set under
        primitive._context_id = self.ID

        return primitive
예제 #5
0
    def test_conversion_rq(self):
        """ Check conversion to a -RQ PDU produces the correct output """
        primitive = N_DELETE()
        primitive.MessageID = 7
        primitive.RequestedSOPClassUID = '1.2.3'
        primitive.RequestedSOPInstanceUID = '1.2.30'

        dimse_msg = N_DELETE_RQ()
        dimse_msg.primitive_to_message(primitive)

        pdvs = []
        for fragment in dimse_msg.encode_msg(1, 16382):
            pdvs.append(fragment)

        assert len(pdvs) == 1
        cs_pdv = pdvs[0].presentation_data_value_list[0][1]

        assert cs_pdv == n_delete_rq_cmd
예제 #6
0
    def test_callback_send_n_delete(self):
        """Check callback for sending DIMSE N-DELETE messages."""
        # N-DELETE-RQ
        primitive = N_DELETE()
        primitive.MessageID = 1
        primitive.AffectedSOPClassUID = '1.1.1'
        self.dimse.send_msg(primitive, 1)

        # N-DELETE-RSP
        primitive = N_DELETE()
        primitive.MessageIDBeingRespondedTo = 1
        primitive.Status = 0x0000
        self.dimse.send_msg(primitive, 1)
예제 #7
0
    def test_conversion_rsp(self):
        """ Check conversion to a -RSP PDU produces the correct output """
        primitive = N_DELETE()
        primitive.MessageIDBeingRespondedTo = 5
        primitive.AffectedSOPClassUID = '1.2.4.10'
        primitive.AffectedSOPInstanceUID = '1.2.4.5.7.8'
        primitive.Status = 0xC201

        dimse_msg = N_DELETE_RSP()
        dimse_msg.primitive_to_message(primitive)

        pdvs = []
        for fragment in dimse_msg.encode_msg(1, 16382):
            pdvs.append(fragment)

        assert len(pdvs) == 1
        cs_pdv = pdvs[0].presentation_data_value_list[0][1]

        assert cs_pdv == n_delete_rsp_cmd
예제 #8
0
 def test_conversion_rsp(self):
     """ Check conversion to a -RSP PDU produces the correct output """
     primitive = N_DELETE()
예제 #9
0
 def test_exceptions(self):
     """ Check incorrect types/values for properties raise exceptions """
     primitive = N_DELETE()
예제 #10
0
 def test_assignment(self):
     """ Check assignment works correctly """
     primitive = N_DELETE()
예제 #11
0
    def message_to_primitive(self):
        """Convert the DIMSEMessage class to a DIMSE primitive.

        Returns
        -------
        primitive
            One of the pynetdicom3.dimse_primitives primitives generated from
            the current DIMSEMessage.
        """
        # pylint: disable=too-many-branches
        cls_type_name = self.__class__.__name__
        if 'C_ECHO' in cls_type_name:
            primitive = C_ECHO()
        elif 'C_STORE' in cls_type_name:
            primitive = C_STORE()
        elif 'C_FIND' in cls_type_name:
            primitive = C_FIND()
        elif 'C_GET' in cls_type_name:
            primitive = C_GET()
        elif 'C_MOVE' in cls_type_name:
            primitive = C_MOVE()
        elif 'C_CANCEL' in cls_type_name:
            primitive = C_CANCEL()
        elif 'N_EVENT' in cls_type_name:
            primitive = N_EVENT_REPORT()
        elif 'N_GET' in cls_type_name:
            primitive = N_GET()
        elif 'N_SET' in cls_type_name:
            primitive = N_SET()
        elif 'N_ACTION' in cls_type_name:
            primitive = N_ACTION()
        elif 'N_CREATE' in cls_type_name:
            primitive = N_CREATE()
        elif 'N_DELETE' in cls_type_name:
            primitive = N_DELETE()
        # elif 'C_CANCEL' in cls_type_name:
        #    primitive = C_CANCEL()

        # Command Set
        # For each parameter in the primitive, set the appropriate value
        #   from the Message's Command Set elements
        for elem in self.command_set:
            if hasattr(primitive, elem.keyword):
                setattr(primitive, elem.keyword,
                        self.command_set.__getattr__(elem.keyword))

        # Datasets
        # Set the primitive's DataSet/Identifier/etc attribute
        if cls_type_name == 'C_STORE_RQ':
            setattr(primitive, 'DataSet', self.data_set)
        elif cls_type_name in [
                'C_FIND_RQ', 'C_FIND_RSP', 'C_GET_RQ', 'C_GET_RSP',
                'C_MOVE_RQ', 'C_MOVE_RSP'
        ]:
            setattr(primitive, 'Identifier', self.data_set)
        elif cls_type_name == 'N_EVENT_REPORT_RQ':
            setattr(primitive, 'EventInformation', self.data_set)
        elif cls_type_name == 'N_EVENT_REPORT_RSP':
            setattr(primitive, 'EventReply', self.data_set)
        elif cls_type_name in [
                'N_GET_RSP', 'N_SET_RSP', 'N_CREATE_RQ', 'N_CREATE_RSP'
        ]:
            setattr(primitive, 'AttributeList', self.data_set)
        elif cls_type_name == 'N_SET_RQ':
            setattr(primitive, 'ModificationList', self.data_set)
        elif cls_type_name == 'N_ACTION_RQ':
            setattr(primitive, 'ActionInformation', self.data_set)
        elif cls_type_name == 'N_ACTION_RSP':
            setattr(primitive, 'ActionReply', self.data_set)

        return primitive
예제 #12
0
    def test_exceptions(self):
        """ Check incorrect types/values for properties raise exceptions """
        primitive = N_DELETE()

        # 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'

        # RequestedSOPClassUID
        with pytest.raises(TypeError):
            primitive.RequestedSOPClassUID = 45.2

        with pytest.raises(TypeError):
            primitive.RequestedSOPClassUID = 100

        with pytest.raises(ValueError):
            primitive.RequestedSOPClassUID = 'abc'

        # RequestedSOPInstanceUID
        with pytest.raises(TypeError):
            primitive.RequestedSOPInstanceUID = 45.2

        with pytest.raises(TypeError):
            primitive.RequestedSOPInstanceUID = 100

        with pytest.raises(ValueError):
            primitive.RequestedSOPInstanceUID = 'abc'

        # Status
        with pytest.raises(TypeError):
            primitive.Status = 19.4
예제 #13
0
    def test_assignment(self):
        """ Check assignment works correctly """
        primitive = N_DELETE()

        # 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)

        # MessageID
        primitive.MessageID = 11
        assert 11 == primitive.MessageID

        # MessageIDBeingRespondedTo
        primitive.MessageIDBeingRespondedTo = 13
        assert 13 == primitive.MessageIDBeingRespondedTo

        # RequestedSOPClassUID
        primitive.RequestedSOPClassUID = '1.1.1'
        assert primitive.RequestedSOPClassUID == UID('1.1.1')
        assert isinstance(primitive.RequestedSOPClassUID, UID)
        primitive.RequestedSOPClassUID = UID('1.1.2')
        assert primitive.RequestedSOPClassUID == UID('1.1.2')
        assert isinstance(primitive.RequestedSOPClassUID, UID)
        primitive.RequestedSOPClassUID = b'1.1.3'
        assert primitive.RequestedSOPClassUID == UID('1.1.3')
        assert isinstance(primitive.RequestedSOPClassUID, UID)

        # RequestedSOPInstanceUID
        primitive.RequestedSOPInstanceUID = b'1.2.1'
        assert primitive.RequestedSOPInstanceUID == UID('1.2.1')
        assert isinstance(primitive.RequestedSOPInstanceUID, UID)
        primitive.RequestedSOPInstanceUID = UID('1.2.2')
        assert primitive.RequestedSOPInstanceUID == UID('1.2.2')
        assert isinstance(primitive.RequestedSOPInstanceUID, UID)
        primitive.RequestedSOPInstanceUID = '1.2.3'
        assert primitive.RequestedSOPInstanceUID == UID('1.2.3')
        assert isinstance(primitive.RequestedSOPInstanceUID, UID)

        # Status
        primitive.Status = 0x0000
        assert primitive.Status == 0x0000
예제 #14
0
    def test_callback_receive_n_delete(self):
        """Check callback for receiving DIMSE N-DELETE messages."""
        # N-DELETE-RQ
        primitive = N_DELETE()
        primitive.MessageID = 1
        primitive.RequestedSOPClassUID = '1.1.1'
        primitive.RequestedSOPInstanceUID = '1.1.1.1'
        msg = N_DELETE_RQ()
        msg.primitive_to_message(primitive)
        msg.ID = 1
        self.dimse.debug_receive_n_delete_rq(msg)

        # N-DELETE-RSP
        primitive = N_DELETE()
        primitive.MessageIDBeingRespondedTo = 1
        primitive.Status = 0x0000
        msg = N_DELETE_RSP()
        msg.primitive_to_message(primitive)
        msg.ID = 1
        self.dimse.debug_receive_n_delete_rsp(msg)

        # User optional
        primitive.AffectedSOPClassUID = '1.2.3'
        msg = N_DELETE_RSP()
        msg.primitive_to_message(primitive)
        msg.ID = 1
        self.dimse.debug_receive_n_delete_rsp(msg)

        primitive.AffectedSOPInstanceUID = '1.2.3.4'
        msg = N_DELETE_RSP()
        msg.primitive_to_message(primitive)
        msg.ID = 1
        self.dimse.debug_receive_n_delete_rsp(msg)
예제 #15
0
    def test_callback_send_n_delete(self):
        """Check callback for sending DIMSE N-DELETE messages."""
        # N-DELETE-RQ
        primitive = N_DELETE()
        primitive.MessageID = 1
        primitive.RequestedSOPClassUID = '1.1.1'
        primitive.RequestedSOPInstanceUID = '1.2.3'
        self.dimse.send_msg(primitive, 1)

        # N-DELETE-RSP
        # No affected SOP class/instance
        primitive = N_DELETE()
        primitive.MessageIDBeingRespondedTo = 1
        primitive.Status = 0x0000
        self.dimse.send_msg(primitive, 1)

        # Affected SOP Class
        primitive.AffectedSOPClassUID = '1.1.2'
        self.dimse.send_msg(primitive, 1)

        # Affected SOP Instance
        primitive.AffectedSOPInstanceUID = '1.1.3'
        self.dimse.send_msg(primitive, 1)
예제 #16
0
        return 0x01


REFERENCE_MSG = [
    (C_ECHO(), ('C_ECHO_RQ', 'C_ECHO_RSP')),
    (C_STORE(), ('C_STORE_RQ', 'C_STORE_RSP')),
    (C_FIND(), ('C_FIND_RQ', 'C_FIND_RSP')),
    (C_GET(), ('C_GET_RQ', 'C_GET_RSP')),
    (C_MOVE(), ('C_MOVE_RQ', 'C_MOVE_RSP')),
    (C_CANCEL(), (None, 'C_CANCEL_RQ')),
    (N_EVENT_REPORT(), ('N_EVENT_REPORT_RQ', 'N_EVENT_REPORT_RSP')),
    (N_GET(), ('N_GET_RQ', 'N_GET_RSP')),
    (N_SET(), ('N_SET_RQ', 'N_SET_RSP')),
    (N_ACTION(), ('N_ACTION_RQ', 'N_ACTION_RSP')),
    (N_CREATE(), ('N_CREATE_RQ', 'N_CREATE_RSP')),
    (N_DELETE(), ('N_DELETE_RQ', 'N_DELETE_RSP')),
]


class TestDIMSEProvider(object):
    """Test DIMSE service provider operations."""
    def setup(self):
        """Set up"""
        self.dimse = DIMSEServiceProvider(DummyDUL(), 1)

    def test_receive_not_pdata(self):
        """Test we get back None if not a P_DATA"""
        assert self.dimse.receive_msg(True) == (None, None)

    @pytest.mark.parametrize("primitive, cls_name", REFERENCE_MSG)
    def test_send_msg(self, primitive, cls_name):