Search in sources :

Example 16 with AuditEntryEntity

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

the class AuditEntryRepositoryTestIT method findFirstByCommonConfigIdOrderByTimeLastSentDescTestIT.

@Test
@Transactional
public void findFirstByCommonConfigIdOrderByTimeLastSentDescTestIT() {
    UUID commonConfigId = UUID.randomUUID();
    OffsetDateTime leastRecent = OffsetDateTime.ofInstant(Instant.ofEpochMilli(100), ZoneOffset.UTC);
    OffsetDateTime middleDate = OffsetDateTime.ofInstant(Instant.ofEpochMilli(200), ZoneOffset.UTC);
    OffsetDateTime mostRecent = OffsetDateTime.ofInstant(Instant.ofEpochMilli(300), ZoneOffset.UTC);
    AuditEntryEntity leastRecentEntity = new AuditEntryEntity(commonConfigId, null, leastRecent, null, null, null);
    AuditEntryEntity middleEntity = new AuditEntryEntity(commonConfigId, null, middleDate, null, null, null);
    AuditEntryEntity mostRecentEntity = new AuditEntryEntity(commonConfigId, null, mostRecent, null, null, null);
    auditEntryRepository.save(leastRecentEntity);
    auditEntryRepository.save(middleEntity);
    auditEntryRepository.save(mostRecentEntity);
    Optional<AuditEntryEntity> foundEntity = auditEntryRepository.findFirstByCommonConfigIdOrderByTimeLastSentDesc(commonConfigId);
    assertNotNull(foundEntity);
    assertTrue(foundEntity.isPresent());
    assertEquals(mostRecentEntity, foundEntity.get());
}
Also used : OffsetDateTime(java.time.OffsetDateTime) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with AuditEntryEntity

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

the class DefaultProcessingAuditAccessorTest method setAuditEntryStatusTest.

private AuditEntryEntity setAuditEntryStatusTest(AuditEntryStatus expectedStatus, AuditAccessorStatusSetter statusSetter) {
    UUID testJobId = UUID.randomUUID();
    Long testNotificationId = 99L;
    Set<Long> testNotificationIds = Set.of(testNotificationId);
    AuditEntryNotificationView testView = new AuditEntryNotificationView(0L, testJobId, testNotificationId, null, null, null, null, null);
    AtomicReference<AuditEntryEntity> savedEntry = new AtomicReference<>();
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    Mockito.when(auditEntryRepository.findByJobIdAndNotificationIds(testJobId, testNotificationIds)).thenReturn(List.of(testView));
    Mockito.when(auditEntryRepository.saveAll(Mockito.anyList())).then(invocation -> {
        List<AuditEntryEntity> savedEntries = invocation.getArgument(0);
        if (!savedEntries.isEmpty()) {
            savedEntry.set(savedEntries.get(0));
        }
        return List.of();
    });
    DefaultProcessingAuditAccessor processingAuditAccessor = new DefaultProcessingAuditAccessor(auditEntryRepository, null);
    statusSetter.setStatus(processingAuditAccessor, testJobId, testNotificationIds);
    AuditEntryEntity auditEntryEntity = savedEntry.get();
    assertNotNull(auditEntryEntity, "Expected an audit entry to have been saved");
    assertEquals(testView.getId(), auditEntryEntity.getId());
    assertEquals(testView.getJobId(), auditEntryEntity.getCommonConfigId());
    assertEquals(expectedStatus.name(), auditEntryEntity.getStatus());
    assertNotNull(auditEntryEntity.getTimeLastSent(), "Expected time last sent to be set");
    return auditEntryEntity;
}
Also used : AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AtomicReference(java.util.concurrent.atomic.AtomicReference) UUID(java.util.UUID) AuditEntryNotificationView(com.synopsys.integration.alert.database.audit.AuditEntryNotificationView)

Example 18 with AuditEntryEntity

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

the class DefaultRestApiAuditAccessorTest method findFirstByJobIdNotNullTest.

@Test
public void findFirstByJobIdNotNullTest() {
    OffsetDateTime timeLastSent = DateUtils.createCurrentDateTimestamp();
    OffsetDateTime timeCreated = timeLastSent.minusMinutes(10L);
    AuditEntryStatus status = AuditEntryStatus.PENDING;
    UUID testUUID = UUID.randomUUID();
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    AuditEntryEntity auditEntryEntity = new AuditEntryEntity(testUUID, timeCreated, timeLastSent, status.name(), null, null);
    Mockito.when(auditEntryRepository.findFirstByCommonConfigIdOrderByTimeLastSentDesc(Mockito.any(UUID.class))).thenReturn(Optional.of(auditEntryEntity));
    DefaultRestApiAuditAccessor auditUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, null, null, null, null, null);
    AuditJobStatusModel auditJobStatusModel = auditUtility.findFirstByJobId(testUUID).get();
    String testTimeAuditCreated = auditJobStatusModel.getTimeAuditCreated();
    String testTimeLastSent = auditJobStatusModel.getTimeLastSent();
    String testStatus = auditJobStatusModel.getStatus();
    assertTrue(auditUtility.findFirstByJobId(testUUID).isPresent());
    assertEquals(DateUtils.formatDate(timeCreated, DateUtils.AUDIT_DATE_FORMAT), testTimeAuditCreated);
    assertEquals(DateUtils.formatDate(timeLastSent, DateUtils.AUDIT_DATE_FORMAT), testTimeLastSent);
    assertEquals(status.getDisplayName(), testStatus);
}
Also used : AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) OffsetDateTime(java.time.OffsetDateTime) AuditJobStatusModel(com.synopsys.integration.alert.common.persistence.model.AuditJobStatusModel) AuditEntryStatus(com.synopsys.integration.alert.common.enumeration.AuditEntryStatus) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 19 with AuditEntryEntity

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

