|
| 1 | +import json |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from cycode.cli.files_collector.sca.maven.restore_maven_dependencies import ( |
| 5 | + RestoreMavenDependencies, |
| 6 | + _has_dependency_graph, |
| 7 | +) |
| 8 | +from cycode.cli.models import Document |
| 9 | + |
| 10 | + |
| 11 | +class TestHasDependencyGraph: |
| 12 | + def test_returns_false_when_content_is_none(self) -> None: |
| 13 | + assert _has_dependency_graph(None) is False |
| 14 | + |
| 15 | + def test_returns_false_when_content_is_empty_string(self) -> None: |
| 16 | + assert _has_dependency_graph('') is False |
| 17 | + |
| 18 | + def test_returns_false_when_dependencies_section_is_missing(self) -> None: |
| 19 | + content = json.dumps({'components': [{'name': 'foo'}]}) |
| 20 | + assert _has_dependency_graph(content) is False |
| 21 | + |
| 22 | + def test_returns_false_when_all_dependencies_have_empty_depends_on(self) -> None: |
| 23 | + content = json.dumps({'dependencies': [{'ref': 'pkg:maven/foo/bar@1.0', 'dependsOn': []}]}) |
| 24 | + assert _has_dependency_graph(content) is False |
| 25 | + |
| 26 | + def test_returns_false_when_dependencies_list_is_empty(self) -> None: |
| 27 | + content = json.dumps({'dependencies': []}) |
| 28 | + assert _has_dependency_graph(content) is False |
| 29 | + |
| 30 | + def test_returns_true_when_at_least_one_dependency_has_depends_on(self) -> None: |
| 31 | + content = json.dumps( |
| 32 | + { |
| 33 | + 'dependencies': [ |
| 34 | + {'ref': 'pkg:maven/com.example/root@1.0', 'dependsOn': ['pkg:maven/io.netty/netty-all@4.1.0']}, |
| 35 | + {'ref': 'pkg:maven/io.netty/netty-all@4.1.0', 'dependsOn': []}, |
| 36 | + ] |
| 37 | + } |
| 38 | + ) |
| 39 | + assert _has_dependency_graph(content) is True |
| 40 | + |
| 41 | + def test_returns_false_when_content_is_invalid_json(self) -> None: |
| 42 | + assert _has_dependency_graph('not valid json {{{') is False |
| 43 | + |
| 44 | + |
| 45 | +class TestRestoreMavenDependenciesFallback: |
| 46 | + def _make_instance(self) -> RestoreMavenDependencies: |
| 47 | + ctx = MagicMock() |
| 48 | + ctx.obj = {} |
| 49 | + return RestoreMavenDependencies(ctx=ctx, is_git_diff=False, command_timeout=60) |
| 50 | + |
| 51 | + def test_falls_back_to_secondary_command_when_bom_has_no_dependency_graph(self) -> None: |
| 52 | + instance = self._make_instance() |
| 53 | + document = MagicMock(spec=Document) |
| 54 | + document.content = 'some content' |
| 55 | + |
| 56 | + bom_doc = MagicMock(spec=Document) |
| 57 | + bom_doc.content = json.dumps({'dependencies': []}) |
| 58 | + fallback_doc = MagicMock(spec=Document) |
| 59 | + fallback_doc.content = '[INFO] com.example:root:jar:1.0\n+- io.netty:netty-all:jar:4.1.0' |
| 60 | + |
| 61 | + with ( |
| 62 | + patch.object(instance, 'get_manifest_file_path', return_value='/project/pom.xml'), |
| 63 | + patch( |
| 64 | + 'cycode.cli.files_collector.sca.maven.restore_maven_dependencies.BaseRestoreDependencies.try_restore_dependencies', |
| 65 | + return_value=bom_doc, |
| 66 | + ), |
| 67 | + patch.object(instance, 'restore_from_secondary_command', return_value=fallback_doc) as mock_fallback, |
| 68 | + ): |
| 69 | + result = instance.try_restore_dependencies(document) |
| 70 | + |
| 71 | + mock_fallback.assert_called_once_with(document, '/project/pom.xml') |
| 72 | + assert result is fallback_doc |
| 73 | + |
| 74 | + def test_returns_bom_document_when_dependency_graph_is_present(self) -> None: |
| 75 | + instance = self._make_instance() |
| 76 | + document = MagicMock(spec=Document) |
| 77 | + document.content = 'some content' |
| 78 | + |
| 79 | + bom_doc = MagicMock(spec=Document) |
| 80 | + bom_doc.content = json.dumps( |
| 81 | + { |
| 82 | + 'dependencies': [ |
| 83 | + {'ref': 'pkg:maven/com.example/root@1.0', 'dependsOn': ['pkg:maven/io.netty/netty@4.1.0']} |
| 84 | + ] |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + with ( |
| 89 | + patch.object(instance, 'get_manifest_file_path', return_value='/project/pom.xml'), |
| 90 | + patch( |
| 91 | + 'cycode.cli.files_collector.sca.maven.restore_maven_dependencies.BaseRestoreDependencies.try_restore_dependencies', |
| 92 | + return_value=bom_doc, |
| 93 | + ), |
| 94 | + patch.object(instance, 'restore_from_secondary_command') as mock_fallback, |
| 95 | + ): |
| 96 | + result = instance.try_restore_dependencies(document) |
| 97 | + |
| 98 | + mock_fallback.assert_not_called() |
| 99 | + assert result is bom_doc |
| 100 | + |
| 101 | + def test_uses_plugin_version_2_9_1(self) -> None: |
| 102 | + instance = self._make_instance() |
| 103 | + commands = instance.get_commands('/path/to/pom.xml') |
| 104 | + assert len(commands) == 1 |
| 105 | + assert 'org.cyclonedx:cyclonedx-maven-plugin:2.9.1:makeAggregateBom' in commands[0] |
0 commit comments