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");
}
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());
}
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;
}
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
}
}
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);
}
Aggregations