the class DefaultRestApiAuditAccessorTest method findFirstByJobIdNullTest.

@Test
public void findFirstByJobIdNullTest() {
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    ContentConverter contentConverter = Mockito.mock(ContentConverter.class);
    AuditEntryEntity auditEntryEntity = new AuditEntryEntity(null, null, null, null, null, null);
    Mockito.when(auditEntryRepository.findFirstByCommonConfigIdOrderByTimeLastSentDesc(Mockito.any(UUID.class))).thenReturn(Optional.of(auditEntryEntity));
    DefaultRestApiAuditAccessor auditUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, null, null, null, null, contentConverter);
    UUID testUUID = UUID.randomUUID();
    AuditJobStatusModel auditJobStatusModel = auditUtility.findFirstByJobId(testUUID).get();
    String timeAuditCreated = auditJobStatusModel.getTimeAuditCreated();
    String timeLastSent = auditJobStatusModel.getTimeLastSent();
    String status = auditJobStatusModel.getStatus();
    assertTrue(auditUtility.findFirstByJobId(testUUID).isPresent());
    assertNull(timeAuditCreated);
    assertNull(timeLastSent);
    assertNull(status);
}
Also used : ContentConverter(com.synopsys.integration.alert.common.ContentConverter) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) AuditJobStatusModel(com.synopsys.integration.alert.common.persistence.model.AuditJobStatusModel) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 20 with AuditEntryEntity

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

the class AuditEntryHandlerTestIT method resendNotificationTestIT.

@Test
public void resendNotificationTestIT() throws Exception {
    String content = ResourceUtil.getResourceAsString(getClass(), "/json/policyOverrideNotification.json", StandardCharsets.UTF_8);
    MockNotificationContent mockNotification = new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), blackDuckProviderKey.getUniversalKey(), DateUtils.createCurrentDateTimestamp(), "POLICY_OVERRIDE", content, 1L, providerConfigModel.getConfigurationId());
    ConfigurationFieldModel providerConfigId = ConfigurationFieldModel.create(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID);
    providerConfigId.setFieldValue(String.valueOf(providerConfigModel.getConfigurationId()));
    DistributionJobRequestModel jobRequestModel = createJobRequestModel();
    DistributionJobModel jobModel = jobAccessor.createJob(jobRequestModel);
    NotificationEntity savedNotificationEntity = notificationContentRepository.save(mockNotification.createEntity());
    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.hasExecutePermission(Mockito.eq(ConfigContextEnum.GLOBAL.name()), Mockito.eq(AuditDescriptor.AUDIT_COMPONENT))).thenReturn(true);
    AuditEntryActions auditEntryActions = createAuditActions(authorizationManager);
    try {
        auditEntryActions.resendNotification(savedNotificationEntity.getId(), null);
        auditEntryActions.resendNotification(savedNotificationEntity.getId(), null);
        auditEntryActions.resendNotification(savedNotificationEntity.getId(), jobModel.getJobId());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        fail("Expected the Audit POST request(s) not to throw an exception");
    }
    assertResponseStatusException(HttpStatus.GONE, () -> auditEntryActions.resendNotification(-1L, null));
    assertResponseStatusException(HttpStatus.GONE, () -> auditEntryActions.resendNotification(savedNotificationEntity.getId(), UUID.randomUUID()));
}
Also used : MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) AuditNotificationRelation(com.synopsys.integration.alert.database.audit.AuditNotificationRelation) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) ResponseStatusException(org.springframework.web.server.ResponseStatusException) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) Test(org.junit.jupiter.api.Test) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest)

Aggregations

AuditEntryEntity (com.synopsys.integration.alert.database.audit.AuditEntryEntity)26 Test (org.junit.jupiter.api.Test)18 AuditNotificationRelation (com.synopsys.integration.alert.database.audit.AuditNotificationRelation)12 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)12 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)10 NotificationEntity (com.synopsys.integration.alert.database.notification.NotificationEntity)9 UUID (java.util.UUID)9 DistributionJobRequestModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel)8 OffsetDateTime (java.time.OffsetDateTime)8 AuditEntryRepository (com.synopsys.integration.alert.database.audit.AuditEntryRepository)7 AuditJobStatusModel (com.synopsys.integration.alert.common.persistence.model.AuditJobStatusModel)4 AlertNotificationModel (com.synopsys.integration.alert.common.rest.model.AlertNotificationModel)4 MockAuditEntryEntity (com.synopsys.integration.alert.component.audit.mock.MockAuditEntryEntity)4 ArrayList (java.util.ArrayList)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AuditEntryStatus (com.synopsys.integration.alert.common.enumeration.AuditEntryStatus)3 AuditEntryModel (com.synopsys.integration.alert.common.persistence.model.AuditEntryModel)3 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)3