64691e75d719341e2870d4b260d4e1ec8ccdbfd3
[multicloud/framework.git] / multivimbroker / multivimbroker / tests / test_check_capacity.py
1 # Copyright (c) 2017-2018 VMware, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import mock
14 import unittest
15
16 from rest_framework import status
17
18 from multivimbroker.forwarder.views import CheckCapacity
19
20
21 class CheckCapacityTest(unittest.TestCase):
22
23     def setUp(self):
24         self.view = CheckCapacity()
25         super(CheckCapacityTest, self).setUp()
26
27     def tearDown(self):
28         pass
29
30     def test_check_capacity_success(self):
31         req = mock.Mock()
32         req.body = """
33         {
34             "vCPU": 1,
35             "Memory": 1,
36             "Storage": 500,
37             "VIMs": ["openstack_RegionOne"]
38         }"""
39         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
40                                           "/v0/check_vim_capacity")
41         req.META.items.return_value = [("Project", "projectname1")]
42         with mock.patch.object(self.view, "send") as send:
43             plugin_resp = mock.Mock()
44             plugin_resp.content = """{
45                 "result": true
46             }"""
47             plugin_resp.status_code = str(status.HTTP_200_OK)
48             send.return_value = plugin_resp
49
50             resp = self.view.post(req)
51             expect_body = {
52                 "VIMs": ["openstack_RegionOne"]
53             }
54             self.assertEqual(status.HTTP_200_OK, resp.status_code)
55             self.assertDictEqual(expect_body, resp.data)
56
57     def test_check_capacity_no_suitable_vim(self):
58         req = mock.Mock()
59         req.body = """
60         {
61             "vCPU": 1,
62             "Memory": 1,
63             "Storage": 500,
64             "VIMs": ["openstack_RegionOne"]
65         }"""
66         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
67                                           "/v0/check_vim_capacity")
68         req.META.items.return_value = [("Project", "projectname1")]
69
70         with mock.patch.object(self.view, "send") as send:
71             plugin_resp = mock.Mock()
72             plugin_resp.content = """{
73                 "result": false
74             }"""
75             plugin_resp.status_code = str(status.HTTP_200_OK)
76             send.return_value = plugin_resp
77
78             resp = self.view.post(req)
79             expect_body = {
80                 "VIMs": []
81             }
82             self.assertEqual(status.HTTP_200_OK, resp.status_code)
83             self.assertDictEqual(expect_body, resp.data)
84
85     def test_check_capacity_invalid_input(self):
86         req = mock.Mock()
87         req.body = "hello world"
88         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
89                                           "/v0/check_vim_capacity")
90         req.META.items.return_value = [("Project", "projectname1")]
91         expect_body = {
92             "error": ("Invalidate request body "
93                       "No JSON object could be decoded.")
94         }
95         resp = self.view.post(req)
96         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
97         self.assertDictEqual(expect_body, resp.data)