def test_get_handler_chain_registered_for_event_request(self):
        test_intent_request = mock.MagicMock(spec=IntentRequest)
        test_request_envelope = mock.MagicMock(spec=RequestEnvelope)
        test_request_envelope.request = test_intent_request
        test_handler_input = HandlerInput(
            request_envelope=test_request_envelope)

        test_intent_handler = mock.MagicMock(spec=AbstractRequestHandler)
        test_intent_handler.can_handle.return_value = False
        test_intent_request_handler_chain = RequestHandlerChain(
            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 = RequestHandlerChain(
            request_handler=test_event_handler)

        test_request_mapper = RequestMapper(
            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_default_handler_chain_with_invalid_request_handler_in_setter_throws_error(self):
        test_handler = mock.MagicMock(spec=AbstractRequestHandler)
        test_handler_chain = RequestHandlerChain(request_handler=test_handler)

        test_invalid_handler = mock.Mock()

        with self.assertRaises(DispatchException) as exc:
            test_handler_chain.request_handler = test_invalid_handler

        assert "Invalid Request Handler provided. Expected Request Handler instance" in str(exc.exception), (
            "Default Request Handler Chain didn't raise exception when "
            "invalid request handler is 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 = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            request_handler_chains=[test_request_handler_chain])

        test_adapter = mock.MagicMock(spec=HandlerAdapter)
        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 = ExceptionMapper(
            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_dispatch_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 = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            request_handler_chains=[test_request_handler_chain])

        test_adapter = HandlerAdapter()

        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_dispatch_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 = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            request_handler_chains=[test_request_handler_chain])

        test_adapter = HandlerAdapter()

        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_default_handler_chain_with_null_request_handler_throws_error(self):
        with self.assertRaises(DispatchException) as exc:
            test_handler_chain = RequestHandlerChain(request_handler=None)

        assert "Invalid Request Handler provided. Expected Request Handler instance" in str(exc.exception), (
            "Default Request Handler Chain didn't raise exception when no "
            "request handler is provided during "
            "instantiation")
Пример #7
0
    def test_dispatch_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 = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            request_handler_chains=[test_request_handler_chain])

        test_adapter = HandlerAdapter()

        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_default_request_mapper_initialization_with_chain_containing_valid_type(self):
        test_request_handler = mock.MagicMock(spec=AbstractRequestHandler)
        test_request_handler_chain = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            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")
Пример #9
0
    def test_add_request_handler_chain_for_valid_chain_type(self):
        test_request_handler = mock.MagicMock(spec=AbstractRequestHandler)
        test_request_handler_chain = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(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
        ], ("Default Request Mapper throws exception when a valid Handler "
            "Chain is provided in the "
            "add_request_handler_chain method")
Пример #10
0
    def test_dispatch_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=Response)
        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 = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            request_handler_chains=[test_request_handler_chain])

        test_adapter = HandlerAdapter()

        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_event_request(self):
        test_event_request = mock.MagicMock(spec=SkillEnabledRequest)
        test_request_envelope = mock.MagicMock(spec=RequestEnvelope)
        test_request_envelope.request = test_event_request
        test_handler_input = HandlerInput(
            request_envelope=test_request_envelope)

        test_request_handler = mock.MagicMock(spec=AbstractRequestHandler)
        test_request_handler.can_handle.return_value = False
        test_request_handler_chain = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            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 "
            "event request")
Пример #12
0
    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 = RequestHandlerChain(
            request_handler=test_request_handler)
        test_request_mapper = RequestMapper(
            request_handler_chains=[test_request_handler_chain])

        test_adapter = mock.MagicMock(spec=HandlerAdapter)
        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"

        self.test_dispatcher = RequestDispatcher(
            request_mappers=[test_request_mapper],
            handler_adapters=[test_adapter],
            exception_mapper=ExceptionMapper(exception_handlers=[
                test_exception_handler_1, test_exception_handler_2
            ]))

        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")