package com.blackduck.integration.detectable.detectables.dart.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.extraction.Extraction;
import com.blackduck.integration.detectable.functional.DetectableFunctionalTest;
import com.blackduck.integration.detectable.util.graph.NameVersionGraphAssert;

public class DartPubSpecLockDetectableTest extends DetectableFunctionalTest {
    public DartPubSpecLockDetectableTest() throws IOException {
        super("dart");
    }

    @Override
    protected void setup() throws IOException {
        addFile(
            Paths.get("pubspec.lock"),
            "# Generated by pub",
            "",
            "packages:",
            "   clock:",
            "    dependency: transitive",
            "    description:",
            "      name: clock",
            "      url: \"https://pub.dartlang.org\"",
            "    source: hosted",
            "    version: \"1.1.0\"",
            "  collection:",
            "    dependency: transitive",
            "    description:",
            "      name: collection",
            "      url: \"https://pub.dartlang.org\"",
            "    source: hosted",
            "    version: \"1.15.0\"",
            "  contacts_service:",
            "    dependency: \"direct main\"",
            "    description:",
            "      name: contacts_service",
            "      url: \"https://pub.dartlang.org\"",
            "    source: hosted",
            "    version: \"0.4.6\""
        );

        addFile(Paths.get("pubspec.yaml"));
    }

    @Override
    @NotNull
    public Detectable create(@NotNull DetectableEnvironment detectableEnvironment) {
        return detectableFactory.createDartPubSpecLockDetectable(detectableEnvironment);
    }

    @Override
    public void assertExtraction(@NotNull Extraction extraction) {
        Assertions.assertNotEquals(0, extraction.getCodeLocations().size(), "A code location should have been generated.");

        NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.DART, extraction.getCodeLocations().get(0).getDependencyGraph());
        graphAssert.hasRootSize(3);
        graphAssert.hasRootDependency("clock", "1.1.0");
        graphAssert.hasRootDependency("collection", "1.15.0");
        graphAssert.hasRootDependency("contacts_service", "0.4.6");
    }
}
