Search in sources :

Example 1 with NotificationContentItem

use of com.blackducksoftware.integration.hub.notification.NotificationContentItem in project hub-alert by blackducksoftware.

the class VulnerabilityCacheTest method testRemoveEvent.

@Test
public void testRemoveEvent() {
    final ProjectService mockedProjectService = Mockito.mock(ProjectService.class);
    final HubService mockedHubService = Mockito.mock(HubService.class);
    final ComponentService mockedComponentService = Mockito.mock(ComponentService.class);
    final HubServicesFactory hubServicesFactory = Mockito.mock(HubServicesFactory.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 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 Map<String, Object> dataSet = new HashMap<>();
    dataSet.put(NotificationEvent.DATA_SET_KEY_NOTIFICATION_CONTENT, new NotificationContentItem(createdAt, projectVersionModel, componentName, componentVersionView, componentVersionUrl, componentIssueUrl));
    dataSet.put(VulnerabilityCache.VULNERABILITY_ID_SET, new HashSet<String>());
    final NotificationEvent notificationEvent = new NotificationEvent("key", NotificationCategoryEnum.HIGH_VULNERABILITY, dataSet);
    assumeTrue(vulnerabilityCache.getEventMap().size() == 0);
    vulnerabilityCache.removeEvent(notificationEvent);
    assumeTrue(vulnerabilityCache.getEventMap().size() == 1);
    vulnerabilityCache.removeEvent(notificationEvent);
    assumeTrue(vulnerabilityCache.getEventMap().size() == 0);
}
Also used : HashMap(java.util.HashMap) ProjectService(com.blackducksoftware.integration.hub.service.ProjectService) HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory) NotificationEvent(com.blackducksoftware.integration.hub.notification.NotificationEvent) ProjectVersionModel(com.blackducksoftware.integration.hub.notification.ProjectVersionModel) Date(java.util.Date) NotificationContentItem(com.blackducksoftware.integration.hub.notification.NotificationContentItem) ComponentVersionView(com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView) ComponentService(com.blackducksoftware.integration.hub.service.ComponentService) HubService(com.blackducksoftware.integration.hub.service.HubService) Test(org.junit.Test)

Example 2 with NotificationContentItem

use of com.blackducksoftware.integration.hub.notification.NotificationContentItem in project hub-alert by blackducksoftware.

the class AccumulatorWriter method write.

@Override
public void write(final List<? extends DBStoreEvent> itemList) throws Exception {
    try {
        if (itemList != null && !itemList.isEmpty()) {
            logger.info("Writing {} notifications", itemList.size());
            itemList.forEach(item -> {
                final List<NotificationEvent> notificationList = item.getNotificationList();
                final List<NotificationModel> entityList = new ArrayList<>();
                notificationList.forEach(notification -> {
                    final String eventKey = notification.getEventKey();
                    final NotificationContentItem content = (NotificationContentItem) notification.getDataSet().get(NotificationEvent.DATA_SET_KEY_NOTIFICATION_CONTENT);
                    final Date createdAt = content.getCreatedAt();
                    final NotificationCategoryEnum notificationType = notification.getCategoryType();
                    final String projectName = content.getProjectVersion().getProjectName();
                    final String projectUrl = content.getProjectVersion().getProjectLink();
                    final String projectVersion = content.getProjectVersion().getProjectVersionName();
                    final String projectVersionUrl = content.getProjectVersion().getUrl();
                    final String componentName = content.getComponentName();
                    final String componentVersion = content.getComponentVersion().versionName;
                    final String policyRuleName = getPolicyRule(notification);
                    final String person = getPerson(notification);
                    final NotificationEntity entity = new NotificationEntity(eventKey, createdAt, notificationType, projectName, projectUrl, projectVersion, projectVersionUrl, componentName, componentVersion, policyRuleName, person);
                    final Collection<VulnerabilityEntity> vulnerabilityList = getVulnerabilities(notification, entity);
                    NotificationModel model = new NotificationModel(entity, vulnerabilityList);
                    model = notificationManager.saveNotification(model);
                    entityList.add(model);
                });
                final RealTimeEvent realTimeEvent = new RealTimeEvent(entityList);
                channelTemplateManager.sendEvent(realTimeEvent);
            });
        } else {
            logger.info("No notifications to write");
        }
    } catch (final Exception ex) {
        logger.error("Error occurred writing notification data", ex);
    }
}
Also used : ArrayList(java.util.ArrayList) NotificationEvent(com.blackducksoftware.integration.hub.notification.NotificationEvent) RealTimeEvent(com.blackducksoftware.integration.hub.alert.event.RealTimeEvent) NotificationModel(com.blackducksoftware.integration.hub.alert.hub.model.NotificationModel) VulnerabilityEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.VulnerabilityEntity) Date(java.util.Date) NotificationContentItem(com.blackducksoftware.integration.hub.notification.NotificationContentItem) NotificationCategoryEnum(com.blackducksoftware.integration.hub.notification.NotificationCategoryEnum) NotificationEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.NotificationEntity)

