use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class IdmNotificationAttachmentControllerTest method testDownload.
@Test
public void testDownload() throws Exception {
NotificationConfigurationDto config = createConfig();
IdmNotificationTemplateDto template = createTestTemplate();
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
//
IdmAttachmentDto attachmentOne = DefaultAttachmentManagerIntegrationTest.prepareDtoWithoutContent();
// prepared attachment
attachmentOne.setInputData(IOUtils.toInputStream(getHelper().createName()));
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());
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class IdmNotificationAttachmentControllerTest method testFindByNotification.
@Test
public void testFindByNotification() {
IdmNotificationAttachmentDto attachmentOne = createDto();
// other
createDto();
//
IdmNotificationAttachmentFilter filter = new IdmNotificationAttachmentFilter();
filter.setNotification(attachmentOne.getNotification());
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 IdmNotificationAttachmentController method download.
@RequestMapping(value = "/{backendId}/download", method = RequestMethod.GET)
@ResponseBody
@PreAuthorize("hasAuthority('" + NotificationGroupPermission.NOTIFICATION_READ + "')")
@ApiOperation(value = "Download notification attachment", nickname = "downloadNotificationAttachment", tags = { IdmNotificationAttachmentController.TAG }, notes = "Returns input stream to notification attachment.", authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = NotificationGroupPermission.NOTIFICATION_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = NotificationGroupPermission.NOTIFICATION_READ, description = "") }) })
public ResponseEntity<InputStreamResource> download(@ApiParam(value = "Notification attachment uuid identifier.", required = true) @PathVariable String backendId) {
IdmNotificationAttachmentDto dto = getDto(backendId);
if (dto == null) {
throw new EntityNotFoundException(getService().getEntityClass(), backendId);
}
//
UUID attachmentId = dto.getAttachment();
IdmAttachmentDto attachment = attachmentManager.get(attachmentId);
if (attachment == null) {
throw new EntityNotFoundException(attachmentManager.getEntityClass(), attachmentId);
}
//
InputStream is = attachmentManager.getAttachmentData(attachment.getId());
//
try {
BodyBuilder response = ResponseEntity.ok().contentLength(is.available()).header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", attachment.getName()));
// append media type, if it's filled
String mimetype = attachment.getMimetype();
if (StringUtils.isNotBlank(mimetype)) {
response = response.contentType(MediaType.valueOf(attachment.getMimetype()));
}
//
return response.body(new InputStreamResource(is));
} catch (IOException e) {
throw new ResultCodeException(CoreResultCode.INTERNAL_SERVER_ERROR, e);
}
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class DefaultRptReportManagerIntegrationTest method testSendDefaultNotificationAfterEnd.
@Test
public void testSendDefaultNotificationAfterEnd() throws Exception {
IdmIdentityDto identity = getHelper().createIdentity();
//
try {
// report is sent to logged identity by default
getHelper().login(identity);
//
RptReportDto report = new RptReportDto();
report.setExecutorName(TestReportExecutor.REPORT_NAME);
report = manager.generate(report);
UUID reportId = report.getId();
Assert.assertNotNull(reportId);
Assert.assertNotNull(report.getData());
//
try (InputStream is = attachmentManager.getAttachmentData(report.getData())) {
Assert.assertEquals(mapper.writeValueAsString(TestReportExecutor.identities), IOUtils.toString(is));
}
//
// test notification is sent
IdmNotificationFilter notificationFilter = new IdmNotificationFilter();
notificationFilter.setTopic(RptModuleDescriptor.TOPIC_REPORT_GENERATE_SUCCESS);
notificationFilter.setRecipient(identity.getUsername());
notificationFilter.setNotificationType(IdmNotificationLog.class);
List<IdmNotificationLogDto> notifications = notificationService.find(notificationFilter, null).getContent();
Assert.assertEquals(1, notifications.size());
//
// test notification attachments
IdmNotificationAttachmentFilter notificationAttachmentFilter = new IdmNotificationAttachmentFilter();
notificationAttachmentFilter.setNotification(notifications.get(0).getId());
List<IdmNotificationAttachmentDto> notificationAttachments = notificationAttachmentService.find(notificationAttachmentFilter, null).getContent();
Assert.assertEquals(1, notificationAttachments.size());
Assert.assertNotNull(notificationAttachments.get(0).getAttachment());
//
reportService.delete(report);
attachmentManager.deleteAttachments(report);
} finally {
logout();
}
}
use of eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto in project CzechIdMng by bcvsolutions.
the class DefaultNotificationServiceIntegrationTest method testSendWithWrongAttachments.
@Test
public void testSendWithWrongAttachments() {
NotificationConfigurationDto config = createConfig();
IdmNotificationTemplateDto template = createTestTemplate();
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
// attachment without content
IdmAttachmentDto attachment = attachmentManager.save(DefaultAttachmentManagerIntegrationTest.prepareDto());
//
List<IdmNotificationLogDto> notifications = notificationManager.send(config.getTopic(), new IdmMessageDto.Builder().setTemplate(template).build(), null, Lists.newArrayList(identity), Lists.newArrayList(attachment));
Assert.assertEquals(1, notifications.size());
Assert.assertTrue(notifications.stream().anyMatch(n -> n.getType().equals(IdmEmailLog.NOTIFICATION_TYPE)));
//
IdmNotificationLogDto notification = notifications.get(0);
Assert.assertEquals(NotificationState.NOT, notification.getState());
//
IdmNotificationAttachmentFilter notificationAttachmentFilter = new IdmNotificationAttachmentFilter();
notificationAttachmentFilter.setNotification(notification.getId());
List<IdmNotificationAttachmentDto> notificationAttachments = notificationAttachmentService.find(notificationAttachmentFilter, null).getContent();
Assert.assertEquals(1, notificationAttachments.size());
Assert.assertTrue(notificationAttachments.stream().allMatch(na -> na.getAttachment() != null));
Assert.assertTrue(notificationAttachments.stream().anyMatch(na -> na.getAttachment().equals(attachment.getId())));
//
notificationLogService.delete(notification);
}
Aggregations