package com.blackduck.integration.detectable.detectables.cargo.functional;

import java.io.IOException;
import java.nio.file.Paths;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;

import com.blackduck.integration.bdio.model.Forge;
import com.blackduck.integration.detectable.Detectable;
import com.blackduck.integration.detectable.DetectableEnvironment;
import com.blackduck.integration.detectable.detectable.util.EnumListFilter;
import com.blackduck.integration.detectable.detectables.cargo.CargoDependencyType;
import com.blackduck.integration.detectable.detectables.cargo.CargoDetectableOptions;
import com.blackduck.integration.detectable.extraction.Extraction;
import com.blackduck.integration.detectable.functional.DetectableFunctionalTest;
import com.blackduck.integration.detectable.util.graph.NameVersionGraphAssert;

public class CargoLockStellarDetectableTest extends DetectableFunctionalTest {

    public CargoLockStellarDetectableTest() throws IOException {
        super("cargo_stellar");
    }

    @Override
    protected void setup() throws IOException {
        addFile(
            Paths.get("Cargo.toml"),
            "[package]  ",
            "name = \"stellar-strkey\"  ",
            "description = \"Encode and decode strkeys for Stellar.\"  ",
            "homepage = \"https://github.com/stellar/rs-stellar-strkey\"  ",
            "repository = \"https://github.com/stellar/rs-stellar-strkey\"  ",
            "authors = [\"Stellar Development Foundation <info@stellar.org>\"]  ",
            "license = \"Apache-2.0\"  ",
            "readme = \"README.md\"  ",
            "version = \"0.0.13\"  ",
            "edition = \"2021\"  ",
            "rust-version = \"1.81.0\"  ",
            "[features]  ",
            "default = []  ",
            "cli = [\"dep:clap\"]  ",
            "serde = [\"dep:serde_with\"]  ",
            "[[bin]]  ",
            "name = \"stellar-strkey\"  ",
            "path = \"src/bin/stellar-strkey/main.rs\"  ",
            "required-features = [\"cli\"]  ",
            "doctest = false  ",
            "[build-dependencies]  ",
            "crate-git-revision = \"0.0.6\"  ",
            "[dev-dependencies]  ",
            "proptest =\"1.0.0\"  ",
            "serde_test = \"1.0.177\"  ",
            "[dependencies]  ",
            "data-encoding = { version = \"2.6.0\", default-features = false, features =   ",
            "[\"alloc\"] }  ",
            "clap = { version = \"4.2.4\", default-features = false, features = [\"std\",   ",
            "\"derive\", \"usage\", \"help\"], optional = true }  ",
            "serde_with = { version = \"3.11.0\", optional = true }"
        );

        addFile(
            Paths.get("Cargo.lock"),
            "# This file is automatically @generated by Cargo.",
            "# It is not intended for manual editing.",
            "version = 3",
            "[[package]]",
            "name = \"data-encoding\"",
            "version = \"2.8.0\"",
            "source = \"registry+https://github.com/rust-lang/crates.io-index\"",
            "checksum = \"575f75dfd25738df5b91b8e43e14d44bda14637a58fae779fd2b064f8bf3e010\"",
            "",
            "[[package]]",
            "name = \"proptest\"",
            "version = \"1.6.0\"",
            "source = \"registry+https://github.com/rust-lang/crates.io-index\"",
            "checksum = \"14cae93065090804185d3b75f0bf93b8eeda30c7a9b4a33d3bdb3988d6229e50\"",
            "",
            "[[package]]",
            "name = \"serde_test\"",
            "version = \"1.0.177\"",
            "source = \"registry+https://github.com/rust-lang/crates.io-index\"",
            "checksum = \"7f901ee573cab6b3060453d2d5f0bae4e6d628c23c0a962ff9b5f1d7c8d4f1ed\"",
            "",
            "[[package]]",
            "name = \"crate-git-revision\"",
            "version = \"0.0.6\"",
            "source = \"registry+https://github.com/rust-lang/crates.io-index\"",
            "checksum = \"c521bf1f43d31ed2f73441775ed31935d77901cb3451e44b38a1c1612fcbaf98\""
        );
    }

    @NotNull
    @Override
    public Detectable create(@NotNull DetectableEnvironment detectableEnvironment) {
        return detectableFactory.createCargoLockfileDetectable(
            detectableEnvironment,
            new CargoDetectableOptions(EnumListFilter.fromExcluded(CargoDependencyType.BUILD))
        );
    }

    @Override
    public void assertExtraction(@NotNull Extraction extraction) {
        Assertions.assertEquals(1, extraction.getCodeLocations().size());

        Assertions.assertEquals("stellar-strkey", extraction.getProjectName());
        Assertions.assertEquals("0.0.13", extraction.getProjectVersion());

        NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.CRATES, extraction.getCodeLocations().get(0).getDependencyGraph());
        graphAssert.hasRootDependency("proptest", "1.6.0");
        graphAssert.hasDependency("serde_test", "1.0.177");
        graphAssert.hasDependency("data-encoding", "2.8.0");
        graphAssert.hasNoDependency("crate-git-revision", "0.0.6");
        graphAssert.hasRootSize(3);
    }    
}
