Skip to content

Commit

Permalink
Merge pull request #344 from RWTH-EBC/337-initial-tenant-when-tenant-…
Browse files Browse the repository at this point in the history
…does-not-exist

337 initial tenant when tenant does not exist
  • Loading branch information
djs0109 authored Oct 30, 2024
2 parents 34355bf + 80fa980 commit 373e183
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
23 changes: 20 additions & 3 deletions filip/clients/ngsi_ld/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Context Broker Module for API Client
"""
import json
import re
import os
import warnings
from math import inf
from enum import Enum
Expand Down Expand Up @@ -62,15 +62,18 @@ def __init__(self,
#base_http_client overwrites empty header with FiwareHeader instead of FiwareLD
init_header = FiwareLDHeader()
if fiware_header:
init_header=fiware_header
init_header = fiware_header

super().__init__(url=url,
session=session,
fiware_header=init_header,
**kwargs)
# set the version specific url-pattern
self._url_version = NgsiURLVersion.ld_url
# init Content-Type header , account for @context field further down
self.headers.update({'Content-Type':'application/json'})
self.headers.update({'Content-Type': 'application/json'})
if init_header.ngsild_tenant is not None:
self.__make_tenant()

def __pagination(self,
*,
Expand Down Expand Up @@ -158,6 +161,20 @@ def get_version(self) -> Dict:
except requests.RequestException as err:
self.logger.error(err)
raise

def __make_tenant(self):
"""
Create tenant if tenant
is given in headers
"""
idhex = f"urn:ngsi-ld:{os.urandom(6).hex()}"
e = ContextLDEntity(id=idhex,type=f"urn:ngsi-ld:{os.urandom(6).hex()}")
try:
self.post_entity(entity=e)
self.delete_entity_by_id(idhex)
except Exception as err:
self.log_error(err=err,msg="Error while creating tenant")
raise

def get_statistics(self) -> Dict:
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/clients/test_ngsi_ld_cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ def test_management_endpoints(self):
self.assertIsNotNone(self.client.get_version())
# TODO: check whether there are other "management" endpoints

@unittest.skip("Only for local testing environment")
def test_not_existing_tenant(self):
"""
Test the expected behavior of the client when the tenant does not exist
This test will not be included in the CI/CD pipeline. For local testing please
comment out the decorator.
"""
# create uuid for the tenant
import uuid
tenant = str(uuid.uuid4()).split('-')[0]
fiware_header = FiwareLDHeader(ngsild_tenant=tenant)
client = ContextBrokerLDClient(fiware_header=fiware_header,
url=settings.LD_CB_URL)
entities = client.get_entity_list()
self.assertEqual(len(entities), 0)

def test_statistics(self):
"""
Test statistics of context broker client
Expand Down

0 comments on commit 373e183

Please sign in to comment.