Search in sources :

Example 41 with BlackDuckApiClient

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

the class BlackDuckComponentPolicyDetailsCreatorTest method toComponentPolicyOverriddenTest.

@Test
public void toComponentPolicyOverriddenTest() throws IntegrationException {
    PolicyRuleExpressionExpressionsView expression = new PolicyRuleExpressionExpressionsView();
    expression.setName(EXAMPLE_VULNERABILITY_EXPRESSION);
    BlackDuckApiClient blackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
    PolicyRuleExpressionView policyRuleExpression = new PolicyRuleExpressionView();
    policyRuleExpression.setExpressions(List.of(expression));
    BlackDuckComponentPolicyDetailsCreator policyDetailsCreator = new BlackDuckComponentPolicyDetailsCreator(POLICY_SEVERITY_CONVERTER, blackDuckApiClient);
    ComponentPolicyRulesView componentPolicyRulesView = new ComponentPolicyRulesView();
    componentPolicyRulesView.setName("override-test-policy");
    componentPolicyRulesView.setSeverity(PolicyRuleSeverityType.TRIVIAL);
    componentPolicyRulesView.setPolicyApprovalStatus(ProjectVersionComponentPolicyStatusType.IN_VIOLATION_OVERRIDDEN);
    ResourceMetadata meta = new ResourceMetadata();
    meta.setHref(new HttpUrl("https://someUrl"));
    componentPolicyRulesView.setMeta(meta);
    PolicyRuleView policyRuleView = new PolicyRuleView();
    policyRuleView.setName(componentPolicyRulesView.getName());
    policyRuleView.setCategory(PolicyRuleCategoryType.UNCATEGORIZED);
    Mockito.when(blackDuckApiClient.getResponse(Mockito.any(), Mockito.any())).thenReturn(policyRuleView);
    ComponentPolicy componentPolicy = policyDetailsCreator.toComponentPolicy(componentPolicyRulesView);
    assertTrue(componentPolicy.isOverridden(), "Expected the policy to be overridden");
}
Also used : PolicyRuleExpressionView(com.synopsys.integration.blackduck.api.generated.component.PolicyRuleExpressionView) BlackDuckComponentPolicyDetailsCreator(com.synopsys.integration.alert.provider.blackduck.processor.message.service.policy.BlackDuckComponentPolicyDetailsCreator) ComponentPolicyRulesView(com.synopsys.integration.blackduck.api.generated.view.ComponentPolicyRulesView) ComponentPolicy(com.synopsys.integration.alert.processor.api.extract.model.project.ComponentPolicy) PolicyRuleExpressionExpressionsView(com.synopsys.integration.blackduck.api.generated.component.PolicyRuleExpressionExpressionsView) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) PolicyRuleView(com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView) ResourceMetadata(com.synopsys.integration.blackduck.api.core.ResourceMetadata) HttpUrl(com.synopsys.integration.rest.HttpUrl) Test(org.junit.jupiter.api.Test)

Example 42 with BlackDuckApiClient

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

the class BlackDuckMessageComponentVersionUpgradeGuidanceServiceTest method requestUpgradeGuidanceItemsBomTest.

@Test
public void requestUpgradeGuidanceItemsBomTest() throws IntegrationException {
    HttpUrl httpUrl = new HttpUrl("https://fake-url");
    UrlSingleResponse<ComponentVersionUpgradeGuidanceView> expectedUrl = new UrlSingleResponse<>(httpUrl, ComponentVersionUpgradeGuidanceView.class);
    ComponentVersionUpgradeGuidanceView upgradeGuidanceView = createUpgradeGuidance(true);
    BlackDuckApiClient blackDuckApiClient = createBlackDuckApiClient(expectedUrl, upgradeGuidanceView);
    LinkSingleResponse<ComponentVersionUpgradeGuidanceView> upgradeGuidanceLink = new LinkSingleResponse<>("upgrade-guidance", ComponentVersionUpgradeGuidanceView.class);
    ProjectVersionComponentVersionView bomComponent = createBomComponent(upgradeGuidanceLink, expectedUrl);
    BlackDuckMessageComponentVersionUpgradeGuidanceService upgradeGuidanceService = new BlackDuckMessageComponentVersionUpgradeGuidanceService(blackDuckApiClient);
    ComponentUpgradeGuidance componentUpgradeGuidance = upgradeGuidanceService.requestUpgradeGuidanceItems(bomComponent);
    assertTrue(componentUpgradeGuidance.getLongTermUpgradeGuidance().isPresent());
    assertTrue(componentUpgradeGuidance.getShortTermUpgradeGuidance().isPresent());
}
Also used : UrlSingleResponse(com.synopsys.integration.blackduck.api.core.response.UrlSingleResponse) ComponentUpgradeGuidance(com.synopsys.integration.alert.processor.api.extract.model.project.ComponentUpgradeGuidance) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) LinkSingleResponse(com.synopsys.integration.blackduck.api.core.response.LinkSingleResponse) HttpUrl(com.synopsys.integration.rest.HttpUrl) ComponentVersionUpgradeGuidanceView(com.synopsys.integration.blackduck.api.generated.response.ComponentVersionUpgradeGuidanceView) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView) Test(org.junit.jupiter.api.Test)

