def test_bad_param(self):
      print ('\n---------'+str(self))
      # constructor tests
      self.assertRaises(ValueError, MQTTSource, server_uri=None, topics='topic1', schema=MqttDataTuple)
      # topics = None
      self.assertRaises(ValueError, MQTTSource, server_uri='tcp://server:1234', topics=None, schema=MqttDataTuple)
      # schema = None
      self.assertRaises(ValueError, MQTTSource, server_uri='tcp://server:1234', topics='topic1', schema=None)
      # server_uri = None
      self.assertRaises(ValueError, MQTTSink, server_uri=None, topic='topic1')
      # topic and topic_attribute_name = None
      self.assertRaises(ValueError, MQTTSink, server_uri='tcp://server:1234', topic=None, topic_attribute_name=None)
      # topic and topic_attribute_name = Not None
      self.assertRaises(ValueError, MQTTSink, server_uri='tcp://server:1234', topic='topic1', topic_attribute_name='topic')
 
      # setter tests
      src = MQTTSource(server_uri='tcp://server:1833', topics=['topic1', 'topic2'], schema=MqttDataTuple)
      with self.assertRaises(TypeError):
          src.qos = ['0', '2']
      with self.assertRaises(ValueError):
          src.qos = 3
      with self.assertRaises(ValueError):
          src.qos = -1
      with self.assertRaises(ValueError):
          src.qos = [0, 3]
      with self.assertRaises(ValueError):
          src.reconnection_bound = -2
      with self.assertRaises(ValueError):
          src.keep_alive_seconds = -1
      with self.assertRaises(ValueError):
          src.command_timeout_millis = -1
      with self.assertRaises(ValueError):
          src.message_queue_size = 0
          
      sink = MQTTSink(server_uri='tcp://server:1833', topic='topic1')
      with self.assertRaises(ValueError):
          sink.qos = 3
      with self.assertRaises(ValueError):
          sink.qos = -1
      with self.assertRaises(TypeError):
          sink.qos = [0, 3]
      with self.assertRaises(ValueError):
          sink.reconnection_bound = -2
      with self.assertRaises(ValueError):
          sink.keep_alive_seconds = -1
      with self.assertRaises(ValueError):
          sink.command_timeout_millis = -1
    def test_MQTTSource_schemas(self):
        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=CommonSchema.Json, data_attribute_name='ignored')
        # topology.source() calls our populate()
        Topology().source(s)
        self.assertEqual(s._op.params['dataAttributeName'], 'jsonString')
        
        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=CommonSchema.String, data_attribute_name='ignored')
        Topology().source(s)
        self.assertEqual(s._op.params['dataAttributeName'], 'string')
        
        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=CommonSchema.Binary, data_attribute_name='ignored')
        Topology().source(s)
        self.assertEqual(s._op.params['dataAttributeName'], 'binary')
        
        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=MqttDataTuple)
        Topology().source(s)
        self.assertNotIn('dataAttributeName', s._op.params)
        
        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=[MqttDataTuple], data_attribute_name='data')
        Topology().source(s)
        self.assertEqual(s._op.params['dataAttributeName'], 'data')

        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema='tuple<rstring data>')
        Topology().source(s)
        self.assertNotIn('dataAttributeName', s._op.params)

        s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=StreamSchema('tuple<rstring data>'))
        Topology().source(s)
        self.assertNotIn('dataAttributeName', s._op.params)
 def test_options_kwargs_MQTTSource(self):
     print ('\n---------'+str(self))
     src = MQTTSource(server_uri='tcp://server:1833',
                      topics=['topic1', 'topic2'],
                      schema=MqttDataTuple,
                      data_attribute_name='data',
                      #kwargs
                      vm_arg = ["-Xmx1G"],
                      ssl_debug = True,
                      reconnection_bound = 5,
                      qos = [1, 2],
                      message_queue_size = 122,
                      trusted_certs = ['cert1', 'cert2'],
                      truststore = "/truststore",
                      truststore_password = "******",
                      client_cert = 'client_cert',
                      client_private_key = 'private_key',
                      keystore = "/keystore",
                      keystore_password = "******",
                      ssl_protocol = 'TLSv1.2',
                      app_config_name = "abbconf",
                      client_id = "client-IDsink",
                      command_timeout_millis = 47,
                      keep_alive_seconds = 3,
                      password = "******",
                      username = "******")
     self.assertEqual(src.server_uri, 'tcp://server:1833')
     self.assertListEqual(src._topics, ['topic1', 'topic2'])
     self.assertEqual(src._data_attribute_name, 'data')
     self.assertEqual(src.reconnection_bound, 5)
     self.assertEqual(src.ssl_debug, True)
     self.assertListEqual(src.vm_arg, ["-Xmx1G"])
     self.assertListEqual(src.qos, [1,2])
     self.assertListEqual(src.trusted_certs, ['cert1', 'cert2'])
     self.assertEqual(src.truststore, '/truststore')
     self.assertEqual(src.truststore_password, 'trustpasswd')
     self.assertEqual(src.client_cert, 'client_cert')
     self.assertEqual(src.client_private_key, 'private_key')
     self.assertEqual(src.keystore, '/keystore')
     self.assertEqual(src.keystore_password, 'keypasswd')
     self.assertEqual(src.ssl_protocol, 'TLSv1.2')
     self.assertEqual(src.app_config_name, 'abbconf')
     self.assertEqual(src.client_id, 'client-IDsink')
     self.assertEqual(src.command_timeout_millis, 47)
     self.assertEqual(src.keep_alive_seconds, 3)
     self.assertEqual(src.password, 'passw0rd')
     self.assertEqual(src.username, 'rolef')
     self.assertEqual(src.message_queue_size, 122)
    def test_device_app(self):
        print ('\n---------'+str(self))
        name = 'test_device_app'
        topo = Topology(name)
        streamsx.spl.toolkit.add_toolkit(topo, self.mqtt_toolkit_home)

        # generate IOT specific Topic for Application
        device_type = "Test"
        event_id = "data"
        device_id = "+"
        message_format = "json"
        # app subscribe topic "iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string"
        app_subscribe_topic = "iot-2/type/"+device_type+"/id/"+device_id+"/evt/"+event_id+"/fmt/"+message_format
        #mqtt config is dict as read by from JSON, JSON uses already correct parameter values
        app_config = self._get_app_config()
        mqtt_source = MQTTSource(server_uri=app_config['serverURI'], topics=app_subscribe_topic, schema=[MqttDataTuple], topic_attribute_name='topic_name')
        mqtt_source.username = app_config['userID']
        mqtt_source.password = app_config['password']
        mqtt_source.client_id = app_config['clientID']
        mqtt_source.vm_arg = "-Dcom.ibm.jsse2.overrideDefaultTLS=true"
        source_stream = topo.source(mqtt_source, name='MqttSubscribe')
        source_stream.print()

        test_stream = self._create_stream(topo)
        test_stream.print()
        # generate IOT specific Topic for device
        # event_id has to be same as for application 
        # message_format has to be same as for application 
        # device event publish topic = "iot-2/evt/event_id/fmt/format_string"
        device_topic = "iot-2/evt/"+event_id+"/fmt/" + message_format
        # device config is dict as read by from JSON, JSON uses already correct parameter values
        device_config = self._get_device_config()
        mqtt_sink = MQTTSink(server_uri=device_config['serverURI'], topic=device_topic, data_attribute_name='data')
        mqtt_sink.client_id = device_config['clientID']
        mqtt_sink.username = device_config['userID']
        mqtt_sink.password = device_config['password']
        mqtt_sink.vm_arg = "-Dcom.ibm.jsse2.overrideDefaultTLS=true"

        test_stream.for_each(mqtt_sink, name='MqttPublish')

        if (("TestDistributed" in str(self)) or ("TestStreamingAnalytics" in str(self))):
            self._launch(topo)
        else:
            # build only
            self._build_only(name, topo)
