Search in sources :

Example 6 with AuditEntryModel

use of com.synopsys.integration.alert.common.persistence.model.AuditEntryModel in project hub-alert by blackducksoftware.

the class NotificationWaitJobTask method waitForNotificationToBeProcessedByAllJobs.

private boolean waitForNotificationToBeProcessedByAllJobs() throws IntegrationException {
    String response = alertRequestUtility.executeGetRequest("/api/audit?pageNumber=0&pageSize=2&searchTerm=VULNERABILITY&sortField=createdAt&sortOrder=desc&onlyShowSentNotifications=false", "Could not get the Alert audit entries.");
    AuditEntryPageModel auditEntryPageModel = gson.fromJson(response, AuditEntryPageModel.class);
    Optional<AuditEntryModel> matchingAuditEntry = auditEntryPageModel.getContent().stream().filter(auditEntryModel -> isNotificationAfterTime(startSearchTime, auditEntryModel.getNotification())).filter(auditEntryModel -> NotificationType.VULNERABILITY.name().equals(auditEntryModel.getNotification().getNotificationType())).filter(auditEntryModel -> isNotificationForNewVulnerabilities(auditEntryModel.getNotification())).findFirst();
    if (matchingAuditEntry.isPresent()) {
        AuditEntryModel auditEntryModel = matchingAuditEntry.get();
        intLogger.info(String.format("The notification has been received by %s jobs.", auditEntryModel.getJobs().size()));
        return haveAllJobsSuccessfullyProcessed(auditEntryModel.getJobs());
    }
    return false;
}
Also used : AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) JsonObject(com.google.gson.JsonObject) WaitJobCondition(com.synopsys.integration.wait.WaitJobCondition) IntegrationException(com.synopsys.integration.exception.IntegrationException) VulnerabilityNotificationContent(com.synopsys.integration.blackduck.api.manual.component.VulnerabilityNotificationContent) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) LocalDateTime(java.time.LocalDateTime) AuditEntryStatus(com.synopsys.integration.alert.common.enumeration.AuditEntryStatus) NotificationType(com.synopsys.integration.blackduck.api.manual.enumeration.NotificationType) IntLogger(com.synopsys.integration.log.IntLogger) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) List(java.util.List) NotificationConfig(com.synopsys.integration.alert.common.rest.model.NotificationConfig) Gson(com.google.gson.Gson) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) JobAuditModel(com.synopsys.integration.alert.common.rest.model.JobAuditModel) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel)

Example 7 with AuditEntryModel

use of com.synopsys.integration.alert.common.persistence.model.AuditEntryModel in project hub-alert by blackducksoftware.

the class AuditEntryActionsTest method testPagedRequest.

