def _assert_node_test_case(self, **kwargs):
        self.assertEqual(kwargs['__jsonpath__'], kwargs['node'].tojsonpath())

        if isinstance(kwargs['node'], RootNode):
            self.assertEqual(kwargs['node'],
                             Path.parse_str(kwargs['__jsonpath__']).root_node)

            with NamedTemporaryFile() as temp_file:
                temp_file.write(bytes(kwargs['__jsonpath__'], 'utf-8'))
                temp_file.seek(0)

                self.assertEqual(kwargs['node'],
                                 Path.parse_file(temp_file.name).root_node)

        else:
            with self.assertRaises(ValueError):
                Path(kwargs['node'])

            with self.assertRaises(ValueError):
                Path.parse_str('__jsonpath__')

        match_data_list = list(kwargs['node'].match(kwargs['root_value'],
                                                    kwargs['current_value']))

        for index, value in enumerate(match_data_list):
            self.assertEqual(kwargs['match_data_list'][index], value)

        for match_data in match_data_list:
            new_match_data_list = list(
                match_data.node.match(kwargs['root_value'],
                                      kwargs['current_value']))

            self.assertEqual([match_data], new_match_data_list)
    def test_proxymod_path(self):
        proxymod_path = Path.parse_file(
            os.path.join(os.path.dirname(__file__), '..', 'jsonpath2',
                         'proxymod.txt'))

        self.assertEqual(1, len(list(proxymod_path.match(self.event_data))))

        return
    def _assert_node_test_case(self, **kwargs):
        self.assertEqual(kwargs["__jsonpath__"], kwargs["node"].tojsonpath())

        if isinstance(kwargs["node"], RootNode):
            self.assertEqual(kwargs["node"],
                             Path.parse_str(kwargs["__jsonpath__"]).root_node)

            with NamedTemporaryFile() as temp_file:
                temp_file.write(bytes(kwargs["__jsonpath__"], "utf-8"))
                temp_file.seek(0)

                self.assertEqual(kwargs["node"],
                                 Path.parse_file(temp_file.name).root_node)

        else:
            with self.assertRaises(ValueError):
                Path(kwargs["node"])

            with self.assertRaises(ValueError):
                Path.parse_str("__jsonpath__")

        match_data_list = list(kwargs["node"].match(kwargs["root_value"],
                                                    kwargs["current_value"]))

        self.assertListEqual(kwargs["match_data_list"], match_data_list)
        self.assertListEqual(
            list(
                map(
                    lambda match_data: match_data.node.tojsonpath(),
                    kwargs["match_data_list"],
                )),
            list(
                map(lambda match_data: match_data.node.tojsonpath(),
                    match_data_list)),
        )

        for match_data in match_data_list:
            new_match_data_list = list(
                match_data.node.match(kwargs["root_value"],
                                      kwargs["current_value"]))

            self.assertListEqual([match_data], new_match_data_list)
            self.assertListEqual(
                list(
                    map(lambda match_data: match_data.node.tojsonpath(),
                        [match_data])),
                list(
                    map(
                        lambda match_data: match_data.node.tojsonpath(),
                        new_match_data_list,
                    )),
            )
Beispiel #4
0
    def test_example_jsonpath2_path(self) -> None:
        """Test the JSONPath for Pacifica Dispatcher Example.

        """

        # Construct the JSONPath for the example event handler by parsing the
        # content of a text file.
        #
        path = Path.parse_file(
            os.path.join(os.path.dirname(__file__), '..', 'pacifica',
                         'dispatcher_example', 'jsonpath2',
                         'example.txt'))  # type: jsonpath2.path.Path

        # Verify that the JSONPath successfully matches the JSON-encoded data
        # for the CloudEvents notification.
        #
        self.assertEqual(1, len(list(path.match(self.event_data))))
Beispiel #5
0
from pacifica.cli.methods import generate_global_config, generate_requests_auth
from pacifica.downloader import Downloader
from pacifica.notifications.client.downloader_runners import RemoteDownloaderRunner
from pacifica.notifications.client.router import Router
from pacifica.notifications.client.uploader_runners import RemoteUploaderRunner
from pacifica.uploader import Uploader

from .event_handlers import ProxEventHandler

config = generate_global_config()

auth = generate_requests_auth(config)

downloader_runner = RemoteDownloaderRunner(
    Downloader(cart_api_url=config.get('endpoints', 'download_url'),
               auth=auth))

uploader_runner = RemoteUploaderRunner(
    Uploader(upload_url=config.get('endpoints', 'upload_url'),
             status_url=config.get('endpoints', 'upload_status_url'),
             auth=auth))

router = Router()

router.add_route(
    Path.parse_file(
        os.path.join(os.path.dirname(__file__), 'jsonpath2', 'proxymod.txt')),
    ProxEventHandler(downloader_runner, uploader_runner))

__all__ = ('router')
 def test_proxymod_path(self):
     """Test proxymod path."""
     proxymod_path = Path.parse_file(
         os.path.join(os.path.dirname(__file__), '..', 'pacifica',
                      'dispatcher_proxymod', 'jsonpath2', 'proxymod.txt'))
     self.assertEqual(1, len(list(proxymod_path.match(self.event_data))))
Beispiel #7
0
# authentication credentials.
#
uploader_runner = RemoteUploaderRunner(
    Uploader(upload_url=config.get('endpoints', 'upload_url'),
             status_url=config.get('endpoints', 'upload_status_url'),
             auth=auth)
)  # type: pacifica.dispatcher.uploader_runners.RemoteUploaderRunner

# Construct an __empty__ router.
#
router = Router()  # type: pacifica.dispatcher.router.Router

# Add a new route to the router, so that it is non-empty.
#
# The call to add a new route to the router receives the following arguments:
# 1. The JSONPath, i.e., the instance of the ``jsonpath2.path.Path`` class.
# 2. The event handler, i.e., the instance of the
#    ``pacifica.dispatcher.event_handlers.EventHandler`` class.
#
# In this example, the JSONPath for the example event handler is parsed from the
# content of a text file.
#
router.add_route(
    Path.parse_file(
        os.path.join(os.path.dirname(__file__), 'jsonpath2', 'example.txt')),
    ExampleEventHandler(downloader_runner, uploader_runner))

# Module exports.
#
__all__ = ('router', )