def test_add_request_handler_chain_throw_error_for_null_chain(self): test_request_mapper = GenericRequestMapper(request_handler_chains=None) with self.assertRaises(DispatchException) as exc: test_request_mapper.add_request_handler_chain(None) assert ("Request Handler Chain is not a GenericRequestHandlerChain " "instance") in str(exc.exception), ( "Request Mapper didn't throw error during " "add_request_handler_chain method call when " "a Null Handler Chain is passed")
def test_add_request_handler_chain_for_valid_chain_type(self): test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper(request_handler_chains=None) test_request_mapper.add_request_handler_chain( test_request_handler_chain) assert test_request_mapper.request_handler_chains == [ test_request_handler_chain ], ("Generic Request Mapper throws exception when a valid Handler " "Chain is provided in the " "add_request_handler_chain 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_handler_input_successful_global_request_interceptors_execution( 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_interceptor_1 = mock.MagicMock(spec=AbstractRequestInterceptor) test_interceptor_2 = mock.MagicMock(spec=AbstractRequestInterceptor) test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) test_adapter = GenericHandlerAdapter() self.test_dispatcher.request_mappers = [test_request_mapper] self.test_dispatcher.handler_adapters = [test_adapter] self.test_dispatcher.request_interceptors = [ test_interceptor_1, test_interceptor_2 ] self.test_dispatcher.dispatch(handler_input=self.valid_handler_input) test_interceptor_1.process.assert_called_once_with( handler_input=self.valid_handler_input), ( "Dispatcher dispatch method didn't process global request " "interceptors before calling dispatch request") test_interceptor_2.process.assert_called_once_with( handler_input=self.valid_handler_input), ( "Dispatcher dispatch method didn't process global request " "interceptors before calling dispatch request")
def test_handler_input_successful_execution_with_supported_chain_and_supported_adapter( 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 = GenericHandlerAdapter() self.test_dispatcher.request_mappers = [test_request_mapper] self.test_dispatcher.handler_adapters = [test_adapter] assert self.test_dispatcher.dispatch( handler_input=self.valid_handler_input) == "Test Response", ( "Dispatcher dispatch method return invalid response when " "supported handler chain and " "supported handler adapter are found") test_request_handler.handle.assert_called_once_with( self.valid_handler_input), ( "Dispatcher dispatch method called handle on Request Handler " "more than once")
def test_no_handler_registered_for_intent_request(self): test_handler_input = TestDispatchInput(request="test_intent") test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler.can_handle.return_value = False test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) assert test_request_mapper.get_request_handler_chain( test_handler_input ) is None, ( "get_request_handler_chain in Request Mapper found an unsupported " "request handler chain for " "intent request")
def test_generic_request_mapper_initialization_with_null_handler_chains( self): test_request_mapper = GenericRequestMapper(request_handler_chains=None) assert test_request_mapper.request_handler_chains == [], ( "Request Mapper didn't initialize empty request handler chains " "instance variable on initialization")
def test_handler_input_unsuccessful_global_request_interceptors_execution( 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_interceptor_1 = mock.MagicMock( spec=AbstractRequestInterceptor) test_request_interceptor_1.process.side_effect = ValueError( "Test exception") test_request_interceptor_2 = mock.MagicMock( spec=AbstractRequestInterceptor) test_response_interceptor_1 = mock.MagicMock( spec=AbstractResponseInterceptor) test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) test_adapter = GenericHandlerAdapter() self.test_dispatcher.request_mappers = [test_request_mapper] self.test_dispatcher.handler_adapters = [test_adapter] self.test_dispatcher.request_interceptors = [ test_request_interceptor_1, test_request_interceptor_2 ] self.test_dispatcher.response_interceptors = [ test_response_interceptor_1 ] with self.assertRaises(ValueError) as exc: self.test_dispatcher.dispatch( handler_input=self.valid_handler_input) assert "Test exception" in str(exc.exception), ( "Dispatcher didn't throw exception raised by global request " "interceptor") test_request_interceptor_1.process.assert_called_once_with( handler_input=self.valid_handler_input), ( "Dispatcher dispatch method didn't process global request " "interceptors before calling dispatch request") test_request_interceptor_2.process.assert_not_called(), ( "Dispatcher dispatch method processed remaining global " "request interceptors when one of them threw " "exception") test_request_handler.assert_not_called(), ( "Dispatcher dispatch method processed request handler 'handle' " "method when one of the global request " "interceptors threw exception") test_response_interceptor_1.process.assert_not_called(), ( "Dispatcher dispatch method processed global response " "interceptors when one of the global request " "interceptors threw exception")
def test_generic_request_mapper_initialization_with_chain_containing_null_throw_error( self): with self.assertRaises(DispatchException) as exc: test_request_mapper = GenericRequestMapper( request_handler_chains=[None]) assert ( "Request Handler Chain is not a GenericRequestHandlerChain " "instance" ) in str(exc.exception), ( "Request Mapper didn't throw error during initialization when a " "Null Handler Chain is passed")
def test_generic_request_mapper_initialization_with_chain_containing_valid_type( self): test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler_chain = GenericRequestHandlerChain( request_handler=test_request_handler) test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) assert test_request_mapper.request_handler_chains == [ test_request_handler_chain ], ("Request Mapper initialization throws exception when a valid " "Handler Chain is provided in the " "handler chains list")
def test_generic_request_mapper_initialization_with_chain_containing_invalid_type_throw_error( self): test_request_handler_chain = mock.Mock() with self.assertRaises(DispatchException) as exc: test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) assert ( "Request Handler Chain is not a GenericRequestHandlerChain " "instance" ) in str(exc.exception), ( "Request Mapper didn't throw error during initialization when an " "invalid Handler Chain is passed")
def test_get_handler_chain_registered_for_event_request(self): test_handler_input = TestDispatchInput(request="test_input") test_intent_handler = mock.MagicMock(spec=AbstractRequestHandler) test_intent_handler.can_handle.return_value = False test_intent_request_handler_chain = GenericRequestHandlerChain( request_handler=test_intent_handler) test_event_handler = mock.MagicMock(spec=AbstractRequestHandler) test_event_handler.can_handle.return_value = True test_event_request_handler_chain = GenericRequestHandlerChain( request_handler=test_event_handler) test_request_mapper = GenericRequestMapper(request_handler_chains=[ test_event_request_handler_chain, test_intent_request_handler_chain ]) assert test_request_mapper.get_request_handler_chain( test_handler_input).request_handler == test_event_handler, ( "get_request_handler_chain in Request Mapper found incorrect " "request handler chain for " "intent request")
def test_handler_input_successful_global_response_interceptors_execution( self): test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler.can_handle.return_value = True test_response = mock.MagicMock(spec=TestDispatchOutput) test_response_before_interceptor = test_response test_request_handler.handle.return_value = test_response test_interceptor_1 = mock.MagicMock(spec=AbstractResponseInterceptor) test_response.interceptor = "Interceptor 1" test_response_from_interceptor_1 = test_response test_interceptor_1.process.return_value = test_response test_interceptor_2 = mock.MagicMock(spec=AbstractResponseInterceptor) test_response.interceptor = "Interceptor 2" test_response_from_interceptor_2 = test_response test_interceptor_2.process.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 = GenericHandlerAdapter() self.test_dispatcher.request_mappers = [test_request_mapper] self.test_dispatcher.handler_adapters = [test_adapter] self.test_dispatcher.response_interceptors = [ test_interceptor_1, test_interceptor_2 ] assert self.test_dispatcher.dispatch( handler_input=self.valid_handler_input ) == test_response_from_interceptor_2, ( "Dispatcher dispatch method returned invalid response after " "processing response through " "global response interceptors") test_interceptor_1.process.assert_called_once_with( handler_input=self.valid_handler_input, response=test_response_before_interceptor), ( "Dispatcher dispatch method didn't process global request " "interceptors after calling dispatch request") test_interceptor_2.process.assert_called_once_with( handler_input=self.valid_handler_input, response=test_response_from_interceptor_1), ( "Dispatcher dispatch method didn't process global request " "interceptors after calling dispatch request")
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_handler_input_with_supported_chain_in_mapper_no_adapters(self): test_request_handler = mock.MagicMock(spec=AbstractRequestHandler) test_request_handler.can_handle.return_value = True test_request_handler_chain = mock.MagicMock( spec=GenericRequestHandlerChain) test_request_handler_chain.request_handler = test_request_handler test_request_mapper = GenericRequestMapper( request_handler_chains=[test_request_handler_chain]) self.test_dispatcher.request_mappers = [test_request_mapper] with self.assertRaises(DispatchException) as exc: self.test_dispatcher.dispatch( handler_input=self.valid_handler_input) assert "Unable to find a suitable request adapter" in str( exc.exception ), ("Dispatcher didn't throw Dispatch Exception when no adapters are " "registered in " "dispatcher")