Search in sources :

Example 6 with DetectorRule

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

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

use of com.synopsys.integration.detector.rule.DetectorRule 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 9 with DetectorRule

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

the class DetectorRuleSetEvaluatorTest method nestableExcept.

@Test
public void nestableExcept() {
    DetectorRuleSetBuilder ruleSet = new DetectorRuleSetBuilder();
    DetectorRule xcode = ruleSet.addDetector(DetectorType.XCODE, "Xcode", XcodeProjectDetectable.class, (e) -> null).defaults().build();
    DetectorRule swift = ruleSet.addDetector(DetectorType.SWIFT, "Swift", SwiftCliDetectable.class, (e) -> null).defaults().nestableExceptTo(DetectorType.XCODE).build();
    // XCODE applied at depth 0, we are now scanning a folder at depth 2.
    Set<DetectorRule> appliedToParent = Sets.newHashSet(xcode);
    Set<DetectorRule> appliedSoFar = Sets.newHashSet();
    SearchEnvironment searchEnvironment = new SearchEnvironment(2, (d) -> true, false, false, appliedToParent, appliedSoFar);
    DetectorResult result = new DetectorRuleSetEvaluator().evaluateSearchable(ruleSet.build(), swift, searchEnvironment);
    Assertions.assertEquals(NotNestableBeneathDetectorResult.class, result.getClass());
}
Also used : NotSelfTypeNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult) Predicate(java.util.function.Predicate) Set(java.util.Set) SwiftCliDetectable(com.synopsys.integration.detectable.detectables.swift.cli.SwiftCliDetectable) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) XcodeWorkspaceDetectable(com.synopsys.integration.detectable.detectables.xcode.XcodeWorkspaceDetectable) NotNestableBeneathDetectorResult(com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult) Sets(com.github.jsonldjava.shaded.com.google.common.collect.Sets) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) XcodeProjectDetectable(com.synopsys.integration.detectable.detectables.xcode.XcodeProjectDetectable) DetectorType(com.synopsys.integration.detector.base.DetectorType) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorRuleSetBuilder(com.synopsys.integration.detector.rule.DetectorRuleSetBuilder) SwiftCliDetectable(com.synopsys.integration.detectable.detectables.swift.cli.SwiftCliDetectable) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) NotSelfTypeNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult) NotNestableBeneathDetectorResult(com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorRuleSetBuilder(com.synopsys.integration.detector.rule.DetectorRuleSetBuilder) Test(org.junit.jupiter.api.Test)

Example 10 with DetectorRule

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

the class DetectorRuleSetEvaluatorTest method nestableExceptByDetectorType.

@Test
public void nestableExceptByDetectorType() {
    DetectorRuleSetBuilder ruleSet = new DetectorRuleSetBuilder();
    DetectorRule workspaceRule = ruleSet.addDetector(DetectorType.XCODE, "Xcode Workspace", XcodeWorkspaceDetectable.class, (e) -> null).defaults().notSelfTypeNestable().build();
    DetectorRule projectRule = ruleSet.addDetector(DetectorType.XCODE, "Xcode Project", XcodeProjectDetectable.class, (e) -> null).defaults().notSelfTypeNestable().build();
    // XCODE applied at depth 0, we are now scanning a folder at depth 2.
    Set<DetectorRule> appliedToParent = Sets.newHashSet(workspaceRule);
    Set<DetectorRule> appliedSoFar = Sets.newHashSet();
    SearchEnvironment searchEnvironment = new SearchEnvironment(2, (d) -> true, false, false, appliedToParent, appliedSoFar);
    DetectorResult result = new DetectorRuleSetEvaluator().evaluateSearchable(ruleSet.build(), projectRule, searchEnvironment);
    Assertions.assertEquals(NotSelfTypeNestableDetectorResult.class, result.getClass());
}
Also used : XcodeWorkspaceDetectable(com.synopsys.integration.detectable.detectables.xcode.XcodeWorkspaceDetectable) NotSelfTypeNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult) Predicate(java.util.function.Predicate) Set(java.util.Set) SwiftCliDetectable(com.synopsys.integration.detectable.detectables.swift.cli.SwiftCliDetectable) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) XcodeWorkspaceDetectable(com.synopsys.integration.detectable.detectables.xcode.XcodeWorkspaceDetectable) NotNestableBeneathDetectorResult(com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult) Sets(com.github.jsonldjava.shaded.com.google.common.collect.Sets) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) XcodeProjectDetectable(com.synopsys.integration.detectable.detectables.xcode.XcodeProjectDetectable) DetectorType(com.synopsys.integration.detector.base.DetectorType) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorRuleSetBuilder(com.synopsys.integration.detector.rule.DetectorRuleSetBuilder) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) NotSelfTypeNestableDetectorResult(com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult) NotNestableBeneathDetectorResult(com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorRuleSetBuilder(com.synopsys.integration.detector.rule.DetectorRuleSetBuilder) XcodeProjectDetectable(com.synopsys.integration.detectable.detectables.xcode.XcodeProjectDetectable) Test(org.junit.jupiter.api.Test)

Aggregations

DetectorRule (com.synopsys.integration.detector.rule.DetectorRule)14 DetectorRuleSet (com.synopsys.integration.detector.rule.DetectorRuleSet)10 Test (org.junit.jupiter.api.Test)9 DetectorResult (com.synopsys.integration.detector.result.DetectorResult)7 Detectable (com.synopsys.integration.detectable.Detectable)6 DetectorEvaluation (com.synopsys.integration.detector.base.DetectorEvaluation)6 DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)6 DetectorType (com.synopsys.integration.detector.base.DetectorType)6 DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)5 DetectorRuleSetBuilder (com.synopsys.integration.detector.rule.DetectorRuleSetBuilder)5 HashSet (java.util.HashSet)5 Predicate (java.util.function.Predicate)5 Assertions (org.junit.jupiter.api.Assertions)5 Mockito (org.mockito.Mockito)5 NotNestableBeneathDetectorResult (com.synopsys.integration.detector.result.NotNestableBeneathDetectorResult)4 NotSelfTypeNestableDetectorResult (com.synopsys.integration.detector.result.NotSelfTypeNestableDetectorResult)4 List (java.util.List)4 Set (java.util.Set)4 Sets (com.github.jsonldjava.shaded.com.google.common.collect.Sets)3 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)3