Merge "Ingore swp file in git"
authorLiang Ke <lokyse@163.com>
Fri, 23 Mar 2018 02:28:55 +0000 (02:28 +0000)
committerGerrit Code Review <gerrit@onap.org>
Fri, 23 Mar 2018 02:28:55 +0000 (02:28 +0000)
multivimbroker/multivimbroker/forwarder/views.py
multivimbroker/multivimbroker/tests/test_check_capacity.py
multivimbroker/multivimbroker/tests/test_restcall.py [new file with mode: 0644]

index c77fe94..83d3172 100644 (file)
@@ -104,7 +104,7 @@ class CheckCapacity(BaseServer):
     def post(self, request):
         try:
             body = json.loads(request.body)
-        except json.JSONDecodeError as e:
+        except ValueError as e:
             return Response(
                 data={'error': 'Invalidate request body %s.' % e},
                 status=status.HTTP_400_BAD_REQUEST)
@@ -123,7 +123,7 @@ class CheckCapacity(BaseServer):
                 continue
             try:
                 resp_body = json.loads(resp.body)
-            except json.JSONDecodeError:
+            except ValueError:
                 continue
             if not resp_body.get("result", False):
                 continue
index 0a852e8..60035e0 100644 (file)
@@ -52,3 +52,42 @@ class CheckCapacityTest(unittest.TestCase):
             }
             self.assertEqual(status.HTTP_200_OK, resp.status_code)
             self.assertDictEqual(expect_body, resp.data)
+
+    def test_check_capacity_no_suitable_vim(self):
+        req = mock.Mock()
+        req.body = """
+        {
+            "vCPU": 1,
+            "Memory": 1,
+            "Storage": 500,
+            "VIMs": ["openstack_RegionOne"]
+        }"""
+        req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
+                                          "/v0/check_vim_capacity")
+        with mock.patch.object(self.view, "send") as send:
+            plugin_resp = mock.Mock()
+            plugin_resp.body = """{
+                "result": false
+            }"""
+            plugin_resp.status_code = status.HTTP_200_OK
+            send.return_value = plugin_resp
+
+            resp = self.view.post(req)
+            expect_body = {
+                "VIMs": []
+            }
+            self.assertEqual(status.HTTP_200_OK, resp.status_code)
+            self.assertDictEqual(expect_body, resp.data)
+
+    def test_check_capacity_invalid_input(self):
+        req = mock.Mock()
+        req.body = "hello world"
+        req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
+                                          "/v0/check_vim_capacity")
+        expect_body = {
+            "error": ("Invalidate request body "
+                      "No JSON object could be decoded.")
+        }
+        resp = self.view.post(req)
+        self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
+        self.assertDictEqual(expect_body, resp.data)
diff --git a/multivimbroker/multivimbroker/tests/test_restcall.py b/multivimbroker/multivimbroker/tests/test_restcall.py
new file mode 100644 (file)
index 0000000..b76b0a6
--- /dev/null
@@ -0,0 +1,24 @@
+# Copyright (c) 2017-2018 VMware, Inc.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
+import unittest
+
+from multivimbroker.pub.utils import restcall
+
+
+class TestRestCall(unittest.TestCase):
+
+    def test_combine_url(self):
+        url = ["http://a.com/test/", "http://a.com/test/",
+               "http://a.com/test"]
+        res = ["/resource", "resource", "/resource"]
+        expected = "http://a.com/test/resource"
+        for i in range(len(url)):
+            self.assertEqual(expected, restcall.combine_url(url[i], res[i]))