Example #1
0
def clean_scenario(transaction):
    # This shouldn't be needed - controller class shouln't expose flask dependent params
    # TODO remove
    args = MultiDict([
        ('page_size', 10),
        ('page_num', 1),
        ('attr_format', 'both')
    ])
    req = {
        'headers': {
            'authorization': generate_token()
        },
        'args': args,
        'body': ''
    }

    params = {
        'page_size': 10,
        'page_num':  1,
        'attr_format': 'both',
        'attr': [],
        'attr_type': [],
        'idsOnly':'false'
    }

    token = generate_token()

    result = DeviceHandler.get_devices(token, params)

    for device in result['devices']:
        DeviceHandler.delete_device(device['id'], token)

    result = TemplateHandler.get_templates(params, token)
    for template in result['templates']:
        TemplateHandler.remove_template(template['id'], token)
Example #2
0
def create_sample_template():
    template = {
        "label":
        "SensorModel",
        "attrs": [{
            "label": "temperature",
            "type": "dynamic",
            "value_type": "float"
        }, {
            "label": "position",
            "type": "dynamic",
            "value_type": "geopoint"
        }, {
            "label": "model-id",
            "type": "static",
            "value_type": "string",
            "static_value": "model-001"
        }, {
            "label": "shared_key",
            "type": "static",
            "value_type": "psk"
        }]
    }
    req = {
        'headers': {
            'authorization': generate_token(),
            'Content-Type': 'application/json'
        },
        'args': {},
        'body': json.dumps(template)
    }

    result = TemplateHandler.create_template(Request(req))
    template_id = result['template']['id']
    return template_id
    def test_create_tempĺate(self, db_mock):
        db_mock.session = AlchemyMagicMock()
        token = generate_token()

        data = """{
            "label": "SensorModel",
            "attrs": [
                {
                    "label": "temperature",
                    "type": "dynamic",
                    "value_type": "float"
                },
                {
                    "label": "model-id",
                    "type": "static",
                    "value_type": "string",
                    "static_value": "model-001"
                }
            ]
        }"""

        params_query = {'content_type': 'application/json', 'data': data}
        result = TemplateHandler.create_template(params_query, token)
        self.assertIsNotNone(result)
        self.assertEqual(result['result'], 'ok')
        self.assertIsNotNone(result['template'])
    def test_get_templates(self, db_mock):
        db_mock.session = AlchemyMagicMock()
        token = generate_token()

        params_query = {
            'page_number': 5,
            'per_page': 1,
            'sortBy': None,
            'attr': [],
            'attr_type': [],
            'label': 'dummy'
        }
        result = TemplateHandler.get_templates(params_query, token)
        self.assertIsNotNone(result)

        # test using attrs
        params_query = {
            'page_number': 1,
            'per_page': 1,
            'sortBy': None,
            'attr': ['foo=bar'],
            'attr_type': []
        }
        result = TemplateHandler.get_templates(params_query, token)
        self.assertIsNotNone(result)

        # test using sortBy
        params_query = {
            'page_number': 1,
            'per_page': 1,
            'sortBy': 'label',
            'attr': ['foo=bar'],
            'attr_type': []
        }
        result = TemplateHandler.get_templates(params_query, token)
        self.assertIsNotNone(result)

        # test without querys
        params_query = {
            'page_number': 5,
            'per_page': 1,
            'sortBy': None,
            'attr': [],
            'attr_type': []
        }
        result = TemplateHandler.get_templates(params_query, token)
        self.assertIsNotNone(result)
    def test_delete_all_templates(self, db_mock):
        db_mock.session = AlchemyMagicMock()
        token = generate_token()

        result = TemplateHandler.delete_all_templates(token)
        self.assertIsNotNone(result)
        self.assertTrue(result)
        self.assertEqual(result['result'], 'ok')
    def test_get_template(self, db_mock):
        db_mock.session = AlchemyMagicMock()
        token = generate_token()

        template = DeviceTemplate(id=1, label='template1')
        params_query = {'attr_format': 'both'}

        with patch('DeviceManager.TemplateHandler.assert_template_exists'
                   ) as mock_template_exist_wrapper:
            mock_template_exist_wrapper.return_value = template
            result = TemplateHandler.get_template(params_query,
                                                  'template_id_test', token)
            self.assertIsNotNone(result)

            mock_template_exist_wrapper.return_value = None
            result = TemplateHandler.get_template(params_query,
                                                  'template_id_test', token)
            self.assertFalse(result)
Example #7
0
def clean_scenario(transaction):
    # This shouldn't be needed - controller class shouln't expose flask dependent params
    # TODO remove
    args = MultiDict([('page_size', 10), ('page_num', 1),
                      ('attr_format', 'both')])
    req = {
        'headers': {
            'authorization': generate_token()
        },
        'args': args,
        'body': ''
    }

    result = DeviceHandler.get_devices(Request(req))
    for device in result['devices']:
        DeviceHandler.delete_device(Request(req), device['id'])

    result = TemplateHandler.get_templates(Request(req))
    for template in result['templates']:
        # print(template)
        TemplateHandler.remove_template(Request(req), template['id'])
    def test_remove_template(self, db_mock):
        db_mock.session = AlchemyMagicMock()
        token = generate_token()

        template = DeviceTemplate(id=1, label='template1')

        with patch('DeviceManager.TemplateHandler.assert_template_exists'
                   ) as mock_template_exist_wrapper:
            mock_template_exist_wrapper.return_value = template

            result = TemplateHandler.remove_template(1, token)
            self.assertIsNotNone(result)
            self.assertTrue(result)
            self.assertTrue(result['removed'])
            self.assertEqual(result['result'], 'ok')
    def test_update_template(self, db_mock):
        db_mock.session = AlchemyMagicMock()
        token = generate_token()

        template = DeviceTemplate(id=1, label='SensorModel')

        data = """{
            "label": "SensorModelUpdated",
            "attrs": [
                {
                    "label": "temperature",
                    "type": "dynamic",
                    "value_type": "float"
                },
                {
                    "label": "model-id",
                    "type": "static",
                    "value_type": "string",
                    "static_value": "model-001"
                }
            ]
        }"""

        params_query = {'content_type': 'application/json', 'data': data}

        with patch('DeviceManager.TemplateHandler.assert_template_exists'
                   ) as mock_template_exist_wrapper:
            mock_template_exist_wrapper.return_value = template

            with patch.object(KafkaInstanceHandler,
                              "getInstance",
                              return_value=MagicMock()):
                result = TemplateHandler.update_template(
                    params_query, 1, token)
                self.assertIsNotNone(result)
                self.assertTrue(result)
                self.assertTrue(result['updated'])
                self.assertEqual(result['result'], 'ok')