Search in sources :

Example 1 with CycleDetectedException

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

the class DetectableTool method extract.

public DetectableToolResult extract() {
    // TODO: Move docker/bazel out of detectable and drop this notion of a detctable tool. Will simplify this logic and make this unneccessary.
    DetectableResult extractable;
    try {
        extractable = detectable.extractable();
    } catch (DetectableException e) {
        extractable = new ExceptionDetectableResult(e);
    }
    if (!extractable.getPassed()) {
        logger.error(String.format("Was not extractable: %s", extractable.toDescription()));
        statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", Arrays.asList(extractable.toDescription())));
        statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
        exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_GENERAL_ERROR, extractable.toDescription());
        return DetectableToolResult.failed(extractable);
    }
    logger.debug("Extractable passed.");
    ExtractionEnvironment extractionEnvironment = extractionEnvironmentProvider.createExtractionEnvironment(name);
    Extraction extraction;
    try {
        extraction = detectable.extract(extractionEnvironment);
    } catch (ExecutableFailedException | ExecutableRunnerException | JsonSyntaxException | IOException | CycleDetectedException | DetectableException | MissingExternalIdException | ParserConfigurationException | SAXException e) {
        extraction = new Extraction.Builder().exception(e).build();
    }
    if (!extraction.isSuccess()) {
        logger.error("Extraction was not success.");
        List<String> errorMessages = collectErrorMessages(extraction);
        statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", errorMessages));
        statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
        exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_GENERAL_ERROR, extraction.getDescription()));
        return DetectableToolResult.failed();
    } else {
        logger.debug("Extraction success.");
        statusEventPublisher.publishStatusSummary(new Status(name, StatusType.SUCCESS));
    }
    Map<CodeLocation, DetectCodeLocation> detectCodeLocationMap = codeLocationConverter.toDetectCodeLocation(sourcePath, extraction, sourcePath, name);
    List<DetectCodeLocation> detectCodeLocations = new ArrayList<>(detectCodeLocationMap.values());
    DockerTargetData dockerTargetData = DockerTargetData.fromExtraction(extraction);
    DetectToolProjectInfo projectInfo = null;
    if (StringUtils.isNotBlank(extraction.getProjectName()) || StringUtils.isNotBlank(extraction.getProjectVersion())) {
        NameVersion nameVersion = new NameVersion(extraction.getProjectName(), extraction.getProjectVersion());
        projectInfo = new DetectToolProjectInfo(detectTool, nameVersion);
    }
    logger.debug("Tool finished.");
    return DetectableToolResult.success(detectCodeLocations, projectInfo, dockerTargetData);
}
Also used : ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) NameVersion(com.synopsys.integration.util.NameVersion) CycleDetectedException(com.synopsys.integration.detectable.util.CycleDetectedException) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) DetectIssue(com.synopsys.integration.detect.workflow.status.DetectIssue) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Extraction(com.synopsys.integration.detectable.extraction.Extraction) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DockerTargetData(com.synopsys.integration.detect.lifecycle.run.data.DockerTargetData) Status(com.synopsys.integration.detect.workflow.status.Status) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest) IOException(java.io.IOException) MissingExternalIdException(com.synopsys.integration.bdio.graph.builder.MissingExternalIdException) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) JsonSyntaxException(com.google.gson.JsonSyntaxException) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) ExceptionDetectableResult(com.synopsys.integration.detectable.detectable.result.ExceptionDetectableResult) ExceptionDetectableResult(com.synopsys.integration.detectable.detectable.result.ExceptionDetectableResult) DetectToolProjectInfo(com.synopsys.integration.detect.workflow.project.DetectToolProjectInfo) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 2 with CycleDetectedException

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

the class ExtractionEvaluatorTest method createEvaluationMocks.

