Search in sources :

Example 1 with NameOptionalVersion

use of com.synopsys.integration.detectable.util.NameOptionalVersion in project synopsys-detect by blackducksoftware.

the class CargoLockPackageDataTransformer method transform.

public CargoLockPackage transform(CargoLockPackageData cargoLockPackageData) {
    String packageName = cargoLockPackageData.getName().orElse("");
    String packageVersion = cargoLockPackageData.getVersion().orElse(null);
    NameVersion nameOptionalVersion = new NameVersion(packageName, packageVersion);
    List<NameOptionalVersion> dependencies = cargoLockPackageData.getDependencies().map((List<String> dependencyLines) -> dependencyLines.stream().map(cargoDependencyLineParser::parseDependencyName).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList())).orElse(Collections.emptyList());
    return new CargoLockPackage(nameOptionalVersion, dependencies);
}
Also used : NameOptionalVersion(com.synopsys.integration.detectable.util.NameOptionalVersion) NameVersion(com.synopsys.integration.util.NameVersion) Optional(java.util.Optional) CargoLockPackage(com.synopsys.integration.detectable.detectables.cargo.model.CargoLockPackage)

Example 2 with NameOptionalVersion

use of com.synopsys.integration.detectable.util.NameOptionalVersion in project synopsys-detect by blackducksoftware.

the class CargoLockPackageTransformerTest method testCorrectNumberOfRootDependencies.

@Test
public void testCorrectNumberOfRootDependencies() throws DetectableException, MissingExternalIdException {
    List<CargoLockPackage> input = new ArrayList<>();
    input.add(createPackage("test1", "1.0.0", new NameOptionalVersion("dep1"), new NameOptionalVersion("dep2")));
    input.add(createPackage("dep1", "0.5.0"));
    input.add(createPackage("dep2", "0.6.0"));
    CargoLockPackageTransformer cargoLockPackageTransformer = new CargoLockPackageTransformer();
    DependencyGraph graph = cargoLockPackageTransformer.transformToGraph(input);
    NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.CRATES, graph);
    graphAssert.hasRootDependency("test1", "1.0.0");
    graphAssert.hasRootDependency("dep1", "0.5.0");
    graphAssert.hasRootDependency("dep2", "0.6.0");
    graphAssert.hasRootSize(3);
}
Also used : NameOptionalVersion(com.synopsys.integration.detectable.util.NameOptionalVersion) ArrayList(java.util.ArrayList) NameVersionGraphAssert(com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) CargoLockPackage(com.synopsys.integration.detectable.detectables.cargo.model.CargoLockPackage) Test(org.junit.jupiter.api.Test)

Example 3 with NameOptionalVersion

use of com.synopsys.integration.detectable.util.NameOptionalVersion in project synopsys-detect by blackducksoftware.

the class CargoDependencyLineParser method parseDependencyName.

public Optional<NameOptionalVersion> parseDependencyName(String dependencyLine) {
    String[] dependencyLinePieces = dependencyLine.split(" ");
    if (dependencyLinePieces.length == 0 || StringUtils.isEmpty(dependencyLine)) {
        logger.warn("Failed to parse dependency line. It will be ignored: {}", dependencyLine);
        return Optional.empty();
    }
    String name = StringUtils.trim(dependencyLinePieces[0]);
    if (dependencyLinePieces.length >= 2) {
        String version = StringUtils.trimToNull(dependencyLinePieces[1]);
        return Optional.of(new NameOptionalVersion(name, version));
    } else {
        return Optional.of(new NameOptionalVersion(name));
    }
}
Also used : NameOptionalVersion(com.synopsys.integration.detectable.util.NameOptionalVersion)

Example 4 with NameOptionalVersion

use of com.synopsys.integration.detectable.util.NameOptionalVersion in project synopsys-detect by blackducksoftware.

the class CargoLockPackageDataTransformerTest method transform.

@Test
void transform() {
    CargoDependencyLineParser cargoDependencyLineParser = new CargoDependencyLineParser();
    CargoLockPackageDataTransformer transformer = new CargoLockPackageDataTransformer(cargoDependencyLineParser);
    List<String> dependencies = Arrays.asList("dep1", "dep2 2.0.0", "dep3 3.0.0 (registry+https://some-registry-url");
    CargoLockPackageData cargoLockPackageData = new CargoLockPackageData("some-name", "some-version", null, null, dependencies);
    CargoLockPackage cargoLockPackage = transformer.transform(cargoLockPackageData);
    assertEquals(new NameVersion("some-name", "some-version"), cargoLockPackage.getPackageNameVersion());
    assertEquals(3, cargoLockPackage.getDependencies().size());
    NameOptionalVersion dep1Actual = cargoLockPackage.getDependencies().get(0);
    assertEquals("dep1", dep1Actual.getName());
    assertFalse(dep1Actual.getVersion().isPresent());
    assertPackageNameVersion("dep2", "2.0.0", cargoLockPackage.getDependencies().get(1));
    assertPackageNameVersion("dep3", "3.0.0", cargoLockPackage.getDependencies().get(2));
}
Also used : NameOptionalVersion(com.synopsys.integration.detectable.util.NameOptionalVersion) NameVersion(com.synopsys.integration.util.NameVersion) CargoLockPackageData(com.synopsys.integration.detectable.detectables.cargo.data.CargoLockPackageData) CargoDependencyLineParser(com.synopsys.integration.detectable.detectables.cargo.parse.CargoDependencyLineParser) CargoLockPackage(com.synopsys.integration.detectable.detectables.cargo.model.CargoLockPackage) Test(org.junit.jupiter.api.Test)

Aggregations

NameOptionalVersion (com.synopsys.integration.detectable.util.NameOptionalVersion)4 CargoLockPackage (com.synopsys.integration.detectable.detectables.cargo.model.CargoLockPackage)3 NameVersion (com.synopsys.integration.util.NameVersion)2 Test (org.junit.jupiter.api.Test)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)1 CargoLockPackageData (com.synopsys.integration.detectable.detectables.cargo.data.CargoLockPackageData)1 CargoDependencyLineParser (com.synopsys.integration.detectable.detectables.cargo.parse.CargoDependencyLineParser)1 NameVersionGraphAssert (com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1