summaryrefslogtreecommitdiffstats
path: root/testapi
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-01-16 17:59:06 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-01-16 21:16:14 +0800
commit16d5fe234367a75a52dc33dae012df4f1098ae0b (patch)
treec06f0f97c3b1b6aaa0f4857d2f6f1c477f8c5801 /testapi
parent43092f3b023fa82c93ce2cbd6e0ca5bc16da5bf1 (diff)
implement create scenario and add unittest
JIRA: RELENG-163 Change-Id: Id715a2e5de1022cfd0a745505771d250935541bd Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi')
-rw-r--r--testapi/opnfv_testapi/resources/scenario_handlers.py18
-rw-r--r--testapi/opnfv_testapi/resources/scenario_models.py13
-rw-r--r--testapi/opnfv_testapi/tests/unit/fake_pymongo.py1
-rw-r--r--testapi/opnfv_testapi/tests/unit/scenario-create.json38
-rw-r--r--testapi/opnfv_testapi/tests/unit/test_base.py17
-rw-r--r--testapi/opnfv_testapi/tests/unit/test_scenario.py55
6 files changed, 135 insertions, 7 deletions
diff --git a/testapi/opnfv_testapi/resources/scenario_handlers.py b/testapi/opnfv_testapi/resources/scenario_handlers.py
index 75754d8..7bf3d5d 100644
--- a/testapi/opnfv_testapi/resources/scenario_handlers.py
+++ b/testapi/opnfv_testapi/resources/scenario_handlers.py
@@ -1,3 +1,4 @@
+from opnfv_testapi.common.constants import HTTP_FORBIDDEN
from opnfv_testapi.resources.handlers import GenericApiHandler
from opnfv_testapi.resources.scenario_models import Scenario
from opnfv_testapi.tornado_swagger import swagger
@@ -29,10 +30,23 @@ class ScenariosCLHandler(GenericScenarioHandler):
"""
@description: create a new scenario by name
@param body: scenario to be created
- @type body: L{string}
+ @type body: L{ScenarioCreateRequest}
+ @in body: body
@rtype: L{CreateResponse}
+ @return 200: scenario is created.
+ @raise 403: scenario already exists
+ @raise 400: body or name not provided
"""
- pass
+ def query(data):
+ return {'name': data.name}
+
+ def error(data):
+ message = '{} already exists as a scenario'.format(data.name)
+ return HTTP_FORBIDDEN, message
+
+ miss_checks = ['name']
+ db_checks = [(self.table, False, query, error)]
+ self._create(miss_checks=miss_checks, db_checks=db_checks)
class ScenarioGURHandler(GenericScenarioHandler):
diff --git a/testapi/opnfv_testapi/resources/scenario_models.py b/testapi/opnfv_testapi/resources/scenario_models.py
index 0748a37..b4bb363 100644
--- a/testapi/opnfv_testapi/resources/scenario_models.py
+++ b/testapi/opnfv_testapi/resources/scenario_models.py
@@ -61,6 +61,17 @@ class ScenarioInstaller(models.ModelBase):
@swagger.model()
+class ScenarioCreateRequest(models.ModelBase):
+ """
+ @property installers:
+ @ptype installers: C{list} of L{ScenarioInstaller}
+ """
+ def __init__(self, name='', installers=None):
+ self.name = name
+ self.installers = installers if installers else list()
+
+
+@swagger.model()
class Scenario(models.ModelBase):
"""
@property installers:
@@ -69,7 +80,7 @@ class Scenario(models.ModelBase):
def __init__(self, name='', create_date='', _id='', installers=None):
self.name = name
self._id = _id
- self.create_date = create_date
+ self.creation_date = create_date
self.installers = installers if installers else list()
diff --git a/testapi/opnfv_testapi/tests/unit/fake_pymongo.py b/testapi/opnfv_testapi/tests/unit/fake_pymongo.py
index 3dd87e6..d86d8ea 100644
--- a/testapi/opnfv_testapi/tests/unit/fake_pymongo.py
+++ b/testapi/opnfv_testapi/tests/unit/fake_pymongo.py
@@ -189,3 +189,4 @@ pods = MemDb()
projects = MemDb()
testcases = MemDb()
results = MemDb()
+scenarios = MemDb()
diff --git a/testapi/opnfv_testapi/tests/unit/scenario-create.json b/testapi/opnfv_testapi/tests/unit/scenario-create.json
new file mode 100644
index 0000000..eba8b6c
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/scenario-create.json
@@ -0,0 +1,38 @@
+{
+ "name": "nosdn-nofeature-ha",
+ "installers":
+ [
+ {
+ "installer": "apex",
+ "versions":
+ [
+ {
+ "owner": "Luke",
+ "version": "master",
+ "projects":
+ [
+ {
+ "project": "functest",
+ "customs": [ "healthcheck", "vping_ssh"],
+ "scores":
+ [
+ {
+ "date": "2017-01-08 22:46:44",
+ "score": "12/14"
+ }
+
+ ],
+ "trust_indicators": []
+ },
+ {
+ "project": "yardstick",
+ "customs": [],
+ "scores": [],
+ "trust_indicators": []
+ }
+ ]
+ }
+ ]
+ }
+ ]
+} \ No newline at end of file
diff --git a/testapi/opnfv_testapi/tests/unit/test_base.py b/testapi/opnfv_testapi/tests/unit/test_base.py
index ff1a193..9343ab2 100644
--- a/testapi/opnfv_testapi/tests/unit/test_base.py
+++ b/testapi/opnfv_testapi/tests/unit/test_base.py
@@ -47,11 +47,11 @@ class TestBase(AsyncHTTPTestCase):
return self.create_help(self.basePath, req, *args)
def create_help(self, uri, req, *args):
- if req:
- req = req.format()
+ if req and not isinstance(req, str):
+ req = json.dumps(req.format())
res = self.fetch(self._update_uri(uri, *args),
method='POST',
- body=json.dumps(req),
+ body=req if req else json.dumps(None),
headers=self.headers)
return self._get_return(res, self.create_res)
@@ -123,9 +123,17 @@ class TestBase(AsyncHTTPTestCase):
self.assertIn(self.basePath, body.href)
def assert_create_body(self, body, req=None, *args):
+ import inspect
if not req:
req = self.req_d
- new_args = args + tuple([req.name])
+ resource_name = ''
+ if inspect.isclass(req):
+ resource_name = req.name
+ elif isinstance(req, dict):
+ resource_name = req['name']
+ elif isinstance(req, str):
+ resource_name = json.loads(req)['name']
+ new_args = args + tuple([resource_name])
self.assertIn(self._get_uri(*new_args), body.href)
@staticmethod
@@ -134,3 +142,4 @@ class TestBase(AsyncHTTPTestCase):
fake_pymongo.projects.clear()
fake_pymongo.testcases.clear()
fake_pymongo.results.clear()
+ fake_pymongo.scenarios.clear()
diff --git a/testapi/opnfv_testapi/tests/unit/test_scenario.py b/testapi/opnfv_testapi/tests/unit/test_scenario.py
new file mode 100644
index 0000000..8e82781
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/test_scenario.py
@@ -0,0 +1,55 @@
+import json
+import os
+
+from opnfv_testapi.common.constants import HTTP_BAD_REQUEST
+from opnfv_testapi.common.constants import HTTP_FORBIDDEN
+from opnfv_testapi.common.constants import HTTP_OK
+from opnfv_testapi.resources.scenario_models import ScenarioCreateRequest
+from test_testcase import TestBase
+
+
+class TestScenarioBase(TestBase):
+ def setUp(self):
+ super(TestScenarioBase, self).setUp()
+ self.basePath = '/api/v1/scenarios'
+ self.load_request('scenario-create.json')
+
+ def tearDown(self):
+ pass
+
+ def assert_body(self, project, req=None):
+ pass
+
+ def load_request(self, f_req):
+ with open(os.path.join(os.path.dirname(__file__), f_req), 'r') as f:
+ self.req_d = json.dumps(json.load(f))
+ f.close()
+
+
+class TestScenarioCreate(TestScenarioBase):
+ def test_withoutBody(self):
+ (code, body) = self.create()
+ self.assertEqual(code, HTTP_BAD_REQUEST)
+
+ def test_emptyName(self):
+ req_empty = ScenarioCreateRequest('')
+ (code, body) = self.create(req_empty)
+ self.assertEqual(code, HTTP_BAD_REQUEST)
+ self.assertIn('name missing', body)
+
+ def test_noneName(self):
+ req_none = ScenarioCreateRequest(None)
+ (code, body) = self.create(req_none)
+ self.assertEqual(code, HTTP_BAD_REQUEST)
+ self.assertIn('name missing', body)
+
+ def test_success(self):
+ (code, body) = self.create_d()
+ self.assertEqual(code, HTTP_OK)
+ self.assert_create_body(body)
+
+ def test_alreadyExist(self):
+ self.create_d()
+ (code, body) = self.create_d()
+ self.assertEqual(code, HTTP_FORBIDDEN)
+ self.assertIn('already exists', body)