Example 3 with NotificationContentItem

use of com.blackducksoftware.integration.hub.notification.NotificationContentItem 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)

Example 4 with NotificationContentItem

use of com.blackducksoftware.integration.hub.notification.NotificationContentItem in project hub-alert by blackducksoftware.

the class AccumulatorWriterTest method generateDataSet.

private Map<String, Object> generateDataSet() {
    final Map<String, Object> dataSet = new HashMap<>();
    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";
    dataSet.put(NotificationEvent.DATA_SET_KEY_NOTIFICATION_CONTENT, new NotificationContentItem(createdAt, projectVersionModel, componentName, componentVersionView, componentVersionUrl, componentIssueUrl));
    dataSet.put(ItemTypeEnum.RULE.name(), "policyRuleName");
    dataSet.put(ItemTypeEnum.PERSON.name(), "policyUserName");
    dataSet.put(VulnerabilityCache.VULNERABILITY_OPERATION, VulnerabilityOperationEnum.ADD.name());
    final Set<String> vulnSet = new HashSet<>();
    vulnSet.add("vulnerabilityId");
    dataSet.put(VulnerabilityCache.VULNERABILITY_ID_SET, vulnSet);
    return dataSet;
}
Also used : NotificationContentItem(com.blackducksoftware.integration.hub.notification.NotificationContentItem) HashMap(java.util.HashMap) ComponentVersionView(com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView) ProjectVersionModel(com.blackducksoftware.integration.hub.notification.ProjectVersionModel) Date(java.util.Date) HashSet(java.util.HashSet)

Example 5 with NotificationContentItem

use of com.blackducksoftware.integration.hub.notification.NotificationContentItem in project hub-alert by blackducksoftware.

the class UserNotificationCacheTest method testAddUserInformation.

@Test
public void testAddUserInformation() throws IntegrationException {
    final ProjectService mockedProjectService = Mockito.mock(ProjectService.class);
    final UserNotificationCache userNotificationCache = new UserNotificationCache(mockedProjectService);
    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 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 Map<String, Object> dataSet = new HashMap<>();
    dataSet.put(NotificationEvent.DATA_SET_KEY_NOTIFICATION_CONTENT, new NotificationContentItem(createdAt, projectVersionModel, componentName, componentVersionView, componentVersionUrl, componentIssueUrl));
    final NotificationEvent notificationEvent = new NotificationEvent("key", NotificationCategoryEnum.HIGH_VULNERABILITY, dataSet);
    final List<NotificationEvent> notificationEvents = Arrays.asList(notificationEvent);
    Collection<NotificationEvent> notEmptyEventList = Arrays.asList();
    assertTrue(notEmptyEventList.size() == 0);
    notEmptyEventList = userNotificationCache.addUserInformation(notificationEvents);
    assertTrue(notEmptyEventList.size() == 1);
}
Also used : HashMap(java.util.HashMap) ProjectService(com.blackducksoftware.integration.hub.service.ProjectService) NotificationEvent(com.blackducksoftware.integration.hub.notification.NotificationEvent) ProjectVersionModel(com.blackducksoftware.integration.hub.notification.ProjectVersionModel) Date(java.util.Date) NotificationContentItem(com.blackducksoftware.integration.hub.notification.NotificationContentItem) ComponentVersionView(com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView) AssignedUserView(com.blackducksoftware.integration.hub.api.generated.view.AssignedUserView) Test(org.junit.Test)

Aggregations

NotificationContentItem (com.blackducksoftware.integration.hub.notification.NotificationContentItem)8 Date (java.util.Date)6 ComponentVersionView (com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView)5 NotificationEvent (com.blackducksoftware.integration.hub.notification.NotificationEvent)5 ProjectVersionModel (com.blackducksoftware.integration.hub.notification.ProjectVersionModel)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)4 ProjectService (com.blackducksoftware.integration.hub.service.ProjectService)3 NotificationCategoryEnum (com.blackducksoftware.integration.hub.notification.NotificationCategoryEnum)2 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)2 ArrayList (java.util.ArrayList)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1 OutputLogger (com.blackducksoftware.integration.hub.alert.OutputLogger)1 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)1 NotificationEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.NotificationEntity)1 VulnerabilityEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.VulnerabilityEntity)1 RealTimeEvent (com.blackducksoftware.integration.hub.alert.event.RealTimeEvent)1 NotificationModel (com.blackducksoftware.integration.hub.alert.hub.model.NotificationModel)1 AssignedUserView (com.blackducksoftware.integration.hub.api.generated.view.AssignedUserView)1 NotificationResults (com.blackducksoftware.integration.hub.notification.NotificationResults)1