add rename scenario exceptions
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / test_scenario.py
index c15dc32..b62c1d2 100644 (file)
@@ -1,16 +1,15 @@
 from copy import deepcopy
+from datetime import datetime
+import functools
 import json
 import os
-from datetime import datetime
 
-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.common import constants
 import opnfv_testapi.resources.scenario_models as models
-from test_testcase import TestBase
+import test_base as base
 
 
-class TestScenarioBase(TestBase):
+class TestScenarioBase(base.TestBase):
     def setUp(self):
         super(TestScenarioBase, self).setUp()
         self.get_res = models.Scenario
@@ -38,13 +37,13 @@ class TestScenarioBase(TestBase):
         return res.href.split('/')[-1]
 
     def assert_res(self, code, scenario, req=None):
-        self.assertEqual(code, HTTP_OK)
+        self.assertEqual(code, constants.HTTP_OK)
         if req is None:
             req = self.req_d
-        scenario_dict = scenario.format_http()
-        self.assertIsNotNone(scenario_dict['_id'])
-        self.assertIsNotNone(scenario_dict['creation_date'])
-        self.assertDictContainsSubset(req, scenario_dict)
+        self.assertIsNotNone(scenario._id)
+        self.assertIsNotNone(scenario.creation_date)
+
+        scenario == models.Scenario.from_dict(req)
 
     @staticmethod
     def _set_query(*args):
@@ -61,29 +60,29 @@ class TestScenarioBase(TestBase):
 class TestScenarioCreate(TestScenarioBase):
     def test_withoutBody(self):
         (code, body) = self.create()
-        self.assertEqual(code, HTTP_BAD_REQUEST)
+        self.assertEqual(code, constants.HTTP_BAD_REQUEST)
 
     def test_emptyName(self):
         req_empty = models.ScenarioCreateRequest('')
         (code, body) = self.create(req_empty)
-        self.assertEqual(code, HTTP_BAD_REQUEST)
+        self.assertEqual(code, constants.HTTP_BAD_REQUEST)
         self.assertIn('name missing', body)
 
     def test_noneName(self):
         req_none = models.ScenarioCreateRequest(None)
         (code, body) = self.create(req_none)
-        self.assertEqual(code, HTTP_BAD_REQUEST)
+        self.assertEqual(code, constants.HTTP_BAD_REQUEST)
         self.assertIn('name missing', body)
 
     def test_success(self):
         (code, body) = self.create_d()
-        self.assertEqual(code, HTTP_OK)
+        self.assertEqual(code, constants.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.assertEqual(code, constants.HTTP_FORBIDDEN)
         self.assertIn('already exists', body)
 
 
@@ -126,7 +125,7 @@ class TestScenarioGet(TestScenarioBase):
     def _query_and_assert(self, query, found=True, reqs=None):
         code, body = self.query(query)
         if not found:
-            self.assertEqual(code, HTTP_OK)
+            self.assertEqual(code, constants.HTTP_OK)
             self.assertEqual(0, len(body.scenarios))
         else:
             self.assertEqual(len(reqs), len(body.scenarios))
@@ -140,22 +139,54 @@ class TestScenarioUpdate(TestScenarioBase):
     def setUp(self):
         super(TestScenarioUpdate, self).setUp()
         self.scenario = self.create_return_name(self.req_d)
+        self.scenario_2 = self.create_return_name(self.req_2)
 
     def _execute(set_update):
+        @functools.wraps(set_update)
         def magic(self):
             update, scenario = set_update(self, deepcopy(self.req_d))
             self._update_and_assert(update, scenario)
         return magic
 
-    def test_renameScenario(self):
+    def _update(expected):
+        def _update(set_update):
+            @functools.wraps(set_update)
+            def wrap(self):
+                update, scenario = set_update(self, deepcopy(self.req_d))
+                code, body = self.update(update, self.scenario)
+                getattr(self, expected)(code, scenario)
+            return wrap
+        return _update
+
+    @_update('_success')
+    def test_renameScenario(self, scenario):
         new_name = 'nosdn-nofeature-noha'
-        new_scenario = deepcopy(self.req_d)
-        new_scenario['name'] = new_name
+        scenario['name'] = new_name
         update_req = models.ScenarioUpdateRequest(field='name',
                                                   op='update',
                                                   locate={},
                                                   term={'name': new_name})
-        self._update_and_assert(update_req, new_scenario, new_name)
+        return update_req, scenario
+
+    @_update('_forbidden')
+    def test_renameScenario_exist(self, scenario):
+        new_name = self.scenario_2
+        scenario['name'] = new_name
+        update_req = models.ScenarioUpdateRequest(field='name',
+                                                  op='update',
+                                                  locate={},
+                                                  term={'name': new_name})
+        return update_req, scenario
+
+    @_update('_bad_request')
+    def test_renameScenario_noName(self, scenario):
+        new_name = self.scenario_2
+        scenario['name'] = new_name
+        update_req = models.ScenarioUpdateRequest(field='name',
+                                                  op='update',
+                                                  locate={},
+                                                  term={})
+        return update_req, scenario
 
     @_execute
     def test_addInstaller(self, scenario):
@@ -296,10 +327,33 @@ class TestScenarioUpdate(TestScenarioBase):
 
     def _update_and_assert(self, update_req, new_scenario, name=None):
         code, _ = self.update(update_req, self.scenario)
-        self.assertEqual(code, HTTP_OK)
-        self._get_and_assert(self._none_default(name, self.scenario),
+        self.assertEqual(code, constants.HTTP_OK)
+        self._get_and_assert(_none_default(name, self.scenario),
                              new_scenario)
 
-    @staticmethod
-    def _none_default(check, default):
-        return check if check else default
+    def _success(self, status, new_scenario):
+        self.assertEqual(status, constants.HTTP_OK)
+        self._get_and_assert(new_scenario.get('name'), new_scenario)
+
+    def _forbidden(self, status, new_scenario):
+        self.assertEqual(status, constants.HTTP_FORBIDDEN)
+
+    def _bad_request(self, status, new_scenario):
+        self.assertEqual(status, constants.HTTP_BAD_REQUEST)
+
+
+class TestScenarioDelete(TestScenarioBase):
+    def test_notFound(self):
+        code, body = self.delete('notFound')
+        self.assertEqual(code, constants.HTTP_NOT_FOUND)
+
+    def test_success(self):
+        scenario = self.create_return_name(self.req_d)
+        code, _ = self.delete(scenario)
+        self.assertEqual(code, constants.HTTP_OK)
+        code, _ = self.get(scenario)
+        self.assertEqual(code, constants.HTTP_NOT_FOUND)
+
+
+def _none_default(check, default):
+    return check if check else default