Improve code coverage in sonar
[multicloud/azure.git] / azure / multicloud_azure / tests / test_registry_view.py
1 # Copyright (c) 2018 Amdocs
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 unittest
14 import mock
15 from rest_framework import status
16 from multicloud_azure.swagger.views.registry.views import Registry
17 from multicloud_azure.swagger.views.registry.views import UnRegistry
18
19
20 from multicloud_azure.pub.msapi import extsys
21 from multicloud_azure.pub.utils.restcall import AAIClient
22 from multicloud_azure.pub.vim.vimapi.compute import OperateFlavors
23
24 VIM_INFO = {'cloud_extra_info': 1, 'username': 'user1',
25             'password': '1234', 'default_tenant': 't1',
26             'cloud_region_id': 'r1'}
27
28
29 class RegistryViewTest(unittest.TestCase):
30
31     def setUp(self):
32         self.reg = Registry()
33
34     def tearDown(self):
35         pass
36
37     @mock.patch.object(OperateFlavors.OperateFlavors, 'list_flavors')
38     @mock.patch.object(extsys, 'get_vim_by_id')
39     def test_reg_get_flavors_view_fail(self, mock_vim_info, mock_flavors):
40         mock_vim_info.return_value = VIM_INFO
41
42         class Flavor:
43             def __init__(self, id, name):
44                 self.id = id
45                 self.name = name
46
47             def to_dict(self):
48                 return {"name": self.name, "id": self.id}
49
50         f1 = Flavor(1, "f1")
51         f2 = Flavor(2, "f2")
52         flavors = [f1.to_dict(), f2.to_dict()]
53         mock_flavors.return_value = flavors
54         auth = {
55             "subscription_id": "1",
56             "username": "user",
57             "password": "1234",
58             "tenant_id": "t1",
59             "region_id": "r1"}
60
61         self.assertEqual(
62             {'flavors': [{'id': 1, 'name': 'f1'},
63                          {'id': 2, 'name': 'f2'}]},
64             self.reg._get_flavors(auth))
65
66     @mock.patch.object(OperateFlavors.OperateFlavors, 'list_flavors')
67     @mock.patch.object(extsys, 'get_vim_by_id')
68     def test_reg_get_flavors_view_fail2(self, mock_vim_info, mock_flavors):
69         mock_vim_info.return_value = VIM_INFO
70         mock_flavors.side_effect = Exception("something wrong")
71         self.assertRaises(Exception, self.reg._get_flavors)
72
73
74 class UnRegistryViewTest(unittest.TestCase):
75
76     def setUp(self):
77         self.reg = UnRegistry()
78
79     def tearDown(self):
80         pass
81
82     @mock.patch.object(AAIClient, 'delete_vim')
83     @mock.patch.object(extsys, 'get_vim_by_id')
84     def test_reg_delete_view(self, mock_vim_info, mock_del_vim):
85         mock_vim_info.return_value = VIM_INFO
86
87         class Request:
88             def __init__(self, query_params):
89                 self.query_params = query_params
90         req = Request({'k': 'v'})
91         self.assertEqual(
92             status.HTTP_204_NO_CONTENT,
93             self.reg.delete(req, "vimid").status_code)