@Override public NotificationDto findNotificationById(String id) { NotificationDto dto = null; LOG.debug("Find notification by id [{}] ", id); if (StringUtils.isNotBlank(id)) { dto = getDto(notificationDao.findById(id)); } LOG.trace("Found notification object {} by id [{}] ", dto, id); return dto; }
@Override public List<NotificationDto> findNotificationsByTopicId(String topicId) { validateId(topicId, "Can't find notifications. Invalid topic id: " + topicId); return convertDtoList(notificationDao.findNotificationsByTopicId(topicId)); }
@Override public List<NotificationDto> findNotificationsByTopicIdAndVersionAndStartSecNum( String topicId, int seqNum, int sysNfVersion, int userNfVersion) { validateSqlId(topicId, "Can't find notifications. Invalid topic id: " + topicId); return convertDtoList(notificationDao.findNotificationsByTopicIdAndVersionAndStartSecNum( topicId, seqNum, sysNfVersion, userNfVersion)); }
@Test public void testRemoveById() throws Exception { NotificationDto expected = generateNotifications(null, null, null, 1, NotificationTypeDto.USER).get(0); notificationDao.removeById(expected.getId()); Assert.assertNull(notificationDao.findById(expected.getId())); }
/** * Sends a notification and increments a topic sequence number. * * @param dto notification * @return saved notification */ public NotificationDto saveNotificationAndIncTopicSecNum(NotificationDto dto) { NotificationDto notificationDto = null; Topic topic = topicDao.getNextSeqNumber(dto.getTopicId()); if (topic != null) { dto.setSecNum(topic.getSequenceNumber()); Notification savedDto = notificationDao.save(dto); notificationDto = savedDto != null ? savedDto.toDto() : null; } else { LOG.warn("Can't find topic by id."); } return notificationDto; }
@Override public List<UpdateNotificationDto<EndpointGroupDto>> removeTopicById(String id) { validateId(id, "Can't remove topic. Invalid topic id " + id); TopicDto topic = findTopicById(id); List<UpdateNotificationDto<EndpointGroupDto>> notificationList = new LinkedList<>(); if (topic != null) { List<EndpointGroup> groups = endpointGroupDao.findEndpointGroupsByTopicIdAndAppId( topic.getApplicationId(), id); if (groups != null && !groups.isEmpty()) { for (EndpointGroup eg : groups) { notificationList.add(endpointService.removeTopicFromEndpointGroup(eg.getId().toString(), id)); } } topicDao.removeById(id); notificationDao.removeNotificationsByTopicId(id); } return notificationList; }
@Test public void testRemoveById() { NotificationDto notification = generateNotificationsDto(null, null, 1, null).get(0); Assert.assertNotNull(notification.getId()); notificationDao.removeById(notification.getId()); Notification found = notificationDao.findById(notification.getId()); Assert.assertNull(found); }
protected List<NotificationDto> generateNotifications(String topicId, String appId, String schemaId, int count, NotificationTypeDto type) { List<NotificationDto> notifications = new ArrayList<>(count); NotificationDto notification; for (int i = 0; i < count; i++) { notification = new NotificationDto(); notification.setApplicationId(appId != null ? appId : UUID.randomUUID().toString()); notification.setSchemaId(schemaId != null ? schemaId : UUID.randomUUID().toString()); notification.setTopicId(topicId != null ? topicId : UUID.randomUUID().toString()); notification.setType(type != null ? type : NotificationTypeDto.USER); notification.setSecNum(i); notification.setBody(UUID.randomUUID().toString().getBytes()); notification.setLastTimeModify(new Date(System.currentTimeMillis())); notification.setNfVersion(1); notification.setExpiredAt(new Date(System.currentTimeMillis() + 7 * 24 * 3600 * 1000)); notifications.add(notificationDao.save(notification).toDto()); } return notifications; }
class OrderManager { private OrderDAO oDao; private NotificationDao nDao; public saveOrder(OrderDTO o) { Long orderId = oDao.save(o); NotificationDTO n = new NotificationDTO(); n.setType(NotificationType.ORDER_CREATED); n.setEntityId(orderId); nDao.save(n); } }
@Test public void testSave() throws Exception { NotificationDto expected = generateNotifications(null, null, null, 1, NotificationTypeDto.USER).get(0); NotificationDto found = notificationDao.findById(expected.getId()).toDto(); Assert.assertEquals(expected, found); }
@Test public void testFindNotificationsByTopicId() throws Exception { String topicId = UUID.randomUUID().toString(); List<NotificationDto> expectedList = new ArrayList<>(); expectedList.addAll(generateNotifications(topicId, null, null, 2, NotificationTypeDto.USER)); expectedList.addAll(generateNotifications(topicId, null, null, 1, NotificationTypeDto.SYSTEM)); List<CassandraNotification> found = notificationDao.findNotificationsByTopicId(topicId); Assert.assertEquals(expectedList.size(), found.size()); }
@Test public void testFindNotificationsByTopicIdAndVersionAndStartSecNum() throws Exception { String topicId = UUID.randomUUID().toString(); List<NotificationDto> expectedList = new ArrayList<>(); expectedList.addAll(generateNotifications(topicId, null, null, 7, NotificationTypeDto.USER)); List<CassandraNotification> foundList = notificationDao.findNotificationsByTopicIdAndVersionAndStartSecNum(topicId, 3, 0, 1); Assert.assertEquals(3, foundList.size()); for (int i = 0; i < foundList.size(); i++) { CassandraNotification notification = foundList.get(i); Assert.assertEquals(NotificationTypeDto.USER, notification.getType()); Assert.assertEquals(topicId, notification.getTopicId()); Assert.assertTrue(3 < notification.getSeqNum()); } }
@Test public void testRemoveNotificationsByTopicId() throws Exception { String topicId = UUID.randomUUID().toString(); List<NotificationDto> expectedList = new ArrayList<>(); expectedList.addAll(generateNotifications(topicId, null, null, 2, NotificationTypeDto.USER)); expectedList.addAll(generateNotifications(topicId, null, null, 1, NotificationTypeDto.SYSTEM)); List<CassandraNotification> found = notificationDao.findNotificationsByTopicId(topicId); Assert.assertEquals(expectedList.size(), found.size()); }