blob: 3ede98cce8ed326a5f6dc5a0c7dc9db63d9b9799 [file] [log] [blame]
Matteo Scandolo2360fd92018-05-29 17:27:51 -07001# Copyright 2017-present Open Networking Foundation
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# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import unittest
16from mock import patch, Mock, MagicMock
17
18# mocking XOS exception, as they're based in Django
19class Exceptions:
20 XOSValidationError = Exception
21
22class XOS:
23 exceptions = Exceptions
24
25class TestOLTDeviceModel(unittest.TestCase):
26 def setUp(self):
27 self.xos = XOS
28
29 self.models_decl = Mock()
30 self.models_decl.OLTDevice_decl = MagicMock
31 self.models_decl.OLTDevice_decl.delete = Mock()
32
33 modules = {
Scott Baker47b47302019-01-30 16:55:07 -080034 'xos': MagicMock(),
Matteo Scandolo2360fd92018-05-29 17:27:51 -070035 'xos.exceptions': self.xos.exceptions,
36 'models_decl': self.models_decl,
37 }
38
39 self.module_patcher = patch.dict('sys.modules', modules)
40 self.module_patcher.start()
41
42 from models import OLTDevice
43
Matteo Scandolo2360fd92018-05-29 17:27:51 -070044 self.olt_device = OLTDevice()
45 self.olt_device.id = None # this is a new model
46 self.olt_device.is_new = True
47 self.olt_device.device_id = 1234
48
Matteo Scandolo33fb1622018-06-19 18:54:35 -070049 def tearDown(self):
50 self.module_patcher.stop()
51
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070052 def test_create_mac_address(self):
53 from models import OLTDevice
54 olt = OLTDevice()
55
56 olt.host = "1.1.1.1"
57 olt.port = "9101"
58 olt.mac_address = "00:0c:d5:00:05:40"
59
60 with self.assertRaises(Exception) as e:
61 olt.save()
62
63 self.assertEqual(e.exception.message,
64 "You can't specify both host/port and mac_address for OLTDevice [host=%s, port=%s, mac_address=%s]" % (olt.host, olt.port, olt.mac_address))
Matteo Scandolo2360fd92018-05-29 17:27:51 -070065
66 def test_delete(self):
67 self.olt_device.delete()
68 self.models_decl.OLTDevice_decl.delete.assert_called()
69
70 def test_prevent_delete(self):
71
72 onu1 = Mock()
73 onu1.id = 1
74
75 pon1 = Mock()
76 pon1.onu_devices.all.return_value = [onu1]
77
78 self.olt_device.pon_ports.all.return_value = [pon1]
79
80 volt_si_1 = Mock()
81 volt_si_1.onu_device_id = onu1.id
82
83 with patch.object(self.olt_device, "get_volt_si")as volt_si_get:
84 volt_si_get.return_value = [volt_si_1]
85 with self.assertRaises(Exception) as e:
86 self.olt_device.delete()
87
88 self.assertEqual(e.exception.message, 'OLT "1234" can\'t be deleted as it has subscribers associated with its ONUs')
89 self.models_decl.OLTDevice_decl.delete.assert_not_called()
90
Matteo Scandolo33fb1622018-06-19 18:54:35 -070091class TestONUDeviceModel(unittest.TestCase):
92
93 def setUp(self):
94 self.xos = XOS
95
96 self.models_decl = Mock()
97 self.models_decl.ONUDevice_decl = MagicMock
98 self.models_decl.ONUDevice_decl.delete = Mock()
99
100 modules = {
Scott Baker47b47302019-01-30 16:55:07 -0800101 'xos': MagicMock(),
Matteo Scandolo33fb1622018-06-19 18:54:35 -0700102 'xos.exceptions': self.xos.exceptions,
103 'models_decl': self.models_decl,
104 }
105
106 self.module_patcher = patch.dict('sys.modules', modules)
107 self.module_patcher.start()
108
109 from models import ONUDevice
110
111 self.onu_device = ONUDevice()
112 self.onu_device.id = None # this is a new model
113 self.onu_device.is_new = True
114 self.onu_device.serial_number = 1234
115
116 def test_delete(self):
117 self.onu_device.delete()
118 self.models_decl.ONUDevice_decl.delete.assert_called()
119
120 def test_prevent_delete(self):
121 volt_si_1 = Mock()
122 volt_si_1.onu_device_id = self.onu_device.id
123 self.onu_device.volt_service_instances.all.return_value = [volt_si_1]
124
125 with self.assertRaises(Exception) as e:
126 self.onu_device.delete()
127
128 self.assertEqual(e.exception.message,
129 'ONU "1234" can\'t be deleted as it has subscribers associated with it')
130 self.models_decl.OLTDevice_decl.delete.assert_not_called()
131
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700132if __name__ == '__main__':
133 unittest.main()