Search in sources :

Example 6 with IdmEmailLogDto

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);
}
Also used : Arrays(java.util.Arrays) IdmNotificationRecipientService(eu.bcvsolutions.idm.core.notification.api.service.IdmNotificationRecipientService) NotificationManager(eu.bcvsolutions.idm.core.notification.api.service.NotificationManager) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) NotificationState(eu.bcvsolutions.idm.core.notification.api.domain.NotificationState) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmNotificationConfiguration(eu.bcvsolutions.idm.core.notification.entity.IdmNotificationConfiguration) IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) IdmNotificationAttachmentFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationAttachmentFilter) NotificationLevel(eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel) IdmNotificationDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationDto) IdmEmailLogService(eu.bcvsolutions.idm.core.notification.api.service.IdmEmailLogService) Page(org.springframework.data.domain.Page) IdmNotificationTemplateDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) IdmEmailLogRepository(eu.bcvsolutions.idm.core.notification.repository.IdmEmailLogRepository) IdmNotificationTemplateService(eu.bcvsolutions.idm.core.notification.api.service.IdmNotificationTemplateService) IdmIdentityService(eu.bcvsolutions.idm.core.api.service.IdmIdentityService) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmNotificationAttachmentDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto) IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmConsoleLog(eu.bcvsolutions.idm.core.notification.entity.IdmConsoleLog) IdmNotificationLogRepository(eu.bcvsolutions.idm.core.notification.repository.IdmNotificationLogRepository) IdmNotificationConfigurationRepository(eu.bcvsolutions.idm.core.notification.repository.IdmNotificationConfigurationRepository) IdmNotificationConfigurationService(eu.bcvsolutions.idm.core.notification.api.service.IdmNotificationConfigurationService) Lists(com.google.common.collect.Lists) NotificationConfigurationDto(eu.bcvsolutions.idm.core.notification.api.dto.NotificationConfigurationDto) DefaultAttachmentManagerIntegrationTest(eu.bcvsolutions.idm.core.ecm.service.impl.DefaultAttachmentManagerIntegrationTest) InitTestDataProcessor(eu.bcvsolutions.idm.core.model.event.processor.module.InitTestDataProcessor) IdmNotificationRecipientDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) EmailNotificationSender(eu.bcvsolutions.idm.core.notification.api.service.EmailNotificationSender) Before(org.junit.Before) IdmNotificationLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationLogDto) AttachmentManager(eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) Test(org.junit.Test) IdmNotificationRecipientFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationRecipientFilter) IdmNotificationLog(eu.bcvsolutions.idm.core.notification.entity.IdmNotificationLog) IdmNotificationLogService(eu.bcvsolutions.idm.core.notification.api.service.IdmNotificationLogService) IdmEmailLog(eu.bcvsolutions.idm.core.notification.entity.IdmEmailLog) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) IdmNotificationFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationFilter) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) IdmNotificationAttachmentService(eu.bcvsolutions.idm.core.notification.api.service.IdmNotificationAttachmentService) Transactional(org.springframework.transaction.annotation.Transactional) IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) NotificationConfigurationDto(eu.bcvsolutions.idm.core.notification.api.dto.NotificationConfigurationDto) IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) IdmNotificationFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationFilter) IdmNotificationTemplateDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto) IdmNotificationRecipientFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationRecipientFilter) IdmNotificationAttachmentDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto) IdmNotificationLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationLogDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmNotificationRecipientDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto) DefaultAttachmentManagerIntegrationTest(eu.bcvsolutions.idm.core.ecm.service.impl.DefaultAttachmentManagerIntegrationTest) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 7 with IdmEmailLogDto

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;
}
Also used : IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmNotificationRecipientDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto)

Example 8 with IdmEmailLogDto

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());
}
Also used : NotificationLevel(eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel) ZonedDateTime(java.time.ZonedDateTime) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) Autowired(org.springframework.beans.factory.annotation.Autowired) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test) AbstractReadWriteDtoControllerRestTest(eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest) NotificationState(eu.bcvsolutions.idm.core.notification.api.domain.NotificationState) List(java.util.List) AbstractReadWriteDtoController(eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoController) IdmNotificationRecipientDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Assert(org.junit.Assert) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Test(org.junit.Test) AbstractReadWriteDtoControllerRestTest(eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)

Example 9 with IdmEmailLogDto

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());
}
Also used : IdmNotificationTemplateFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationTemplateFilter) NotificationConfigurationDto(eu.bcvsolutions.idm.core.notification.api.dto.NotificationConfigurationDto) IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmNotificationFilter(eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationFilter) IdmNotificationTemplateDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IOException(java.io.IOException) ConstraintViolationException(javax.validation.ConstraintViolationException) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 10 with IdmEmailLogDto

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;
}
Also used : IdmEmailLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto) IdmNotificationRecipientDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto)

Aggregations

IdmEmailLogDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmEmailLogDto)13 Transactional (org.springframework.transaction.annotation.Transactional)7 IdmMessageDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto)6 IdmNotificationRecipientDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto)6 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)3 IdmNotificationDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationDto)3 Test (org.junit.Test)3 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 NotificationLevel (eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel)2 NotificationState (eu.bcvsolutions.idm.core.notification.api.domain.NotificationState)2 IdmNotificationTemplateDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto)2 NotificationConfigurationDto (eu.bcvsolutions.idm.core.notification.api.dto.NotificationConfigurationDto)2 IdmNotificationFilter (eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationFilter)2 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 ZonedDateTime (java.time.ZonedDateTime)2 List (java.util.List)2 Assert (org.junit.Assert)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Lists (com.google.common.collect.Lists)1