@Test
public void testPagedRequest() {
    int totalPages = 2;
    int currentPage = 0;
    int pageSize = 2;
    OffsetDateTime createdAt = DateUtils.createCurrentDateTimestamp();
    AlertNotificationModel entity_1 = new AlertNotificationModel(1L, 1L, "provider", "providerConfigName", "notificationType", "{content: \"content is here...\"}", createdAt, createdAt, false);
    entity_1.setId(1L);
    AlertNotificationModel entity_2 = new AlertNotificationModel(2L, 2L, "provider", "providerConfigName", "notificationType", "{content: \"content is here...\"}", createdAt, createdAt, false);
    entity_2.setId(2L);
    List<AlertNotificationModel> pagedEntryList = Arrays.asList(entity_1, entity_2);
    Page<AlertNotificationModel> pageResponse = Mockito.mock(Page.class);
    Mockito.when(pageResponse.getContent()).thenReturn(pagedEntryList);
    Mockito.when(pageResponse.getTotalPages()).thenReturn(totalPages);
    Mockito.when(pageResponse.getNumber()).thenReturn(currentPage);
    Mockito.when(pageResponse.getSize()).thenReturn(pageSize);
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(Boolean.TRUE);
    AuditDescriptorKey auditDescriptorKey = new AuditDescriptorKey();
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    DefaultNotificationAccessor notificationManager = Mockito.mock(DefaultNotificationAccessor.class);
    Mockito.when(notificationManager.findAll(Mockito.any(PageRequest.class), Mockito.anyBoolean())).thenReturn(pageResponse);
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize, Sort.unsorted());
    Mockito.when(notificationManager.getPageRequestForNotifications(Mockito.anyInt(), Mockito.anyInt(), Mockito.any(), Mockito.any())).thenReturn(pageRequest);
    NotificationContentRepository notificationRepository = Mockito.mock(NotificationContentRepository.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
    Mockito.when(jobAccessor.getJobById(Mockito.any())).thenReturn(null);
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    NotificationEntity notificationContent = new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), "provider", DateUtils.createCurrentDateTimestamp(), "notificationType", "{content: \"content is here...\"}", 1L, 1L).createEntity();
    ContentConverter contentConverter = new ContentConverter(new DefaultConversionService());
    DistributionJobModel distributionJob = DistributionJobModel.builder().jobId(UUID.randomUUID()).enabled(true).blackDuckGlobalConfigId(2L).channelDescriptorName("distributionType").name("name").createdAt(OffsetDateTime.now()).distributionFrequency(FrequencyType.REAL_TIME).filterByProject(false).notificationTypes(List.of("type")).processingType(ProcessingType.DEFAULT).build();
    Mockito.doReturn(Optional.of(distributionJob)).when(jobAccessor).getJobById(Mockito.any());
    Mockito.when(notificationRepository.findAllById(Mockito.anyList())).thenReturn(Collections.singletonList(notificationContent));
    DefaultRestApiAuditAccessor auditEntryUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, jobAccessor, configurationModelConfigurationAccessor, notificationManager, contentConverter);
    AuditEntryActions auditEntryActions = new AuditEntryActions(authorizationManager, auditDescriptorKey, auditEntryUtility, notificationManager, jobAccessor, null, null);
    ActionResponse<AuditEntryPageModel> response = auditEntryActions.get(currentPage, pageSize, null, null, null, true);
    assertTrue(response.hasContent());
    AuditEntryPageModel restModel = response.getContent().orElse(null);
    assertEquals(pageResponse.getTotalPages(), restModel.getTotalPages());
    assertEquals(pageResponse.getNumber(), restModel.getCurrentPage());
    assertEquals(pageResponse.getSize(), restModel.getPageSize());
    for (int index = 0; index < pageSize; index++) {
        AlertNotificationModel entity = pageResponse.getContent().get(index);
        AuditEntryModel entryRestModel = restModel.getContent().get(index);
        assertEquals(String.valueOf(entity.getId()), entryRestModel.getId());
    }
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) NotificationContentRepository(com.synopsys.integration.alert.database.notification.NotificationContentRepository) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) PageRequest(org.springframework.data.domain.PageRequest) JobAccessor(com.synopsys.integration.alert.common.persistence.accessor.JobAccessor) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) DefaultRestApiAuditAccessor(com.synopsys.integration.alert.database.api.DefaultRestApiAuditAccessor) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) DefaultNotificationAccessor(com.synopsys.integration.alert.database.api.DefaultNotificationAccessor) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) ContentConverter(com.synopsys.integration.alert.common.ContentConverter) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) OffsetDateTime(java.time.OffsetDateTime) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Example 8 with AuditEntryModel

use of com.synopsys.integration.alert.common.persistence.model.AuditEntryModel in project hub-alert by blackducksoftware.

the class DefaultRestApiAuditAccessor method convertToAuditEntryModelFromNotification.

