use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto 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.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class DefaultNotificationServiceIntegrationTest method testSendWithAttachments.
@Test
public void testSendWithAttachments() {
NotificationConfigurationDto config = createConfig();
IdmNotificationTemplateDto template = createTestTemplate();
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
//
IdmAttachmentDto attachmentOne = DefaultAttachmentManagerIntegrationTest.prepareDtoWithoutContent();
// prepared attachment
attachmentOne.setInputData(IOUtils.toInputStream("mock-content"));
IdmAttachmentDto attachment = DefaultAttachmentManagerIntegrationTest.prepareDtoWithoutContent();
attachment.setInputData(IOUtils.toInputStream("mock-content"));
IdmAttachmentDto attachmentTwo = attachmentManager.saveAttachment(identity, attachment);
List<IdmAttachmentDto> attachments = Lists.newArrayList(attachmentOne, attachmentTwo);
//
List<IdmNotificationLogDto> notifications = notificationManager.send(config.getTopic(), new IdmMessageDto.Builder().setTemplate(template).build(), null, Lists.newArrayList(identity), attachments);
Assert.assertEquals(1, notifications.size());
Assert.assertTrue(notifications.stream().anyMatch(n -> n.getType().equals(IdmEmailLog.NOTIFICATION_TYPE)));
//
IdmNotificationLogDto notification = notifications.get(0);
//
IdmNotificationAttachmentFilter notificationAttachmentFilter = new IdmNotificationAttachmentFilter();
notificationAttachmentFilter.setNotification(notification.getId());
List<IdmNotificationAttachmentDto> notificationAttachments = notificationAttachmentService.find(notificationAttachmentFilter, null).getContent();
Assert.assertEquals(2, notificationAttachments.size());
Assert.assertTrue(notificationAttachments.stream().allMatch(na -> na.getAttachment() != null));
Assert.assertTrue(notificationAttachments.stream().anyMatch(na -> na.getAttachment().equals(attachmentTwo.getId())));
//
notificationLogService.delete(notification);
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class IdmNotificationAttachmentControllerTest method testFindByText.
@Test
public void testFindByText() {
IdmNotificationAttachmentDto attachmentOne = createDto();
// other
createDto();
//
IdmNotificationAttachmentFilter filter = new IdmNotificationAttachmentFilter();
filter.setText(attachmentOne.getName());
List<IdmNotificationAttachmentDto> attachments = find(filter);
Assert.assertEquals(1, attachments.size());
Assert.assertTrue(attachments.stream().anyMatch(r -> r.getId().equals(attachmentOne.getId())));
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class IdmNotificationAttachmentControllerTest method prepareDto.
@Override
protected IdmNotificationAttachmentDto prepareDto() {
IdmNotificationLogDto notification = new IdmNotificationLogDto();
notification.setMessage(new IdmMessageDto.Builder(NotificationLevel.SUCCESS).setMessage(getHelper().createName()).build());
// related notification
notification = notificationLogService.save(notification);
//
IdmAttachmentDto attachment = attachmentManager.save(DefaultAttachmentManagerIntegrationTest.prepareDto());
//
IdmNotificationAttachmentDto dto = new IdmNotificationAttachmentDto();
dto.setAttachment(attachment.getId());
dto.setName(attachment.getName());
dto.setNotification(notification.getId());
//
return dto;
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class DefaultEmailer method send.
@Transactional
public boolean send(IdmEmailLogDto emailLog) {
LOG.debug("Sending email [{}]", emailLog);
//
UUID notificationId = emailLog.getId();
if (ObjectUtils.isEmpty(emailLog.getRecipients())) {
LOG.info("Email recipiets is empty. Email [{}] is logged only.", emailLog);
emailLogService.setEmailSentLog(notificationId, "Email recipients is empty. Email was logged only.");
return false;
}
try {
Endpoint endpoint = configureEndpoint();
//
// If message contain template transform message, otherwise get simple message
IdmMessageDto idmMessage = notificationTemplateService.buildMessage(emailLog.getMessage(), true);
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setHeaders(createEmailHeaders(emailLog));
// message - html has higher priority
String message = idmMessage.getHtmlMessage();
if (StringUtils.isEmpty(message)) {
message = idmMessage.getTextMessage();
}
in.setBody(message);
for (IdmAttachmentDto attachment : emailLog.getAttachments()) {
// charset is required for the 'text/*' mime types - TODO: append encoding only there.
UUID attachmentId = attachment.getId();
String attachmentName = attachment.getName();
String mimeTypeWithCharset = String.format("%s; charset=%s", attachment.getMimetype(), attachment.getEncoding());
InputStream attachmentData = attachmentManager.getAttachmentData(attachmentId);
DataSource dataSource = new javax.mail.util.ByteArrayDataSource(attachmentData, mimeTypeWithCharset);
// TODO: check attachment with same names?
LOG.info("Sending attachment [{}] for email [{}]", attachment.getName(), notificationId);
//
in.addAttachment(attachment.getName(), new DataHandler(dataSource));
//
IdmNotificationAttachmentDto notificationAttachment = new IdmNotificationAttachmentDto();
notificationAttachment.setNotification(notificationId);
notificationAttachment.setName(attachmentName);
notificationAttachment.setAttachment(attachment.getId());
notificationAttachmentService.save(notificationAttachment);
}
//
entityEventManager.publishEvent(new DefaultSendOperation(emailLog, endpoint, exchange));
//
return true;
} catch (Exception ex) {
LOG.error("Sending email [{}] failed: [{}]", emailLog, ex);
emailLogService.setEmailSentLog(emailLog.getId(), StringUtils.abbreviate(ex.toString(), DefaultFieldLengths.LOG));
return false;
}
}
Aggregations