@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); ConfigurationAccessor configurationAccessor = Mockito.mock(ConfigurationAccessor.class); ContentConverter contentConverter = new ContentConverter(gson, new DefaultConversionService()); AlertNotificationModel alertNotificationModel = new AlertNotificationModel(id, providerConfigId, provider, providerConfigName, notificationType, content, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp()); 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)); ConfigurationModelMutable configurationModel = new ConfigurationModelMutable(10L, 11L, "createdAt-test", "lastUpdate-test", ConfigContextEnum.DISTRIBUTION); ConfigurationFieldModel configurationFieldModel = ConfigurationFieldModel.create("channel.common.name"); configurationFieldModel.setFieldValue("test-channel.common.name-value"); ConfigurationFieldModel configurationFieldModel2 = ConfigurationFieldModel.create("channel.common.channel.name"); configurationFieldModel2.setFieldValue("test-channel.common.channel.name-value"); configurationModel.put(configurationFieldModel); configurationModel.put(configurationFieldModel2); ConfigurationJobModel configurationJobModel = new ConfigurationJobModel(UUID.randomUUID(), Set.of(configurationModel)); Mockito.when(configurationAccessor.getJobById(Mockito.any())).thenReturn(Optional.of(configurationJobModel)); DefaultAuditAccessor auditUtility = new DefaultAuditAccessor(auditEntryRepository, auditNotificationRepository, configurationAccessor, 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()); }
@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 commonConfigId = 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(commonConfigId); 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<ConfigurationJobModel> commonConfig = Optional.empty(); try { commonConfig = configurationAccessor.getJobById(commonConfigId); } catch (AlertDatabaseConstraintException e) { logger.error("There was an issue accessing the job."); } String distributionConfigName = null; String eventType = null; if (commonConfig.isPresent()) { distributionConfigName = commonConfig.get().getName(); eventType = commonConfig.get().getChannelName(); } String statusDisplayName = null; if (null != status) { statusDisplayName = status.getDisplayName(); } AuditJobStatusModel auditJobStatusModel = new AuditJobStatusModel(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); }
DefaultAuditAccessor implements AuditAccessor { @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 commonConfigId = 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(commonConfigId); 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<ConfigurationJobModel> commonConfig = Optional.empty(); try { commonConfig = configurationAccessor.getJobById(commonConfigId); } catch (AlertDatabaseConstraintException e) { logger.error("There was an issue accessing the job."); } String distributionConfigName = null; String eventType = null; if (commonConfig.isPresent()) { distributionConfigName = commonConfig.get().getName(); eventType = commonConfig.get().getChannelName(); } String statusDisplayName = null; if (null != status) { statusDisplayName = status.getDisplayName(); } AuditJobStatusModel auditJobStatusModel = new AuditJobStatusModel(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); } }
DefaultAuditAccessor implements AuditAccessor { @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 commonConfigId = 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(commonConfigId); 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<ConfigurationJobModel> commonConfig = Optional.empty(); try { commonConfig = configurationAccessor.getJobById(commonConfigId); } catch (AlertDatabaseConstraintException e) { logger.error("There was an issue accessing the job."); } String distributionConfigName = null; String eventType = null; if (commonConfig.isPresent()) { distributionConfigName = commonConfig.get().getName(); eventType = commonConfig.get().getChannelName(); } String statusDisplayName = null; if (null != status) { statusDisplayName = status.getDisplayName(); } AuditJobStatusModel auditJobStatusModel = new AuditJobStatusModel(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); } @Autowired  DefaultAuditAccessor(AuditEntryRepository auditEntryRepository, AuditNotificationRepository auditNotificationRepository, ConfigurationAccessor configurationAccessor,
        DefaultNotificationAccessor notificationAccessor, ContentConverter contentConverter); }
