blob: 92a939b2259d93d808e026f87abff8f6f668025e [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 = {
34 'xos.exceptions': self.xos.exceptions,
35 'models_decl': self.models_decl,
36 }
37
38 self.module_patcher = patch.dict('sys.modules', modules)
39 self.module_patcher.start()
40
41 from models import OLTDevice
42
Matteo Scandolo2360fd92018-05-29 17:27:51 -070043 self.olt_device = OLTDevice()
44 self.olt_device.id = None # this is a new model
45 self.olt_device.is_new = True
46 self.olt_device.device_id = 1234
47
Matteo Scandolo33fb1622018-06-19 18:54:35 -070048 def tearDown(self):
49 self.module_patcher.stop()
50
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070051 def test_create_mac_address(self):
52 from models import OLTDevice
53 olt = OLTDevice()
54
55 olt.host = "1.1.1.1"
56 olt.port = "9101"
57 olt.mac_address = "00:0c:d5:00:05:40"
58
59 with self.assertRaises(Exception) as e:
60 olt.save()
61
62 self.assertEqual(e.exception.message,
63 "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 -070064
65 def test_delete(self):
66 self.olt_device.delete()
67 self.models_decl.OLTDevice_decl.delete.assert_called()
68
69 def test_prevent_delete(self):
70
71 onu1 = Mock()
72 onu1.id = 1
73
74 pon1 = Mock()
75 pon1.onu_devices.all.return_value = [onu1]
76
77 self.olt_device.pon_ports.all.return_value = [pon1]
78
79 volt_si_1 = Mock()
80 volt_si_1.onu_device_id = onu1.id
81
82 with patch.object(self.olt_device, "get_volt_si")as volt_si_get:
83 volt_si_get.return_value = [volt_si_1]
84 with self.assertRaises(Exception) as e:
85 self.olt_device.delete()
86
87 self.assertEqual(e.exception.message, 'OLT "1234" can\'t be deleted as it has subscribers associated with its ONUs')
88 self.models_decl.OLTDevice_decl.delete.assert_not_called()
89
Matteo Scandolo33fb1622018-06-19 18:54:35 -070090class TestONUDeviceModel(unittest.TestCase):
91
92 def setUp(self):
93 self.xos = XOS
94
95 self.models_decl = Mock()
96 self.models_decl.ONUDevice_decl = MagicMock
97 self.models_decl.ONUDevice_decl.delete = Mock()
98
99 modules = {
100 'xos.exceptions': self.xos.exceptions,
101 'models_decl': self.models_decl,
102 }
103
104 self.module_patcher = patch.dict('sys.modules', modules)
105 self.module_patcher.start()
106
107 from models import ONUDevice
108
109 self.onu_device = ONUDevice()
110 self.onu_device.id = None # this is a new model
111 self.onu_device.is_new = True
112 self.onu_device.serial_number = 1234
113
114 def test_delete(self):
115 self.onu_device.delete()
116 self.models_decl.ONUDevice_decl.delete.assert_called()
117
118 def test_prevent_delete(self):
119 volt_si_1 = Mock()
120 volt_si_1.onu_device_id = self.onu_device.id
121 self.onu_device.volt_service_instances.all.return_value = [volt_si_1]
122
123 with self.assertRaises(Exception) as e:
124 self.onu_device.delete()
125
126 self.assertEqual(e.exception.message,
127 'ONU "1234" can\'t be deleted as it has subscribers associated with it')
128 self.models_decl.OLTDevice_decl.delete.assert_not_called()
129
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700130if __name__ == '__main__':
131 unittest.main()