Example 43 with BlackDuckApiClient

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

the class BlackDuckApiTokenValidator method hasPermittedRole.

private boolean hasPermittedRole(BlackDuckServicesFactory blackDuckServicesFactory) {
    BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
    ApiDiscovery apiDiscovery = blackDuckServicesFactory.getApiDiscovery();
    UserView currentUser;
    try {
        currentUser = blackDuckApiClient.getResponse(apiDiscovery.metaCurrentUserLink());
    } catch (IntegrationException integrationException) {
        logger.error("Failed to GET the currently authenticated Black Duck user", integrationException);
        return false;
    }
    try {
        List<RoleAssignmentView> allRolesForCurrentUser = blackDuckApiClient.getAllResponses(currentUser.metaRolesLink());
        return allRolesForCurrentUser.stream().anyMatch(this::isPermittedRole);
    } catch (IntegrationException integrationException) {
        logger.error("Failed to GET the currently authenticated Black Duck user's roles", integrationException);
    }
    return false;
}
Also used : IntegrationException(com.synopsys.integration.exception.IntegrationException) RoleAssignmentView(com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView)

Example 44 with BlackDuckApiClient

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

the class BlackDuckSSOConfigRetrieverTest method retrieveExceptionTest.

@Test
public void retrieveExceptionTest() throws IntegrationException {
    HttpUrl baseUrl = new HttpUrl("https://a-blackduck-server");
    ApiDiscovery apiDiscovery = new ApiDiscovery(baseUrl);
    BlackDuckApiClient blackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
    Mockito.when(blackDuckApiClient.getResponse(Mockito.any(BlackDuckRequest.class))).thenThrow(new AlertException());
    BlackDuckSSOConfigRetriever ssoConfigRetriever = new BlackDuckSSOConfigRetriever(apiDiscovery, blackDuckApiClient);
    try {
        ssoConfigRetriever.retrieve();
        fail(String.format("Expected an %s to be thrown", AlertException.class.getSimpleName()));
    } catch (AlertException e) {
    // Pass
    }
}
Also used : ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckRequest(com.synopsys.integration.blackduck.service.request.BlackDuckRequest) HttpUrl(com.synopsys.integration.rest.HttpUrl) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Example 45 with BlackDuckApiClient

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

the class NotificationContentProcessorTest method createRuleViolationNotificationMessageExtractor.