sk.keystore_password = '******'
sk.ssl_protocol = 'TLSv1.2'
sk.vm_arg = ["-Xmx1G"]
sk.ssl_debug = True
sk.app_config_name = "abbconf"
sk.client_id = "client-ID"
sk.command_timeout_millis = 47
sk.keep_alive_seconds = 3
sk.password = "******"
sk.username = "******"
sk.retain = True
#c.message_queue_size=23

src = MQTTSource(server_uri,
                 topics=['t1', 't2'],
                 schema=CommonSchema.String,
                 data_attribute_name="xyz",
                 topic_attribute_name="bla")
#src.qos = 1
a = 4 - 3
b = a + 1
print('a=' + str(a))
print('b=' + str(b))
src.qos = [a, b]
src.message_queue_size = 122
src.client_id = "client-IDsrc"
src.reconnection_bound = -1
src.trusted_certs = [
    '/tmp/secrets/cluster_ca_cert.pem', '/tmp/secrets/cluster-ca.crt'
]
#src.truststore = "/tmp/truststore-4657686007309004.jks"
 def test_compile_MQTTSource(self):
     print ('\n---------'+str(self))
     name = 'test_MQTTSource'
     topo = Topology(name)
     streamsx.spl.toolkit.add_toolkit(topo, self.mqtt_toolkit_home)
     src = MQTTSource(server_uri='tcp://server:1833', topics=['topic1', 'topic2'], schema=MqttDataTuple)
     # simply add all parameters; let' see if it compiles
     src.qos = [1, 2]
     src.message_queue_size = 122
     src.client_id = "client-IDsrc"
     src.reconnection_bound = 25
     src.trusted_certs = [TRUSTED_CERT_PEM, CLIENT_CA_CERT_PEM]
     src.client_cert = CLIENT_CERT_PEM
     src.client_private_key = PRIVATE_KEY_PEM
     src.ssl_protocol = 'TLSv1.1'
     src.vm_arg = ["-Xmx13G"]
     src.ssl_debug = True
     src.app_config_name = "abbconf2"
     src.command_timeout_millis=30000
     src.keep_alive_seconds = 65
     src.password = "******"
     src.username = "******"
     src.app_config_name = "mqtt_app_cfg"
     
     source_stream = topo.source(src, name='MqttStream')
     source_stream.print()
     # build only
     self._build_only(name, topo)
 def test_MQTTSource_schemas_bad(self):
     s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=CommonSchema.XML)
     # topology.source() calls our populate()
     self.assertRaises(TypeError, Topology().source, s)
     s = MQTTSource(server_uri='tcp://server:1833', topics='topic1', schema=CommonSchema.Python)
     self.assertRaises(TypeError, Topology().source, s)
from streamsx.mqtt import MQTTSource, MQTTSink
import streamsx.topology.context as context
from streamsx.topology.context import ContextTypes, JobConfig
from streamsx.topology.topology import Topology
from streamsx.topology.schema import CommonSchema

mqtt_server_uri = 'tcp://172.16.33.10:1883'
topology = Topology()
s = 'Each character will be an MQTT message'
data = topology.source([c for c in s]).as_string()
# publish to MQTT
data.for_each(MQTTSink(server_uri=mqtt_server_uri, topic='topic'),
              name='MQTTpublish')

# subscribe for data and print to stdout
received = topology.source(
    MQTTSource(mqtt_server_uri, schema=CommonSchema.String, topics='topic'))
received.print()

job_config = JobConfig(job_name='MQTTBasic', tracing='info')
submission_config = {}
# add the job_config into the submission config:
job_config.add(submission_config)
print('submission_config: ' + str(submission_config))
context.submit(ContextTypes.DISTRIBUTED, topology, config=submission_config)
# the Streams Job keeps running and must be cancelled manually