use of com.blackducksoftware.integration.hub.service.NotificationService 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.NotificationService 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