Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to set unresolved license for license policy #2214

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@ public PolicyCondition.Subject supportedSubject() {
public List<PolicyConditionViolation> evaluate(final Policy policy, final Component component) {
final List<PolicyConditionViolation> violations = new ArrayList<>();
final License license = component.getResolvedLicense();
if (license == null) {
return violations;
}

for (final PolicyCondition condition: super.extractSupportedConditions(policy)) {
LOGGER.debug("Evaluating component (" + component.getUuid() + ") against policy condition (" + condition.getUuid() + ")");
final License l = qm.getObjectByUuid(License.class, condition.getValue());
if (l != null && PolicyCondition.Operator.IS == condition.getOperator()) {
if (component.getResolvedLicense().getId() == l.getId()) {
if (condition.getValue().equals("unresolved")) {
if (license == null && PolicyCondition.Operator.IS == condition.getOperator()) {
violations.add(new PolicyConditionViolation(condition, component));
}
} else if (l != null && PolicyCondition.Operator.IS_NOT == condition.getOperator()) {
if (component.getResolvedLicense().getId() != l.getId()) {
} else if (license != null && PolicyCondition.Operator.IS_NOT == condition.getOperator()) {
violations.add(new PolicyConditionViolation(condition, component));
}
} else if (license != null) {
final License l = qm.getObjectByUuid(License.class, condition.getValue());
if (l != null && PolicyCondition.Operator.IS == condition.getOperator()) {
if (component.getResolvedLicense().getId() == l.getId()) {
violations.add(new PolicyConditionViolation(condition, component));
}
} else if (l != null && PolicyCondition.Operator.IS_NOT == condition.getOperator()) {
if (component.getResolvedLicense().getId() != l.getId()) {
violations.add(new PolicyConditionViolation(condition, component));
}
}
}
}
return violations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,28 @@ public void wrongOperator() {
Assert.assertEquals(0, violations.size());
}

@Test
public void valueIsUnresolved() {
License license = new License();
license.setName("Apache 2.0");
license.setLicenseId("Apache-2.0");
license.setUuid(UUID.randomUUID());
license = qm.persist(license);

Policy policy = qm.createPolicy("Test Policy", Policy.Operator.ANY, Policy.ViolationState.INFO);
qm.createPolicyCondition(policy, PolicyCondition.Subject.LICENSE, PolicyCondition.Operator.IS, "unresolved");

Component componentWithLicense = new Component();
componentWithLicense.setResolvedLicense(license);

Component componentWithoutLicense = new Component();

PolicyEvaluator evaluator = new LicensePolicyEvaluator();
List<PolicyConditionViolation> violations = evaluator.evaluate(policy, componentWithLicense);
Assert.assertEquals(0, violations.size());

violations = evaluator.evaluate(policy, componentWithoutLicense);
Assert.assertEquals(1, violations.size());
}

}