Search in sources :

Example 1 with NotificationConfig

use of com.synopsys.integration.alert.common.rest.model.NotificationConfig in project hub-alert by blackducksoftware.

the class DefaultRestApiAuditAccessorTest method getPageOfAuditEntriesTest.

@Test
public void getPageOfAuditEntriesTest() {
    Integer pageNumber = 0;
    int pageSize = 2;
    String searchTerm = null;
    String sortField = "lastSent";
    String sortOrder = "ASC";
    Boolean onlyShowSentNotifications = Boolean.TRUE;
    String overallStatus = "overallStatusString";
    String lastSent = DateUtils.createCurrentDateString(DateUtils.AUDIT_DATE_FORMAT);
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    DefaultNotificationAccessor notificationManager = Mockito.mock(DefaultNotificationAccessor.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    PageRequest pageRequest = PageRequest.of(0, 10);
    Mockito.when(auditEntryRepository.findMatchingAudit(Mockito.anyLong(), Mockito.any(UUID.class))).thenReturn(Optional.empty());
    Mockito.when(notificationManager.getPageRequestForNotifications(pageNumber, pageSize, sortField, sortOrder)).thenReturn(pageRequest);
    // At least two AlertNotificationModel are required for the comparator
    AlertNotificationModel alertNotificationModel = new AlertNotificationModel(1L, 1L, "provider-test", "providerConfigName-test", "notificationType-test", "{content: \"content is here...\"}", DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp(), false);
    AlertNotificationModel alertNotificationModel2 = new AlertNotificationModel(2L, 2L, "provider-test2", "providerConfigName-test2", "notificationType-test2", "{content: \"content is here2..\"}", DateUtils.createCurrentDateTimestamp().minusSeconds(15), DateUtils.createCurrentDateTimestamp().minusSeconds(10), false);
    Pageable auditPageable = Mockito.mock(Pageable.class);
    Mockito.when(auditPageable.getOffset()).thenReturn(pageNumber.longValue());
    Mockito.when(auditPageable.getPageSize()).thenReturn(pageSize);
    Page<AlertNotificationModel> auditPage = new PageImpl<>(List.of(alertNotificationModel, alertNotificationModel2), auditPageable, 1);
    Mockito.when(notificationManager.findAll(pageRequest, onlyShowSentNotifications)).thenReturn(auditPage);
    NotificationConfig notificationConfig = new NotificationConfig("3", "createdAtString", "providerString", 2L, "providerConfigNameString", "providerCreationTimeString", "notificationTypeString", "content-test");
    AuditEntryModel auditEntryModel = new AuditEntryModel("2", notificationConfig, List.of(), overallStatus, lastSent);
    Function<AlertNotificationModel, AuditEntryModel> notificationToAuditEntryConverter = (AlertNotificationModel notificationModel) -> auditEntryModel;
    DefaultRestApiAuditAccessor auditUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, null, null, notificationManager, null);
    AuditEntryPageModel alertPagedModel = auditUtility.getPageOfAuditEntries(pageNumber, pageSize, searchTerm, sortField, sortOrder, onlyShowSentNotifications, notificationToAuditEntryConverter);
    assertEquals(1, alertPagedModel.getTotalPages());
    assertEquals(pageNumber.intValue(), alertPagedModel.getCurrentPage());
    assertEquals(pageSize, alertPagedModel.getPageSize());
    assertEquals(2, alertPagedModel.getContent().size());
    assertEquals(lastSent, alertPagedModel.getContent().get(0).getLastSent());
    assertEquals(lastSent, alertPagedModel.getContent().get(1).getLastSent());
    AuditEntryModel auditContentTest = alertPagedModel.getContent().stream().findFirst().orElse(null);
    assertEquals(auditEntryModel.getId(), auditContentTest.getId());
    assertEquals(notificationConfig, auditContentTest.getNotification());
    assertEquals(0, auditContentTest.getJobs().size());
    assertEquals(overallStatus, auditContentTest.getOverallStatus());
    assertEquals(lastSent, auditContentTest.getLastSent());
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) PageImpl(org.springframework.data.domain.PageImpl) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) NotificationConfig(com.synopsys.integration.alert.common.rest.model.NotificationConfig) UUID(java.util.UUID) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) Test(org.junit.jupiter.api.Test)

Example 2 with NotificationConfig

use of com.synopsys.integration.alert.common.rest.model.NotificationConfig in project hub-alert by blackducksoftware.

the class AuditEntryHandlerTestIT method getTestIT.

