Search in sources :

Example 1 with AuditEntryEntity

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

the class DefaultDistributionAccessorTestIT method assertValidQueryFunctionality.

private AlertPagedModel<DistributionWithAuditInfo> assertValidQueryFunctionality(int expectedNumberOfResults, Supplier<AlertPagedModel<DistributionWithAuditInfo>> dBQuery) throws ParseException {
    Map<UUID, Pair<DistributionJobModel, List<AuditEntryEntity>>> jobAndAuditData = createAndSave6JobAndAudit();
    assertEquals(TOTAL_NUMBER_OF_RECORDS, jobAndAuditData.keySet().size());
    jobAndAuditData.keySet().stream().forEach(uuid -> {
        Optional<DistributionJobModel> job = jobAccessor.getJobById(uuid);
        assertTrue(job.isPresent());
    });
    AlertPagedModel<DistributionWithAuditInfo> queryResult = dBQuery.get();
    assertNotNull(queryResult);
    assertEquals(expectedNumberOfResults, queryResult.getModels().size());
    for (DistributionWithAuditInfo distributionWithAuditInfo : queryResult.getModels()) {
        Pair<DistributionJobModel, List<AuditEntryEntity>> distributionJobModelListPair = jobAndAuditData.get(distributionWithAuditInfo.getJobId());
        DistributionJobModel distributionJobModel = distributionJobModelListPair.getLeft();
        assertEquals(distributionJobModel.getName(), distributionWithAuditInfo.getJobName());
        assertNotEquals(AuditEntryStatus.PENDING.name(), distributionWithAuditInfo.getAuditStatus());
        List<AuditEntryEntity> audits = distributionJobModelListPair.getRight();
        if (!audits.isEmpty()) {
            OffsetDateTime mostRecentAuditEntryTime = audits.stream().filter(auditEntryEntity -> auditEntryEntity.getTimeLastSent() != null).max(Comparator.comparing(AuditEntryEntity::getTimeLastSent)).map(AuditEntryEntity::getTimeLastSent).orElse(null);
            String formattedTime = null;
            if (null != mostRecentAuditEntryTime) {
                formattedTime = DateUtils.formatDate(mostRecentAuditEntryTime, DateUtils.AUDIT_DATE_FORMAT);
            }
            assertEquals(formattedTime, distributionWithAuditInfo.getAuditTimeLastSent());
        }
    }
    return queryResult;
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) AuditEntryStatus(com.synopsys.integration.alert.common.enumeration.AuditEntryStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) FrequencyType(com.synopsys.integration.alert.common.enumeration.FrequencyType) DistributionJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.DistributionJobDetailsModel) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) DescriptorMap(com.synopsys.integration.alert.common.descriptor.DescriptorMap) SlackJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.SlackJobDetailsModel) DistributionWithAuditInfo(com.synopsys.integration.alert.common.rest.model.DistributionWithAuditInfo) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) JobAccessor(com.synopsys.integration.alert.common.persistence.accessor.JobAccessor) Pair(org.apache.commons.lang3.tuple.Pair) ProcessingType(com.synopsys.integration.alert.common.enumeration.ProcessingType) SlackChannelKey(com.synopsys.integration.alert.descriptor.api.SlackChannelKey) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Direction(org.springframework.data.domain.Sort.Direction) ParseException(java.text.ParseException) LinkedList(java.util.LinkedList) DateUtils(com.synopsys.integration.alert.common.util.DateUtils) MsTeamsKey(com.synopsys.integration.alert.descriptor.api.MsTeamsKey) Modifying(org.springframework.data.jpa.repository.Modifying) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) Set(java.util.Set) UUID(java.util.UUID) AlertPagedModel(com.synopsys.integration.alert.common.rest.model.AlertPagedModel) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Collectors(java.util.stream.Collectors) MSTeamsJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.MSTeamsJobDetailsModel) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) OffsetDateTime(java.time.OffsetDateTime) DistributionAccessor(com.synopsys.integration.alert.common.persistence.accessor.DistributionAccessor) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Comparator(java.util.Comparator) Transactional(org.springframework.transaction.annotation.Transactional) DistributionWithAuditInfo(com.synopsys.integration.alert.common.rest.model.DistributionWithAuditInfo) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) OffsetDateTime(java.time.OffsetDateTime) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with AuditEntryEntity

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

the class DefaultDistributionAccessorTestIT method createAndSave6JobAndAudit.

