Search in sources :

Example 1 with Detectable

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

the class ApplicableEvaluatorTest method createEvaluationMocks.

private DetectorEvaluation createEvaluationMocks(DetectorEvaluationOptions evaluationOptions, DetectorEvaluationTree detectorEvaluationTree, boolean alreadyApplicable, boolean searchable) {
    DetectorEvaluation detectorEvaluation = Mockito.mock(DetectorEvaluation.class);
    List<DetectorEvaluation> detectorEvaluations = Collections.singletonList(detectorEvaluation);
    Mockito.when(detectorEvaluationTree.getOrderedEvaluations()).thenReturn(detectorEvaluations);
    DetectorRule detectorRule = Mockito.mock(DetectorRule.class);
    Mockito.when(detectorRule.getDescriptiveName()).thenReturn("test rule");
    Mockito.when(detectorEvaluation.getDetectorRule()).thenReturn(detectorRule);
    Mockito.when(detectorEvaluation.isApplicable()).thenReturn(alreadyApplicable);
    Mockito.when(detectorEvaluation.isSearchable()).thenReturn(searchable);
    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(detectorEvaluation.isSearchable()).thenReturn(true);
    Detectable detectable = Mockito.mock(Detectable.class);
    Mockito.when(detectorRule.createDetectable(Mockito.any(DetectableEnvironment.class))).thenReturn(detectable);
    Mockito.when(detectable.applicable()).thenReturn(new FailedDetectableResult());
    return detectorEvaluation;
}
Also used : Predicate(java.util.function.Predicate) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) 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) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Detectable(com.synopsys.integration.detectable.Detectable) FailedDetectableResult(com.synopsys.integration.detectable.detectable.result.FailedDetectableResult) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Detectable(com.synopsys.integration.detectable.Detectable) FailedDetectableResult(com.synopsys.integration.detectable.detectable.result.FailedDetectableResult) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment)

Example 2 with Detectable

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

the class ExtractableEvaluatorTest method createEvaluationMocks.

private DetectorEvaluation createEvaluationMocks(DetectorEvaluationOptions evaluationOptions, DetectorEvaluationTree detectorEvaluationTree, boolean extractable, boolean throwException) throws DetectableException {
    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);
    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(detectorEvaluation.isSearchable()).thenReturn(true);
    Mockito.when(detectorEvaluation.isApplicable()).thenReturn(true);
    Mockito.when(detectorEvaluation.isExtractable()).thenReturn(extractable);
    Mockito.when(detectorRule.createDetectable(Mockito.any(DetectableEnvironment.class))).thenReturn(detectable);
    Mockito.when(detectable.applicable()).thenReturn(new PassedDetectableResult());
    if (throwException) {
        Mockito.when(detectable.extractable()).thenThrow(new DetectableException("JUnit Expected Exception."));
    } else {
        Mockito.when(detectable.extractable()).thenReturn(detectableExtractableResult);
    }
    return detectorEvaluation;
}
Also used : DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) Predicate(java.util.function.Predicate) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) Function(java.util.function.Function) 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) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Detectable(com.synopsys.integration.detectable.Detectable) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) Collections(java.util.Collections) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Detectable(com.synopsys.integration.detectable.Detectable) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet)

Example 3 with Detectable

use of com.synopsys.integration.detectable.Detectable 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)

Example 4 with Detectable

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

the class HelpJsonManager method convertDetectorRule.

