use of com.synopsys.integration.blackduck.api.manual.temporary.response.PolicySummaryView in project blackduck-common by blackducksoftware.
the class ProjectBomServiceTestIT method testGetActivePoliciesForVersion.
@Test
public void testGetActivePoliciesForVersion() throws Exception {
BlackDuckServicesFactory blackDuckServicesFactory = intHttpClientTestHelper.createBlackDuckServicesFactory();
BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
ProjectService projectService = blackDuckServicesFactory.createProjectService();
ProjectBomService projectBomService = blackDuckServicesFactory.createProjectBomService();
PolicyRuleService policyRuleService = blackDuckServicesFactory.createPolicyRuleService();
IntLogger logger = blackDuckServicesFactory.getLogger();
String projectName = "get_active_policies_test";
String projectVersionName = "1.0.0";
String testPolicyName = "testPolicy";
String componentGroup = "com.synopsys.integration";
String componentName = "blackduck-common";
String componentVersion = "47.0.0";
ExternalId componentExternalId = externalIdFactory.createMavenExternalId(componentGroup, componentName, componentVersion);
// delete the project, if it exists
intHttpClientTestHelper.deleteIfProjectExists(logger, projectService, blackDuckApiClient, projectName);
// create the project
ProjectSyncModel projectSyncModel = ProjectSyncModel.createWithDefaults(projectName, projectVersionName);
ProjectVersionWrapper projectVersionWrapper = projectService.syncProjectAndVersion(projectSyncModel);
// check for presence of active-policy-rules link for project version (if not present then this is an older/incompatible BlackDuck, test should abort)
try {
projectVersionWrapper.getProjectVersionView().metaActivePolicyRulesLink();
} catch (NoSuchElementException e) {
// skip test if exception is thrown
Assume.assumeNoException(e);
}
// verify the bom
List<ProjectVersionComponentVersionView> bomComponents = projectBomService.getComponentsForProjectVersion(projectVersionWrapper.getProjectVersionView());
assertEquals(0, bomComponents.size());
projectBomService.addComponentToProjectVersion(componentExternalId, projectVersionWrapper.getProjectVersionView());
// get added component
ProjectVersionView projectVersionView = projectVersionWrapper.getProjectVersionView();
ProjectVersionComponentVersionView projectVersionComponentVersionView = blackDuckApiClient.getAllResponses(projectVersionView.metaComponentsLink()).stream().filter(component -> component.getComponentName().equals(componentName)).findFirst().orElse(null);
// find corresponding ComponentVersionView
HttpUrl projectVersionComponentUrl = new HttpUrl(projectVersionComponentVersionView.getComponentVersion());
ComponentVersionView componentVersionView = blackDuckApiClient.getResponse(projectVersionComponentUrl, ComponentVersionView.class);
// create policy rule that should be violated by that projectversion
PolicyRuleView policyRule = createTestPolicyRuleForProjectWithComponentVersion(projectVersionWrapper.getProjectView(), componentVersionView, testPolicyName);
Optional<PolicyRuleView> existingDuplicateRule = policyRuleService.getAllPolicyRules().stream().filter(rule -> rule.getName().equals(testPolicyName)).findFirst();
if (existingDuplicateRule.isPresent()) {
blackDuckApiClient.delete(existingDuplicateRule.get());
}
policyRuleService.createPolicyRule(policyRule);
// need this to give Black Duck enough time to check the project version against the policy rule
Thread.sleep(10000);
// query projectBomService to see if project version has violated rule
Optional<List<PolicySummaryView>> activePolicies = projectBomService.getActivePoliciesForVersion(projectVersionView);
Assertions.assertTrue(activePolicies.isPresent());
Assertions.assertFalse(activePolicies.get().isEmpty());
Assertions.assertTrue(activePolicies.get().stream().filter(rule -> ProjectVersionComponentPolicyStatusType.IN_VIOLATION.equals(rule.getStatus())).map(PolicySummaryView::getName).anyMatch(name -> name.equals(testPolicyName)));
}
Aggregations