private RuleViolationNotificationMessageExtractor createRuleViolationNotificationMessageExtractor() throws IntegrationException {
    BlackDuckProviderKey providerKey = new BlackDuckProviderKey();
    NotificationExtractorBlackDuckServicesFactoryCache servicesFactoryCache = Mockito.mock(NotificationExtractorBlackDuckServicesFactoryCache.class);
    BlackDuckPolicySeverityConverter blackDuckPolicySeverityConverter = new BlackDuckPolicySeverityConverter();
    BlackDuckPolicyComponentConcernCreator blackDuckPolicyComponentConcernCreator = new BlackDuckPolicyComponentConcernCreator(blackDuckPolicySeverityConverter);
    BlackDuckComponentVulnerabilityDetailsCreator vulnerabilityDetailsCreator = new BlackDuckComponentVulnerabilityDetailsCreator();
    BlackDuckComponentPolicyDetailsCreatorFactory blackDuckComponentPolicyDetailsCreatorFactory = new BlackDuckComponentPolicyDetailsCreatorFactory(blackDuckPolicySeverityConverter);
    BlackDuckMessageBomComponentDetailsCreatorFactory detailsCreatorFactory = new BlackDuckMessageBomComponentDetailsCreatorFactory(vulnerabilityDetailsCreator, blackDuckComponentPolicyDetailsCreatorFactory);
    // Mocks for AbstractRuleViolationNotificationMessageExtractor
    BlackDuckServicesFactory blackDuckServicesFactory = Mockito.mock(BlackDuckServicesFactory.class);
    BlackDuckHttpClient blackDuckHttpClient = Mockito.mock(BlackDuckHttpClient.class);
    Mockito.when(servicesFactoryCache.retrieveBlackDuckServicesFactory(Mockito.any())).thenReturn(blackDuckServicesFactory);
    Mockito.when(blackDuckServicesFactory.getBlackDuckHttpClient()).thenReturn(blackDuckHttpClient);
    Mockito.when(blackDuckHttpClient.getBlackDuckUrl()).thenReturn(new HttpUrl(PROVIDER_DETAILS.getProvider().getUrl().get()));
    BlackDuckApiClient blackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
    Mockito.when(blackDuckServicesFactory.getBlackDuckApiClient()).thenReturn(blackDuckApiClient);
    ProjectVersionComponentVersionView projectVersionComponentVersionView = blackDuckResponseTestUtility.createProjectVersionComponentVersionView();
    Mockito.when(blackDuckApiClient.getResponse(Mockito.any(), Mockito.eq(ProjectVersionComponentVersionView.class))).thenReturn(projectVersionComponentVersionView);
    BomComponent404Handler bomComponent404Handler = new BomComponent404Handler();
    return new RuleViolationNotificationMessageExtractor(providerKey, servicesFactoryCache, blackDuckPolicyComponentConcernCreator, detailsCreatorFactory, bomComponent404Handler);
}
Also used : BlackDuckProviderKey(com.synopsys.integration.alert.descriptor.api.BlackDuckProviderKey) BlackDuckPolicyComponentConcernCreator(com.synopsys.integration.alert.provider.blackduck.processor.message.service.policy.BlackDuckPolicyComponentConcernCreator) BlackDuckComponentPolicyDetailsCreatorFactory(com.synopsys.integration.alert.provider.blackduck.processor.message.service.policy.BlackDuckComponentPolicyDetailsCreatorFactory) BomComponent404Handler(com.synopsys.integration.alert.provider.blackduck.processor.message.service.BomComponent404Handler) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) NotificationExtractorBlackDuckServicesFactoryCache(com.synopsys.integration.alert.provider.blackduck.processor.NotificationExtractorBlackDuckServicesFactoryCache) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) HttpUrl(com.synopsys.integration.rest.HttpUrl) BlackDuckMessageBomComponentDetailsCreatorFactory(com.synopsys.integration.alert.provider.blackduck.processor.message.service.BlackDuckMessageBomComponentDetailsCreatorFactory) BlackDuckComponentVulnerabilityDetailsCreator(com.synopsys.integration.alert.provider.blackduck.processor.message.service.BlackDuckComponentVulnerabilityDetailsCreator) BlackDuckPolicySeverityConverter(com.synopsys.integration.alert.provider.blackduck.processor.message.service.policy.BlackDuckPolicySeverityConverter) RuleViolationNotificationMessageExtractor(com.synopsys.integration.alert.provider.blackduck.processor.message.RuleViolationNotificationMessageExtractor) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView)

Aggregations

BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)47 HttpUrl (com.synopsys.integration.rest.HttpUrl)36 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)28 Test (org.junit.jupiter.api.Test)24 ProjectVersionComponentVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView)21 BomComponentDetails (com.synopsys.integration.alert.processor.api.extract.model.project.BomComponentDetails)19 ComponentUpgradeGuidance (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentUpgradeGuidance)14 IntegrationRestException (com.synopsys.integration.rest.exception.IntegrationRestException)13 IntegrationException (com.synopsys.integration.exception.IntegrationException)10 ApiDiscovery (com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery)9 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)9 ResourceMetadata (com.synopsys.integration.blackduck.api.core.ResourceMetadata)7 UrlSingleResponse (com.synopsys.integration.blackduck.api.core.response.UrlSingleResponse)7 PolicyRuleView (com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)7 ComponentPolicy (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentPolicy)6 ComponentVersionUpgradeGuidanceView (com.synopsys.integration.blackduck.api.generated.response.ComponentVersionUpgradeGuidanceView)6 ComponentPolicyRulesView (com.synopsys.integration.blackduck.api.generated.view.ComponentPolicyRulesView)6 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)5 BlackDuckMessageBomComponentDetailsCreator (com.synopsys.integration.alert.provider.blackduck.processor.message.service.BlackDuckMessageBomComponentDetailsCreator)5 ProjectVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView)5