private HelpJsonDetector convertDetectorRule(DetectorRule rule, DetectorRuleSet ruleSet) {
    HelpJsonDetector helpData = new HelpJsonDetector();
    helpData.setDetectorName(rule.getName());
    helpData.setDetectorDescriptiveName(rule.getDescriptiveName());
    helpData.setDetectorType(rule.getDetectorType().toString());
    helpData.setMaxDepth(rule.getMaxDepth());
    helpData.setNestable(rule.isNestable());
    helpData.setNestInvisible(rule.isNestInvisible());
    helpData.setYieldsTo(ruleSet.getYieldsTo(rule).stream().map(DetectorRule::getDescriptiveName).collect(Collectors.toList()));
    // Attempt to create the detectable.
    // Not currently possible. Need a full DetectableConfiguration to be able to make Detectables.
    Class<Detectable> detectableClass = rule.getDetectableClass();
    Optional<DetectableInfo> infoSearch = Arrays.stream(detectableClass.getAnnotations()).filter(annotation -> annotation instanceof DetectableInfo).map(annotation -> (DetectableInfo) annotation).findFirst();
    if (infoSearch.isPresent()) {
        DetectableInfo info = infoSearch.get();
        helpData.setDetectableLanguage(info.language());
        helpData.setDetectableRequirementsMarkdown(info.requirementsMarkdown());
        helpData.setDetectableForge(info.forge());
    }
    return helpData;
}
Also used : Arrays(java.util.Arrays) List(java.util.List) DetectDetectableFactory(com.synopsys.integration.detect.tool.detector.factory.DetectDetectableFactory) DetectableInfo(com.synopsys.integration.detectable.detectable.annotation.DetectableInfo) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Gson(com.google.gson.Gson) Detectable(com.synopsys.integration.detectable.Detectable) DetectorRuleFactory(com.synopsys.integration.detect.tool.detector.DetectorRuleFactory) Optional(java.util.Optional) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) Collectors(java.util.stream.Collectors) DetectProperties(com.synopsys.integration.detect.configuration.DetectProperties) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Detectable(com.synopsys.integration.detectable.Detectable) DetectableInfo(com.synopsys.integration.detectable.detectable.annotation.DetectableInfo)

Example 5 with Detectable

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

the class ExtractionEvaluator method extractionEvaluation.

public void extractionEvaluation(DetectorEvaluationTree detectorEvaluationTree) {
    logger.trace("Extracting detectors in the directory: {}", detectorEvaluationTree.getDirectory());
    for (DetectorEvaluation detectorEvaluation : detectorEvaluationTree.getOrderedEvaluations()) {
        if (detectorEvaluation.isExtractable() && detectorEvaluation.getExtractionEnvironment() != null) {
            logger.trace("Detector was searchable, applicable and extractable, will perform extraction: {}", detectorEvaluation.getDetectorRule().getDescriptiveName());
            Detectable detectable = detectorEvaluation.getDetectable();
            getDetectorEvaluatorListener().ifPresent(it -> it.extractionStarted(detectorEvaluation));
            try {
                Extraction extraction = detectable.extract(detectorEvaluation.getExtractionEnvironment());
                detectorEvaluation.setExtraction(extraction);
            } catch (Exception e) {
                detectorEvaluation.setExtraction(new Extraction.Builder().exception(e).build());
            }
            getDetectorEvaluatorListener().ifPresent(it -> it.extractionEnded(detectorEvaluation));
            logger.trace("Extraction result: {}", detectorEvaluation.wasExtractionSuccessful());
        }
    }
    for (DetectorEvaluationTree childDetectorEvaluationTree : detectorEvaluationTree.getChildren()) {
        extractionEvaluation(childDetectorEvaluationTree);
    }
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) Detectable(com.synopsys.integration.detectable.Detectable) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation)

Aggregations

Detectable (com.synopsys.integration.detectable.Detectable)7 DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)5 DetectorEvaluation (com.synopsys.integration.detector.base.DetectorEvaluation)5 DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)5 DetectorRule (com.synopsys.integration.detector.rule.DetectorRule)5 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)4 List (java.util.List)4 Test (org.junit.jupiter.api.Test)4 Extraction (com.synopsys.integration.detectable.extraction.Extraction)3 ExtractionEnvironment (com.synopsys.integration.detectable.extraction.ExtractionEnvironment)3 DetectorRuleSet (com.synopsys.integration.detector.rule.DetectorRuleSet)3 File (java.io.File)3 Collections (java.util.Collections)3 Predicate (java.util.function.Predicate)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Mockito (org.mockito.Mockito)3 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)2 PassedDetectableResult (com.synopsys.integration.detectable.detectable.result.PassedDetectableResult)2 DetectorResult (com.synopsys.integration.detector.result.DetectorResult)2 Gson (com.google.gson.Gson)1