@Override
@Transactional
public AuditEntryModel convertToAuditEntryModelFromNotification(AlertNotificationModel notificationContentEntry) {
    List<AuditNotificationRelation> relations = auditNotificationRepository.findByNotificationId(notificationContentEntry.getId());
    List<Long> auditEntryIds = relations.stream().map(AuditNotificationRelation::getAuditEntryId).collect(Collectors.toList());
    List<AuditEntryEntity> auditEntryEntities = auditEntryRepository.findAllById(auditEntryIds);
    AuditEntryStatus overallStatus = null;
    String timeLastSent = null;
    OffsetDateTime timeLastSentOffsetDateTime = null;
    List<JobAuditModel> jobAuditModels = new ArrayList<>();
    for (AuditEntryEntity auditEntryEntity : auditEntryEntities) {
        UUID jobId = auditEntryEntity.getCommonConfigId();
        if (null != auditEntryEntity.getTimeLastSent() && (null == timeLastSentOffsetDateTime || timeLastSentOffsetDateTime.isBefore(auditEntryEntity.getTimeLastSent()))) {
            timeLastSentOffsetDateTime = auditEntryEntity.getTimeLastSent();
            timeLastSent = formatAuditDate(timeLastSentOffsetDateTime);
        }
        String id = contentConverter.getStringValue(auditEntryEntity.getId());
        String configId = contentConverter.getStringValue(jobId);
        String timeCreated = formatAuditDate(auditEntryEntity.getTimeCreated());
        AuditEntryStatus status = null;
        if (auditEntryEntity.getStatus() != null) {
            status = AuditEntryStatus.valueOf(auditEntryEntity.getStatus());
            overallStatus = getWorstStatus(overallStatus, status);
        }
        String errorMessage = auditEntryEntity.getErrorMessage();
        String errorStackTrace = auditEntryEntity.getErrorStackTrace();
        Optional<DistributionJobModel> distributionJobModel = jobAccessor.getJobById(jobId);
        String distributionConfigName = distributionJobModel.map(DistributionJobModelData::getName).orElse(null);
        String eventType = distributionJobModel.map(DistributionJobModelData::getChannelDescriptorName).orElse(null);
        String statusDisplayName = null;
        if (null != status) {
            statusDisplayName = status.getDisplayName();
        }
        AuditJobStatusModel auditJobStatusModel = new AuditJobStatusModel(jobId, timeCreated, timeLastSent, statusDisplayName);
        jobAuditModels.add(new JobAuditModel(id, configId, distributionConfigName, eventType, auditJobStatusModel, errorMessage, errorStackTrace));
    }
    String id = contentConverter.getStringValue(notificationContentEntry.getId());
    NotificationConfig notificationConfig = populateConfigFromEntity(notificationContentEntry);
    String overallStatusDisplayName = null;
    if (null != overallStatus) {
        overallStatusDisplayName = overallStatus.getDisplayName();
    }
    return new AuditEntryModel(id, notificationConfig, jobAuditModels, overallStatusDisplayName, timeLastSent);
}
Also used : ArrayList(java.util.ArrayList) JobAuditModel(com.synopsys.integration.alert.common.rest.model.JobAuditModel) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) OffsetDateTime(java.time.OffsetDateTime) AuditJobStatusModel(com.synopsys.integration.alert.common.persistence.model.AuditJobStatusModel) AuditNotificationRelation(com.synopsys.integration.alert.database.audit.AuditNotificationRelation) NotificationConfig(com.synopsys.integration.alert.common.rest.model.NotificationConfig) AuditEntryStatus(com.synopsys.integration.alert.common.enumeration.AuditEntryStatus) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with AuditEntryModel

use of com.synopsys.integration.alert.common.persistence.model.AuditEntryModel in project hub-alert by blackducksoftware.

the class AlertPagedRestModelTest method testRestModel.

@Test
public void testRestModel() {
    final int totalPages = 2;
    final int currentPage = 1;
    final int pageSize = 2;
    List<AuditEntryModel> contentList = new ArrayList<>();
    AuditEntryModel item = new AuditEntryModel();
    contentList.add(item);
    contentList.add(item);
    AlertPagedModel<AuditEntryModel> restModel = new AlertPagedModel<>(totalPages, currentPage, pageSize, contentList);
    assertEquals(totalPages, restModel.getTotalPages());
    assertEquals(currentPage, restModel.getCurrentPage());
    assertEquals(pageSize, restModel.getPageSize());
    assertEquals(contentList, restModel.getModels());
}
Also used : ArrayList(java.util.ArrayList) AlertPagedModel(com.synopsys.integration.alert.common.rest.model.AlertPagedModel) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) Test(org.junit.jupiter.api.Test)

Aggregations

AuditEntryModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryModel)9 Test (org.junit.jupiter.api.Test)7 NotificationConfig (com.synopsys.integration.alert.common.rest.model.NotificationConfig)5 AuditEntryPageModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel)4 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)4 JobAuditModel (com.synopsys.integration.alert.common.rest.model.JobAuditModel)4 AuditEntryRepository (com.synopsys.integration.alert.database.audit.AuditEntryRepository)4 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)3 AlertNotificationModel (com.synopsys.integration.alert.common.rest.model.AlertNotificationModel)3 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)3 AuditDescriptorKey (com.synopsys.integration.alert.component.audit.AuditDescriptorKey)3 AuditEntryEntity (com.synopsys.integration.alert.database.audit.AuditEntryEntity)3 AuditNotificationRelation (com.synopsys.integration.alert.database.audit.AuditNotificationRelation)3 AuditNotificationRepository (com.synopsys.integration.alert.database.audit.AuditNotificationRepository)3 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)3 OffsetDateTime (java.time.OffsetDateTime)3 ArrayList (java.util.ArrayList)3 ContentConverter (com.synopsys.integration.alert.common.ContentConverter)2 AuditEntryStatus (com.synopsys.integration.alert.common.enumeration.AuditEntryStatus)2 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)2