# Path: devicecloud/monitor.py
# MON_TOPIC_ATTR = Attribute("monTopic")
# 
# MON_TRANSPORT_TYPE_ATTR = Attribute("monTransportType")
# 
# Path: devicecloud/test/unit/test_utilities.py
# class HttpTestBase(unittest.TestCase):
#     def setUp(self):
#         httpretty.enable()
#         # setup Device Cloud ping response
#         self.prepare_response("GET", "/ws/DeviceCore?size=1", "", status=200)
#         self.dc = DeviceCloud('user', 'pass')
# 
#     def tearDown(self):
#         httpretty.disable()
#         httpretty.reset()
# 
#     def _get_last_request(self):
#         return httpretty.last_request()
# 
#     def _get_last_request_params(self):
#         # Get the query params from the last request as a dictionary
#         params = urllib_parse.parse_qs(urllib_parse.urlparse(self._get_last_request().path).query)
#         return {k: v[0] for k, v in params.items()}  # convert from list values to single-value
# 
#     def prepare_response(self, method, path, data=None, status=200, match_querystring=False, **kwargs):
#         # TODO:
#         #   Should probably assert on more request headers and
#         #   respond with correct content type, etc.
#         if data is not None:
#             kwargs['body'] = data
#         httpretty.register_uri(method,
#                                "https://devicecloud.digi.com{}".format(path),
#                                match_querystring=match_querystring,
#                                status=status,
#                                **kwargs)
# 
#     def prepare_json_response(self, method, path, data, status=200):
#         self.prepare_response(method, path, json.dumps(data), status=status)


from devicecloud.monitor import MON_TOPIC_ATTR, MON_TRANSPORT_TYPE_ATTR
from devicecloud.test.unit.test_utilities import HttpTestBase
import six
        mon = self.dc.monitor.create_tcp_monitor(['topA', 'topB'], batch_size=10, batch_duration=0,
                                                 compression='gzip', format_type='json')
        self.assertEqual(self._get_last_request().body, six.b(CREATE_TCP_MONITOR_GOOD_REQUEST))
        self.assertEqual(mon.get_id(), 178008)

    def test_create_http_monitor(self):
        self.prepare_response("POST", "/ws/Monitor", data=CREATE_MONITOR_GOOD_RESPONSE)
        mon = self.dc.monitor.create_http_monitor(['topA', 'topB'], 'http://digi.com', transport_token=None,
                                                  transport_method='PUT', connect_timeout=0, response_timeout=0,
                                                  batch_size=1, batch_duration=0, compression='none',
                                                  format_type='json')
        self.assertEqual(self._get_last_request().body, six.b(CREATE_HTTP_MONITOR_GOOD_REQUEST))
        self.assertEqual(mon.get_id(), 178008)

    def test_get_tcp_monitors(self):
        self.prepare_response("GET", "/ws/Monitor", data=GET_TCP_MONITOR_SINGLE_FOUND)
        mons = list(self.dc.monitor.get_monitors((MON_TOPIC_ATTR == "DeviceCore") &
                                                 (MON_TRANSPORT_TYPE_ATTR == "tcp")))
        self.assertEqual(len(mons), 1)
        mon = mons[0]
        self.assertEqual(mon.get_id(), 178007)
        self.assertEqual(self._get_last_request_params(), {
            'condition': "monTopic='DeviceCore' and monTransportType='tcp'",
            'start': '0',
            'size': '1000'
        })

    def test_get_http_monitors(self):
        self.prepare_response("GET", "/ws/Monitor", data=GET_TCP_MONITOR_SINGLE_FOUND)
        mons = list(self.dc.monitor.get_monitors((MON_TOPIC_ATTR == "DeviceCore") &

# Path: devicecloud/monitor.py
# MON_TOPIC_ATTR = Attribute("monTopic")
# 
# MON_TRANSPORT_TYPE_ATTR = Attribute("monTransportType")
# 
# Path: devicecloud/test/unit/test_utilities.py
# class HttpTestBase(unittest.TestCase):
#     def setUp(self):
#         httpretty.enable()
#         # setup Device Cloud ping response
#         self.prepare_response("GET", "/ws/DeviceCore?size=1", "", status=200)
#         self.dc = DeviceCloud('user', 'pass')
# 
#     def tearDown(self):
#         httpretty.disable()
#         httpretty.reset()
# 
#     def _get_last_request(self):
#         return httpretty.last_request()
# 
#     def _get_last_request_params(self):
#         # Get the query params from the last request as a dictionary
#         params = urllib_parse.parse_qs(urllib_parse.urlparse(self._get_last_request().path).query)
#         return {k: v[0] for k, v in params.items()}  # convert from list values to single-value
# 
#     def prepare_response(self, method, path, data=None, status=200, match_querystring=False, **kwargs):
#         # TODO:
#         #   Should probably assert on more request headers and
#         #   respond with correct content type, etc.
#         if data is not None:
#             kwargs['body'] = data
#         httpretty.register_uri(method,
#                                "https://devicecloud.digi.com{}".format(path),
#                                match_querystring=match_querystring,
#                                status=status,
#                                **kwargs)
# 
#     def prepare_json_response(self, method, path, data, status=200):
#         self.prepare_response(method, path, json.dumps(data), status=status)


# Path: devicecloud/test/unit/test_monitor.py
from devicecloud.monitor import MON_TOPIC_ATTR, MON_TRANSPORT_TYPE_ATTR
from devicecloud.test.unit.test_utilities import HttpTestBase
import six

        mon = self.dc.monitor.create_tcp_monitor(['topA', 'topB'], batch_size=10, batch_duration=0,
                                                 compression='gzip', format_type='json')
        self.assertEqual(self._get_last_request().body, six.b(CREATE_TCP_MONITOR_GOOD_REQUEST))
        self.assertEqual(mon.get_id(), 178008)

    def test_create_http_monitor(self):
        self.prepare_response("POST", "/ws/Monitor", data=CREATE_MONITOR_GOOD_RESPONSE)
        mon = self.dc.monitor.create_http_monitor(['topA', 'topB'], 'http://digi.com', transport_token=None,
                                                  transport_method='PUT', connect_timeout=0, response_timeout=0,
                                                  batch_size=1, batch_duration=0, compression='none',
                                                  format_type='json')
        self.assertEqual(self._get_last_request().body, six.b(CREATE_HTTP_MONITOR_GOOD_REQUEST))
        self.assertEqual(mon.get_id(), 178008)

    def test_get_tcp_monitors(self):
        self.prepare_response("GET", "/ws/Monitor", data=GET_TCP_MONITOR_SINGLE_FOUND)
        mons = list(self.dc.monitor.get_monitors((MON_TOPIC_ATTR == "DeviceCore") &
                                                 (MON_TRANSPORT_TYPE_ATTR == "tcp")))
        self.assertEqual(len(mons), 1)
        mon = mons[0]
        self.assertEqual(mon.get_id(), 178007)
        self.assertEqual(self._get_last_request_params(), {
            'condition': "monTopic='DeviceCore' and monTransportType='tcp'",
            'start': '0',
            'size': '1000'
        })

    def test_get_http_monitors(self):
        self.prepare_response("GET", "/ws/Monitor", data=GET_TCP_MONITOR_SINGLE_FOUND)
        mons = list(self.dc.monitor.get_monitors((MON_TOPIC_ATTR == "DeviceCore") &

                                                 (MON_TRANSPORT_TYPE_ATTR == "http")))