Exemplo n.º 1
0
    def test_patch_device(self):
        """
            Test the methode: patch_device of the iota client
        """

        device = Device(**self.device)
        device.endpoint = "http://test.com"
        device.transport = "MQTT"

        device.add_attribute(DeviceAttribute(
            name="Att1", object_id="o1", type=DataType.STRUCTUREDVALUE))
        device.add_attribute(StaticDeviceAttribute(
            name="Stat1", value="test", type=DataType.STRUCTUREDVALUE))
        device.add_attribute(StaticDeviceAttribute(
            name="Stat2", value="test", type=DataType.STRUCTUREDVALUE))
        device.add_command(DeviceCommand(name="Com1"))

        # use patch_device to post
        self.client.patch_device(device=device)

        cb_client = ContextBrokerClient(url=settings.CB_URL,
                                        fiware_header=self.fiware_header)

        # test if attributes exists correctly
        live_entity = cb_client.get_entity(entity_id=device.entity_name)
        live_entity.get_attribute("Att1")
        live_entity.get_attribute("Com1")
        live_entity.get_attribute("Com1_info")
        live_entity.get_attribute("Com1_status")
        self.assertEqual(live_entity.get_attribute("Stat1").value, "test")

        # change device attributes and update
        device.get_attribute("Stat1").value = "new_test"
        device.delete_attribute(device.get_attribute("Stat2"))
        device.delete_attribute(device.get_attribute("Att1"))
        device.delete_attribute(device.get_attribute("Com1"))
        device.add_attribute(DeviceAttribute(
            name="Att2", object_id="o1", type=DataType.STRUCTUREDVALUE))
        device.add_attribute(StaticDeviceAttribute(
            name="Stat3", value="test3", type=DataType.STRUCTUREDVALUE))
        device.add_command(DeviceCommand(name="Com2"))

        self.client.patch_device(device=device, cb_url=settings.CB_URL)

        # test if update does what it should, for the device. It does not
        # change the entity completely:
        live_entity = cb_client.get_entity(entity_id=device.entity_name)
        with self.assertRaises(KeyError):
            live_entity.get_attribute("Att1")
        with self.assertRaises(KeyError):
            live_entity.get_attribute("Com1_info")
        with self.assertRaises(KeyError):
            live_entity.get_attribute("Stat2")
        self.assertEqual(live_entity.get_attribute("Stat1").value, "new_test")
        live_entity.get_attribute("Stat3")
        live_entity.get_attribute("Com2_info")
        live_entity.get_attribute("Att2")

        # test update where device information were changed
        device_settings = {"endpoint": "http://localhost:7071",
                           "device_id": "new_id",
                           "entity_name": "new_name",
                           "entity_type": "new_type",
                           "timestamp": False,
                           "apikey": "zuiop",
                           "protocol": "HTTP",
                           "transport": "HTTP"}

        for key, value in device_settings.items():
            device.__setattr__(key, value)
            self.client.patch_device(device=device)
            live_device = self.client.get_device(device_id=device.device_id)
            self.assertEqual(live_device.__getattribute__(key), value)
            cb_client.close()
Exemplo n.º 2
0
    def test_update_device(self):
        """
        Test the methode: update_device of the iota client
        """

        device = Device(**self.device)
        device.endpoint = "http://test.com"
        device.transport = "MQTT"

        device.add_attribute(DeviceAttribute(
            name="Att1", object_id="o1", type=DataType.STRUCTUREDVALUE))
        device.add_attribute(StaticDeviceAttribute(
            name="Stat1", value="test", type=DataType.STRUCTUREDVALUE))
        device.add_attribute(StaticDeviceAttribute(
            name="Stat2", value="test", type=DataType.STRUCTUREDVALUE))
        device.add_command(DeviceCommand(name="Com1"))

        # use update_device to post
        self.client.update_device(device=device, add=True)

        cb_client = ContextBrokerClient(url=settings.CB_URL,
                                        fiware_header=self.fiware_header)

        # test if attributes exists correctly
        live_entity = cb_client.get_entity(entity_id=device.entity_name)
        live_entity.get_attribute("Att1")
        live_entity.get_attribute("Com1")
        live_entity.get_attribute("Com1_info")
        live_entity.get_attribute("Com1_status")
        self.assertEqual(live_entity.get_attribute("Stat1").value, "test")

        # change device attributes and update
        device.get_attribute("Stat1").value = "new_test"
        device.delete_attribute(device.get_attribute("Stat2"))
        device.delete_attribute(device.get_attribute("Att1"))
        device.delete_attribute(device.get_attribute("Com1"))
        device.add_attribute(DeviceAttribute(
            name="Att2", object_id="o1", type=DataType.STRUCTUREDVALUE))
        device.add_attribute(StaticDeviceAttribute(
            name="Stat3", value="test3", type=DataType.STRUCTUREDVALUE))
        device.add_command(DeviceCommand(name="Com2"))

        # device.endpoint = "http://localhost:8080"
        self.client.update_device(device=device)

        # test if update does what it should, for the device. It does not
        # change the entity completely:

        live_device = self.client.get_device(device_id=device.device_id)

        with self.assertRaises(KeyError):
            live_device.get_attribute("Att1")
        with self.assertRaises(KeyError):
            live_device.get_attribute("Com1_info")
        with self.assertRaises(KeyError):
            live_device.get_attribute("Stat2")
        self.assertEqual(live_device.get_attribute("Stat1").value, "new_test")
        live_device.get_attribute("Stat3")
        live_device.get_command("Com2")
        live_device.get_attribute("Att2")

        cb_client.close()