private Map<UUID, Pair<DistributionJobModel, List<AuditEntryEntity>>> createAndSave6JobAndAudit() {
    DistributionJobRequestModel firstJob = createSlackJob(true);
    DistributionJobRequestModel secondJob = createSlackJob(false);
    DistributionJobRequestModel thirdJob = createSlackJob(true);
    DistributionJobRequestModel fourthJob = createSlackJob(false);
    DistributionJobRequestModel fifthJob = createMSTeamsJob(true);
    DistributionJobRequestModel sixthJob = createMSTeamsJob(false);
    DistributionJobModel firstJobSaved = jobAccessor.createJob(firstJob);
    DistributionJobModel secondJobSaved = jobAccessor.createJob(secondJob);
    DistributionJobModel thirdJobSaved = jobAccessor.createJob(thirdJob);
    DistributionJobModel fourthJobSaved = jobAccessor.createJob(fourthJob);
    DistributionJobModel fifthJobSaved = jobAccessor.createJob(fifthJob);
    DistributionJobModel sixthJobSaved = jobAccessor.createJob(sixthJob);
    createdJobs.add(firstJobSaved.getJobId());
    createdJobs.add(secondJobSaved.getJobId());
    createdJobs.add(thirdJobSaved.getJobId());
    createdJobs.add(fourthJobSaved.getJobId());
    createdJobs.add(fifthJobSaved.getJobId());
    createdJobs.add(sixthJobSaved.getJobId());
    AuditEntryEntity firstAudit = createAuditEntryEntity(firstJobSaved.getJobId(), OffsetDateTime.now(), AuditEntryStatus.SUCCESS);
    AuditEntryEntity secondAudit = createAuditEntryEntity(firstJobSaved.getJobId(), OffsetDateTime.now().minusDays(1), AuditEntryStatus.PENDING);
    AuditEntryEntity thirdAudit = createAuditEntryEntity(secondJobSaved.getJobId(), OffsetDateTime.now().minusMinutes(1), AuditEntryStatus.FAILURE);
    AuditEntryEntity fourthAudit = createAuditEntryEntity(fourthJobSaved.getJobId(), OffsetDateTime.now().minusHours(1), AuditEntryStatus.SUCCESS);
    AuditEntryEntity fifthAudit = createAuditEntryEntity(fifthJobSaved.getJobId(), OffsetDateTime.now().minusHours(2), AuditEntryStatus.SUCCESS);
    AuditEntryEntity sixthAudit = createAuditEntryEntity(fifthJobSaved.getJobId(), OffsetDateTime.now().minusMinutes(2), AuditEntryStatus.FAILURE);
    AuditEntryEntity seventhAudit = createAuditEntryEntity(sixthJobSaved.getJobId(), null, AuditEntryStatus.SUCCESS);
    AuditEntryEntity eighthAudit = createAuditEntryEntity(sixthJobSaved.getJobId(), OffsetDateTime.now(), AuditEntryStatus.FAILURE);
    AuditEntryEntity ninthAudit = createAuditEntryEntity(sixthJobSaved.getJobId(), null, AuditEntryStatus.PENDING);
    saveAllAudits(List.of(firstAudit, secondAudit, thirdAudit, fourthAudit, fifthAudit, sixthAudit, seventhAudit, eighthAudit, ninthAudit));
    return Map.of(firstJobSaved.getJobId(), Pair.of(firstJobSaved, List.of(firstAudit, secondAudit)), secondJobSaved.getJobId(), Pair.of(secondJobSaved, List.of(thirdAudit)), thirdJobSaved.getJobId(), Pair.of(thirdJobSaved, List.of()), fourthJobSaved.getJobId(), Pair.of(fourthJobSaved, List.of(fourthAudit)), fifthJobSaved.getJobId(), Pair.of(fifthJobSaved, List.of(fifthAudit, sixthAudit)), sixthJobSaved.getJobId(), Pair.of(sixthJobSaved, List.of(seventhAudit, eighthAudit, ninthAudit)));
}
Also used : AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)

Example 3 with AuditEntryEntity

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

the class NotificationContentRepositoryIT method initializeNotificationRepo.

private Long initializeNotificationRepo(String searchTerm, int numberToCreate) throws ParseException {
    List<NotificationEntity> notifications = new ArrayList<>(numberToCreate);
    long searchableCount = 0;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(RestConstants.JSON_DATE_FORMAT);
    for (int i = 0; i < numberToCreate; i++) {
        Date newDate = new Date();
        String dateString = simpleDateFormat.format(newDate);
        NotificationEntity entity;
        if (i % 31 == 0) {
            entity = createEntity(dateString, searchTerm);
            searchableCount++;
        } else {
            entity = createEntity(dateString);
        }
        notifications.add(entity);
    }
    List<NotificationEntity> savedNotifications = notificationContentRepository.saveAll(notifications);
    DistributionJobRequestModel jobRequestModel = createJobRequestModel();
    DistributionJobModel jobModel = jobAccessor.createJob(jobRequestModel);
    UUID jobId = jobModel.getJobId();
    // more investigation is needed
    for (NotificationEntity savedNotification : savedNotifications) {
        AuditEntryEntity newEntry = new AuditEntryEntity(jobId, OffsetDateTime.now(), null, AuditEntryStatus.PENDING.name(), null, null);
        AuditEntryEntity savedEntry = auditEntryRepository.save(newEntry);
        AuditNotificationRelation notifAudit = new AuditNotificationRelation(savedEntry.getId(), savedNotification.getId());
        auditNotificationRepository.save(notifAudit);
    }
    auditEntryRepository.flush();
    return searchableCount;
}
Also used : ArrayList(java.util.ArrayList) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) Date(java.util.Date) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) AuditNotificationRelation(com.synopsys.integration.alert.database.audit.AuditNotificationRelation) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) UUID(java.util.UUID) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with AuditEntryEntity

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

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

the class DefaultProcessingAuditAccessorTest method setAuditEntryFailureTest.

@Test
public void setAuditEntryFailureTest() {
    String testErrorMessage = "Uh oh, an error occurred!";
    String testExceptionMessage = "Something bad happened. Yikes...";
    Throwable testThrowable = new AlertException(testExceptionMessage);
    AuditEntryEntity testResultEntry = setAuditEntryStatusTest(AuditEntryStatus.FAILURE, (accessor, jobId, notificationIds) -> accessor.setAuditEntryFailure(jobId, notificationIds, testErrorMessage, testThrowable));
    assertEquals(testErrorMessage, testResultEntry.getErrorMessage());
    assertNotNull(testResultEntry.getErrorStackTrace(), "Expected the audit entry to contain an error stack trace");
    assertTrue(testResultEntry.getErrorStackTrace().contains(testExceptionMessage), "Expected the error stack trace to contain a specific message, but that message was missing");
}
Also used : AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

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