Skip to content

Commit

Permalink
Merge aa64b37 into 2968846
Browse files Browse the repository at this point in the history
  • Loading branch information
chicco785 committed Sep 23, 2020
2 parents 2968846 + aa64b37 commit e3a560a
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 58 deletions.
20 changes: 19 additions & 1 deletion specification/quantumleap.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
swagger: '2.0' # For 3.0 see (https://github.com/zalando/connexion/issues/420)
info:
title: "QuantumLeap API"
version: "0.7.5" # we'll keep it aligned with QL version
version: "0.7.6" # we'll keep it aligned with QL version
host: "localhost:8668" # it'll run in the same container, hence localhost.
produces:
- text/plain
Expand Down Expand Up @@ -1759,3 +1759,21 @@ paths:
}
]
}

/v2/storage/{entityType}:
delete:
operationId: reporter.delete.drop_entity_storage
summary: "Drop supporting storage for the given entity type."
description: "Given an entity type, delete all the records of that
type, drop the table in which they were stored, and clean up the
metadata table accordingly."
tags:
- input
parameters:
# In Path...
- $ref: '#/parameters/entityTypeInPath'
# In Header...
- $ref: '#/parameters/fiware-Service'
responses:
204:
description: "Storage successfully deleted."
12 changes: 9 additions & 3 deletions src/reporter/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def delete_entity(entity_id, type_=None, from_date=None, to_date=None):

