class TestGenericHandlerAdapter(unittest.TestCase): def setUp(self): self.test_handler_adapter = GenericHandlerAdapter() def test_adapter_supports_valid_handler(self): test_handler = mock.MagicMock(spec=AbstractRequestHandler) test_handler.can_handle.return_value = True assert self.test_handler_adapter.supports(test_handler), ( "Handler Adapter supports method returns False for supported " "Request Handler implementation") def test_adapter_doesnt_supports_invalid_handler(self): test_handler = mock.Mock() assert not self.test_handler_adapter.supports(test_handler), ( "Handler Adapter supports method returns True for unsupported " "Request Handler implementation") def test_adapter_executes_valid_handler(self): test_handler = mock.MagicMock(spec=AbstractRequestHandler) test_handler.handle.return_value = "Test Response" test_input = TestDispatchInput(request="test_input") assert self.test_handler_adapter.execute( handler=test_handler, handler_input=test_input) == "Test Response", ( "Handler Adapter executes method returns unexpected response " "output for supported Request Handler " "implementation")
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_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_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 setUp(self): self.test_handler_adapter = GenericHandlerAdapter()