Merge "Enable parser project"
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / executor.py
index b30c325..aa99b90 100644 (file)
@@ -9,6 +9,53 @@
 import functools
 import httplib
 
+from concurrent.futures import ThreadPoolExecutor
+import mock
+
+
+O_get_secure_cookie = (
+    'opnfv_testapi.resources.handlers.GenericApiHandler.get_secure_cookie')
+
+
+def thread_execute(method, *args, **kwargs):
+        with ThreadPoolExecutor(max_workers=2) as executor:
+            result = executor.submit(method, *args, **kwargs)
+        return result
+
+
+def mock_invalid_lfid():
+    def _mock_invalid_lfid(xstep):
+        def wrap(self, *args, **kwargs):
+            with mock.patch(O_get_secure_cookie) as m_cookie:
+                m_cookie.return_value = 'InvalidUser'
+                return xstep(self, *args, **kwargs)
+        return wrap
+    return _mock_invalid_lfid
+
+
+def mock_valid_lfid():
+    def _mock_valid_lfid(xstep):
+        def wrap(self, *args, **kwargs):
+            with mock.patch(O_get_secure_cookie) as m_cookie:
+                m_cookie.return_value = 'ValidUser'
+                return xstep(self, *args, **kwargs)
+        return wrap
+    return _mock_valid_lfid
+
+
+def upload(excepted_status, excepted_response):
+    def _upload(create_request):
+        @functools.wraps(create_request)
+        def wrap(self):
+            request = create_request(self)
+            status, body = self.upload(request)
+            if excepted_status == httplib.OK:
+                getattr(self, excepted_response)(body)
+            else:
+                self.assertIn(excepted_response, body)
+        return wrap
+    return _upload
+
 
 def create(excepted_status, excepted_response):
     def _create(create_request):