@Override public void removeById(String id) { LOG.debug("Remove notification by id {}", id); CassandraNotification nf = new CassandraNotification(id); Delete.Where deleteQuery = delete().from(getColumnFamilyName()) .where(eq(NF_TOPIC_ID_PROPERTY, nf.getTopicId())) .and(eq(NF_NOTIFICATION_TYPE_PROPERTY, nf.getType().name())) .and(eq(NF_VERSION_PROPERTY, nf.getNfVersion())) .and(eq(NF_SEQ_NUM_PROPERTY, nf.getSeqNum())); LOG.trace("Remove notification by id {}", deleteQuery); execute(deleteQuery); }
@Override public CassandraNotification save(CassandraNotification object) { LOG.debug("Save notification {} ", object); if (isBlank(object.getId())) { object.generateId(); } return super.save(object); }
@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 testToDto() { NotificationDto expected = generateNotifications(null, null, null, 1, NotificationTypeDto.USER).get(0); Assert.assertEquals(expected, new CassandraNotification(expected).toDto()); } }
@Override public CassandraNotification save(NotificationDto notification) { return save(new CassandraNotification(notification)); }
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; }
/** * Get value of field <code>id</code>, if it is blank than generate this field by method * <code>generateId()</code> and return. * * @return id */ public String getStringId() { if (isBlank(id)) { generateId(); } return id; }
@Override public NotificationDto toDto() { NotificationDto dto = new NotificationDto(); dto.setId(getStringId()); dto.setApplicationId(applicationId); dto.setSchemaId(schemaId); dto.setTopicId(topicId); dto.setLastTimeModify(lastModifyTime); dto.setNfVersion(nfVersion); dto.setType(type); dto.setBody(body != null ? getBytes(body) : null); dto.setExpiredAt(expiredAt); dto.setSecNum(seqNum); return dto; }
@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); }
/** * Create new instance of <code>CassandraNotification</code>. * * @param dto data transfer object contain data that * assign on fields of new instance */ public CassandraNotification(NotificationDto dto) { this.applicationId = dto.getApplicationId(); this.schemaId = dto.getSchemaId(); this.topicId = dto.getTopicId(); this.type = dto.getType(); this.nfVersion = dto.getNfVersion(); this.seqNum = dto.getSecNum(); this.lastModifyTime = dto.getLastTimeModify(); this.body = getByteBuffer(dto.getBody()); this.expiredAt = dto.getExpiredAt(); this.seqNum = dto.getSecNum(); this.id = dto.getId(); if (isBlank(id)) { generateId(); } }
@Override public CassandraNotification findById(String id) { LOG.debug("Try to find notification by id {}", id); CassandraNotification nf = new CassandraNotification(id); Where query = select().from(getColumnFamilyName()) .where(eq(NF_TOPIC_ID_PROPERTY, nf.getTopicId())) .and(eq(NF_NOTIFICATION_TYPE_PROPERTY, nf.getType().name())) .and(eq(NF_VERSION_PROPERTY, nf.getNfVersion())) .and(eq(NF_SEQ_NUM_PROPERTY, nf.getSeqNum())); LOG.trace("Execute query {}", query); nf = findOneByStatement(query); LOG.trace("Found notification {} by id {}", nf, id); return nf; }