summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi/opnfv_testapi/resources
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-02-08 10:15:11 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-02-09 16:31:00 +0800
commit4c0d26b4e72b32140d370877700e8a0371d2d30f (patch)
tree87b6968c238d2299e8aee63baa283f2b19826a2e /utils/test/testapi/opnfv_testapi/resources
parent9fba1bdb9bff216c3d9a59e0b7ec61e7a2733922 (diff)
update scenario and related unittest
JIRA: RELENG-163 Change-Id: I137898dc84de5f8cb6cab5de9b4ac5de5e4bb978 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/resources')
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/handlers.py8
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py163
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/scenario_models.py37
3 files changed, 196 insertions, 12 deletions
diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py
index 5f6c3df57..a2628e249 100644
--- a/utils/test/testapi/opnfv_testapi/resources/handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py
@@ -172,8 +172,7 @@ class GenericApiHandler(RequestHandler):
.format(new_query, self.table))
# we merge the whole document """
- edit_request = data.format()
- edit_request.update(self._update_requests(data))
+ edit_request = self._update_requests(data)
""" Updating the DB """
yield self._eval_db(self.table, 'update', query, edit_request,
@@ -188,7 +187,10 @@ class GenericApiHandler(RequestHandler):
data.__getattribute__(k))
if not request:
raise HTTPError(HTTP_FORBIDDEN, "Nothing to update")
- return request
+
+ edit_request = data.format()
+ edit_request.update(request)
+ return edit_request
@staticmethod
def _update_request(edit_request, key, new_value, old_value):
diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py
index a9b89eb89..a8c1a94fe 100644
--- a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py
@@ -1,6 +1,7 @@
from opnfv_testapi.common.constants import HTTP_FORBIDDEN
from opnfv_testapi.resources.handlers import GenericApiHandler
from opnfv_testapi.resources.scenario_models import Scenario
+import opnfv_testapi.resources.scenario_models as models
from opnfv_testapi.tornado_swagger import swagger
@@ -104,11 +105,169 @@ class ScenarioGURHandler(GenericScenarioHandler):
"""
@description: update a single scenario by name
@param body: fields to be updated
- @type body: L{ScenarioCreateRequest}
+ @type body: L{ScenarioUpdateRequest}
@in body: body
@rtype: L{Scenario}
@return 200: update success
@raise 404: scenario not exist
@raise 403: nothing to update
"""
- pass
+ query = {'name': name}
+ db_keys = ['name']
+ self._update(query, db_keys)
+
+ def _update_query(self, keys, data):
+ query = dict()
+ equal = True
+ if self._is_rename():
+ new = self._term.get('name')
+ if data.name != new:
+ equal = False
+ query['name'] = new
+
+ return equal, query
+
+ def _update_requests(self, data):
+ updates = {
+ ('name', 'update'): self._update_requests_rename,
+ ('installer', 'add'): self._update_requests_add_installer,
+ ('installer', 'delete'): self._update_requests_delete_installer,
+ ('version', 'add'): self._update_requests_add_version,
+ ('version', 'delete'): self._update_requests_delete_version,
+ ('owner', 'update'): self._update_requests_change_owner,
+ ('project', 'add'): self._update_requests_add_project,
+ ('project', 'delete'): self._update_requests_delete_project,
+ ('customs', 'add'): self._update_requests_add_customs,
+ ('customs', 'delete'): self._update_requests_delete_customs,
+ ('score', 'add'): self._update_requests_add_score,
+ ('trust_indicator', 'add'): self._update_requests_add_ti,
+ }
+
+ updates[(self._field, self._op)](data)
+
+ return data.format()
+
+ def _iter_installers(xstep):
+ def magic(self, data):
+ [xstep(self, installer)
+ for installer in self._filter_installers(data.installers)]
+ return magic
+
+ def _iter_versions(xstep):
+ def magic(self, installer):
+ [xstep(self, version)
+ for version in (self._filter_versions(installer.versions))]
+ return magic
+
+ def _iter_projects(xstep):
+ def magic(self, version):
+ [xstep(self, project)
+ for project in (self._filter_projects(version.projects))]
+ return magic
+
+ def _update_requests_rename(self, data):
+ data.name = self._term.get('name')
+
+ def _update_requests_add_installer(self, data):
+ data.installers.append(models.ScenarioInstaller.from_dict(self._term))
+
+ def _update_requests_delete_installer(self, data):
+ data.installers = self._remove_installers(data.installers)
+
+ @_iter_installers
+ def _update_requests_add_version(self, installer):
+ installer.versions.append(models.ScenarioVersion.from_dict(self._term))
+
+ @_iter_installers
+ def _update_requests_delete_version(self, installer):
+ installer.versions = self._remove_versions(installer.versions)
+
+ @_iter_installers
+ @_iter_versions
+ def _update_requests_change_owner(self, version):
+ version.owner = self._term.get('owner')
+
+ @_iter_installers
+ @_iter_versions
+ def _update_requests_add_project(self, version):
+ version.projects.append(models.ScenarioProject.from_dict(self._term))
+
+ @_iter_installers
+ @_iter_versions
+ def _update_requests_delete_project(self, version):
+ version.projects = self._remove_projects(version.projects)
+
+ @_iter_installers
+ @_iter_versions
+ @_iter_projects
+ def _update_requests_add_customs(self, project):
+ project.customs = list(set(project.customs + self._term))
+
+ @_iter_installers
+ @_iter_versions
+ @_iter_projects
+ def _update_requests_delete_customs(self, project):
+ project.customs = filter(
+ lambda f: f not in self._term,
+ project.customs)
+
+ @_iter_installers
+ @_iter_versions
+ @_iter_projects
+ def _update_requests_add_score(self, project):
+ project.scores.append(
+ models.ScenarioScore.from_dict(self._term))
+
+ @_iter_installers
+ @_iter_versions
+ @_iter_projects
+ def _update_requests_add_ti(self, project):
+ project.trust_indicators.append(
+ models.ScenarioTI.from_dict(self._term))
+
+ def _is_rename(self):
+ return self._field == 'name' and self._op == 'update'
+
+ def _remove_installers(self, installers):
+ return self._remove('installer', installers)
+
+ def _filter_installers(self, installers):
+ return self._filter('installer', installers)
+
+ def _remove_versions(self, versions):
+ return self._remove('version', versions)
+
+ def _filter_versions(self, versions):
+ return self._filter('version', versions)
+
+ def _remove_projects(self, projects):
+ return self._remove('project', projects)
+
+ def _filter_projects(self, projects):
+ return self._filter('project', projects)
+
+ def _remove(self, field, fields):
+ return filter(
+ lambda f: getattr(f, field) != self._locate.get(field),
+ fields)
+
+ def _filter(self, field, fields):
+ return filter(
+ lambda f: getattr(f, field) == self._locate.get(field),
+ fields)
+
+ @property
+ def _field(self):
+ return self.json_args.get('field')
+
+ @property
+ def _op(self):
+ return self.json_args.get('op')
+
+ @property
+ def _locate(self):
+ return self.json_args.get('locate')
+
+ @property
+ def _term(self):
+ return self.json_args.get('term')
diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_models.py b/utils/test/testapi/opnfv_testapi/resources/scenario_models.py
index f89a12428..73bcbe99e 100644
--- a/utils/test/testapi/opnfv_testapi/resources/scenario_models.py
+++ b/utils/test/testapi/opnfv_testapi/resources/scenario_models.py
@@ -2,6 +2,14 @@ import models
from opnfv_testapi.tornado_swagger import swagger
+def list_default(value):
+ return value if value else list()
+
+
+def dict_default(value):
+ return value if value else dict()
+
+
@swagger.model()
class ScenarioTI(models.ModelBase):
def __init__(self, date=None, status='silver'):
@@ -32,9 +40,9 @@ class ScenarioProject(models.ModelBase):
scores=None,
trust_indicators=None):
self.project = project
- self.customs = customs
- self.scores = scores
- self.trust_indicators = trust_indicators
+ self.customs = list_default(customs)
+ self.scores = list_default(scores)
+ self.trust_indicators = list_default(trust_indicators)
@staticmethod
def attr_parser():
@@ -50,7 +58,7 @@ class ScenarioVersion(models.ModelBase):
"""
def __init__(self, version=None, projects=None):
self.version = version
- self.projects = projects
+ self.projects = list_default(projects)
@staticmethod
def attr_parser():
@@ -65,7 +73,7 @@ class ScenarioInstaller(models.ModelBase):
"""
def __init__(self, installer=None, versions=None):
self.installer = installer
- self.versions = versions if versions else list()
+ self.versions = list_default(versions)
@staticmethod
def attr_parser():
@@ -80,7 +88,7 @@ class ScenarioCreateRequest(models.ModelBase):
"""
def __init__(self, name='', installers=None):
self.name = name
- self.installers = installers if installers else list()
+ self.installers = list_default(installers)
@staticmethod
def attr_parser():
@@ -88,6 +96,21 @@ class ScenarioCreateRequest(models.ModelBase):
@swagger.model()
+class ScenarioUpdateRequest(models.ModelBase):
+ """
+ @property field: update field
+ @property op: add/delete/update
+ @property locate: information used to locate the field
+ @property term: new value
+ """
+ def __init__(self, field=None, op=None, locate=None, term=None):
+ self.field = field
+ self.op = op
+ self.locate = dict_default(locate)
+ self.term = dict_default(term)
+
+
+@swagger.model()
class Scenario(models.ModelBase):
"""
@property installers:
@@ -97,7 +120,7 @@ class Scenario(models.ModelBase):
self.name = name
self._id = _id
self.creation_date = create_date
- self.installers = installers if installers else list()
+ self.installers = list_default(installers)
@staticmethod
def attr_parser():