def test_execute_static_action_handle_execute_excs(self, mock_execute, _):
        """Test whether execute handles all exceptions thrown correctly."""
        request_data = {
            "className": 'TestClass',
            "methodName": 'TestAction',
            "packageName": 'TestPackage',
            "classVersion": '=0',
            "parameters": {
                'name': 'John'
            }
        }

        exc_types = [
            'NoClassFound', 'NoMethodFound', 'NoPackageFound',
            'NoPackageForClassFound', 'MethodNotExposed',
            'NoMatchingMethodException'
        ]
        for exc_type in exc_types:
            mock_execute.side_effect = client.RemoteError(exc_type=exc_type)
            req = self._post('/actions', jsonutils.dump_as_bytes(request_data))
            self.assertRaises(exc.HTTPNotFound, self.controller.execute, req,
                              request_data)
        self.assertEqual(mock_execute.call_count, len(exc_types))

        exc_type = 'ContractViolationException'
        mock_execute.side_effect = client.RemoteError(exc_type=exc_type)
        req = self._post('/actions', jsonutils.dump_as_bytes(request_data))
        self.assertRaises(exc.HTTPBadRequest, self.controller.execute, req,
                          request_data)
        exc_types.append(exc_type)
        self.assertEqual(mock_execute.call_count, len(exc_types))

        exc_type = 'ThisIsARandomTestException'
        mock_execute.side_effect = client.RemoteError(exc_type=exc_type)
        req = self._post('/actions', jsonutils.dump_as_bytes(request_data))
        self.assertRaises(exc.HTTPServiceUnavailable, self.controller.execute,
                          req, request_data)
        exc_types.append(exc_type)
        self.assertEqual(mock_execute.call_count, len(exc_types))

        try:
            int('this will throw a value error')
        except ValueError as e:
            setattr(e, 'message', None)
            exc_type = e
        mock_execute.side_effect = exc_type
        req = self._post('/actions', jsonutils.dump_as_bytes(request_data))
        self.assertRaises(exc.HTTPBadRequest, self.controller.execute, req,
                          request_data)
        exc_types.append(exc_type)
        self.assertEqual(mock_execute.call_count, len(exc_types))
Пример #2
0
    def test_engine_client_remote_error(self):
        mocked = mock.Mock()
        mocked.sync_call.side_effect = rpc_client.RemoteError(
            'InputException', 'Input is wrong')
        self.engine_client._client = mocked

        self.assertRaises(exc.InputException,
                          self.engine_client.start_workflow, 'some_wf', {},
                          'some_description')
Пример #3
0
    def test_get_schema_negative(self, mock_rpc, *args):
        dummy_context = test_utils.dummy_context()
        dummy_context.GET = {
            'classVersion': 'test_class_version',
            'packageName': 'test_package_name'
        }
        mock_request = mock.MagicMock(context=dummy_context)

        # Test exception handling for pre-defined exception types.
        exc_types = ('NoClassFound', 'NoPackageForClassFound',
                     'NoPackageFound')
        for exc_type in exc_types:
            dummy_error = client.RemoteError(exc_type=exc_type,
                                             value='dummy_value')
            mock_rpc.engine().generate_schema.side_effect = dummy_error
            with self.assertRaisesRegex(exc.HTTPNotFound, dummy_error.value):
                self.controller.get_schema(mock_request, 'test_class')

        # Test exception handling for miscellaneous exception type.
        dummy_error = client.RemoteError(exc_type='TestExcType',
                                         value='dummy_value')
        mock_rpc.engine().generate_schema.side_effect = dummy_error
        with self.assertRaisesRegex(client.RemoteError, dummy_error.value):
            self.controller.get_schema(mock_request, 'test_class')