Skip to content

Commit

Permalink
Merge pull request #391 from Limmen/import
Browse files Browse the repository at this point in the history
unit_test import_util
  • Loading branch information
Limmen authored Jul 17, 2024
2 parents f288fcc + 0525270 commit 5a86e0b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
2 changes: 0 additions & 2 deletions simulation-system/libs/csle-common/tests/test_grpc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_grpc_server_on(self, mock_channel_ready_future) -> None:
Test utility function to test if a given gRPC channel is working or not
:param mock_channel_ready_future: mock_channel_ready_future
:return: None
"""
mock_future = MagicMock()
Expand All @@ -29,7 +28,6 @@ def test_grpc_server_on_timeout(self, mock_channel_ready_future) -> None:
Test utility function to test if a given gRPC channel is not working
:param mock_channel_ready_future: mock_channel_ready_future
:return: None
"""
mock_future = MagicMock()
Expand Down
64 changes: 64 additions & 0 deletions simulation-system/libs/csle-common/tests/test_import_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from unittest.mock import patch, MagicMock
from csle_common.util.import_util import ImportUtil


class TestImportUtilSuite:
"""
Test suite for import_util
"""

@patch("os.path.exists")
@patch("csle_common.dao.system_identification.emulation_statistics.EmulationStatistics.from_json_file")
@patch("csle_common.metastore.metastore_facade.MetastoreFacade.save_emulation_statistic")
def test_import_emulation_statistics_from_disk_json(
self, mock_save_emulation_statistic, mock_from_json_file, mock_path_exists
) -> None:
"""
Test the method that imports emulation statistics from disk to the metastore
:param mock_save_emulation_statistic: mock_save_emulation_statistic
:param mock_from_json_file: mock_from_json_file
:param mock_path_exists: mock_path_exists
:return: None
"""
mock_path_exists.return_value = True
mock_statistics = MagicMock()
mock_from_json_file.return_value = mock_statistics
input_file = "file.json"
emulation_name = "test_emulation"
ImportUtil.import_emulation_statistics_from_disk_json(input_file=input_file, emulation_name=emulation_name)

mock_path_exists.assert_called()
mock_from_json_file.assert_called_once_with(input_file)
assert mock_statistics.emulation_name == emulation_name
mock_save_emulation_statistic.assert_called()

@patch("os.path.exists")
@patch("csle_common.dao.emulation_config.emulation_trace.EmulationTrace.load_traces_from_disk")
@patch("csle_common.metastore.metastore_facade.MetastoreFacade.save_emulation_trace")
def test_import_emulation_traces_from_disk_json(
self, mock_save_emulation_trace, mock_load_traces_from_disk, mock_path_exists
) -> None:
"""
Test the method that imports emulation traces from disk to the metastore
:param mock_save_emulation_trace: mock_save_emulation_trac
:param mock_load_traces_from_disk: mock_load_traces_from_disk
:param mock_path_exists: mock_path_exists
:return: None
"""
mock_path_exists.return_value = True
mock_trace_1 = MagicMock()
mock_trace_2 = MagicMock()
mock_load_traces_from_disk.return_value = [mock_trace_1, mock_trace_2]
input_file = "file.json"
emulation_name = "test_emulation"
ImportUtil.import_emulation_traces_from_disk_json(input_file=input_file, emulation_name=emulation_name)

mock_path_exists.assert_called()
mock_load_traces_from_disk.assert_called()
assert mock_trace_1.emulation_name == emulation_name
assert mock_trace_2.emulation_name == emulation_name
assert mock_save_emulation_trace.call_count == 2

0 comments on commit 5a86e0b

Please sign in to comment.