Passed
Push — master ( 73e764...5e9a14 )
by Humberto
07:26 queued 43s
created

TestJSONEncoderOF10.test_cast()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""Test v0x01.utils methods."""
2
from unittest import TestCase, mock
3
from unittest.mock import MagicMock, PropertyMock, patch
4
5
from kytos.lib.helpers import get_connection_mock, get_switch_mock
6
from napps.kytos.of_core.v0x01.utils import (handle_features_reply, say_hello,
7
                                             send_desc_request, send_echo,
8
                                             send_set_config)
9
from tests.helpers import get_controller_mock
10
11
12
class TestUtils(TestCase):
13
    """Test utils."""
14
15
    def setUp(self):
16
        """Execute steps before each tests."""
17
        self.mock_controller = get_controller_mock()
18
        self.mock_switch = get_switch_mock('00:00:00:00:00:00:00:01', 0x01)
19
        self.mock_connection = get_connection_mock(0x01, self.mock_switch)
20
21
    @patch('napps.kytos.of_core.v0x01.utils.emit_message_out')
22
    def test_send_desc_request(self, mock_emit_message_out):
23
        """Test send_desc_request."""
24
        send_desc_request(self.mock_controller, self.mock_switch)
25
        mock_emit_message_out.assert_called()
26
27
    def test_handle_features_reply(self):
28
        """test Handle features reply."""
29
        mock_event = MagicMock()
30
        mock_features = MagicMock()
31
        mock_controller = MagicMock()
32
        self.mock_switch.get_interface_by_port_no.side_effect = [MagicMock(),
33
                                                                 False]
34
        type(mock_features).ports = PropertyMock(return_value=[MagicMock()])
35
        type(mock_event).content = PropertyMock(return_value={'message':
36
                                                mock_features})
37
        mock_controller.get_switch_or_create.return_value = self.mock_switch
38
        response = handle_features_reply(mock_controller, mock_event)
39
        self.assertEqual(self.mock_switch, response)
40
        self.assertEqual(self.mock_switch.update_features.call_count, 1)
41
42
        self.mock_switch.update_features.call_count = 0
43
        response = handle_features_reply(mock_controller, mock_event)
44
        self.assertEqual(self.mock_switch, response)
45
        self.assertEqual(self.mock_switch.update_features.call_count, 1)
46
47
    @patch('napps.kytos.of_core.v0x01.utils.emit_message_out')
48
    def test_send_echo(self, mock_emit_message_out):
49
        """Test send_echo."""
50
        send_echo(self.mock_controller, self.mock_switch)
51
        mock_emit_message_out.assert_called()
52
53
    @patch('napps.kytos.of_core.v0x01.utils.emit_message_out')
54
    def test_set_config(self, mock_emit_message_out):
55
        """Test set_config."""
56
        send_set_config(self.mock_controller, self.mock_switch)
57
        mock_emit_message_out.assert_called()
58
59
    @patch('napps.kytos.of_core.v0x01.utils.emit_message_out')
60
    def test_say_hello(self, mock_emit_message_out):
61
        """Test say_hello."""
62
        say_hello(self.mock_controller, self.mock_switch)
63
        mock_emit_message_out.assert_called()
64
65
66
class TestJSONEncoderOF10(TestCase):
67
    """Test custom JSON encoder for OF 1.0 ."""
68
69
    def setUp(self):
70
        """Execute steps before each tests.
71
        Set the server_name_url from kytos/of_core
72
        """
73
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
74
        # pylint: disable=import-outside-toplevel
75
        from napps.kytos.of_core.v0x01.utils import JSONEncoderOF10
76
        self.addCleanup(patch.stopall)
77
78
        self.encoder = JSONEncoderOF10()
79
80
    @patch('napps.kytos.of_core.v0x01.utils.UBIntBase', new=mock.Mock)
81
    def test_cast(self):
82
        """Test custom JSON encoder for OF 1.0 flow representation."""
83
        object_mock = MagicMock()
84
        response = self.encoder.default(object_mock)
85
        self.assertEqual(response, 1)
86
87
    @patch('json.JSONEncoder.default')
88
    def test_cast_not_equal_case(self, mock_json):
89
        """Test the custom JSON encoder in case the object is not UBInt."""
90
        self.encoder.default('1')
91
        mock_json.assert_called()
92