Search in sources :

Example 1 with AuditEntryRepository

use of com.synopsys.integration.alert.database.audit.AuditEntryRepository in project hub-alert by blackducksoftware.

the class DefaultProcessingAuditAccessorTest method createOrUpdatePendingAuditEntryForJobTest.

@Test
public void createOrUpdatePendingAuditEntryForJobTest() {
    UUID testJobId = UUID.randomUUID();
    Set<Long> testNotificationIds = Set.of(1L, 2L, 10L);
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    Mockito.when(auditEntryRepository.findByJobIdAndNotificationIds(Mockito.eq(testJobId), Mockito.eq(testNotificationIds))).thenReturn(List.of());
    Mockito.when(auditEntryRepository.save(Mockito.any())).then(invocation -> {
        AuditEntryEntity auditEntry = invocation.getArgument(0);
        auditEntry.setId(RANDOM.nextLong());
        return auditEntry;
    });
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    Mockito.when(auditNotificationRepository.saveAll(Mockito.anyCollection())).thenReturn(List.of());
    DefaultProcessingAuditAccessor processingAuditAccessor = new DefaultProcessingAuditAccessor(auditEntryRepository, auditNotificationRepository);
    processingAuditAccessor.createOrUpdatePendingAuditEntryForJob(testJobId, testNotificationIds);
    Mockito.verify(auditEntryRepository, Mockito.times(testNotificationIds.size())).save(Mockito.any());
}
Also used : AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 2 with AuditEntryRepository

use of com.synopsys.integration.alert.database.audit.AuditEntryRepository in project hub-alert by blackducksoftware.

the class DefaultRestApiAuditAccessorTest method convertToAuditEntryModelFromNotificationTest.

@Test
public void convertToAuditEntryModelFromNotificationTest() throws Exception {
    Long id = 1L;
    Long providerConfigId = 2L;
    String provider = "provider-test";
    String providerConfigName = "providerConfigName-test";
    String notificationType = "notificationType-test";
    String content = "content-test";
    OffsetDateTime timeLastSent = DateUtils.createCurrentDateTimestamp();
    OffsetDateTime timeCreated = timeLastSent.minusSeconds(10);
    Long auditEntryId = 3L;
    String channelName = "test-channel.common.name-value";
    String eventType = "test-channel.common.channel.name-value";
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    ContentConverter contentConverter = new ContentConverter(new DefaultConversionService());
    AlertNotificationModel alertNotificationModel = new AlertNotificationModel(id, providerConfigId, provider, providerConfigName, notificationType, content, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp(), false);
    AuditNotificationRelation auditNotificationRelation = new AuditNotificationRelation(auditEntryId, alertNotificationModel.getId());
    AuditEntryEntity auditEntryEntity = new AuditEntryEntity(UUID.randomUUID(), timeCreated, timeLastSent, AuditEntryStatus.SUCCESS.name(), null, null);
    Mockito.when(auditNotificationRepository.findByNotificationId(Mockito.any())).thenReturn(List.of(auditNotificationRelation));
    Mockito.when(auditEntryRepository.findAllById(Mockito.any())).thenReturn(List.of(auditEntryEntity));
    DistributionJobModel distributionJob = DistributionJobModel.builder().jobId(UUID.randomUUID()).enabled(true).blackDuckGlobalConfigId(2L).channelDescriptorName("test-channel.common.channel.name-value").name("test-channel.common.name-value").distributionFrequency(FrequencyType.REAL_TIME).filterByProject(false).notificationTypes(List.of(NotificationType.LICENSE_LIMIT.name())).processingType(ProcessingType.DEFAULT).createdAt(DateUtils.createCurrentDateTimestamp()).build();
    Mockito.when(jobAccessor.getJobById(Mockito.any())).thenReturn(Optional.of(distributionJob));
    DefaultRestApiAuditAccessor auditUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, jobAccessor, configurationModelConfigurationAccessor, null, contentConverter);
    AuditEntryModel testAuditEntryModel = auditUtility.convertToAuditEntryModelFromNotification(alertNotificationModel);
    assertEquals(id, Long.valueOf(testAuditEntryModel.getId()));
    assertNotNull(testAuditEntryModel.getNotification());
    assertFalse(testAuditEntryModel.getJobs().isEmpty());
    assertEquals(1, testAuditEntryModel.getJobs().size());
    JobAuditModel testJob = testAuditEntryModel.getJobs().get(0);
    assertEquals(channelName, testJob.getName());
    assertEquals(eventType, testJob.getEventType());
    assertEquals(AuditEntryStatus.SUCCESS.getDisplayName(), testAuditEntryModel.getOverallStatus());
    assertEquals(DateUtils.formatDate(timeLastSent, DateUtils.AUDIT_DATE_FORMAT), testAuditEntryModel.getLastSent());
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) JobAuditModel(com.synopsys.integration.alert.common.rest.model.JobAuditModel) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) ContentConverter(com.synopsys.integration.alert.common.ContentConverter) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) JobAccessor(com.synopsys.integration.alert.common.persistence.accessor.JobAccessor) OffsetDateTime(java.time.OffsetDateTime) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) AuditNotificationRelation(com.synopsys.integration.alert.database.audit.AuditNotificationRelation) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) Test(org.junit.jupiter.api.Test)