try:
with crate.CrateTranslatorInstance() as trans:
deleted = trans.delete_entity(entity_id=entity_id,
entity_type=type_,
deleted = trans.delete_entity(eid=entity_id,
etype=type_,
from_date=from_date,
to_date=to_date,
fiware_service=fiware_s,
Expand All @@ -37,7 +37,7 @@ def delete_entities(entity_type, from_date=None, to_date=None):
fiware_sp = request.headers.get('fiware-servicepath', None)

with crate.CrateTranslatorInstance() as trans:
deleted = trans.delete_entities(entity_type=entity_type,
deleted = trans.delete_entities(etype=entity_type,
from_date=from_date,
to_date=to_date,
fiware_service=fiware_s,
Expand All @@ -51,3 +51,9 @@ def delete_entities(entity_type, from_date=None, to_date=None):

if deleted > 0:
return '{} records successfully deleted.'.format(deleted), 204


def drop_entity_storage(entity_type: str):
fiware_s = request.headers.get('fiware-service', None)
with crate.CrateTranslatorInstance() as trans:
trans.drop_table(etype=entity_type, fiware_service=fiware_s)
154 changes: 149 additions & 5 deletions src/reporter/tests/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
notify_url = "{}/notify".format(QL_URL)


def insert_test_data(service, entity_id=None):
def insert_test_data(service, service_path=None, entity_id=None):
# 3 entity types, 2 entities for each, 10 updates for each entity.
for t in ("AirQualityObserved", "Room", "TrafficFlowObserved"):
for e in range(2):
Expand All @@ -22,12 +22,15 @@ def insert_test_data(service, entity_id=None):
'Content-Type': 'application/json',
'Fiware-Service': service
}
if service_path:
h['Fiware-ServicePath'] = service_path
r = requests.post(notify_url,
data=data,
headers=h)
assert r.status_code == 200, r.text
time.sleep(1)


@pytest.mark.parametrize("service", [
"t1", "t2"
])
Expand All @@ -44,7 +47,7 @@ def test_delete_entity(service):
h = {
'Fiware-Service': service
}
url = '{}/entities/{}'.format(QL_URL, entity_type+'0')
url = '{}/entities/{}'.format(QL_URL, entity_type + '0')

# Values are there
r = requests.get(url, params=params, headers=h)
Expand All @@ -61,7 +64,7 @@ def test_delete_entity(service):
assert r.status_code == 404, r.text

# But not for other entities of same type
url = '{}/entities/{}'.format(QL_URL, entity_type+'1')
url = '{}/entities/{}'.format(QL_URL, entity_type + '1')
r = requests.get(url, params=params, headers=h)
assert r.status_code == 200, r.text
assert r.text != ''
Expand Down Expand Up @@ -124,7 +127,7 @@ def test_not_found(service):
h = {
'Fiware-Service': service
}
url = '{}/entities/{}'.format(QL_URL, entity_type+'0')
url = '{}/entities/{}'.format(QL_URL, entity_type + '0')

r = requests.delete(url, params=params, headers=h)
assert r.status_code == 404, r.text
Expand Down Expand Up @@ -160,6 +163,7 @@ def test_no_type_not_unique(service):
for t in ("AirQualityObserved", "Room", "TrafficFlowObserved"):
delete_test_data(service, [t])


@pytest.mark.parametrize("service", [
"t1", "t2"
])
Expand Down Expand Up @@ -216,4 +220,144 @@ def test_delete_no_type_with_multitenancy(service):
assert r.status_code == 200, r.text
delete_test_data(service, ["Car"])
delete_test_data('USA', ["Car"])
delete_test_data('EU', ["Car"])
delete_test_data('EU', ["Car"])


def test_delete_347():
"""
Test to replicate issue #347.
"""
entity_type = "deletetestDuno"
service = 'bbbbb'
service_path = '/'
params = {
'type': entity_type,
}
h = {
'Fiware-Service': service,
'Fiware-ServicePath': service_path
}

data = {
'subscriptionId': 'ID_FROM_SUB',
'data': [{
'id': 'un3',
'type': 'deletetestDuno',
'batteryVoltage': {
'type': 'Text',
'value': 'ilariso'
}
}]
}

hn = {
'Content-Type': 'application/json',
'Fiware-Service': service,
'Fiware-ServicePath': service_path
}
r = requests.post(notify_url,
data=json.dumps(data),
headers=hn)
assert r.status_code == 200, r.text
time.sleep(1)
# check that value is in the database
url = '{}/entities/{}'.format(QL_URL, 'un3')
r = requests.get(url, params=params, headers=h)
assert r.status_code == 200, r.text
assert r.text != ''

# Delete call
time.sleep(1)
url = '{}/types/{}'.format(QL_URL, entity_type)
r = requests.delete(url, params=params, headers=h)
assert r.status_code == 204, r.text

# Values are gone
time.sleep(1)
url = '{}/entities/{}'.format(QL_URL, '{}{}'.format(entity_type, 'un3'))
r = requests.get(url, params=params, headers=h)
assert r.status_code == 404, r.text

def test_delete_different_servicepaths():
"""
Selective delete by service Path.
"""
entity_type = "deletetestDuno"
service = 'bbbbb'
service_path = '/a'
params = {
'type': entity_type,
}
h = {
'Fiware-Service': service,
'Fiware-ServicePath': service_path
}

data = {
'subscriptionId': 'ID_FROM_SUB',
'data': [{
'id': 'un3',
'type': 'deletetestDuno',
'batteryVoltage': {
'type': 'Text',
'value': 'ilariso'
}
}]
}

hn = {
'Content-Type': 'application/json',
'Fiware-Service': service,
'Fiware-ServicePath': service_path
}
r = requests.post(notify_url,
data=json.dumps(data),
headers=hn)
assert r.status_code == 200, r.text

#insert the same entity in a different service path
service_path = '/b'
hn = {
'Content-Type': 'application/json',
'Fiware-Service': service,
'Fiware-ServicePath': service_path
}
r = requests.post(notify_url,
data=json.dumps(data),
headers=hn)
assert r.status_code == 200, r.text
time.sleep(1)
# check that value is in the database
url = '{}/entities/{}'.format(QL_URL, 'un3')
r = requests.get(url, params=params, headers=h)
assert r.status_code == 200, r.text
assert r.text != ''

# Delete /a
time.sleep(2)
url = '{}/types/{}'.format(QL_URL, entity_type)
r = requests.delete(url, params=params, headers=h)
assert r.status_code == 204, r.text

h = {
'Fiware-Service': service,
'Fiware-ServicePath': service_path
}

# 1 entity is still there
time.sleep(2)
url = '{}/entities/{}'.format(QL_URL, 'un3')
r = requests.get(url, params=params, headers=h)
assert r.status_code == 200, r.text

# Delete /b
time.sleep(2)
url = '{}/types/{}'.format(QL_URL, entity_type)
r = requests.delete(url, params=params, headers=h)
assert r.status_code == 204, r.text

# No entity
time.sleep(2)
url = '{}/entities/{}'.format(QL_URL, 'un3')
r = requests.get(url, params=params, headers=h)
assert r.status_code == 404, r.text
6 changes: 3 additions & 3 deletions src/reporter/tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def test_integration_multiple_entities(diffEntityWithDifferentAttrs, orion_clien
assert r.status_code == 200
entities = r.json()
assert len(entities) == 3
delete_entity_type("service", diffEntityWithDifferentAttrs[0]['type'])
delete_entity_type("service", diffEntityWithDifferentAttrs[0]['type'], "/Root")

@pytest.mark.skip(reason="See issue #105")
@pytest.mark.parametrize("service", services)
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_multiple_data_elements(service, notification, diffEntityWithDifferentAt
r = requests.get(entities_url, params=None, headers=query_header(service))
entities = r.json()
assert len(entities) == 3
delete_entity_type(None, diffEntityWithDifferentAttrs[0]['type'])
delete_entity_type(service, diffEntityWithDifferentAttrs[0]['type'])


@pytest.mark.parametrize("service", services)
Expand Down Expand Up @@ -376,7 +376,7 @@ def test_multiple_data_elements_different_servicepath(service, notification, dif
r = requests.get(entities_url, params=None, headers=query_headers)
entities = r.json()
assert len(entities) == 3
delete_entity_type(service, diffEntityWithDifferentAttrs[0]['type'])
delete_entity_type(service, diffEntityWithDifferentAttrs[0]['type'], '/Test')


@pytest.mark.parametrize("service", services)
Expand Down
9 changes: 6 additions & 3 deletions src/reporter/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ def insert_test_data(service, entity_types, n_entities=1, index_size=30,
time.sleep(1)


def delete_entity_type(service, entity_type):
def delete_entity_type(service, entity_type, service_path=None):
h = {}
if service:
h = {'Fiware-Service': service}
url = '{}/types/{}'.format(QL_URL, entity_type)
h['Fiware-Service'] = service
if service_path:
h['Fiware-ServicePath'] = service_path

url = '{}/storage/{}'.format(QL_URL, entity_type)

r = requests.delete(url, headers=h)
# assert r.status_code == 204
Expand Down
Loading

0 comments on commit e3a560a

Please sign in to comment.