def test_generic_handler_chain_add_response_interceptors_to_empty_list( self): test_handler = mock.Mock() test_handler_chain = GenericRequestHandlerChain( request_handler=test_handler) test_response_interceptor = mock.Mock() test_handler_chain.add_response_interceptor( interceptor=test_response_interceptor) assert test_handler_chain.response_interceptors == [ test_response_interceptor ], ("Generic Request Handler Chain didn't add interceptor to list of " "response interceptors when no " "response interceptors are provided during instantiation")
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_generic_handler_chain_add_response_interceptors_to_non_empty_list( self): test_handler = mock.Mock() test_interceptor_1 = mock.MagicMock(spec=AbstractResponseInterceptor) test_interceptors = [test_interceptor_1] test_handler_chain = GenericRequestHandlerChain( request_handler=test_handler, response_interceptors=test_interceptors) test_response_interceptor = mock.MagicMock( spec=AbstractResponseInterceptor) test_handler_chain.add_response_interceptor( interceptor=test_response_interceptor) assert test_handler_chain.response_interceptors == [ test_interceptor_1, test_response_interceptor ], ("Generic Request Handler Chain didn't add interceptor to list of " "response interceptors when " "response interceptors are provided during instantiation")
def test_generic_handler_chain_instantiate_response_interceptors(self): test_handler = mock.Mock() test_handler_chain = GenericRequestHandlerChain( request_handler=test_handler) assert test_handler_chain.response_interceptors == [], ( "Generic Request Handler Chain didn't instantiate empty list of " "response interceptors when no " "response interceptors are provided during instantiation")
def test_generic_handler_chain_instantiate_call_parent_instantiation(self): test_handler = mock.Mock() test_handler_chain = GenericRequestHandlerChain( request_handler=test_handler) assert ( test_handler_chain.request_interceptors == [] and test_handler_chain.response_interceptors == [] ), ("Generic Request Handler Chain didn't call parent instantiation " "method during init")
def test_generic_handler_chain_with_null_request_handler_throws_error( self): with self.assertRaises(DispatchException) as exc: test_handler_chain = GenericRequestHandlerChain( request_handler=None) assert "No Request Handler provided" in str(exc.exception), ( "Generic Request Handler Chain didn't raise exception when no " "request handler is provided during " "instantiation")
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_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_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_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_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_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_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")