private DetectorEvaluation createEvaluationMocks(DetectorEvaluationOptions evaluationOptions, DetectorEvaluationTree detectorEvaluationTree, boolean extractionExists, boolean throwException) throws DetectableException, ExecutableFailedException, IOException, CycleDetectedException, MissingExternalIdException, ExecutableRunnerException, ParserConfigurationException, SAXException {
    ExtractionEnvironment extractionEnvironment = Mockito.mock(ExtractionEnvironment.class);
    DetectorEvaluation detectorEvaluation = Mockito.mock(DetectorEvaluation.class);
    Detectable detectable = Mockito.mock(Detectable.class);
    DetectableResult detectableExtractableResult = Mockito.mock(DetectableResult.class);
    Mockito.when(detectableExtractableResult.getPassed()).thenReturn(true);
    Mockito.when(detectableExtractableResult.toDescription()).thenReturn("test detectable");
    Mockito.when(detectable.extractable()).thenReturn(detectableExtractableResult);
    Mockito.when(detectorEvaluation.getDetectable()).thenReturn(detectable);
    Mockito.when(detectorEvaluation.getExtractionEnvironment()).thenReturn(extractionEnvironment);
    Mockito.when(detectorEvaluation.isSearchable()).thenReturn(true);
    Mockito.when(detectorEvaluation.isApplicable()).thenReturn(true);
    Mockito.when(detectorEvaluation.isExtractable()).thenReturn(true);
    List<DetectorEvaluation> detectorEvaluations = Collections.singletonList(detectorEvaluation);
    Mockito.when(detectorEvaluationTree.getOrderedEvaluations()).thenReturn(detectorEvaluations);
    DetectorRuleSet detectorRuleSet = Mockito.mock(DetectorRuleSet.class);
    Mockito.when(detectorEvaluationTree.getDetectorRuleSet()).thenReturn(detectorRuleSet);
    DetectorRule detectorRule = Mockito.mock(DetectorRule.class);
    Mockito.when(detectorRule.getDescriptiveName()).thenReturn("test rule");
    Mockito.when(detectorEvaluation.getDetectorRule()).thenReturn(detectorRule);
    Mockito.when(detectorEvaluationTree.getDepthFromRoot()).thenReturn(0);
    Mockito.when(evaluationOptions.isForceNested()).thenReturn(true);
    Predicate<DetectorRule> rulePredicate = it -> true;
    Mockito.when(evaluationOptions.getDetectorFilter()).thenReturn(rulePredicate);
    Mockito.when(detectorRule.createDetectable(Mockito.any(DetectableEnvironment.class))).thenReturn(detectable);
    Mockito.when(detectable.applicable()).thenReturn(new PassedDetectableResult());
    if (throwException) {
        Mockito.when(detectable.extract(Mockito.eq(extractionEnvironment))).thenThrow(new RuntimeException("JUnit expected exception"));
    } else {
        Mockito.when(detectable.extract(Mockito.eq(extractionEnvironment))).thenReturn(new Extraction.Builder().success().build());
    }
    return detectorEvaluation;
}
Also used : DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Detectable(com.synopsys.integration.detectable.Detectable) CycleDetectedException(com.synopsys.integration.detectable.util.CycleDetectedException) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) Predicate(java.util.function.Predicate) IOException(java.io.IOException) MissingExternalIdException(com.synopsys.integration.bdio.graph.builder.MissingExternalIdException) File(java.io.File) Test(org.junit.jupiter.api.Test) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) Mockito(org.mockito.Mockito) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Collections(java.util.Collections) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Detectable(com.synopsys.integration.detectable.Detectable) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Extraction(com.synopsys.integration.detectable.extraction.Extraction)

Aggregations

MissingExternalIdException (com.synopsys.integration.bdio.graph.builder.MissingExternalIdException)2 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)2 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)2 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)2 Extraction (com.synopsys.integration.detectable.extraction.Extraction)2 ExtractionEnvironment (com.synopsys.integration.detectable.extraction.ExtractionEnvironment)2 CycleDetectedException (com.synopsys.integration.detectable.util.CycleDetectedException)2 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)2 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXException (org.xml.sax.SAXException)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 DockerTargetData (com.synopsys.integration.detect.lifecycle.run.data.DockerTargetData)1 ExitCodeRequest (com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest)1 DetectCodeLocation (com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation)1 DetectToolProjectInfo (com.synopsys.integration.detect.workflow.project.DetectToolProjectInfo)1 DetectIssue (com.synopsys.integration.detect.workflow.status.DetectIssue)1 Status (com.synopsys.integration.detect.workflow.status.Status)1 Detectable (com.synopsys.integration.detectable.Detectable)1 DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)1