Search in sources :

Example 1 with NotificationService

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;
}
Also used : NotificationResults(com.blackducksoftware.integration.hub.notification.NotificationResults) ZonedDateTime(java.time.ZonedDateTime) HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory) NotificationService(com.blackducksoftware.integration.hub.service.NotificationService) File(java.io.File) Date(java.util.Date) NonTransientResourceException(org.springframework.batch.item.NonTransientResourceException) IOException(java.io.IOException) ParseException(org.springframework.batch.item.ParseException) UnexpectedInputException(org.springframework.batch.item.UnexpectedInputException)

Example 2 with NotificationService

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);
}
Also used : NotificationContentItem(com.blackducksoftware.integration.hub.notification.NotificationContentItem) NotificationResults(com.blackducksoftware.integration.hub.notification.NotificationResults) GlobalProperties(com.blackducksoftware.integration.hub.alert.config.GlobalProperties) TreeSet(java.util.TreeSet) HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory) NotificationService(com.blackducksoftware.integration.hub.service.NotificationService) ComponentVersionView(com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView) ProjectVersionModel(com.blackducksoftware.integration.hub.notification.ProjectVersionModel) Date(java.util.Date) Test(org.junit.Test)

Aggregations

NotificationResults (com.blackducksoftware.integration.hub.notification.NotificationResults)2 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)2 NotificationService (com.blackducksoftware.integration.hub.service.NotificationService)2 Date (java.util.Date)2 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)1 ComponentVersionView (com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView)1 NotificationContentItem (com.blackducksoftware.integration.hub.notification.NotificationContentItem)1 ProjectVersionModel (com.blackducksoftware.integration.hub.notification.ProjectVersionModel)1 File (java.io.File)1 IOException (java.io.IOException)1 ZonedDateTime (java.time.ZonedDateTime)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1 NonTransientResourceException (org.springframework.batch.item.NonTransientResourceException)1 ParseException (org.springframework.batch.item.ParseException)1 UnexpectedInputException (org.springframework.batch.item.UnexpectedInputException)1