Skip to content

Commit

Permalink
Merge pull request #175 from RWTH-EBC/167-Change-does_entity_exists-c…
Browse files Browse the repository at this point in the history
…reate-error-message-if-entity-does-not-exist

fix: misleading log for does_entity_exist
  • Loading branch information
tstorek authored Dec 7, 2022
2 parents f992474 + 301fd4c commit e8245ef
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### v.0.2.4
#### v0.2.4
- fixed ContextAttribute: wrong type conversion for value ([#173](https://github.com/RWTH-EBC/FiLiP/issues/173))
- fixed Change does_entity_exists create error message if entity does not exist ([#167](https://github.com/RWTH-EBC/FiLiP/issues/167))

#### v0.2.3
- added `override_metadata` argument according to new metadata update semantics in orion (https://fiware-orion.readthedocs.io/en/master/user/metadata.html#updating-metadata) ([#157](https://github.com/RWTH-EBC/FiLiP/issues/157))
Expand Down
32 changes: 19 additions & 13 deletions filip/clients/ngsi_v2/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,9 +1623,9 @@ def post_command(self,
entity_type=entity_type,
attrs=[command])

def does_entity_exists(self,
entity_id: str,
entity_type: str) -> bool:
def does_entity_exist(self,
entity_id: str,
entity_type: str) -> bool:
"""
Test if an entity with given id and type is present in the CB
Expand All @@ -1637,17 +1637,23 @@ def does_entity_exists(self,
bool; True if entity exists
Raises:
RequestException, if any error occurres (e.g: No Connection),
RequestException, if any error occurs (e.g: No Connection),
except that the entity is not found
"""
url = urljoin(self.base_url, f'v2/entities/{entity_id}')
headers = self.headers.copy()
params = {'type': entity_type}

try:
self.get_entity(entity_id=entity_id, entity_type=entity_type)
res = self.get(url=url, params=params, headers=headers)
if res.ok:
return True
res.raise_for_status()
except requests.RequestException as err:
if err.response is None or not err.response.status_code == 404:
raise
return False

return True


def patch_entity(self,
entity: ContextEntity,
Expand Down Expand Up @@ -1678,8 +1684,8 @@ def patch_entity(self,
if old_entity is None:
# If no old entity_was provided we use the current state to compare
# the entity to
if self.does_entity_exists(entity_id=new_entity.id,
entity_type=new_entity.type):
if self.does_entity_exist(entity_id=new_entity.id,
entity_type=new_entity.type):
old_entity = self.get_entity(entity_id=new_entity.id,
entity_type=new_entity.type)
else:
Expand All @@ -1691,8 +1697,8 @@ def patch_entity(self,
# An old_entity was provided
# check if the old_entity (still) exists else recall methode
# and discard old_entity
if not self.does_entity_exists(entity_id=old_entity.id,
entity_type=old_entity.type):
if not self.does_entity_exist(entity_id=old_entity.id,
entity_type=old_entity.type):
self.patch_entity(new_entity,
override_attr_metadata=override_attr_metadata)
return
Expand All @@ -1705,8 +1711,8 @@ def patch_entity(self,
self.delete_entity(entity_id=old_entity.id,
entity_type=old_entity.type)

if not self.does_entity_exists(entity_id=new_entity.id,
entity_type=new_entity.type):
if not self.does_entity_exist(entity_id=new_entity.id,
entity_type=new_entity.type):
self.post_entity(entity=new_entity, update=False)
return

Expand Down
8 changes: 4 additions & 4 deletions filip/semantics/semantics_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ def does_instance_exists(self, identifier: InstanceIdentifier) -> bool:
return False
else:
client = self.get_client(identifier.header)
return client.does_entity_exists(entity_id=identifier.id,
entity_type=identifier.type)
return client.does_entity_exist(entity_id=identifier.id,
entity_type=identifier.type)

def was_instance_deleted(self, identifier: InstanceIdentifier) -> bool:
"""
Expand Down Expand Up @@ -1114,8 +1114,8 @@ def _get_added_and_removed_values(

# instance is new. Save it as is
client = self.get_client(instance.header)
if not client.does_entity_exists(entity_id=instance.id,
entity_type=instance.get_type()):
if not client.does_entity_exist(entity_id=instance.id,
entity_type=instance.get_type()):
return

client = self.get_client(instance.header)
Expand Down
17 changes: 17 additions & 0 deletions tests/clients/test_ngsi_v2_cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
import random
import json
import uuid

import paho.mqtt.client as mqtt
from datetime import datetime, timedelta
Expand Down Expand Up @@ -834,6 +835,22 @@ def test_send_receive_string(self):

self.client.delete_entity(entity_id="string_test", entity_type="test_type1")

def test_does_entity_exist(self):
_id = uuid.uuid4()

entity = ContextEntity(id=str(_id), type="test_type1")
self.assertFalse(
self.client.does_entity_exist(
entity_id=entity.id,
entity_type=entity.type))
self.client.post_entity(entity=entity)
self.assertTrue(
self.client.does_entity_exist(
entity_id=entity.id,
entity_type=entity.type)
)


def tearDown(self) -> None:
"""
Cleanup test server
Expand Down

0 comments on commit e8245ef

Please sign in to comment.