-
-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4381 from nscuro/backport-pr-4374
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/org/dependencytrack/tasks/PolicyEvaluationTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.dependencytrack.tasks; | ||
|
||
import alpine.persistence.PaginatedResult; | ||
import org.dependencytrack.PersistenceCapableTest; | ||
import org.dependencytrack.event.PolicyEvaluationEvent; | ||
import org.dependencytrack.model.Component; | ||
import org.dependencytrack.model.Policy; | ||
import org.dependencytrack.model.PolicyCondition; | ||
import org.dependencytrack.model.Project; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class PolicyEvaluationTaskTest extends PersistenceCapableTest { | ||
|
||
@Test | ||
public void testPolicyEvaluationForSingleComponent() { | ||
Project project = new Project(); | ||
project.setName("my-project"); | ||
project.setGroup("com.example"); | ||
project.setVersion("1.0.0"); | ||
qm.createProject(project, Collections.emptyList(), false); | ||
|
||
Component component = new Component(); | ||
component.setGroup("com.example"); | ||
component.setName("my-component"); | ||
component.setVersion("1.0.0"); | ||
component.setPurl("pkg:maven/com.example/[email protected]"); | ||
component.setProject(project); | ||
qm.createComponent(component, false); | ||
|
||
// a policy that identifies the upper component and thus should be violated | ||
Policy policy = qm.createPolicy("my-policy", Policy.Operator.ALL, Policy.ViolationState.FAIL); | ||
qm.createPolicyCondition(policy, PolicyCondition.Subject.PACKAGE_URL, PolicyCondition.Operator.MATCHES, "pkg:maven/com.example/[email protected]"); | ||
|
||
PolicyEvaluationTask task = new PolicyEvaluationTask(); | ||
task.inform(new PolicyEvaluationEvent(component).project(project)); | ||
|
||
PaginatedResult policyViolations = qm.getPolicyViolations(project, false); | ||
assertThat(policyViolations.getTotal()).isEqualTo(1); | ||
} | ||
|
||
} |