Search in sources :

Example 1 with PolicyRuleService

use of com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService in project hub-alert by blackducksoftware.

the class BlackDuckProviderService method triggerBlackDuckPolicyNotification.

public void triggerBlackDuckPolicyNotification(String policyName, Supplier<ExternalId> externalIdSupplier) throws IntegrationException {
    PolicyRuleView policyRuleView = createBlackDuckPolicyRuleView(policyName, externalIdSupplier);
    deleteExistingBlackDuckPolicy(policyRuleView);
    PolicyRuleService policyRuleService = blackDuckServicesFactory.createPolicyRuleService();
    intLogger.info(String.format("Creating policy with the name: %s", policyRuleView.getName()));
    policyRuleService.createPolicyRule(policyRuleView);
}
Also used : PolicyRuleService(com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)

Example 2 with PolicyRuleService

use of com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService in project hub-alert by blackducksoftware.

the class BlackDuckProviderService method deleteExistingBlackDuckPolicy.

public void deleteExistingBlackDuckPolicy(PolicyRuleView policyRuleView) throws IntegrationException {
    setupBlackDuckServicesFactory();
    PolicyRuleService policyRuleService = blackDuckServicesFactory.createPolicyRuleService();
    policyRuleService.getPolicyRuleViewByName(policyRuleView.getName());
    Optional<PolicyRuleView> policyRuleViewOptional = policyRuleService.getPolicyRuleViewByName(policyRuleView.getName());
    if (policyRuleViewOptional.isPresent()) {
        PolicyRuleView notificationPolicy = policyRuleViewOptional.get();
        intLogger.info(String.format("Policy: %s already exists. Deleting the existing policy.", notificationPolicy.getName()));
        BlackDuckApiClient blackDuckService = blackDuckServicesFactory.getBlackDuckApiClient();
        blackDuckService.delete(notificationPolicy);
    }
}
Also used : PolicyRuleService(com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)

Example 3 with PolicyRuleService

use of com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService 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)

Example 4 with PolicyRuleService

use of com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService in project blackduck-alert by blackducksoftware.

the class BlackDuckProviderService method triggerBlackDuckPolicyNotification.

public void triggerBlackDuckPolicyNotification(String policyName, Supplier<ExternalId> externalIdSupplier) throws IntegrationException {
    PolicyRuleView policyRuleView = createBlackDuckPolicyRuleView(policyName, externalIdSupplier);
    deleteExistingBlackDuckPolicy(policyRuleView);
    PolicyRuleService policyRuleService = blackDuckServicesFactory.createPolicyRuleService();
    intLogger.info(String.format("Creating policy with the name: %s", policyRuleView.getName()));
    policyRuleService.createPolicyRule(policyRuleView);
}
Also used : PolicyRuleService(com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)

Example 5 with PolicyRuleService

use of com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService in project blackduck-alert by blackducksoftware.

the class BlackDuckProviderService method deleteExistingBlackDuckPolicy.

public void deleteExistingBlackDuckPolicy(PolicyRuleView policyRuleView) throws IntegrationException {
    setupBlackDuckServicesFactory();
    PolicyRuleService policyRuleService = blackDuckServicesFactory.createPolicyRuleService();
    policyRuleService.getPolicyRuleViewByName(policyRuleView.getName());
    Optional<PolicyRuleView> policyRuleViewOptional = policyRuleService.getPolicyRuleViewByName(policyRuleView.getName());
    if (policyRuleViewOptional.isPresent()) {
        PolicyRuleView notificationPolicy = policyRuleViewOptional.get();
        intLogger.info(String.format("Policy: %s already exists. Deleting the existing policy.", notificationPolicy.getName()));
        BlackDuckApiClient blackDuckService = blackDuckServicesFactory.getBlackDuckApiClient();
        blackDuckService.delete(notificationPolicy);
    }
}
Also used : PolicyRuleService(com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)

Aggregations

PolicyRuleView (com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)5 PolicyRuleService (com.synopsys.integration.blackduck.service.dataservice.PolicyRuleService)5 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)3 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 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 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)1 ProjectBomService (com.synopsys.integration.blackduck.service.dataservice.ProjectBomService)1 ProjectService (com.synopsys.integration.blackduck.service.dataservice.ProjectService)1