use of com.synopsys.integration.blackduck.service.BlackDuckServicesFactory in project hub-alert by blackducksoftware.
the class BlackDuckProjectSyncTaskTest method testRun.
@Test
public void testRun() throws Exception {
MockProviderDataAccessor providerDataAccessor = new MockProviderDataAccessor();
ApiDiscovery apiDiscovery = new ApiDiscovery(new HttpUrl("https://someblackduckserver"));
BlackDuckApiClient blackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
ProjectUsersService projectUsersService = Mockito.mock(ProjectUsersService.class);
BlackDuckServicesFactory blackDuckServicesFactory = Mockito.mock(BlackDuckServicesFactory.class);
Mockito.when(blackDuckServicesFactory.getBlackDuckApiClient()).thenReturn(blackDuckApiClient);
Mockito.when(blackDuckServicesFactory.createProjectUsersService()).thenReturn(projectUsersService);
Mockito.when(blackDuckServicesFactory.getApiDiscovery()).thenReturn(apiDiscovery);
Long providerConfigId = 1000L;
BlackDuckProperties blackDuckProperties = Mockito.mock(BlackDuckProperties.class);
Mockito.when(blackDuckProperties.getConfigId()).thenReturn(providerConfigId);
Mockito.when(blackDuckProperties.createBlackDuckHttpClientAndLogErrors(Mockito.any())).thenReturn(Optional.of(Mockito.mock(BlackDuckHttpClient.class)));
Mockito.when(blackDuckProperties.createBlackDuckServicesFactory(Mockito.any(), Mockito.any())).thenReturn(blackDuckServicesFactory);
ProjectView projectView = createProjectView("project", "description1", "https://projectUrl1");
ProjectView projectView2 = createProjectView("project2", "description2", "https://projectUrl2");
ProjectView projectView3 = createProjectView("project3", "description3", "https://projectUrl3");
Mockito.when(blackDuckApiClient.getAllResponses(Mockito.eq(apiDiscovery.metaProjectsLink()))).thenReturn(List.of(projectView, projectView2, projectView3));
Mockito.doReturn(null).when(blackDuckApiClient).getResponse(Mockito.any(BlackDuckRequest.class));
String email1 = "user1@email.com";
String email2 = "user2@email.com";
String email3 = "user3@email.com";
String email4 = "user4@email.com";
UserView user1 = createUserView(email1, true);
UserView user2 = createUserView(email2, true);
UserView user3 = createUserView(email3, true);
UserView user4 = createUserView(email4, true);
Mockito.when(blackDuckApiClient.getAllResponses(Mockito.eq(apiDiscovery.metaUsersLink()))).thenReturn(List.of(user1, user2, user3, user4));
Mockito.when(projectUsersService.getAllActiveUsersForProject(ArgumentMatchers.same(projectView))).thenReturn(new HashSet<>(List.of(user2, user4)));
Mockito.when(projectUsersService.getAllActiveUsersForProject(ArgumentMatchers.same(projectView2))).thenReturn(new HashSet<>(List.of(user3)));
Mockito.when(projectUsersService.getAllActiveUsersForProject(ArgumentMatchers.same(projectView3))).thenReturn(new HashSet<>(List.of(user1, user2, user3)));
Mockito.doNothing().when(projectUsersService).addUserToProject(Mockito.any(), Mockito.any(UserView.class));
BlackDuckDataSyncTask projectSyncTask = new BlackDuckDataSyncTask(new BlackDuckProviderKey(), null, providerDataAccessor, blackDuckProperties);
projectSyncTask.run();
assertEquals(3, providerDataAccessor.getProjectsByProviderConfigId(providerConfigId).size());
Mockito.when(blackDuckApiClient.getAllResponses(Mockito.eq(apiDiscovery.metaProjectsLink()))).thenReturn(List.of(projectView, projectView2));
Mockito.when(projectUsersService.getAllActiveUsersForProject(ArgumentMatchers.same(projectView))).thenReturn(new HashSet<>(List.of(user2, user4)));
Mockito.when(projectUsersService.getAllActiveUsersForProject(ArgumentMatchers.same(projectView2))).thenReturn(new HashSet<>(List.of(user3)));
Mockito.when(blackDuckApiClient.getAllResponses(ArgumentMatchers.same(projectView2.metaUsersLink()))).thenReturn(List.of());
projectSyncTask = new BlackDuckDataSyncTask(new BlackDuckProviderKey(), null, providerDataAccessor, blackDuckProperties);
projectSyncTask.run();
assertEquals(2, providerDataAccessor.getProjectsByProviderConfigId(providerConfigId).size());
}
use of com.synopsys.integration.blackduck.service.BlackDuckServicesFactory in project hub-alert by blackducksoftware.
the class ProjectNotificationMessageExtractorTest method extractTest.
@Test
public void extractTest() throws IntegrationException {
NotificationExtractorBlackDuckServicesFactoryCache servicesFactoryCache = Mockito.mock(NotificationExtractorBlackDuckServicesFactoryCache.class);
BlackDuckServicesFactory blackDuckServicesFactory = Mockito.mock(BlackDuckServicesFactory.class);
BlackDuckHttpClient blackDuckHttpClient = Mockito.mock(BlackDuckHttpClient.class);
Mockito.when(blackDuckHttpClient.getBlackDuckUrl()).thenReturn(new HttpUrl("https://a.blackduck.server.example.com"));
Mockito.when(blackDuckServicesFactory.getBlackDuckHttpClient()).thenReturn(blackDuckHttpClient);
Mockito.when(servicesFactoryCache.retrieveBlackDuckServicesFactory(Mockito.anyLong())).thenReturn(blackDuckServicesFactory);
ProjectNotificationContent projectNotificationContent = createProjectNotificationContent();
NotificationContentWrapper notificationContentWrapper = createNotificationContentWrapper(projectNotificationContent);
ProjectNotificationMessageExtractor extractor = new ProjectNotificationMessageExtractor(providerKey, servicesFactoryCache);
ProviderMessageHolder providerMessageHolder = extractor.extract(notificationContentWrapper, projectNotificationContent);
assertEquals(1, providerMessageHolder.getProjectMessages().size());
assertEquals(0, providerMessageHolder.getSimpleMessages().size());
ProjectMessage projectMessage = providerMessageHolder.getProjectMessages().get(0);
assertEquals(MessageReason.PROJECT_STATUS, projectMessage.getMessageReason());
assertTrue(projectMessage.getOperation().isPresent());
assertEquals(ProjectOperation.CREATE, projectMessage.getOperation().get());
assertTrue(projectMessage.getBomComponents().isEmpty());
assertEquals(PROJECT, projectMessage.getProject().getValue());
assertTrue(projectMessage.getProject().getUrl().isPresent());
assertEquals(PROJECT_URL, projectMessage.getProject().getUrl().get());
assertFalse(projectMessage.getProjectVersion().isPresent());
}
use of com.synopsys.integration.blackduck.service.BlackDuckServicesFactory in project hub-alert by blackducksoftware.
the class NotificationExtractorBlackDuckServicesFactoryCacheTest method retrieveBlackDuckServicesFactoryTest.
@Test
public void retrieveBlackDuckServicesFactoryTest() throws AlertConfigurationException {
ConfigurationModel configurationModel = createConfigurationModel();
Mockito.when(configurationModelConfigurationAccessor.getConfigurationById(Mockito.eq(blackDuckConfigId))).thenReturn(Optional.of(configurationModel));
ProxyInfo proxyInfo = ProxyInfo.NO_PROXY_INFO;
Mockito.when(proxyManager.createProxyInfoForHost(Mockito.any())).thenReturn(proxyInfo);
// Create a BlackDuckServiceFactory and verify the same object is cached and returned.
BlackDuckServicesFactory blackDuckServicesFactory = cache.retrieveBlackDuckServicesFactory(blackDuckConfigId);
BlackDuckServicesFactory blackDuckServicesFactoryCached = cache.retrieveBlackDuckServicesFactory(blackDuckConfigId);
assertSame(blackDuckServicesFactory, blackDuckServicesFactoryCached, "Object reference of cached BlackDuckServiceFactory does not match. Verify the first factory is getting cached correctly.");
}
use of com.synopsys.integration.blackduck.service.BlackDuckServicesFactory in project hub-alert by blackducksoftware.
the class NotificationExtractorBlackDuckServicesFactoryCacheTest method cacheClearedTest.
@Test
public void cacheClearedTest() throws AlertConfigurationException {
ConfigurationModel configurationModel = createConfigurationModel();
Mockito.when(configurationModelConfigurationAccessor.getConfigurationById(Mockito.eq(blackDuckConfigId))).thenReturn(Optional.of(configurationModel));
ProxyInfo proxyInfo = ProxyInfo.NO_PROXY_INFO;
Mockito.when(proxyManager.createProxyInfoForHost(Mockito.any())).thenReturn(proxyInfo);
BlackDuckServicesFactory blackDuckServicesFactory = cache.retrieveBlackDuckServicesFactory(blackDuckConfigId);
cache.clear();
BlackDuckServicesFactory blackDuckServicesFactoryCached = cache.retrieveBlackDuckServicesFactory(blackDuckConfigId);
assertNotSame(blackDuckServicesFactory, blackDuckServicesFactoryCached, "Object reference of cached BlackDuckServiceFactory matches. Verify that the first factory is getting cleared correctly.");
}
use of com.synopsys.integration.blackduck.service.BlackDuckServicesFactory in project hub-alert by blackducksoftware.
the class BlackDuckNotificationRetrieverFactory method createBlackDuckNotificationRetriever.
public Optional<BlackDuckNotificationRetriever> createBlackDuckNotificationRetriever(BlackDuckProperties blackDuckProperties) {
Optional<BlackDuckHttpClient> optionalBlackDuckHttpClient = blackDuckProperties.createBlackDuckHttpClientAndLogErrors(logger);
if (optionalBlackDuckHttpClient.isPresent()) {
BlackDuckHttpClient blackDuckHttpClient = optionalBlackDuckHttpClient.get();
BlackDuckServicesFactory blackDuckServicesFactory = blackDuckProperties.createBlackDuckServicesFactory(blackDuckHttpClient, new Slf4jIntLogger(logger));
BlackDuckNotificationRetriever notificationRetriever = new BlackDuckNotificationRetriever(blackDuckServicesFactory.getBlackDuckApiClient(), blackDuckServicesFactory.getApiDiscovery());
return Optional.of(notificationRetriever);
} else {
logger.warn("The BlackDuck configuration '{}' could not be used to retrieve notifications", blackDuckProperties.getConfigName());
}
return Optional.empty();
}
Aggregations