Search in sources :

Example 6 with HubServicesFactory

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);
}
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 7 with HubServicesFactory

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;
}
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 8 with HubServicesFactory

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());
    }
}
Also used : HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory)

Example 9 with HubServicesFactory

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()));
}
Also used : CodeLocationService(com.blackducksoftware.integration.hub.service.CodeLocationService) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory)

Example 10 with HubServicesFactory

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);
}
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

HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)21 Test (org.junit.Test)7 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)6 HubService (com.blackducksoftware.integration.hub.service.HubService)6 ProjectService (com.blackducksoftware.integration.hub.service.ProjectService)5 Logger (org.slf4j.Logger)5 RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)4 ArrayList (java.util.ArrayList)4 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)3 HubGroup (com.blackducksoftware.integration.hub.alert.hub.model.HubGroup)3 HubProject (com.blackducksoftware.integration.hub.alert.hub.model.HubProject)3 ComponentVersionView (com.blackducksoftware.integration.hub.api.generated.view.ComponentVersionView)3 UserGroupView (com.blackducksoftware.integration.hub.api.generated.view.UserGroupView)3 ProjectVersionModel (com.blackducksoftware.integration.hub.notification.ProjectVersionModel)3 Date (java.util.Date)3 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)2 ProjectView (com.blackducksoftware.integration.hub.api.generated.view.ProjectView)2 NotificationContentItem (com.blackducksoftware.integration.hub.notification.NotificationContentItem)2 NotificationEvent (com.blackducksoftware.integration.hub.notification.NotificationEvent)2 NotificationResults (com.blackducksoftware.integration.hub.notification.NotificationResults)2