DefaultAuditAccessor implements AuditAccessor { @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 commonConfigId = 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(commonConfigId); 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<ConfigurationJobModel> commonConfig = Optional.empty(); try { commonConfig = configurationAccessor.getJobById(commonConfigId); } catch (AlertDatabaseConstraintException e) { logger.error("There was an issue accessing the job."); } String distributionConfigName = null; String eventType = null; if (commonConfig.isPresent()) { distributionConfigName = commonConfig.get().getName(); eventType = commonConfig.get().getChannelName(); } String statusDisplayName = null; if (null != status) { statusDisplayName = status.getDisplayName(); } AuditJobStatusModel auditJobStatusModel = new AuditJobStatusModel(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); } @Autowired  DefaultAuditAccessor(AuditEntryRepository auditEntryRepository, AuditNotificationRepository auditNotificationRepository, ConfigurationAccessor configurationAccessor,
        DefaultNotificationAccessor notificationAccessor, ContentConverter contentConverter); @Override Optional<Long> findMatchingAuditId(Long notificationId, UUID commonDistributionId); @Override @Transactional Optional<AuditJobStatusModel> findFirstByJobId(UUID jobId); @Override @Transactional AuditEntryPageModel getPageOfAuditEntries(Integer pageNumber, Integer pageSize, String searchTerm, String sortField, String sortOrder, boolean onlyShowSentNotifications,
        Function<AlertNotificationModel, AuditEntryModel> notificationToAuditEntryConverter); @Override @Transactional Map<Long, Long> createAuditEntry(Map<Long, Long> existingNotificationIdToAuditId, UUID jobId, MessageContentGroup contentGroup); @Override @Transactional void setAuditEntrySuccess(Collection<Long> auditEntryIds); @Override @Transactional void setAuditEntryFailure(Collection<Long> auditEntryIds, String errorMessage, Throwable t); @Override @Transactional AuditEntryModel convertToAuditEntryModelFromNotification(AlertNotificationModel notificationContentEntry); }
DefaultAuditAccessor implements AuditAccessor { @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 commonConfigId = 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(commonConfigId); 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<ConfigurationJobModel> commonConfig = Optional.empty(); try { commonConfig = configurationAccessor.getJobById(commonConfigId); } catch (AlertDatabaseConstraintException e) { logger.error("There was an issue accessing the job."); } String distributionConfigName = null; String eventType = null; if (commonConfig.isPresent()) { distributionConfigName = commonConfig.get().getName(); eventType = commonConfig.get().getChannelName(); } String statusDisplayName = null; if (null != status) { statusDisplayName = status.getDisplayName(); } AuditJobStatusModel auditJobStatusModel = new AuditJobStatusModel(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); } @Autowired  DefaultAuditAccessor(AuditEntryRepository auditEntryRepository, AuditNotificationRepository auditNotificationRepository, ConfigurationAccessor configurationAccessor,
        DefaultNotificationAccessor notificationAccessor, ContentConverter contentConverter); @Override Optional<Long> findMatchingAuditId(Long notificationId, UUID commonDistributionId); @Override @Transactional Optional<AuditJobStatusModel> findFirstByJobId(UUID jobId); @Override @Transactional AuditEntryPageModel getPageOfAuditEntries(Integer pageNumber, Integer pageSize, String searchTerm, String sortField, String sortOrder, boolean onlyShowSentNotifications,
        Function<AlertNotificationModel, AuditEntryModel> notificationToAuditEntryConverter); @Override @Transactional Map<Long, Long> createAuditEntry(Map<Long, Long> existingNotificationIdToAuditId, UUID jobId, MessageContentGroup contentGroup); @Override @Transactional void setAuditEntrySuccess(Collection<Long> auditEntryIds); @Override @Transactional void setAuditEntryFailure(Collection<Long> auditEntryIds, String errorMessage, Throwable t); @Override @Transactional AuditEntryModel convertToAuditEntryModelFromNotification(AlertNotificationModel notificationContentEntry);  }