@Test
public void testDownload() throws Exception {

	NotificationConfigurationDto config = createConfig();
	IdmNotificationTemplateDto template = createTestTemplate();
	IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
	//
	IdmAttachmentDto attachmentOne = DefaultAttachmentManagerIntegrationTest.prepareDtoWithoutContent();
	attachmentOne.setInputData(IOUtils.toInputStream(getHelper().createName())); // prepared attachment
	IdmAttachmentDto attachment = DefaultAttachmentManagerIntegrationTest.prepareDtoWithoutContent();
	String content = getHelper().createName();
	attachment.setInputData(IOUtils.toInputStream(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())));
	IdmNotificationAttachmentDto notificationAttachment = notificationAttachments
			.stream()
			.filter(na -> na.getAttachment().equals(attachmentTwo.getId()))
			.findFirst()
			.get();
	//
	// download attachment
	String response = getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(notificationAttachment.getId()) + "/download")
       		.with(authentication(getAdminAuthentication())))
			.andExpect(status().isOk())
               .andReturn()
               .getResponse()
               .getContentAsString();
	Assert.assertEquals(content, response);
	//
	// 404 - notification attachment not found
	getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(UUID.randomUUID()) + "/download")
       		.with(authentication(getAdminAuthentication())))
			.andExpect(status().isNotFound());
	//
	// 404 - delete attachment (e.g. simulate ecm store purge)
	attachmentManager.delete(attachmentTwo);
	getMockMvc().perform(MockMvcRequestBuilders.get(getDetailUrl(notificationAttachment.getId()) + "/download")
       		.with(authentication(getAdminAuthentication())))
			.andExpect(status().isNotFound());
}