use of com.synopsys.integration.detector.rule.DetectorRule 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;
}
use of com.synopsys.integration.detector.rule.DetectorRule 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;
}
use of com.synopsys.integration.detector.rule.DetectorRule 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;
}
use of com.synopsys.integration.detector.rule.DetectorRule in project synopsys-detect by blackducksoftware.
the class ApplicableEvaluator method searchAndApplicableEvaluation.
public void searchAndApplicableEvaluation(DetectorEvaluationTree detectorEvaluationTree, Set<DetectorRule> appliedInParent) {
logger.trace("Determining applicable detectors on the directory: {}", detectorEvaluationTree.getDirectory());
Set<DetectorRule> appliedSoFar = new HashSet<>();
for (DetectorEvaluation detectorEvaluation : detectorEvaluationTree.getOrderedEvaluations()) {
getDetectorEvaluatorListener().ifPresent(it -> it.applicableStarted(detectorEvaluation));
DetectorRule detectorRule = detectorEvaluation.getDetectorRule();
logger.trace("Evaluating detector: {}", detectorRule.getDescriptiveName());
SearchEnvironment searchEnvironment = new SearchEnvironment(detectorEvaluationTree.getDepthFromRoot(), getEvaluationOptions().getDetectorFilter(), getEvaluationOptions().isForceNested(), getEvaluationOptions().isFollowSymLinks(), appliedInParent, appliedSoFar);
detectorEvaluation.setSearchEnvironment(searchEnvironment);
DetectorResult searchableResult = detectorRuleSetEvaluator.evaluateSearchable(detectorEvaluationTree.getDetectorRuleSet(), detectorEvaluation.getDetectorRule(), searchEnvironment);
detectorEvaluation.setSearchable(searchableResult);
if (detectorEvaluation.isSearchable()) {
logger.trace("Searchable passed, will continue evaluating.");
// TODO: potential todo, this could be invoked as part of the rule - ie we make a DetectableEnvironmentCreatable and the file could be given to the creatable (detectorRule.createEnvironment(file)
DetectableEnvironment detectableEnvironment = new DetectableEnvironment(detectorEvaluationTree.getDirectory());
detectorEvaluation.setDetectableEnvironment(detectableEnvironment);
Detectable detectable = detectorRule.createDetectable(detectableEnvironment);
detectorEvaluation.setDetectable(detectable);
DetectableResult applicable = detectable.applicable();
DetectorResult applicableResult = new DetectorResult(applicable.getPassed(), applicable.toDescription(), applicable.getClass(), applicable.getExplanation(), applicable.getRelevantFiles());
detectorEvaluation.setApplicable(applicableResult);
if (detectorEvaluation.isApplicable()) {
logger.trace("Found applicable detector: {}", detectorRule.getDescriptiveName());
appliedSoFar.add(detectorRule);
} else {
logger.trace("Applicable did not pass: {}", detectorEvaluation.getApplicabilityMessage());
}
} else {
logger.trace("Searchable did not pass: {}", detectorEvaluation.getSearchabilityMessage());
}
getDetectorEvaluatorListener().ifPresent(it -> it.applicableEnded(detectorEvaluation));
}
if (!appliedSoFar.isEmpty()) {
// TODO: Perfect log level also matters here. To little and we may appear stuck, but we may also be flooding the logs.
logger.debug("Found ({}) applicable detectors in: {}", appliedSoFar.size(), detectorEvaluationTree.getDirectory());
}
Set<DetectorRule> nextAppliedInParent = new HashSet<>();
nextAppliedInParent.addAll(appliedInParent);
nextAppliedInParent.addAll(appliedSoFar);
for (DetectorEvaluationTree childDetectorEvaluationTree : detectorEvaluationTree.getChildren()) {
searchAndApplicableEvaluation(childDetectorEvaluationTree, nextAppliedInParent);
}
}
Aggregations