Example 3 with AuditEntryRepository

use of com.synopsys.integration.alert.database.audit.AuditEntryRepository in project hub-alert by blackducksoftware.

the class DefaultRestApiAuditAccessorTest method findMatchingAuditIdTest.

@Test
public void findMatchingAuditIdTest() {
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    Mockito.when(auditEntryRepository.findMatchingAudit(Mockito.anyLong(), Mockito.any(UUID.class))).thenReturn(Optional.empty());
    DefaultRestApiAuditAccessor auditUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, null, null, null, null, null);
    Optional<Long> nullValue = auditUtility.findMatchingAuditId(1L, UUID.randomUUID());
    assertFalse(nullValue.isPresent());
    AuditEntryEntity auditEntryEntity = new AuditEntryEntity(null, null, null, null, null, null);
    Long expectedLong = 2L;
    auditEntryEntity.setId(expectedLong);
    Mockito.when(auditEntryRepository.findMatchingAudit(Mockito.anyLong(), Mockito.any(UUID.class))).thenReturn(Optional.of(auditEntryEntity));
    Optional<Long> actualValue = auditUtility.findMatchingAuditId(expectedLong, UUID.randomUUID());
    assertTrue(actualValue.isPresent());
    assertEquals(expectedLong, actualValue.get());
}
Also used : AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 4 with AuditEntryRepository

use of com.synopsys.integration.alert.database.audit.AuditEntryRepository 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 5 with AuditEntryRepository

use of com.synopsys.integration.alert.database.audit.AuditEntryRepository in project hub-alert by blackducksoftware.

the class DefaultNotificationAccessorTest method getPageRequestForNotificationsTest.

@Test
void getPageRequestForNotificationsTest() {
    final int pageNumber = 1;
    final int pageSize = 1;
    final String sortField = "content";
    String sortOrder = Sort.Direction.ASC.name();
    NotificationContentRepository notificationContentRepository = Mockito.mock(NotificationContentRepository.class);
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    DefaultNotificationAccessor notificationManager = new DefaultNotificationAccessor(notificationContentRepository, auditEntryRepository, configurationModelConfigurationAccessor);
    PageRequest pageRequest = notificationManager.getPageRequestForNotifications(pageNumber, pageSize, sortField, sortOrder);
    assertEquals(pageNumber, pageRequest.getPageNumber());
    assertEquals(pageSize, pageRequest.getPageSize());
}
Also used : AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) PageRequest(org.springframework.data.domain.PageRequest) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) NotificationContentRepository(com.synopsys.integration.alert.database.notification.NotificationContentRepository) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

AuditEntryRepository (com.synopsys.integration.alert.database.audit.AuditEntryRepository)14 Test (org.junit.jupiter.api.Test)13 AuditNotificationRepository (com.synopsys.integration.alert.database.audit.AuditNotificationRepository)7 UUID (java.util.UUID)7 AuditEntryEntity (com.synopsys.integration.alert.database.audit.AuditEntryEntity)6 ContentConverter (com.synopsys.integration.alert.common.ContentConverter)5 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)5 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)5 AuditDescriptorKey (com.synopsys.integration.alert.component.audit.AuditDescriptorKey)5 DefaultRestApiAuditAccessor (com.synopsys.integration.alert.database.api.DefaultRestApiAuditAccessor)5 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)5 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)4 JobAccessor (com.synopsys.integration.alert.common.persistence.accessor.JobAccessor)4 AuditEntryModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryModel)4 AuditEntryPageModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel)4 AlertNotificationModel (com.synopsys.integration.alert.common.rest.model.AlertNotificationModel)4 DefaultNotificationAccessor (com.synopsys.integration.alert.database.api.DefaultNotificationAccessor)4 NotificationContentRepository (com.synopsys.integration.alert.database.notification.NotificationContentRepository)4 PageRequest (org.springframework.data.domain.PageRequest)4 AuditJobStatusModel (com.synopsys.integration.alert.common.persistence.model.AuditJobStatusModel)3