Search in sources :

Example 31 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class PackageJsonExtractorTest method extractWithDevNoPeerDependencies.

@Test
void extractWithDevNoPeerDependencies() {
    PackageJson packageJson = createPackageJson();
    Extraction extraction = createExtractor(NpmDependencyType.PEER).extract(packageJson);
    assertEquals(1, extraction.getCodeLocations().size());
    CodeLocation codeLocation = extraction.getCodeLocations().get(0);
    DependencyGraph dependencyGraph = codeLocation.getDependencyGraph();
    GraphAssert graphAssert = new GraphAssert(Forge.RUBYGEMS, dependencyGraph);
    graphAssert.hasRootDependency(testDep1);
    graphAssert.hasRootDependency(testDep2);
    graphAssert.hasRootDependency(testDevDep1);
    graphAssert.hasRootDependency(testDevDep2);
    graphAssert.hasRootSize(4);
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Example 32 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class GoModCliExtractorTest method handleMultipleReplacementsForOneComponentTest.

// These tests weren't updated to use the new json format for go the first call of go list, yet somehow still passing?
// With the removal of -u, Mockito kept returning null for that first go list call instead of ExecutableOutput.
// I think this test is redundant with the existence of GoModDetectableMinusWhyTest
// Leaving it here for now to review one day. JM-01/2022
@Test
public void handleMultipleReplacementsForOneComponentTest() throws ExecutableRunnerException, ExecutableFailedException, DetectableException {
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    File directory = new File("");
    ExecutableTarget goExe = ExecutableTarget.forFile(new File(""));
    Answer<ExecutableOutput> executableAnswer = new Answer<ExecutableOutput>() {

        String[] goListArgs = { "list", "-m", "-json" };

        String[] goListJsonArgs = { "list", "-m", "-json", "all" };

        String[] goModGraphArgs = { "mod", "graph" };

        @Override
        public ExecutableOutput answer(InvocationOnMock invocation) {
            Executable executable = invocation.getArgument(0, Executable.class);
            List<String> commandLine = executable.getCommandWithArguments();
            ExecutableOutput result = null;
            if (commandLine.containsAll(Arrays.asList(goListJsonArgs))) {
                result = goListJsonOutput();
            } else if (commandLine.containsAll(Arrays.asList(goListArgs))) {
                result = goListOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModGraphArgs))) {
                result = goModGraphOutput();
            } else {
                result = new ExecutableOutput(0, "", "");
            }
            return result;
        }
    };
    GoModCliExtractor goModCliExtractor = buildGoModCliExtractor(executableRunner, executableAnswer);
    boolean wasSuccessful = true;
    Extraction extraction = goModCliExtractor.extract(directory, goExe);
    if (extraction.getError() instanceof ArrayIndexOutOfBoundsException) {
        wasSuccessful = false;
    }
    Assertions.assertTrue(wasSuccessful);
}
Also used : Answer(org.mockito.stubbing.Answer) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Test(org.junit.jupiter.api.Test)

Example 33 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class GoModCliExtractorTest method handleGoModWhyExceptionTest.

@Test
public void handleGoModWhyExceptionTest() throws ExecutableRunnerException, ExecutableFailedException, DetectableException {
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    File directory = new File("");
    ExecutableTarget goExe = ExecutableTarget.forFile(new File(""));
    Answer<ExecutableOutput> executableAnswer = new Answer<ExecutableOutput>() {

        String[] goListArgs = { "list", "-m", "-json" };

        String[] goListJsonArgs = { "list", "-m", "-json", "all" };

        String[] goModGraphArgs = { "mod", "graph" };

        String[] goModWhyArgs = { "mod", "why", "-m", "all" };

        @Override
        public ExecutableOutput answer(InvocationOnMock invocation) throws Throwable {
            Executable executable = invocation.getArgument(0, Executable.class);
            List<String> commandLine = executable.getCommandWithArguments();
            ExecutableOutput result = null;
            if (commandLine.containsAll(Arrays.asList(goListJsonArgs))) {
                result = goListJsonOutput();
            } else if (commandLine.containsAll(Arrays.asList(goListArgs))) {
                result = goListOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModGraphArgs))) {
                result = goModGraphOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModWhyArgs))) {
                throw new ExecutableRunnerException(new DetectableException("Unit Test Go Mod Why error"));
            } else {
                result = new ExecutableOutput(0, "", "");
            }
            return result;
        }
    };
    GoModCliExtractor goModCliExtractor = buildGoModCliExtractor(executableRunner, executableAnswer);
    boolean wasSuccessful = true;
    Extraction extraction = goModCliExtractor.extract(directory, goExe);
    if (extraction.getError() instanceof ArrayIndexOutOfBoundsException) {
        wasSuccessful = false;
    }
    Assertions.assertTrue(wasSuccessful);
}
Also used : ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Answer(org.mockito.stubbing.Answer) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Test(org.junit.jupiter.api.Test)

