use of com.blackducksoftware.integration.hub.service.HubServicesFactory in project hub-alert by blackducksoftware.
the class VulnerabilityCacheTest method testGetEvents.
@Test
public void testGetEvents() throws IntegrationException, URISyntaxException {
final ProjectService mockedProjectService = Mockito.mock(ProjectService.class);
final HubService mockedHubService = Mockito.mock(HubService.class);
final HubServicesFactory hubServicesFactory = Mockito.mock(HubServicesFactory.class);
final ComponentService mockedComponentService = Mockito.mock(ComponentService.class);
final VulnerabilityCache vulnerabilityCache = new VulnerabilityCache(mockedProjectService, hubServicesFactory);
Mockito.when(hubServicesFactory.createProjectService()).thenReturn(mockedProjectService);
Mockito.when(hubServicesFactory.createHubService()).thenReturn(mockedHubService);
Mockito.when(hubServicesFactory.createComponentService()).thenReturn(mockedComponentService);
final AssignedUserView assignedUser = new AssignedUserView();
assignedUser.name = "test name";
final List<AssignedUserView> assignedUsersList = Arrays.asList(assignedUser);
Mockito.when(mockedProjectService.getAssignedUsersToProject(Mockito.anyString())).thenReturn(assignedUsersList);
final VulnerabilityV2View vulnView = new VulnerabilityV2View();
vulnView.severity = "HIGH";
vulnView.name = "vulnerable";
Mockito.when(mockedComponentService.getVulnerabilitiesFromComponentVersion(Mockito.any())).thenReturn(Arrays.asList(vulnView));
final Date createdAt = new Date();
final ProjectVersionModel projectVersionModel = new ProjectVersionModel();
projectVersionModel.setProjectLink("New project link");
final String componentName = "notification test";
final ComponentVersionView componentVersionView = new ComponentVersionView();
final String componentVersionUrl = "sss";
final String componentIssueUrl = "ddd";
Mockito.when(mockedHubService.getAllResponses(componentVersionView, ComponentVersionView.VULNERABILITIES_LINK_RESPONSE)).thenReturn(Arrays.asList(vulnView));
final Map<String, Object> dataSet = new HashMap<>();
dataSet.put(NotificationEvent.DATA_SET_KEY_NOTIFICATION_CONTENT, new VulnerabilityContentItem(createdAt, projectVersionModel, componentName, componentVersionView, componentVersionUrl, Arrays.asList(), Arrays.asList(), Arrays.asList(), componentIssueUrl));
dataSet.put(VulnerabilityCache.VULNERABILITY_ID_SET, Collections.singleton("vulnerable"));
final NotificationEvent notificationEvent = new NotificationEvent("key", NotificationCategoryEnum.HIGH_VULNERABILITY, dataSet);
final Collection<NotificationEvent> emptyNotifications = vulnerabilityCache.getEvents();
assertTrue(emptyNotifications.size() == 0);
vulnerabilityCache.addEvent(notificationEvent);
final Collection<NotificationEvent> notEmptyNotifications = vulnerabilityCache.getEvents();
assertTrue(notEmptyNotifications.size() == 1);
}
use of com.blackducksoftware.integration.hub.service.HubServicesFactory in project hub-alert by blackducksoftware.
the class AccumulatorReader method read.
@Override
public NotificationResults read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
try {
logger.info("Accumulator Reader Starting Operation");
final HubServicesFactory hubServicesFactory = globalProperties.createHubServicesFactoryAndLogErrors(logger);
if (hubServicesFactory != null) {
ZonedDateTime zonedEndDate = ZonedDateTime.now();
zonedEndDate = zonedEndDate.withZoneSameInstant(ZoneOffset.UTC);
zonedEndDate = zonedEndDate.withSecond(0).withNano(0);
ZonedDateTime zonedStartDate = zonedEndDate;
final Date endDate = Date.from(zonedEndDate.toInstant());
Date startDate = Date.from(zonedStartDate.toInstant());
try {
final File lastRunFile = new File(lastRunPath);
if (lastRunFile.exists()) {
final String lastRunValue = FileUtils.readFileToString(lastRunFile, "UTF-8");
final Date startTime = RestConnection.parseDateString(lastRunValue);
zonedStartDate = ZonedDateTime.ofInstant(startTime.toInstant(), zonedEndDate.getZone());
} else {
zonedStartDate = zonedEndDate;
}
zonedStartDate = zonedStartDate.withSecond(0).withNano(0);
startDate = Date.from(zonedStartDate.toInstant());
FileUtils.write(lastRunFile, RestConnection.formatDate(endDate), "UTF-8");
} catch (final Exception e) {
logger.error("Error creating date range", e);
}
final NotificationService notificationService = hubServicesFactory.createNotificationService();
final NotificationResults notificationResults = notificationService.getAllNotificationResults(startDate, endDate);
if (notificationResults.getNotificationContentItems().isEmpty()) {
logger.debug("Read Notification Count: 0");
return null;
}
logger.debug("Read Notification Count: {}", notificationResults.getNotificationContentItems().size());
return notificationResults;
}
} catch (final Exception ex) {
logger.error("Error in Accumulator Reader", ex);
} finally {
logger.info("Accumulator Reader Finished Operation");
}
return null;
}
use of com.blackducksoftware.integration.hub.service.HubServicesFactory in project hub-alert by blackducksoftware.
the class EmailGroupChannel method sendMessage.
@Override
public void sendMessage(final EmailGroupEvent event, final EmailGroupDistributionConfigEntity emailConfigEntity) throws Exception {
if (emailConfigEntity != null) {
final String hubGroupName = emailConfigEntity.getGroupName();
final String subjectLine = emailConfigEntity.getEmailSubjectLine();
final HubServicesFactory hubServicesFactory = globalProperties.createHubServicesFactory(logger);
final List<String> emailAddresses = getEmailAddressesForGroup(hubServicesFactory, hubGroupName);
sendMessage(emailAddresses, event, subjectLine);
} else {
logger.warn("No configuration found with id {}.", event.getCommonDistributionConfigId());
}
}
use of com.blackducksoftware.integration.hub.service.HubServicesFactory in project hub-docker-inspector by blackducksoftware.
the class HubClient method uploadBdioToHub.
public void uploadBdioToHub(final File bdioFile) throws IntegrationException {
final RestConnection restConnection = createRestConnection();
final HubServicesFactory hubServicesFactory = new HubServicesFactory(restConnection);
final CodeLocationService bomImportRequestService = hubServicesFactory.createCodeLocationService();
bomImportRequestService.importBomFile(bdioFile);
logger.info(String.format("Uploaded bdio file %s to %s", bdioFile.getName(), config.getHubUrl()));
}
use of com.blackducksoftware.integration.hub.service.HubServicesFactory in project hub-alert by blackducksoftware.
the class AccumulatorReaderTest method testRead.
@Test
public void testRead() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception {
final GlobalProperties globalProperties = Mockito.mock(GlobalProperties.class);
final HubServicesFactory hubServicesFactory = Mockito.mock(HubServicesFactory.class);
final NotificationService notificationService = Mockito.mock(NotificationService.class);
final SortedSet<NotificationContentItem> notificationContentItems = new TreeSet<>();
final Date createdAt = new Date();
final ProjectVersionModel projectVersionModel = new ProjectVersionModel();
projectVersionModel.setProjectLink("New project link");
final String componentName = "notification test";
final ComponentVersionView componentVersionView = new ComponentVersionView();
final String componentVersionUrl = "sss";
final String componentIssueUrl = "ddd";
final NotificationContentItem notificationContentItem = new NotificationContentItem(createdAt, projectVersionModel, componentName, componentVersionView, componentVersionUrl, componentIssueUrl);
notificationContentItems.add(notificationContentItem);
final NotificationResults notificationResults = new NotificationResults(notificationContentItems, null);
Mockito.doReturn(hubServicesFactory).when(globalProperties).createHubServicesFactoryAndLogErrors(Mockito.any());
Mockito.doReturn(notificationService).when(hubServicesFactory).createNotificationService();
Mockito.doReturn(notificationResults).when(notificationService).getAllNotificationResults(Mockito.any(), Mockito.any());
final AccumulatorReader accumulatorReader = new AccumulatorReader(globalProperties);
final NotificationResults actualNotificationResults = accumulatorReader.read();
assertNotNull(actualNotificationResults);
}
Aggregations