Fix check_vim_capacity
[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         with mock.patch.object(self.view, "send") as send:
42             plugin_resp = mock.Mock()
43             plugin_resp.content = """{
44                 "result": true
45             }"""
46             plugin_resp.status_code = str(status.HTTP_200_OK)
47             send.return_value = plugin_resp
48
49             resp = self.view.post(req)
50             expect_body = {
51                 "VIMs": ["openstack_RegionOne"]
52             }
53             self.assertEqual(status.HTTP_200_OK, resp.status_code)
54             self.assertDictEqual(expect_body, resp.data)
55
56     def test_check_capacity_no_suitable_vim(self):
57         req = mock.Mock()
58         req.body = """
59         {
60             "vCPU": 1,
61             "Memory": 1,
62             "Storage": 500,
63             "VIMs": ["openstack_RegionOne"]
64         }"""
65         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
66                                           "/v0/check_vim_capacity")
67         with mock.patch.object(self.view, "send") as send:
68             plugin_resp = mock.Mock()
69             plugin_resp.content = """{
70                 "result": false
71             }"""
72             plugin_resp.status_code = str(status.HTTP_200_OK)
73             send.return_value = plugin_resp
74
75             resp = self.view.post(req)
76             expect_body = {
77                 "VIMs": []
78             }
79             self.assertEqual(status.HTTP_200_OK, resp.status_code)
80             self.assertDictEqual(expect_body, resp.data)
81
82     def test_check_capacity_invalid_input(self):
83         req = mock.Mock()
84         req.body = "hello world"
85         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
86                                           "/v0/check_vim_capacity")
87         expect_body = {
88             "error": ("Invalidate request body "
89                       "No JSON object could be decoded.")
90         }
91         resp = self.view.post(req)
92         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
93         self.assertDictEqual(expect_body, resp.data)