Search in sources :

Example 6 with DetectorRuleSet

use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.

the class DetectorRuleSetEvaluator method evaluateSearchable.

public DetectorResult evaluateSearchable(DetectorRuleSet detectorRuleSet, DetectorRule detectorRule, SearchEnvironment environment) {
    if (!environment.getDetectorFilter().test(detectorRule)) {
        return new ExcludedDetectorResult();
    }
    int maxDepth = detectorRule.getMaxDepth();
    if (environment.getDepth() > maxDepth) {
        return new MaxDepthExceededDetectorResult(environment.getDepth(), maxDepth);
    }
    Set<DetectorRule> yieldTo = environment.getAppliedSoFar().stream().filter(it -> detectorRuleSet.getYieldsTo(detectorRule).contains(it)).collect(Collectors.toSet());
    if (yieldTo.size() > 0) {
        return new YieldedDetectorResult(yieldTo.stream().map(DetectorRule::getName).collect(Collectors.toSet()));
    }
    boolean nestable = detectorRule.isNestable();
    boolean selfNestable = detectorRule.isSelfNestable();
    boolean selfTypeNestable = detectorRule.isSelfTypeNestable();
    DetectorType detectorType = detectorRule.getDetectorType();
    Set<DetectorType> notNestableBeneath = detectorRule.getNotNestableBeneath();
    if (environment.isForceNestedSearch()) {
        return new ForcedNestedPassedDetectorResult();
    } else if (nestable) {
        if (!selfNestable && environment.getAppliedToParent().stream().anyMatch(detectorRule::equals)) {
            return new NotSelfNestableDetectorResult();
        }
        if (!selfTypeNestable && environment.getAppliedToParent().stream().map(DetectorRule::getDetectorType).anyMatch(detectorType::equals)) {
            return new NotSelfTypeNestableDetectorResult(detectorType);
        }
        if (notNestableBeneath.size() > 0) {
            Optional<DetectorType> notNestableBeneathType = environment.getAppliedToParent().stream().map(DetectorRule::getDetectorType).filter(notNestableBeneath::contains).findAny();
            if (notNestableBeneathType.isPresent()) {
                return new NotNestableBeneathDetectorResult(notNestableBeneathType.get());
            }
        }
    } else if (environment.getAppliedToParent().stream().anyMatch(it -> !it.isNestInvisible())) {
        return new NotNestableDetectorResult();
    }
    return new PassedDetectorResult();
}
Also used : MaxDepthExceededDetectorResult(com.synopsys.integration.detector.result.MaxDepthExceededDetectorResult) PassedDetectorResult(com.synopsys.integration.detector.result.PassedDetectorResult) NotSelfTypeNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult) NotSelfNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfNestableDetectorResult) Set(java.util.Set) Collectors(java.util.stream.Collectors) NotNestableBeneathDetectorResult(com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Optional(java.util.Optional) DetectorType(com.synopsys.integration.detector.base.DetectorType) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) ForcedNestedPassedDetectorResult(com.synopsys.integration.detector.result.ForcedNestedPassedDetectorResult) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) ExcludedDetectorResult(com.synopsys.integration.detector.result.ExcludedDetectorResult) NotNestableDetectorResult(com.synopsys.integration.detector.result.NotNestableDetectorResult) YieldedDetectorResult(com.synopsys.integration.detector.result.YieldedDetectorResult) Optional(java.util.Optional) YieldedDetectorResult(com.synopsys.integration.detector.result.YieldedDetectorResult) PassedDetectorResult(com.synopsys.integration.detector.result.PassedDetectorResult) ForcedNestedPassedDetectorResult(com.synopsys.integration.detector.result.ForcedNestedPassedDetectorResult) NotSelfTypeNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult) MaxDepthExceededDetectorResult(com.synopsys.integration.detector.result.MaxDepthExceededDetectorResult) DetectorType(com.synopsys.integration.detector.base.DetectorType) NotSelfNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfNestableDetectorResult) NotNestableBeneathDetectorResult(com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ForcedNestedPassedDetectorResult(com.synopsys.integration.detector.result.ForcedNestedPassedDetectorResult) NotNestableDetectorResult(com.synopsys.integration.detector.result.NotNestableDetectorResult) ExcludedDetectorResult(com.synopsys.integration.detector.result.ExcludedDetectorResult)

Example 7 with DetectorRuleSet

use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.

the class HelpJsonManager method createHelpJsonDetectorList.

private List<HelpJsonDetector> createHelpJsonDetectorList(boolean buildless) {
    DetectorRuleFactory ruleFactory = new DetectorRuleFactory();
    // TODO: Is there a better way to build a fake set of rules?
    DetectDetectableFactory mockFactory = new DetectDetectableFactory(null, null, null, null, null, null, null, null);
    DetectorRuleSet ruleSet = ruleFactory.createRules(mockFactory, buildless);
    return ruleSet.getOrderedDetectorRules().stream().map(detectorRule -> convertDetectorRule(detectorRule, ruleSet)).collect(Collectors.toList());
}
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) DetectorRuleFactory(com.synopsys.integration.detect.tool.detector.DetectorRuleFactory) DetectDetectableFactory(com.synopsys.integration.detect.tool.detector.factory.DetectDetectableFactory) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet)

Example 8 with DetectorRuleSet

use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.

the class DetectorToolTest method executeToolTest.

