def test_string_output(self): """Test string output""" pc = PresentationContext() pc.context_id = 1 pc.abstract_syntax = '1.1.1' pc.transfer_syntax = ['1.2.840.10008.1.2', '1.2.3'] pc._scp_role = True pc._scu_role = False pc.result = 0x02 assert '1.1.1' in pc.__str__() assert 'Implicit' in pc.__str__() assert 'Provider Rejected' in pc.__str__()
def test_status(self): """Test presentation context status""" pc = PresentationContext() pc.context_id = 1 statuses = [None, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05] results = [ 'Pending', 'Accepted', 'User Rejected', 'Provider Rejected', 'Abstract Syntax Not Supported', 'Transfer Syntax(es) Not Supported', 'Unknown' ] for status, result in zip(statuses, results): pc.result = status assert pc.status == result
def acceptor_contexts(self, contexts): """Set the Acceptor's presentation contexts. Must be a list of pynetdicom3.utils.PresentationContext There are two possible situations 1. The local AE issues the request and receives the response 2. The peer AE issues the request and the local must determine the response The first situation means that the acceptor has already decided on a Result and (if accepted) which Transfer Syntax to use The second situation means that we must determine whether to accept or reject presentation context and which Transfer Syntax to use requestor_contexts cannot be an empty list When the local AE is making the request, this is just the contents of the A-ASSOCIATE PresentationContextDefinitionResultList parameter (Result value will not be None) When the peer AE is making the request this will be the list of the SCP supported SOP classes combined with the supported Transfer Syntax(es) (Result value will be None) FIXME: This needs to be refactored, its slow and overly complex FIXME: It would be better to have a separate method to call when the user wants the contexts evaluated """ if self.requestor_contexts == []: raise RuntimeError("You can only set the Acceptor's presentation " "contexts after the Requestor's") if not isinstance(contexts, list): raise TypeError("acceptor_contexts must be a list of " "PresentationContext items") # Validate the supplied contexts self._acceptor_contexts = [] for ii in contexts: if isinstance(ii, PresentationContext): self._acceptor_contexts.append(ii) else: raise TypeError("acceptor_contexts must be a list of " "PresentationContext items") # Generate accepted_contexts and rejected_contexts self.accepted = [] self.rejected = [] if self._acceptor_contexts != [] and self._requestor_contexts != []: # For each of the contexts available to the acceptor for ii_req in self._requestor_contexts: # Get the acceptor context with the same Abstract Syntax as # the requestor context acc_context = None for ii_acc in self._acceptor_contexts: # The acceptor context will only have an abstract syntax # if we are the Acceptor, otherwise we have to match # using the IDs # If we are the Requestor then the Acceptor contexts # will have no Abstract Syntax if ii_acc.abstract_syntax is not None: if ii_acc.abstract_syntax == ii_req.abstract_syntax: acc_context = ii_acc else: if ii_acc.context_id == ii_req.context_id: acc_context = ii_acc # Set Abstract Syntax (for convenience) ii_acc.abstract_syntax = ii_req.abstract_syntax # Create a new PresentationContext item that will store the # results from the negotiation result = PresentationContext() result.context_id = ii_req.context_id result.abstract_syntax = ii_req.abstract_syntax # If no matching Abstract Syntax then we are the Acceptor and # we reject the current context (0x03 - abstract syntax not # supported) if acc_context is None: # FIXME: make pdu not require this. result.transfer_syntax = [ii_req.transfer_syntax[0]] result.result = 0x03 result = self.negotiate_scp_scu_role(ii_req, result) self.rejected.append(result) # If there is a matching Abstract Syntax then check to see if # the result is None (indicates we are the Acceptor) or # has a value set (indicates we are the Requestor) else: # We are the Acceptor and must decide to accept or reject # the context if acc_context.result is None: # Check the Transfer Syntaxes # We accept the first matching transfer syntax for transfer_syntax in acc_context.transfer_syntax: # The local transfer syntax is used in order to # enforce preference based on position if transfer_syntax in ii_req.transfer_syntax: result.transfer_syntax = [transfer_syntax] result.result = 0x00 result = self.negotiate_scp_scu_role( ii_req, result) self.accepted.append(result) break # Refuse sop class because TS not supported else: # FIXME: make pdu not require this. result.transfer_syntax = [transfer_syntax] result.result = 0x04 result = self.negotiate_scp_scu_role( ii_req, result) self.rejected.append(result) # We are the Requestor and the Acceptor has accepted this # context elif acc_context.result == 0x00: # The accepted transfer syntax (there is only 1) result.transfer_syntax = [ acc_context.transfer_syntax[0] ] # Add it to the list of accepted presentation contexts self.accepted.append(result) # We are the Requestor and the Acceptor has rejected this # context elif acc_context.result in [0x01, 0x02, 0x03, 0x04]: # The rejected transfer syntax(es) result.transfer_syntax = acc_context.transfer_syntax # Add it to the list of accepted presentation contexts self.rejected.append(result) else: raise ValueError( "Invalid 'Result' parameter in the " "Acceptor's Presentation Context list")