import pytest

from anchore_engine.util.cpe_generators import (
    generate_fuzzy_cpes,
    generate_fuzzy_go_cpes,
    generate_gem_products,
    generate_java_cpes,
    generate_npm_products,
    generate_products,
    generate_python_products,
    generate_simple_cpe,
)


@pytest.mark.parametrize(
    "name, expected_list, generator_function",
    [
        pytest.param("gem1", ["gem1"], generate_gem_products, id="gem-simple"),
        pytest.param(
            "cremefraiche",
            ["creme_fraishe", "cremefraiche"],
            generate_gem_products,
            id="gem-matches-inclusion-list",
        ),
        pytest.param("npm1", ["npm1"], generate_gem_products, id="npm-simple"),
        pytest.param(
            "hapi",
            ["hapi", "hapi_server_framework"],
            generate_npm_products,
            id="npm-matches-inclusion-list",
        ),
        pytest.param("python1", ["python1"], generate_gem_products, id="python-simple"),
        pytest.param(
            "python-rrdtool",
            ["python-rrdtool", "rrdtool"],
            generate_python_products,
            id="python-matches-inclusion-list",
        ),
    ],
)
def test_generate_product_functions(name, expected_list, generator_function):
    assert generator_function(name).sort() == expected_list.sort()


@pytest.mark.parametrize(
    "name, package_type, expected_list",
    [
        pytest.param("gem1", "gem", ["gem1"], id="gem-simple"),
        pytest.param(
            "cremefraiche",
            "gem",
            ["creme_fraiche", "cremefraiche"],
            id="gem-matches-inclusion-list",
        ),
        pytest.param("npm1", "npm", ["npm1"], id="npm-simple"),
        pytest.param(
            "hapi",
            "npm",
            ["hapi", "hapi_server_framework"],
            id="npm-matches-inclusion-list",
        ),
        pytest.param("python1", "python", ["python1"], id="python-simple"),
        pytest.param(
            "python-rrdtool",
            "python",
            ["python-rrdtool", "rrdtool"],
            id="python-matches-inclusion-list",
        ),
        pytest.param(
            "Microsoft.NETCore.App",
            "nuget",
            ["Microsoft.NETCore.App", ".net_core"],
            id="nuget-matches-inclusion-list",
        ),
        pytest.param(
            "foobar",
            "unknownpackagetype",
            ["foobar"],
            id="unknown-package-type",
        ),
    ],
)
def test_generate_products(name, package_type, expected_list):
    # Function under test
    products = generate_products(name, package_type)

    # Sort the observed and expected lists
    # sort() is an in-place operation and returns None, so don't store or compare the call results
    products.sort()
    expected_list.sort()

    # Validate output
    assert products == expected_list


@pytest.mark.parametrize(
    "name, version, cpe",
    [("product", "version", "cpe:2.3:a:*:product:version:*:*:*:*:*:*:*")],
)
def test_simple_cpe_generation(name, version, cpe):
    assert generate_simple_cpe(name, version) == cpe


@pytest.mark.parametrize(
    "name, version, cpes",
    [
        ("product", "version", ["cpe:2.3:*:product:version:*:*:*:*:*:*:*"]),
        (