private DetectorToolResult executeToolTest(Extraction extraction, DetectableResult extractionResult, String projectBomTool) throws DetectableException, ExecutableFailedException {
    ExtractionEnvironmentProvider extractionEnvironmentProvider = Mockito.mock(ExtractionEnvironmentProvider.class);
    DetectorFinder detectorFinder = Mockito.mock(DetectorFinder.class);
    EventSystem eventSystem = Mockito.mock(EventSystem.class);
    CodeLocationConverter codeLocationConverter = Mockito.mock(CodeLocationConverter.class);
    DetectorIssuePublisher detectorIssuePublisher = Mockito.mock(DetectorIssuePublisher.class);
    StatusEventPublisher statusEventPublisher = Mockito.mock(StatusEventPublisher.class);
    ExitCodePublisher exitCodePublisher = Mockito.mock(ExitCodePublisher.class);
    DetectorEventPublisher detectorEventPublisher = Mockito.mock(DetectorEventPublisher.class);
    DetectorTool tool = new DetectorTool(detectorFinder, extractionEnvironmentProvider, eventSystem, codeLocationConverter, detectorIssuePublisher, statusEventPublisher, exitCodePublisher, detectorEventPublisher);
    File directory = new File(".");
    GoModCliDetectable detectable = createDetectable(extraction, extractionResult);
    DetectorRule<GoModCliDetectable> rule = createRule(detectable);
    DetectorRuleSet detectorRuleSet = createRuleSet(rule);
    DetectorFinderOptions detectorFinderOptions = createFinderOptions();
    DetectorEvaluationOptions evaluationOptions = createEvaluationOptions();
    DetectorEvaluationTree evaluationTree = createEvaluationTree(extraction, extractionResult, directory, rule, detectorRuleSet);
    Mockito.when(detectorFinder.findDetectors(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Optional.of(evaluationTree));
    return tool.performDetectors(directory, detectorRuleSet, detectorFinderOptions, evaluationOptions, projectBomTool, new ArrayList<>(), new SimpleFileFinder());
}
Also used : DetectorEvaluationOptions(com.synopsys.integration.detector.evaluation.DetectorEvaluationOptions) GoModCliDetectable(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliDetectable) DetectorFinderOptions(com.synopsys.integration.detector.finder.DetectorFinderOptions) ExtractionEnvironmentProvider(com.synopsys.integration.detect.tool.detector.extraction.ExtractionEnvironmentProvider) ExitCodePublisher(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) DetectorFinder(com.synopsys.integration.detector.finder.DetectorFinder) StatusEventPublisher(com.synopsys.integration.detect.workflow.status.StatusEventPublisher) EventSystem(com.synopsys.integration.detect.workflow.event.EventSystem) File(java.io.File)

Example 9 with DetectorRuleSet

use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.

the class DetectorFinderTest method testSymLinks.

private void testSymLinks(boolean followSymLinks) throws IOException {
    Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS);
    File initialDirectory = createDirWithSymLink("testSymLinks");
    DetectorRuleSet detectorRuleSet = new DetectorRuleSet(new ArrayList<>(0), new HashMap<>(0));
    DetectorFinderOptions options = createFinderOptions(followSymLinks);
    DetectorFinder finder = new DetectorFinder();
    Optional<DetectorEvaluationTree> tree = finder.findDetectors(initialDirectory, detectorRuleSet, options, new SimpleFileFinder());
    // make sure the symlink was omitted from results
    // final Set<DetectorEvaluationTree> subDirResults = tree.get().getChildren().iterator().next().getChildren();
    Set<DetectorEvaluationTree> testDirs = tree.get().getChildren();
    DetectorEvaluationTree symLinkTestDir = null;
    for (DetectorEvaluationTree testDir : testDirs) {
        if (testDir.getDirectory().getName().equals("testSymLinks")) {
            symLinkTestDir = testDir;
            break;
        }
    }
    Set<DetectorEvaluationTree> subDirResults = symLinkTestDir.getChildren();
    if (followSymLinks) {
        assertEquals(2, subDirResults.size());
    } else {
        assertEquals(1, subDirResults.size());
        String subDirContentsName = subDirResults.iterator().next().getDirectory().getName();
        assertEquals("regularDir", subDirContentsName);
    }
    FileUtils.deleteDirectory(initialDirectory);
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) File(java.io.File) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet)

Example 10 with DetectorRuleSet

use of com.synopsys.integration.detector.rule.DetectorRuleSet 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)

Aggregations

DetectorRuleSet (com.synopsys.integration.detector.rule.DetectorRuleSet)13 DetectorRule (com.synopsys.integration.detector.rule.DetectorRule)8 Test (org.junit.jupiter.api.Test)7 File (java.io.File)6 DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)5 SimpleFileFinder (com.synopsys.integration.common.util.finder.SimpleFileFinder)4 Detectable (com.synopsys.integration.detectable.Detectable)4 DetectorType (com.synopsys.integration.detector.base.DetectorType)4 Optional (java.util.Optional)4 Predicate (java.util.function.Predicate)4 DetectorRuleFactory (com.synopsys.integration.detect.tool.detector.DetectorRuleFactory)3 DetectorFinder (com.synopsys.integration.detector.finder.DetectorFinder)3 DetectorResult (com.synopsys.integration.detector.result.DetectorResult)3 DetectorRuleSetBuilder (com.synopsys.integration.detector.rule.DetectorRuleSetBuilder)3 List (java.util.List)3 Set (java.util.Set)3 Assertions (org.junit.jupiter.api.Assertions)3 Gson (com.google.gson.Gson)2 DetectProperties (com.synopsys.integration.detect.configuration.DetectProperties)2 ExitCodePublisher (com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher)2