@Test
public void getTestIT() {
    NotificationEntity savedNotificationEntity = notificationContentRepository.save(mockNotification.createEntity());
    notificationContentRepository.save(new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), "provider", DateUtils.createCurrentDateTimestamp(), "notificationType", "{}", 234L, providerConfigModel.getConfigurationId()).createEntity());
    DistributionJobRequestModel jobRequestModel = createJobRequestModel();
    DistributionJobModel jobModel = jobAccessor.createJob(jobRequestModel);
    AuditEntryEntity savedAuditEntryEntity = auditEntryRepository.save(new AuditEntryEntity(jobModel.getJobId(), DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp(), AuditEntryStatus.SUCCESS.toString(), null, null));
    auditNotificationRepository.save(new AuditNotificationRelation(savedAuditEntryEntity.getId(), savedNotificationEntity.getId()));
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(Boolean.TRUE);
    AuditEntryActions auditEntryActions = createAuditActions(authorizationManager);
    AuditEntryPageModel auditEntries = auditEntryActions.get(null, null, null, null, null, true).getContent().orElse(null);
    assertEquals(1, auditEntries.getContent().size());
    AuditEntryModel auditEntryResponse = auditEntryActions.get(savedNotificationEntity.getId()).getContent().orElse(null);
    assertNotNull(auditEntryResponse);
    AuditEntryModel auditEntry = auditEntries.getContent().get(0);
    assertEquals(savedNotificationEntity.getId().toString(), auditEntry.getId());
    assertFalse(auditEntry.getJobs().isEmpty());
    assertEquals(1, auditEntry.getJobs().size());
    NotificationConfig notification = auditEntry.getNotification();
    String createdAtStringValue = DateUtils.formatDate(savedNotificationEntity.getCreatedAt(), DateUtils.AUDIT_DATE_FORMAT);
    assertEquals(createdAtStringValue, notification.getCreatedAt());
    assertEquals(savedNotificationEntity.getNotificationType(), notification.getNotificationType());
    assertNotNull(notification.getContent());
    auditEntries = auditEntryActions.get(null, null, null, null, null, false).getContent().orElse(null);
    assertEquals(2, auditEntries.getContent().size());
}
Also used : MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) AuditNotificationRelation(com.synopsys.integration.alert.database.audit.AuditNotificationRelation) NotificationConfig(com.synopsys.integration.alert.common.rest.model.NotificationConfig) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) 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) Test(org.junit.jupiter.api.Test) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest)

Example 3 with NotificationConfig

use of com.synopsys.integration.alert.common.rest.model.NotificationConfig in project hub-alert by blackducksoftware.

the class AuditEntryRestModelTest method testRestModel.

@Test
public void testRestModel() {
    String id = "1";
    String timeLastSent = new Date(500).toString();
    String overallStatus = AuditEntryStatus.SUCCESS.name();
    NotificationConfig notification = new NotificationConfig();
    List<JobAuditModel> jobAuditModels = Collections.singletonList(new JobAuditModel());
    AuditEntryModel restModel = new AuditEntryModel(id, notification, jobAuditModels, overallStatus, timeLastSent);
    assertEquals(notification, restModel.getNotification());
    assertEquals(jobAuditModels, restModel.getJobs());
    assertEquals(overallStatus, restModel.getOverallStatus());
    assertEquals(timeLastSent, restModel.getLastSent());
    assertEquals(id, restModel.getId());
}
Also used : NotificationConfig(com.synopsys.integration.alert.common.rest.model.NotificationConfig) JobAuditModel(com.synopsys.integration.alert.common.rest.model.JobAuditModel) Date(java.util.Date) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) Test(org.junit.jupiter.api.Test)

Example 4 with NotificationConfig

use of com.synopsys.integration.alert.common.rest.model.NotificationConfig 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)

Aggregations

AuditEntryModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryModel)4 NotificationConfig (com.synopsys.integration.alert.common.rest.model.NotificationConfig)4 Test (org.junit.jupiter.api.Test)3 AuditEntryPageModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel)2 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)2 JobAuditModel (com.synopsys.integration.alert.common.rest.model.JobAuditModel)2 AuditEntryEntity (com.synopsys.integration.alert.database.audit.AuditEntryEntity)2 AuditNotificationRelation (com.synopsys.integration.alert.database.audit.AuditNotificationRelation)2 UUID (java.util.UUID)2 AuditEntryStatus (com.synopsys.integration.alert.common.enumeration.AuditEntryStatus)1 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)1 AuditJobStatusModel (com.synopsys.integration.alert.common.persistence.model.AuditJobStatusModel)1 DistributionJobRequestModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel)1 AlertNotificationModel (com.synopsys.integration.alert.common.rest.model.AlertNotificationModel)1 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)1 AuditDescriptorKey (com.synopsys.integration.alert.component.audit.AuditDescriptorKey)1 AuditEntryRepository (com.synopsys.integration.alert.database.audit.AuditEntryRepository)1 AuditNotificationRepository (com.synopsys.integration.alert.database.audit.AuditNotificationRepository)1 NotificationEntity (com.synopsys.integration.alert.database.notification.NotificationEntity)1 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)1