use of eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto in project CzechIdMng by bcvsolutions.
the class DefaultNotificationServiceIntegrationTest method testReferentialIntegrity.
@Test
public void testReferentialIntegrity() {
// delete recipients and children
NotificationConfigurationDto config = createConfig();
IdmNotificationTemplateDto template = createTestTemplate();
IdmIdentityDto identityOne = getHelper().createIdentity((GuardedString) null);
IdmIdentityDto identityTwo = getHelper().createIdentity((GuardedString) null);
IdmIdentityDto identitySender = getHelper().createIdentity((GuardedString) null);
List<IdmNotificationLogDto> notifications = notificationManager.send(config.getTopic(), new IdmMessageDto.Builder().setTemplate(template).build(), identitySender, Lists.newArrayList(identityOne, identityTwo));
Assert.assertEquals(1, notifications.size());
List<IdmNotificationLogDto> notificationOthers = notificationManager.send(config.getTopic(), new IdmMessageDto.Builder().setTemplate(template).build(), identitySender, Lists.newArrayList(identityOne, identityTwo));
Assert.assertEquals(1, notificationOthers.size());
IdmNotificationLogDto notificationOther = notificationLogService.get(notificationOthers.get(0));
//
IdmNotificationLogDto notification = notificationLogService.get(notifications.get(0));
Assert.assertNotNull(notification);
Assert.assertEquals(identitySender.getId(), notification.getIdentitySender());
IdmNotificationFilter notificationFilter = new IdmNotificationFilter();
notificationFilter.setParent(notification.getId());
List<IdmEmailLogDto> emails = emailLogService.find(notificationFilter, null).getContent();
Assert.assertEquals(1, emails.size());
IdmEmailLogDto emailNotification = emails.get(0);
Assert.assertEquals(notification.getId(), emailNotification.getParent());
//
IdmNotificationRecipientFilter recipientFilter = new IdmNotificationRecipientFilter();
recipientFilter.setNotification(notification.getId());
List<IdmNotificationRecipientDto> notificationRecipients = recipientService.find(recipientFilter, null).getContent();
Assert.assertEquals(2, notificationRecipients.size());
Assert.assertTrue(notificationRecipients.stream().anyMatch(r -> r.getIdentityRecipient().equals(identityOne.getId())));
Assert.assertTrue(notificationRecipients.stream().anyMatch(r -> r.getIdentityRecipient().equals(identityTwo.getId())));
recipientFilter.setNotification(emailNotification.getId());
List<IdmNotificationRecipientDto> emailRecipients = recipientService.find(recipientFilter, null).getContent();
Assert.assertEquals(2, emailRecipients.size());
Assert.assertTrue(emailRecipients.stream().anyMatch(r -> r.getIdentityRecipient().equals(identityOne.getId())));
Assert.assertTrue(emailRecipients.stream().anyMatch(r -> r.getIdentityRecipient().equals(identityTwo.getId())));
//
// sender is removed from notifications when sender identity was deleted
IdmNotificationFilter senderFilter = new IdmNotificationFilter();
senderFilter.setIdentitySender(identitySender.getId());
List<IdmNotificationLogDto> notificationLogDtos = notificationLogService.find(senderFilter, null).getContent();
Assert.assertFalse(notificationLogDtos.isEmpty());
notificationLogDtos.forEach(notif -> {
Assert.assertEquals(identitySender.getId(), notif.getIdentitySender());
});
identityService.delete(identitySender);
Assert.assertNull(identityService.get(identitySender.getId()));
// shouldn't throw despite the fact that identitySender contained in notificationLogDtos has been deleted
notificationLogDtos = notificationLogService.find(senderFilter, null).getContent();
//
// create notifications and attachments
// other owner
IdmAttachmentDto attachmentOther = attachmentManager.save(DefaultAttachmentManagerIntegrationTest.prepareDto());
IdmAttachmentDto attachment = DefaultAttachmentManagerIntegrationTest.prepareDto();
attachment.setOwnerType(null);
attachment.setInputData(IOUtils.toInputStream("mock content"));
attachment = attachmentManager.saveAttachment(notification, attachment);
//
IdmNotificationAttachmentDto notificationAttachment = new IdmNotificationAttachmentDto();
notificationAttachment.setAttachment(attachment.getId());
notificationAttachment.setName(attachment.getName());
notificationAttachment.setNotification(notification.getId());
notificationAttachment = notificationAttachmentService.save(notificationAttachment);
Assert.assertNotNull(attachmentManager.get(attachment));
Assert.assertNotNull(notificationAttachmentService.get(notificationAttachment));
//
IdmNotificationAttachmentDto notificationAttachmentOther = new IdmNotificationAttachmentDto();
notificationAttachmentOther.setAttachment(attachmentOther.getId());
notificationAttachmentOther.setName(attachmentOther.getName());
notificationAttachmentOther.setNotification(notificationOther.getId());
notificationAttachmentOther = notificationAttachmentService.save(notificationAttachmentOther);
Assert.assertNotNull(attachmentManager.get(attachmentOther));
Assert.assertNotNull(notificationAttachmentService.get(notificationAttachmentOther));
//
// delete parent notification
notificationLogService.delete(notification);
//
// all removed
Assert.assertNull(notificationLogService.get(notification));
Assert.assertNull(notificationLogService.get(emailNotification));
recipientFilter.setNotification(notification.getId());
Assert.assertTrue(recipientService.find(recipientFilter, null).getContent().isEmpty());
recipientFilter.setNotification(emailNotification.getId());
Assert.assertTrue(recipientService.find(recipientFilter, null).getContent().isEmpty());
Assert.assertNull(attachmentManager.get(attachment));
Assert.assertNotNull(attachmentManager.get(attachmentOther));
Assert.assertNull(notificationAttachmentService.get(notificationAttachment));
Assert.assertNotNull(notificationAttachmentService.get(notificationAttachmentOther));
//
notificationLogService.delete(notificationOther);
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto in project CzechIdMng by bcvsolutions.
the class IdmEmailLogControllerRestTest method prepareDto.
@Override
protected IdmEmailLogDto prepareDto() {
IdmIdentityDto recipient = getHelper().createIdentity((GuardedString) null);
IdmEmailLogDto dto = new IdmEmailLogDto();
dto.setMessage(new IdmMessageDto.Builder(NotificationLevel.SUCCESS).setMessage(getHelper().createName()).build());
dto.getRecipients().add(new IdmNotificationRecipientDto(recipient.getId()));
//
return dto;
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto in project CzechIdMng by bcvsolutions.
the class IdmEmailLogControllerRestTest method testFindByState.
@Test
public void testFindByState() {
String message = getHelper().createName();
IdmEmailLogDto emailLog = prepareDto();
emailLog.setMessage(new IdmMessageDto.Builder(NotificationLevel.SUCCESS).setMessage(message).build());
emailLog.setSent(ZonedDateTime.now());
IdmEmailLogDto emailLogOne = createDto(emailLog);
// other
emailLog = prepareDto();
emailLog.setMessage(new IdmMessageDto.Builder(NotificationLevel.SUCCESS).setMessage(message).build());
emailLog.setSent(null);
IdmEmailLogDto emailLogTwo = createDto(emailLog);
//
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("text", message);
parameters.set("state", NotificationState.ALL.name());
List<IdmEmailLogDto> results = find(parameters);
Assert.assertEquals(1, results.size());
Assert.assertTrue(results.stream().anyMatch(r -> r.getId().equals(emailLogOne.getId())));
//
parameters.set("state", NotificationState.NOT.name());
results = find(parameters);
Assert.assertEquals(1, results.size());
Assert.assertTrue(results.stream().anyMatch(r -> r.getId().equals(emailLogTwo.getId())));
//
parameters.set("state", NotificationState.PARTLY.name());
results = find(parameters);
Assert.assertTrue(results.isEmpty());
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmNotificationTemplateServiceIntegrationTest method templateReferentialIntegrityTest.
@Test
public void templateReferentialIntegrityTest() {
IdmNotificationTemplateDto template = new IdmNotificationTemplateDto();
template.setCode(getHelper().createName());
template.setName(getHelper().createName());
template.setSubject(getHelper().createName());
template = notificationTemplateService.save(template);
// Template Dto successfully saved
IdmNotificationTemplateFilter templateFilter = new IdmNotificationTemplateFilter();
templateFilter.setText(template.getCode());
assertEquals(1, notificationTemplateService.find(templateFilter, null).getContent().size());
// Prevent from template deleting if used in notification configuration
NotificationConfigurationDto notificationCfgDto = new NotificationConfigurationDto(getHelper().createName(), NotificationLevel.INFO, getHelper().createName(), getHelper().createName(), template.getId());
notificationCfgDto = notificationConfigService.save(notificationCfgDto);
try {
notificationTemplateService.delete(template);
fail("Template deleted although used in a notification configuration.");
} catch (ResultCodeException e) {
// Success
} catch (Exception e) {
fail(e.getMessage());
}
notificationConfigService.delete(notificationCfgDto);
// Found proper notification according to the used template
IdmMessageDto message = new IdmMessageDto.Builder().setTemplate(template).build();
List<IdmEmailLogDto> emailLogDtos = emailSenderService.send(getHelper().createName(), message);
assertEquals(1, emailLogDtos.size());
IdmNotificationFilter notificationFilter = new IdmNotificationFilter();
notificationFilter.setTemplateId(template.getId());
List<IdmEmailLogDto> foundNotificationDtos = emailLogService.find(notificationFilter, null).getContent();
assertEquals(1, foundNotificationDtos.size());
assertEquals(emailLogDtos.get(0).getId(), foundNotificationDtos.get(0).getId());
// Prevent from template deleting if used in notification
try {
notificationTemplateService.delete(template);
fail("Template deleted although used in a notification.");
} catch (ResultCodeException e) {
// Success
} catch (Exception e) {
fail(e.getMessage());
}
emailLogService.delete(emailLogDtos.get(0));
// Template Dto successfully deleted
notificationTemplateService.delete(template);
assertEquals(0, notificationTemplateService.find(templateFilter, null).getContent().size());
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto in project CzechIdMng by bcvsolutions.
the class DefaultEmailNotificationSender method createLog.
/**
* Persists new emailog record from given notification
*
* @param notification
* @return
*/
private IdmEmailLogDto createLog(IdmNotificationDto notification) {
Assert.notNull(notification, "Notification is required.");
Assert.notNull(notification.getMessage(), "Message is required.");
//
IdmEmailLogDto emailLog = new IdmEmailLogDto();
// parent message
if (notification.getId() != null) {
emailLog.setParent(notification.getId());
}
// clone message
emailLog.setMessage(cloneMessage(notification));
// clone recipients - resolve real email
for (IdmNotificationRecipientDto recipient : notification.getRecipients()) {
emailLog.getRecipients().add(cloneRecipient(emailLog, recipient));
}
emailLog.setIdentitySender(notification.getIdentitySender());
emailLog.setType(IdmEmailLog.NOTIFICATION_TYPE);
emailLog.setAttachments(notification.getAttachments());
emailLog.setTopic(notification.getTopic());
emailLog = emailLogService.save(emailLog);
// TODO: remove after attachments will be persisted
emailLog.setAttachments(notification.getAttachments());
return emailLog;
}
Aggregations