Completed
Push — master ( c2db14...cb8881 )
by Humberto
03:32 queued 01:27
created

test_main.TestMain.test_update_topology_success_case()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
"""Test Main methods."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, patch
4
5
from kytos.core.events import KytosEvent
6
from kytos.lib.helpers import get_controller_mock, get_test_client
7
8
from napps.kytos.pathfinder.main import Main
9
from tests.helpers import get_topology_mock
10
11
12
# pylint: disable=protected-access
13
class TestMain(TestCase):
14
    """Tests for the Main class."""
15
16
    def setUp(self):
17
        """Execute steps before each tests."""
18
        self.napp = Main(get_controller_mock())
19
20
    @patch('napps.kytos.pathfinder.graph.KytosGraph.update_topology')
21
    def test_update_topology_success_case(self, mock_update_topology):
22
        """Test update topology method to success case."""
23
        topology = get_topology_mock()
24
        event = KytosEvent(name='kytos.topology.updated',
25
                           content={'topology': topology})
26
        self.napp.update_topology(event)
27
28
        self.assertEqual(self.napp._topology, topology)
29
30
    @patch('napps.kytos.pathfinder.graph.KytosGraph.update_topology')
31
    def test_update_topology_failure_case(self, mock_update_topology):
32
        """Test update topology method to failure case."""
33
        event = KytosEvent(name='kytos.topology.updated')
34
        self.napp.update_topology(event)
35
36
        self.assertIsNone(self.napp._topology)
37
38
    @patch('napps.kytos.pathfinder.graph.KytosGraph.shortest_paths')
39
    def test_shortest_path(self, mock_shortest_paths):
40
        """Test shortest path."""
41
        self.napp._topology = get_topology_mock()
42
        path = ["00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:1"]
43
        mock_shortest_paths.return_value = [path]
44
45
        api = get_test_client(self.napp.controller, self.napp)
46
        url = "http://127.0.0.1:8181/api/kytos/pathfinder/v2"
47
        data = {"source": "00:00:00:00:00:00:00:01:1",
48
                "destination": "00:00:00:00:00:00:00:02:1",
49
                "desired_links": ["1"],
50
                "undesired_links": None}
51
        response = api.open(url, method='POST', json=data)
52
53
        expected_response = {'paths': [{'hops': path}]}
54
        self.assertEqual(response.json, expected_response)
55
        self.assertEqual(response.status_code, 200)
56
57
    def test_filter_paths(self):
58
        """Test filter paths."""
59
        self.napp._topology = get_topology_mock()
60
        paths = [{"hops": ["00:00:00:00:00:00:00:01:1",
61
                           "00:00:00:00:00:00:00:02:1"]}]
62
        desired, undesired = ["1"], None
63
64
        filtered_paths = self.napp._filter_paths(paths, desired, undesired)
65
        self.assertEqual(filtered_paths, paths)
66
67
        paths = [{"hops": ["00:00:00:00:00:00:00:01:2",
68
                           "00:00:00:00:00:00:00:03:1"]}]
69
        desired, undesired = None, ["2"]
70
        filtered_paths = self.napp._filter_paths(paths, desired, undesired)
71
        self.assertEqual(filtered_paths, [])
72