From 017153393c1055608d33dd3169b02469e29191b7 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Wed, 16 Aug 2017 15:38:23 +0800 Subject: impl update trust_indicator in scenario Change-Id: I1afe38412926778bc84d178dbbfc3fe0cde15f69 Signed-off-by: SerenaFeng --- .../opnfv_testapi/resources/scenario_handlers.py | 107 ++++++++++++++++----- testapi/opnfv_testapi/router/url_mappings.py | 2 + .../tests/unit/resources/test_scenario.py | 11 +++ 3 files changed, 98 insertions(+), 22 deletions(-) (limited to 'testapi') diff --git a/testapi/opnfv_testapi/resources/scenario_handlers.py b/testapi/opnfv_testapi/resources/scenario_handlers.py index a89e7ee..66e8559 100644 --- a/testapi/opnfv_testapi/resources/scenario_handlers.py +++ b/testapi/opnfv_testapi/resources/scenario_handlers.py @@ -13,10 +13,10 @@ class GenericScenarioHandler(handlers.GenericApiHandler): self.table = self.db_scenarios self.table_cls = models.Scenario - def set_query(self, filters): + def set_query(self, locators): query = dict() elem_query = dict() - for k, v in filters.iteritems(): + for k, v in locators.iteritems(): if k == 'scenario': query['name'] = v elif k == 'installer': @@ -134,11 +134,12 @@ class ScenarioUpdater(object): self.version = version self.project = project - def update(self, item, op): + def update(self, item, action): updates = { - ('score', 'add'): self._update_requests_add_score, + ('scores', 'post'): self._update_requests_add_score, + ('trust_indicators', 'post'): self._update_requests_add_ti, } - updates[(item, op)](self.data) + updates[(item, action)](self.data) return self.data.format() @@ -170,6 +171,13 @@ class ScenarioUpdater(object): project.scores.append( models.ScenarioScore.from_dict(self.body)) + @iter_installers + @iter_versions + @iter_projects + def _update_requests_add_ti(self, project): + project.trust_indicators.append( + models.ScenarioTI.from_dict(self.body)) + def _filter_installers(self, installers): return self._filter('installer', installers) @@ -185,7 +193,36 @@ class ScenarioUpdater(object): items) -class ScenarioScoresHandler(GenericScenarioHandler): +class GenericScenarioUpdateHandler(GenericScenarioHandler): + def __init__(self, application, request, **kwargs): + super(GenericScenarioUpdateHandler, self).__init__(application, + request, + **kwargs) + self.installer = None + self.version = None + self.project = None + self.item = None + self.action = None + + def do_post(self, scenario, item, action, locators): + self.item = item + self.action = action + for k in locators.keys(): + v = self.get_query_argument(k) + setattr(self, k, v) + locators[k] = v + db_keys = ['name'] + self._update(query=self.set_query(locators=locators), db_keys=db_keys) + + def _update_requests(self, data): + return ScenarioUpdater(data, + self.json_args, + self.installer, + self.version, + self.project).update(self.item, self.action) + + +class ScenarioScoresHandler(GenericScenarioUpdateHandler): @swagger.operation(nickname="addScoreRecord") def post(self, scenario): """ @@ -214,20 +251,46 @@ class ScenarioScoresHandler(GenericScenarioHandler): @return 200: score is created. @raise 404: scenario/installer/version/project not existed """ - self.installer = self.get_query_argument('installer') - self.version = self.get_query_argument('version') - self.project = self.get_query_argument('project') - - filters = {'scenario': scenario, - 'installer': self.installer, - 'version': self.version, - 'project': self.project} - db_keys = ['name'] - self._update(query=self.set_query(filters=filters), db_keys=db_keys) + self.do_post(scenario, + 'scores', + 'post', + locators={'installer': None, + 'version': None, + 'project': None}) - def _update_requests(self, data): - return ScenarioUpdater(data, - self.json_args, - self.installer, - self.version, - self.project).update('score', 'add') + +class ScenarioTIsHandler(GenericScenarioUpdateHandler): + @swagger.operation(nickname="addTrustIndicatorRecord") + def post(self, scenario): + """ + @description: add a new trust indicator record + @notes: add a new trust indicator record to a project + POST /api/v1/scenarios//trust_indicators? \ + installer=& \ + version=& \ + project= + @param body: trust indicator to be added + @type body: L{ScenarioTI} + @in body: body + @param installer: installer type + @type installer: L{string} + @in installer: query + @required installer: True + @param version: version + @type version: L{string} + @in version: query + @required version: True + @param project: project name + @type project: L{string} + @in project: query + @required project: True + @rtype: L{Scenario} + @return 200: trust indicator is added. + @raise 404: scenario/installer/version/project not existed + """ + self.do_post(scenario, + 'trust_indicators', + 'post', + locators={'installer': None, + 'version': None, + 'project': None}) diff --git a/testapi/opnfv_testapi/router/url_mappings.py b/testapi/opnfv_testapi/router/url_mappings.py index 4f990f0..4589425 100644 --- a/testapi/opnfv_testapi/router/url_mappings.py +++ b/testapi/opnfv_testapi/router/url_mappings.py @@ -56,6 +56,8 @@ mappings = [ (r"/api/v1/scenarios/([^/]+)", scenario_handlers.ScenarioGURHandler), (r"/api/v1/scenarios/([^/]+)/scores", scenario_handlers.ScenarioScoresHandler), + (r"/api/v1/scenarios/([^/]+)/trust_indicators", + scenario_handlers.ScenarioTIsHandler), # static path (r'/(.*\.(css|png|gif|js|html|json|map|woff2|woff|ttf))', diff --git a/testapi/opnfv_testapi/tests/unit/resources/test_scenario.py b/testapi/opnfv_testapi/tests/unit/resources/test_scenario.py index c12c52b..0558ea3 100644 --- a/testapi/opnfv_testapi/tests/unit/resources/test_scenario.py +++ b/testapi/opnfv_testapi/tests/unit/resources/test_scenario.py @@ -189,6 +189,17 @@ class TestScenarioUpdate(TestScenarioBase): return add, scenario + @update_partial('_add', '_success') + def test_addTrustIndicator(self, scenario): + add = models.ScenarioTI(date=str(datetime.now()), status='gold') + projects = scenario['installers'][0]['versions'][0]['projects'] + functest = filter(lambda f: f['project'] == 'functest', projects)[0] + functest['trust_indicators'].append(add.format()) + self.update_url = '{}/trust_indicators?{}'.format(self.scenario_url, + self.locate_project) + + return add, scenario + def _add(self, update_req, new_scenario): return self.post_direct_url(self.update_url, update_req) -- cgit 1.2.3-korg