def test_args_override_spring_application_json(self):
     os.environ[
         'SPRING_APPLICATION_JSON'] = '{"spring.cloud.stream.bindings.output.producer.requiredGroups":"original","spring.cloud.stream.bindings.output.destination":"original.time"}'
     env = environment.env(sys.argv)
     env = environment.env(['--spring.cloud.stream.bindings.output.producer.requiredGroups=ticktock',
                            '--spring.cloud.stream.bindings.output.destination=ticktock.time'])
     self.assertEquals('ticktock', env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEquals('ticktock.time', env['spring.cloud.stream.bindings.output.destination'])
    def test_include_app_config_file(self):
        env = environment.env([], [os.path.abspath(__file__) + '/../application-test.cfg'])
        self.assertEqual('group1,group2', env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
        self.assertEqual('True', env['spring.cloud.stream.rabbit.bindings.output.producer.autoBindDlq'])
        self.assertEqual('prefix-',env['spring.cloud.stream.rabbit.bindings.output.producer.prefix'])

        self.assertEqual('5', env['spring.cloud.stream.rabbit.bindings.input.consumer.prefetch'])
 def test_env_overrides_app_config_file(self):
     os.environ[
         'SPRING_APPLICATION_JSON'] = '{"spring.cloud.stream.bindings.output.producer.requiredGroups":"override"}'
     env = environment.env([],
                           os.path.abspath(__file__) +
                           '/../application-test.cfg')
     self.assertEqual(
         'override',
         env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
 def setUp(self):
     pika.BlockingConnection.__init__ = mock.MagicMock(return_value=None)
     self.connection = pika.BlockingConnection(pika.URLParameters('amqp://somehost:5672'))
     self.channel = mock.MagicMock(return_value=None)
     pika.channel.Channel.exchange_declare = self.channel
     pika.BlockingConnection.channel = mock.MagicMock(return_value=self.channel)
     self.binder = Binder(
                 self.connection,
                 env=environment.env([], os.path.abspath(__file__) + '/../application-test.cfg'))
 def test_env_from_spring_application_json(self):
     os.environ[
         'SPRING_APPLICATION_JSON'] = '{"spring.cloud.stream.bindings.output.producer.requiredGroups":"original","spring.cloud.stream.bindings.output.destination":"original.time"}'
     env = environment.env(sys.argv)
     self.assertEqual(
         'original',
         env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEqual(
         'original.time',
         env['spring.cloud.stream.bindings.output.destination'])
 def test_env_with_args(self):
     env = environment.env([
         '--spring.cloud.stream.bindings.output.producer.requiredGroups=ticktock',
         '--spring.cloud.stream.bindings.output.destination=ticktock.time'
     ])
     self.assertEqual(
         'ticktock',
         env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEqual(
         'ticktock.time',
         env['spring.cloud.stream.bindings.output.destination'])
 def setUp(self):
     pika.BlockingConnection.__init__ = mock.MagicMock(return_value=None)
     self.connection = pika.BlockingConnection(
         pika.URLParameters('amqp://somehost:5672'))
     self.channel = mock.MagicMock(return_value=None)
     pika.channel.Channel.exchange_declare = self.channel
     pika.BlockingConnection.channel = mock.MagicMock(
         return_value=self.channel)
     self.binder = Binder(self.connection,
                          env=environment.env([],
                                              os.path.abspath(__file__) +
                                              '/../application-test.cfg'))
 def test_args_override_spring_application_json(self):
     os.environ[
         'SPRING_APPLICATION_JSON'] = '{"spring.cloud.stream.bindings.output.producer.requiredGroups":"original","spring.cloud.stream.bindings.output.destination":"original.time"}'
     env = environment.env([
         '--spring.cloud.stream.bindings.output.producer.requiredGroups=ticktock',
         '--spring.cloud.stream.bindings.output.destination=ticktock.time'
     ])
     self.assertEqual(
         'ticktock',
         env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEqual(
         'ticktock.time',
         env['spring.cloud.stream.bindings.output.destination'])
    def test_in_memory_config(self):
        #unicode required for python 2
        test_config = u"""
[spring.cloud.stream.bindings.output]
destination=destination

[spring.cloud.stream.bindings.input]
destination=destination_out
group=group1
        """
        config = configparser.ConfigParser()
        config.optionxform = str
        config.read_string(test_config)
        env = environment.env([], config)
        self.assertEqual('destination', env['spring.cloud.stream.bindings.output.destination'])
    def test_in_memory_config(self):
        #unicode required for python 2
        test_config = u"""
[spring.cloud.stream.bindings.output]
destination=destination

[spring.cloud.stream.bindings.input]
destination=destination_out
group=group1
        """
        config = configparser.ConfigParser()
        config.optionxform = str
        config.read_string(test_config)
        env = environment.env([], config)
        self.assertEqual(
            'destination',
            env['spring.cloud.stream.bindings.output.destination'])
    def test_include_app_config_file(self):
        env = environment.env(
            [], [os.path.abspath(__file__) + '/../application-test.cfg'])
        self.assertEqual(
            'group1,group2',
            env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
        self.assertEqual(
            'True',
            env['spring.cloud.stream.rabbit.bindings.output.producer.autoBindDlq']
        )
        self.assertEqual(
            'prefix-',
            env['spring.cloud.stream.rabbit.bindings.output.producer.prefix'])

        self.assertEqual(
            '5',
            env['spring.cloud.stream.rabbit.bindings.input.consumer.prefetch'])
#!/usr/bin/env python
from spring.cloud.stream.binder.rabbit import Binder
from spring.cloud.stream import components
from spring.cloud import environment, cf

import pika
import sys

env = environment.env(sys.argv)

'''Dump the environment just for fun'''
for (k,v) in env.iteritems():
    print ("{0}={1}".format(k,v))

connection = None

app = cf.App(env)

'''start a thread to listen on the health check endpoint. Another option is to disable health checks'''
health_check = app.start_health_check()

try:

    '''Get service info from VCAP_SERVICES'''
    connectionUrl = app.service('rabbit')['credentials']['uri']
    connection = pika.BlockingConnection(pika.URLParameters(connectionUrl))

    sink = components.Sink()
    sink.bind(Binder(connection, env))

    def callback(channel, method, properties, body):
#!/usr/bin/env python
import sys

import pika

from spring.cloud.stream import components
from spring.cloud.stream.binder.rabbit import Binder
from spring.cloud import environment

env = environment.env(sys.argv)
#
# If deployed using Spring Cloud Dataflow, or you want to set these environment variables, use this connection String
#connectionUrl = 'amqp://{0}:{1}'.format(env['SPRING_RABBITMQ_HOST'],env['SPRING_RABBITMQ_PORT'])
#

connectionUrl = 'amqp://localhost:5672'
connection = pika.BlockingConnection(pika.URLParameters(connectionUrl))

sink = components.Sink()
sink.bind(Binder(connection, env))


def callback(channel, method, properties, body):
    print(" [x] Received %r" % body, properties)


sink.receive(callback)

connection.close()
 def test_default_env_is_os_env(self):
     env = environment.env(sys.argv)
     self.assertEqual(os.environ.__len__(), env.__len__())
 def test_env_from_os_environment(self):
     os.environ['SPRING_RABBITMQ_HOST'] = 'localhost'
     env = environment.env(sys.argv)
     self.assertEqual('localhost', env['SPRING_RABBITMQ_HOST'])
 def test_env_overrides_app_config_file(self):
     os.environ[
         'SPRING_APPLICATION_JSON'] = '{"spring.cloud.stream.bindings.output.producer.requiredGroups":"override"}'
     env = environment.env([], os.path.abspath(__file__) + '/../application-test.cfg')
     self.assertEqual('override', env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
 def test_default_env_is_os_env(self):
     env = environment.env(sys.argv)
     self.assertEqual(os.environ.__len__(), env.__len__())
 def test_env_with_args(self):
     env = environment.env(['--spring.cloud.stream.bindings.output.producer.requiredGroups=ticktock',
                            '--spring.cloud.stream.bindings.output.destination=ticktock.time'])
     self.assertEqual('ticktock', env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEqual('ticktock.time', env['spring.cloud.stream.bindings.output.destination'])
 def test_env_from_os_environment(self):
     os.environ['SPRING_RABBITMQ_HOST'] = 'localhost'
     env = environment.env(sys.argv)
     self.assertEquals('localhost', env['SPRING_RABBITMQ_HOST'])
 def test_env_from_spring_application_json(self):
     os.environ[
         'SPRING_APPLICATION_JSON'] = '{"spring.cloud.stream.bindings.output.producer.requiredGroups":"original","spring.cloud.stream.bindings.output.destination":"original.time"}'
     env = environment.env(sys.argv)
     self.assertEquals('original', env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEquals('original.time', env['spring.cloud.stream.bindings.output.destination'])
 def test_default_env_is_os_env(self):
     del os.environ['SPRING_APPLICATION_JSON']
     env = environment.env(sys.argv)
     self.assertEquals(os.environ.__len__(), env.__len__())
 def test_env_with_args(self):
     del os.environ['SPRING_APPLICATION_JSON']
     env = environment.env(['--spring.cloud.stream.bindings.output.producer.requiredGroups=ticktock',
                            '--spring.cloud.stream.bindings.output.destination=ticktock.time'])
     self.assertEquals('ticktock', env['spring.cloud.stream.bindings.output.producer.requiredGroups'])
     self.assertEquals('ticktock.time', env['spring.cloud.stream.bindings.output.destination'])