Example 34 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class DockerExtractorTest method testExtractTarReturningContainerFileSystem.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractTarReturningContainerFileSystem() throws ExecutableRunnerException, IOException {
    String image = null;
    String imageId = null;
    String tar = fakeDockerTarFile.getAbsolutePath();
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, fakeContainerFileSystemFile, null, fakeResultsFile, executableRunner);
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get().endsWith("testDockerTarfile.tar"));
    assertTrue(extraction.getMetaData(DockerExtractor.CONTAINER_FILESYSTEM_META_DATA).get().getName().endsWith("_containerfilesystem.tar.gz"));
    ArgumentCaptor<Executable> executableArgumentCaptor = ArgumentCaptor.forClass(Executable.class);
    Mockito.verify(executableRunner).execute(executableArgumentCaptor.capture());
    Executable executableToVerify = executableArgumentCaptor.getValue();
    List<String> command = executableToVerify.getCommandWithArguments();
    assertTrue(command.get(0).endsWith("/fake/test/java"));
    assertEquals("-jar", command.get(1));
    assertTrue(command.get(2).endsWith("/fake/test/dockerinspector.jar"));
    assertTrue(command.get(3).startsWith("--spring.config.location="));
    assertTrue(command.get(3).endsWith("/application.properties"));
    assertTrue(command.get(4).startsWith("--docker.tar="));
    assertTrue(command.get(4).endsWith("testDockerTarfile.tar"));
}
Also used : DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test)

Example 35 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class DockerExtractorTest method testExtractTarReturningOriginalTar.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractTarReturningOriginalTar() throws ExecutableRunnerException, IOException {
    String image = null;
    String imageId = null;
    String tar = fakeDockerTarFile.getAbsolutePath();
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, null, null, fakeResultsFile, executableRunner);
    // No returned .tar.gz: scan given docker tar instead
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get().endsWith("testDockerTarfile.tar"));
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_TAR_META_DATA).get().getName().endsWith("testDockerTarfile.tar"));
    ArgumentCaptor<Executable> executableArgumentCaptor = ArgumentCaptor.forClass(Executable.class);
    Mockito.verify(executableRunner).execute(executableArgumentCaptor.capture());
    Executable executableToVerify = executableArgumentCaptor.getValue();
    List<String> command = executableToVerify.getCommandWithArguments();
    assertTrue(command.get(0).endsWith("/fake/test/java"));
    assertEquals("-jar", command.get(1));
    assertTrue(command.get(2).endsWith("/fake/test/dockerinspector.jar"));
    assertTrue(command.get(3).startsWith("--spring.config.location="));
    assertTrue(command.get(3).endsWith("/application.properties"));
    assertTrue(command.get(4).startsWith("--docker.tar="));
    assertTrue(command.get(4).endsWith("testDockerTarfile.tar"));
}
Also used : DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test)

Aggregations

Extraction (com.synopsys.integration.detectable.extraction.Extraction)63 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)38 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)23 IOException (java.io.IOException)19 Test (org.junit.jupiter.api.Test)18 File (java.io.File)17 ArrayList (java.util.ArrayList)14 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)13 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)12 NameVersion (com.synopsys.integration.util.NameVersion)12 Executable (com.synopsys.integration.executable.Executable)11 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)9 List (java.util.List)9 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)8 HashMap (java.util.HashMap)8 Optional (java.util.Optional)8 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)7 Collectors (java.util.stream.Collectors)7 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)6 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)6