def test_add_request_handler_chain_throw_error_for_null_chain(self): test_exception_mapper = GenericExceptionMapper(exception_handlers=None) with self.assertRaises(DispatchException) as exc: test_exception_mapper.add_exception_handler(None) assert "Input is not an AbstractExceptionHandler instance" in str( exc.exception ), ("Exception Mapper didn't throw error during add_exception_handler " "method call when " "an invalid Exception Handler is passed")
def test_add_exception_handler_for_valid_handler_type(self): test_exception_handler = mock.MagicMock(spec=AbstractExceptionHandler) test_exception_mapper = GenericExceptionMapper(exception_handlers=None) test_exception_mapper.add_exception_handler(test_exception_handler) assert test_exception_mapper.exception_handlers == [ test_exception_handler ], ("Exception Mapper throws exception when a valid Exception Handler " "is provided in the " "add_handler method")
def test_dispatch_raise_low_level_exception_when_no_suitable_exception_handler_registered( self): test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler.can_handle.return_value = True test_request_handler.handle.return_value = "Test Response" test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) test_adapter = mock.MagicMock(spec=GenericHandlerAdapter) test_adapter.supports.return_value = True test_adapter.execute.side_effect = Exception( "Test low level Exception") test_exception_handler = mock.MagicMock(spec=AbstractExceptionHandler) test_exception_handler.can_handle.return_value = False test_exception_mapper = GenericExceptionMapper( exception_handlers=[test_exception_handler]) self.test_dispatcher.request_mappers = [test_request_mapper] self.test_dispatcher.handler_adapters = [test_adapter] self.test_dispatcher.exception_mapper = test_exception_mapper with self.assertRaises(Exception) as exc: self.test_dispatcher.dispatch( handler_input=self.valid_handler_input) assert "Test low level Exception" in str(exc.exception), ( "Dispatcher didn't throw low level exception when request " "dispatch throws exception and " "no suitable exception handler is registered")
def test_no_handler_registered_for_custom_exception(self): test_handler_input = TestDispatchInput(request="test_input") test_exception_handler = mock.MagicMock(spec=AbstractExceptionHandler) test_exception_handler.can_handle.return_value = False test_exception_mapper = GenericExceptionMapper( exception_handlers=[test_exception_handler]) test_custom_exception = DispatchException("Test Custom Exception") assert test_exception_mapper.get_handler( handler_input=test_handler_input, exception=test_custom_exception) is None, ( "get_handler in Generic Exception Mapper found an unsupported " "exception handler for " "handler input and custom exception")
def test_exception_mapper_initialization_with_handler_list_containing_null_throw_error( self): with self.assertRaises(DispatchException) as exc: test_exception_mapper = GenericExceptionMapper( exception_handlers=[None]) assert "Input is not an AbstractExceptionHandler instance" in str( exc.exception ), ("Exception Mapper didn't throw error during initialization when a " "Null handler is passed")
def test_get_handler_registered_for_custom_exception(self): test_handler_input = TestDispatchInput(request="test_input") test_exception_handler_1 = mock.MagicMock( spec=AbstractExceptionHandler) test_exception_handler_1.can_handle.return_value = False test_exception_handler_2 = mock.MagicMock( spec=AbstractExceptionHandler) test_exception_handler_2.can_handle.return_value = True test_exception_mapper = GenericExceptionMapper(exception_handlers=[ test_exception_handler_1, test_exception_handler_2 ]) test_custom_exception = DispatchException("Test Custom Exception") assert test_exception_mapper.get_handler( handler_input=test_handler_input, exception=test_custom_exception ) == test_exception_handler_2, ( "get_handler in Generic Exception Mapper found incorrect request " "exception handler for " "input and custom exception")
def test_exception_mapper_initialization_with_handler_list_containing_valid_handler( self): test_exception_handler = mock.MagicMock(spec=AbstractExceptionHandler) test_exception_mapper = GenericExceptionMapper( exception_handlers=[test_exception_handler]) assert test_exception_mapper.exception_handlers == [ test_exception_handler ], ("Exception Mapper initialization throws exception when a valid " "Handler is provided in the " "exception handlers list")
def test_exception_mapper_initialization_with_handler_list_containing_invalid_handler_throw_error( self): test_invalid_handler = mock.Mock() with self.assertRaises(DispatchException) as exc: test_exception_mapper = GenericExceptionMapper( exception_handlers=[test_invalid_handler]) assert "Input is not an AbstractExceptionHandler instance" in str( exc.exception ), ("Exception Mapper didn't throw error during initialization when " "an invalid handler is passed")
def test_dispatch_process_handled_exception_when_suitable_exception_handler_registered( self): test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler.can_handle.return_value = True test_request_handler.handle.return_value = "Test Response" test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) test_adapter = mock.MagicMock(spec=GenericHandlerAdapter) test_adapter.supports.return_value = True test_adapter.execute.side_effect = DispatchException( "Custom dispatch exception") test_exception_handler_1 = mock.MagicMock( spec=AbstractExceptionHandler) test_exception_handler_1.can_handle.return_value = False test_exception_handler_2 = mock.MagicMock( spec=AbstractExceptionHandler) test_exception_handler_2.can_handle.return_value = True test_exception_handler_2.handle.return_value = "Custom exception " \ "handler response" options = RuntimeConfiguration( request_mappers=[test_request_mapper], handler_adapters=[test_adapter], exception_mapper=GenericExceptionMapper(exception_handlers=[ test_exception_handler_1, test_exception_handler_2 ])) self.test_dispatcher = GenericRequestDispatcher(options=options) assert self.test_dispatcher.dispatch( handler_input=self.valid_handler_input ) == "Custom exception handler response", ( "Dispatcher didn't handle exception when a suitable exception handler is registered" ) test_exception_handler_1.handle.assert_not_called(), ( "Incorrect Exception Handler called when handling custom " "exception") test_exception_handler_2.handle.assert_called_once(), ( "Suitable exception handler didn't handle custom exception")
def test_exception_mapper_with_null_input(self): test_exception_mapper = GenericExceptionMapper(exception_handlers=None) assert test_exception_mapper.exception_handlers == [], ( "Exception Mapper instantiated empty list of exceptions handlers " "when no input provided")