Search in sources :

Example 1 with VulnerabilityV2View

use of com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View 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);
}
Also used : HashMap(java.util.HashMap) VulnerabilityV2View(com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View) 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) VulnerabilityContentItem(com.blackducksoftware.integration.hub.notification.VulnerabilityContentItem) ComponentVersionView(com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView) ComponentService(com.blackducksoftware.integration.hub.service.ComponentService) AssignedUserView(com.blackducksoftware.integration.hub.api.generated.view.AssignedUserView) HubService(com.blackducksoftware.integration.hub.service.HubService) Test(org.junit.Test)

Example 2 with VulnerabilityV2View

use of com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View in project hub-alert by blackducksoftware.

the class VulnerabilityCache method addEventsToList.

@SuppressWarnings("unchecked")
private void addEventsToList(final NotificationEvent originalEvent, final List<VulnerabilityV2View> vulnerabilityList, final List<NotificationEvent> eventList) {
    final Map<NotificationCategoryEnum, NotificationEvent> eventMap = new HashMap<>();
    final Set<String> eventVulnIdSet = (Set<String>) originalEvent.getDataSet().get(VULNERABILITY_ID_SET);
    for (final String vulnId : eventVulnIdSet) {
        for (final VulnerabilityV2View vulnerability : vulnerabilityList) {
            final NotificationCategoryEnum eventCategory = getEventCategory(vulnerability.severity);
            final String vulnName = vulnerability.name;
            if (vulnId.equals(vulnName)) {
                if (eventMap.containsKey(eventCategory)) {
                    final NotificationEvent event = eventMap.get(eventCategory);
                    final Set<String> vulnSet = (Set<String>) event.getDataSet().get(VULNERABILITY_ID_SET);
                    vulnSet.add(vulnName);
                } else {
                    final Set<String> vulnset = new HashSet<>();
                    vulnset.add(vulnName);
                    final Map<String, Object> dataSet = new HashMap<>(originalEvent.getDataSet());
                    dataSet.put(VULNERABILITY_ID_SET, vulnset);
                    final NotificationEvent event = new NotificationEvent(originalEvent.getEventKey(), eventCategory, dataSet);
                    eventMap.put(eventCategory, event);
                    eventList.add(event);
                }
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) VulnerabilityV2View(com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View) NotificationCategoryEnum(com.blackducksoftware.integration.hub.notification.NotificationCategoryEnum) NotificationEvent(com.blackducksoftware.integration.hub.notification.NotificationEvent) HashSet(java.util.HashSet)

Example 3 with VulnerabilityV2View

use of com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View in project hub-alert by blackducksoftware.

the class VulnerabilityCache method createVulnerabilityEvents.

private List<NotificationEvent> createVulnerabilityEvents(final NotificationEvent originalEvent) throws IntegrationException {
    final List<NotificationEvent> eventList = new LinkedList<>();
    final VulnerabilityContentItem vulnerabilityContent = (VulnerabilityContentItem) originalEvent.getDataSet().get(NotificationEvent.DATA_SET_KEY_NOTIFICATION_CONTENT);
    final List<VulnerabilityV2View> vulnerabilityList = hubServicesFactory.createHubService().getAllResponses(vulnerabilityContent.getComponentVersion(), ComponentVersionView.VULNERABILITIES_LINK_RESPONSE);
    addEventsToList(originalEvent, vulnerabilityList, eventList);
    addCountsToDataSet(eventList);
    return eventList;
}
Also used : VulnerabilityContentItem(com.blackducksoftware.integration.hub.notification.VulnerabilityContentItem) VulnerabilityV2View(com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View) NotificationEvent(com.blackducksoftware.integration.hub.notification.NotificationEvent) LinkedList(java.util.LinkedList)

Aggregations

VulnerabilityV2View (com.blackducksoftware.integration.hub.api.generated.view.VulnerabilityV2View)3 NotificationEvent (com.blackducksoftware.integration.hub.notification.NotificationEvent)3 VulnerabilityContentItem (com.blackducksoftware.integration.hub.notification.VulnerabilityContentItem)2 HashMap (java.util.HashMap)2 AssignedUserView (com.blackducksoftware.integration.hub.api.generated.view.AssignedUserView)1 ComponentVersionView (com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView)1 NotificationCategoryEnum (com.blackducksoftware.integration.hub.notification.NotificationCategoryEnum)1 ProjectVersionModel (com.blackducksoftware.integration.hub.notification.ProjectVersionModel)1 ComponentService (com.blackducksoftware.integration.hub.service.ComponentService)1 HubService (com.blackducksoftware.integration.hub.service.HubService)1 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)1 ProjectService (com.blackducksoftware.integration.hub.service.ProjectService)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 Test (org.junit.Test)1