Search in sources :

Example 1 with PolicySummaryView

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)));
}
Also used : PolicyRuleExpressionSetBuilder(com.synopsys.integration.blackduck.service.model.PolicyRuleExpressionSetBuilder) PolicyRuleExpressionView(com.synopsys.integration.blackduck.api.generated.component.PolicyRuleExpressionView) ProjectBomService(com.synopsys.integration.blackduck.service.dataservice.ProjectBomService) PolicySummaryView(com.synopsys.integration.blackduck.api.manual.temporary.response.PolicySummaryView) IntLogger(com.synopsys.integration.log.IntLogger) HttpUrl(com.synopsys.integration.rest.HttpUrl) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProjectService(com.synopsys.integration.blackduck.service.dataservice.ProjectService) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) Assume(org.junit.Assume) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Tag(org.junit.jupiter.api.Tag) NoSuchElementException(java.util.NoSuchElementException) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) PolicyRuleCategoryType(com.synopsys.integration.blackduck.api.generated.enumeration.PolicyRuleCategoryType) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView) PolicyRuleConditionOperatorType(com.synopsys.integration.blackduck.api.enumeration.PolicyRuleConditionOperatorType) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) PolicyRuleService(com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService) ProjectVersionWrapper(com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper) ComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ComponentVersionView) ProjectSyncModel(com.synopsys.integration.blackduck.service.model.ProjectSyncModel) Test(org.junit.jupiter.api.Test) List(java.util.List) TimingExtension(com.synopsys.integration.blackduck.TimingExtension) Assertions(org.junit.jupiter.api.Assertions) IntHttpClientTestHelper(com.synopsys.integration.blackduck.http.client.IntHttpClientTestHelper) Optional(java.util.Optional) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) ProjectVersionComponentPolicyStatusType(com.synopsys.integration.blackduck.api.generated.enumeration.ProjectVersionComponentPolicyStatusType) PolicyRuleService(com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService) ProjectSyncModel(com.synopsys.integration.blackduck.service.model.ProjectSyncModel) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) ProjectService(com.synopsys.integration.blackduck.service.dataservice.ProjectService) PolicySummaryView(com.synopsys.integration.blackduck.api.manual.temporary.response.PolicySummaryView) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) IntLogger(com.synopsys.integration.log.IntLogger) ProjectBomService(com.synopsys.integration.blackduck.service.dataservice.ProjectBomService) HttpUrl(com.synopsys.integration.rest.HttpUrl) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView) ComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ComponentVersionView) List(java.util.List) ProjectVersionWrapper(com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper) NoSuchElementException(java.util.NoSuchElementException) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView) Test(org.junit.jupiter.api.Test)

Aggregations

ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)1 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)1 TimingExtension (com.synopsys.integration.blackduck.TimingExtension)1 PolicyRuleConditionOperatorType (com.synopsys.integration.blackduck.api.enumeration.PolicyRuleConditionOperatorType)1 PolicyRuleExpressionView (com.synopsys.integration.blackduck.api.generated.component.PolicyRuleExpressionView)1 PolicyRuleCategoryType (com.synopsys.integration.blackduck.api.generated.enumeration.PolicyRuleCategoryType)1 ProjectVersionComponentPolicyStatusType (com.synopsys.integration.blackduck.api.generated.enumeration.ProjectVersionComponentPolicyStatusType)1 ComponentVersionView (com.synopsys.integration.blackduck.api.generated.view.ComponentVersionView)1 PolicyRuleView (com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)1 ProjectVersionComponentVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView)1 ProjectVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView)1 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)1 PolicySummaryView (com.synopsys.integration.blackduck.api.manual.temporary.response.PolicySummaryView)1 BlackDuckIntegrationException (com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException)1 IntHttpClientTestHelper (com.synopsys.integration.blackduck.http.client.IntHttpClientTestHelper)1 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)1 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)1 PolicyRuleService (com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService)1 ProjectBomService (com.synopsys.integration.blackduck.service.dataservice.ProjectBomService)1 ProjectService (com.synopsys.integration.blackduck.service.dataservice.ProjectService)1