From 1a270a8c0a91d58bc905d92db73e118abd3cc353 Mon Sep 17 00:00:00 2001 From: SystemsPurge Date: Tue, 8 Oct 2024 13:55:13 +0200 Subject: [PATCH 1/6] Adjusted checks on clear cb cleanup function to allow for client object only --- filip/utils/cleanup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/filip/utils/cleanup.py b/filip/utils/cleanup.py index ac13fddb..57441a99 100644 --- a/filip/utils/cleanup.py +++ b/filip/utils/cleanup.py @@ -14,8 +14,8 @@ QuantumLeapClient -def clear_context_broker(url: str, - fiware_header: FiwareHeader, +def clear_context_broker(url: str=None, + fiware_header: FiwareHeader=None, clear_registrations: bool = False, cb_client: ContextBrokerClient = None ): @@ -40,7 +40,7 @@ def clear_context_broker(url: str, Returns: None """ - assert url or cb_client, "Either url or client object must be given" + assert (url and fiware_header) or cb_client, "Either url and headers or client object must be given" # create client if cb_client is None: client = ContextBrokerClient(url=url, fiware_header=fiware_header) From 9892d6f43b7406c1afc026da50f9cbbef83cecf7 Mon Sep 17 00:00:00 2001 From: Saira Bano Date: Wed, 23 Oct 2024 14:34:53 +0200 Subject: [PATCH 2/6] fix: made fiware_header optional for uniformity --- filip/utils/cleanup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filip/utils/cleanup.py b/filip/utils/cleanup.py index 57441a99..6cda8402 100644 --- a/filip/utils/cleanup.py +++ b/filip/utils/cleanup.py @@ -40,7 +40,7 @@ def clear_context_broker(url: str=None, Returns: None """ - assert (url and fiware_header) or cb_client, "Either url and headers or client object must be given" + assert url or cb_client, "Either url or client object must be given" # create client if cb_client is None: client = ContextBrokerClient(url=url, fiware_header=fiware_header) From 5a7df937d8834e4e109b9ac3864c80fb1e9e616f Mon Sep 17 00:00:00 2001 From: Saira Bano Date: Mon, 28 Oct 2024 09:28:16 +0100 Subject: [PATCH 3/6] test: included tests for cleanup utility functions --- tests/utils/test_clear.py | 176 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 tests/utils/test_clear.py diff --git a/tests/utils/test_clear.py b/tests/utils/test_clear.py new file mode 100644 index 00000000..f6fc1c72 --- /dev/null +++ b/tests/utils/test_clear.py @@ -0,0 +1,176 @@ +""" +Tests clear functions in filip.utils.cleanup +""" +import random +import unittest +from datetime import datetime +from uuid import uuid4 + +from filip.clients.ngsi_v2 import ContextBrokerClient, IoTAClient, QuantumLeapClient +from filip.models.base import FiwareHeader +from filip.models.ngsi_v2.context import ContextEntity +from filip.models.ngsi_v2.iot import Device, ServiceGroup +from filip.models.ngsi_v2.subscriptions import Subscription +from filip.utils.cleanup import clear_context_broker, clear_iot_agent +from tests.config import settings + + +class TestClearFunctions(unittest.TestCase): + + def setUp(self) -> None: + """ + Setup test data and clients + + Returns: + None + """ + self.fiware_header = FiwareHeader( + service=settings.FIWARE_SERVICE, + service_path=settings.FIWARE_SERVICEPATH) + self.cb_url = settings.CB_URL + self.cb_client = ContextBrokerClient(url=self.cb_url, + fiware_header=self.fiware_header) + self.iota_url = settings.IOTA_URL + self.iota_client = IoTAClient(url=self.iota_url, + fiware_header=self.fiware_header) + + self.ql_url = settings.QL_URL + self.ql_client = QuantumLeapClient(url=self.ql_url, fiware_header=self.fiware_header) + + def test_clear_context_broker(self): + """ + Test for clearing context broker using context broker client + """ + entity = ContextEntity(id=str(random.randint(1, 50)), + type=f'filip:object:Type') + self.cb_client.post_entity(entity=entity) + subscription = Subscription.model_validate({ + "description": "One subscription to rule them all", + "subject": { + "entities": [ + { + "idPattern": ".*", + "type": "Room" + } + ], + "condition": { + "attrs": [ + "temperature" + ], + "expression": { + "q": "temperature>40" + } + } + }, + "notification": { + "http": { + "url": "http://localhost:1234" + }, + "attrs": [ + "temperature", + "humidity" + ] + }, + "expires": datetime.now(), + "throttling": 0 + }) + self.cb_client.post_subscription(subscription=subscription) + clear_context_broker(cb_client=self.cb_client) + + self.assertEqual(0, len(self.cb_client.get_entity_list()) or len(self.cb_client.get_subscription_list())) + + def test_clear_context_broker_with_url(self): + """ + Test for clearing context broker using context broker url and fiware header as parameters + """ + entity = ContextEntity(id=str(random.randint(1, 50)), + type=f'filip:object:Type') + self.cb_client.post_entity(entity=entity) + subscription = Subscription.model_validate({ + "description": "One subscription to rule them all", + "subject": { + "entities": [ + { + "idPattern": ".*", + "type": "Room" + } + ], + "condition": { + "attrs": [ + "temperature" + ], + "expression": { + "q": "temperature>40" + } + } + }, + "notification": { + "http": { + "url": "http://localhost:1234" + }, + "attrs": [ + "temperature", + "humidity" + ] + }, + "expires": datetime.now(), + "throttling": 0 + }) + self.cb_client.post_subscription(subscription=subscription) + clear_context_broker(url=self.cb_url, fiware_header=self.fiware_header) + + self.assertEqual(0, len(self.cb_client.get_entity_list()) or len(self.cb_client.get_entity_list())) + + def test_clear_iot_agent(self): + """ + Test for clearing iota using iota client + """ + service_group = ServiceGroup(entity_type='Thing', + resource='/iot/json', + apikey=str(uuid4())) + device = { + "device_id": "test_device", + "service": self.fiware_header.service, + "service_path": self.fiware_header.service_path, + "entity_name": "test_entity", + } + + self.iota_client.post_groups(service_group, update=False) + self.iota_client.post_device(device=Device(**device), update=False) + + clear_iot_agent(iota_client=self.iota_client) + + self.assertEqual(0, len(self.iota_client.get_device_list()) or len(self.iota_client.get_group_list())) + + def test_clear_iot_agent_url(self): + """ + Test for clearing iota using iota url and fiware header as parameters + """ + service_group = ServiceGroup(entity_type='Thing', + resource='/iot/json', + apikey=str(uuid4())) + device = { + "device_id": "test_device", + "service": self.fiware_header.service, + "service_path": self.fiware_header.service_path, + "entity_name": "test_entity", + } + + self.iota_client.post_groups(service_group, update=False) + self.iota_client.post_device(device=Device(**device), update=False) + + clear_iot_agent(url=self.iota_url, fiware_header=self.fiware_header) + + self.assertEqual(0, len(self.iota_client.get_device_list()) or len(self.iota_client.get_group_list())) + + def test_clear_quantumleap(self): + # TODO + pass + + def tearDown(self) -> None: + """ + Cleanup test servers + """ + self.cb_client.close() + self.iota_client.close() + self.ql_client.close() From d6bd3543ef134175b593f068551f015aaeeda363 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Tue, 29 Oct 2024 10:00:35 +0100 Subject: [PATCH 4/6] fix: add missing entity type --- tests/utils/test_clear.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/utils/test_clear.py b/tests/utils/test_clear.py index f6fc1c72..1fb84f67 100644 --- a/tests/utils/test_clear.py +++ b/tests/utils/test_clear.py @@ -133,6 +133,7 @@ def test_clear_iot_agent(self): "service": self.fiware_header.service, "service_path": self.fiware_header.service_path, "entity_name": "test_entity", + "entity_type": "test_type", } self.iota_client.post_groups(service_group, update=False) @@ -154,6 +155,7 @@ def test_clear_iot_agent_url(self): "service": self.fiware_header.service, "service_path": self.fiware_header.service_path, "entity_name": "test_entity", + "entity_type": "test_type", } self.iota_client.post_groups(service_group, update=False) From d33e0d60c7553bdb375f68b6cd63fdb9a87e470c Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Tue, 29 Oct 2024 10:39:35 +0100 Subject: [PATCH 5/6] feat: add ql clear test --- tests/utils/test_clear.py | 107 ++++++++++++++++++++++---------------- 1 file changed, 61 insertions(+), 46 deletions(-) diff --git a/tests/utils/test_clear.py b/tests/utils/test_clear.py index 1fb84f67..e43e6fa8 100644 --- a/tests/utils/test_clear.py +++ b/tests/utils/test_clear.py @@ -2,16 +2,20 @@ Tests clear functions in filip.utils.cleanup """ import random +import time import unittest from datetime import datetime +from typing import List from uuid import uuid4 +from requests import RequestException + from filip.clients.ngsi_v2 import ContextBrokerClient, IoTAClient, QuantumLeapClient from filip.models.base import FiwareHeader from filip.models.ngsi_v2.context import ContextEntity from filip.models.ngsi_v2.iot import Device, ServiceGroup -from filip.models.ngsi_v2.subscriptions import Subscription -from filip.utils.cleanup import clear_context_broker, clear_iot_agent +from filip.models.ngsi_v2.subscriptions import Subscription, Message +from filip.utils.cleanup import clear_context_broker, clear_iot_agent, clear_quantumleap from tests.config import settings @@ -24,8 +28,9 @@ def setUp(self) -> None: Returns: None """ + # use specific service for testing clear functions self.fiware_header = FiwareHeader( - service=settings.FIWARE_SERVICE, + service="filip_clear_test", service_path=settings.FIWARE_SERVICEPATH) self.cb_url = settings.CB_URL self.cb_client = ContextBrokerClient(url=self.cb_url, @@ -35,16 +40,9 @@ def setUp(self) -> None: fiware_header=self.fiware_header) self.ql_url = settings.QL_URL - self.ql_client = QuantumLeapClient(url=self.ql_url, fiware_header=self.fiware_header) - - def test_clear_context_broker(self): - """ - Test for clearing context broker using context broker client - """ - entity = ContextEntity(id=str(random.randint(1, 50)), - type=f'filip:object:Type') - self.cb_client.post_entity(entity=entity) - subscription = Subscription.model_validate({ + self.ql_client = QuantumLeapClient(url=self.ql_url, + fiware_header=self.fiware_header) + self.sub_dict = { "description": "One subscription to rule them all", "subject": { "entities": [ @@ -73,7 +71,16 @@ def test_clear_context_broker(self): }, "expires": datetime.now(), "throttling": 0 - }) + } + + def test_clear_context_broker(self): + """ + Test for clearing context broker using context broker client + """ + entity = ContextEntity(id=str(random.randint(1, 50)), + type=f'filip:object:Type') + self.cb_client.post_entity(entity=entity) + subscription = Subscription.model_validate(self.sub_dict) self.cb_client.post_subscription(subscription=subscription) clear_context_broker(cb_client=self.cb_client) @@ -86,36 +93,7 @@ def test_clear_context_broker_with_url(self): entity = ContextEntity(id=str(random.randint(1, 50)), type=f'filip:object:Type') self.cb_client.post_entity(entity=entity) - subscription = Subscription.model_validate({ - "description": "One subscription to rule them all", - "subject": { - "entities": [ - { - "idPattern": ".*", - "type": "Room" - } - ], - "condition": { - "attrs": [ - "temperature" - ], - "expression": { - "q": "temperature>40" - } - } - }, - "notification": { - "http": { - "url": "http://localhost:1234" - }, - "attrs": [ - "temperature", - "humidity" - ] - }, - "expires": datetime.now(), - "throttling": 0 - }) + subscription = Subscription.model_validate(self.sub_dict) self.cb_client.post_subscription(subscription=subscription) clear_context_broker(url=self.cb_url, fiware_header=self.fiware_header) @@ -166,8 +144,45 @@ def test_clear_iot_agent_url(self): self.assertEqual(0, len(self.iota_client.get_device_list()) or len(self.iota_client.get_group_list())) def test_clear_quantumleap(self): - # TODO - pass + from random import random + + clear_quantumleap(ql_client=self.ql_client) + rec_numbs = 3 + def create_data_points(): + def create_entities(_id) -> List[ContextEntity]: + def create_attr(): + return {'temperature': {'value': random(), + 'type': 'Number'}, + 'humidity': {'value': random(), + 'type': 'Number'}, + 'co2': {'value': random(), + 'type': 'Number'}} + + return [ContextEntity(id=f'Room:{_id}', type='Room', **create_attr())] + + fiware_header = self.fiware_header + + with QuantumLeapClient(url=settings.QL_URL, fiware_header=fiware_header) \ + as client: + for i in range(rec_numbs): + notification_message = Message(data=create_entities(i), + subscriptionId="test") + client.post_notification(notification_message) + + create_data_points() + time.sleep(1) + self.assertEqual(len(self.ql_client.get_entities()), rec_numbs) + clear_quantumleap(url=self.ql_url, + fiware_header=self.fiware_header) + with self.assertRaises(RequestException): + self.ql_client.get_entities() + + create_data_points() + time.sleep(1) + self.assertEqual(len(self.ql_client.get_entities()), rec_numbs) + clear_quantumleap(ql_client=self.ql_client) + with self.assertRaises(RequestException): + self.ql_client.get_entities() def tearDown(self) -> None: """ From 2ff69c7c4e793a57db55436066b1406aa291ec30 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Tue, 29 Oct 2024 10:42:21 +0100 Subject: [PATCH 6/6] docs: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f61ccde3..0e71047b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ### v0.6.X - add: Tutorial for connecting with secured endpoints ([#319](https://github.com/RWTH-EBC/FiLiP/pull/319)) +- add: tests for clear functions ([#318](https://github.com/RWTH-EBC/FiLiP/pull/336)) +- fix: clear functions for context broker ([#318](https://github.com/RWTH-EBC/FiLiP/pull/336)) ### v0.5.0 - update: allow duplicated name in device, check uniqueness of object_id ([#279](https://github.com/RWTH-EBC/FiLiP/pull/279))