AlertDispatchDAO { @RequiresSession public List<AlertNoticeEntity> findAllNotices() { TypedQuery<AlertNoticeEntity> query = entityManagerProvider.get().createNamedQuery( "AlertNoticeEntity.findAll", AlertNoticeEntity.class); return daoUtils.selectList(query); }  @RequiresSession AlertGroupEntity findGroupById(long groupId); @RequiresSession List<AlertGroupEntity> findGroupsById(List<Long> groupIds); @RequiresSession AlertTargetEntity findTargetById(long targetId); @RequiresSession List<AlertTargetEntity> findTargetsById(List<Long> targetIds); @RequiresSession AlertNoticeEntity findNoticeById(long noticeId); @RequiresSession AlertNoticeEntity findNoticeByUuid(String uuid); @RequiresSession List<AlertNoticeEntity> findPendingNotices(); @RequiresSession AlertGroupEntity findGroupByName(long clusterId, String groupName); @RequiresSession AlertTargetEntity findTargetByName(String targetName); @RequiresSession List<AlertGroupEntity> findAllGroups(); @RequiresSession List<AlertGroupEntity> findAllGroups(long clusterId); @RequiresSession List<AlertTargetEntity> findAllTargets(); @RequiresSession List<AlertTargetEntity> findAllGlobalTargets(); @RequiresSession List<AlertGroupEntity> findGroupsByDefinition(
      AlertDefinitionEntity definitionEntity); @RequiresSession AlertGroupEntity findDefaultServiceGroup(long clusterId,
      String serviceName); @RequiresSession List<AlertNoticeEntity> findAllNotices(); @RequiresSession List<AlertNoticeEntity> findAllNotices(AlertNoticeRequest request); @RequiresSession int getNoticesCount(Predicate predicate); @Transactional void createGroups(List<AlertGroupEntity> entities); @Transactional void create(AlertGroupEntity group); @Transactional AlertGroupEntity createDefaultGroup(long clusterId, String serviceName); @Transactional void refresh(AlertGroupEntity alertGroup); @Transactional AlertGroupEntity merge(AlertGroupEntity alertGroup); @Transactional void remove(AlertGroupEntity alertGroup); @Transactional void removeAllGroups(long clusterId); @Transactional void createTargets(List<AlertTargetEntity> entities); @Transactional List<AlertNoticeEntity> createNotices(List<AlertNoticeEntity> entities); @Transactional void create(AlertTargetEntity alertTarget); @Transactional void refresh(AlertTargetEntity alertTarget); @Transactional AlertTargetEntity merge(AlertTargetEntity alertTarget); @Transactional void remove(AlertTargetEntity alertTarget); @Transactional void create(AlertNoticeEntity alertNotice); @Transactional void refresh(AlertNoticeEntity alertNotice); @Transactional AlertNoticeEntity merge(AlertNoticeEntity alertNotice); @Transactional void remove(AlertNoticeEntity alertNotice); @Transactional void removeNoticeByDefinitionId(long definitionId);  }
@Test public void testAlertNoticePredicate() throws Exception { m_helper.addHost(m_clusters, m_cluster, HOSTNAME); m_helper.installHdfsService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_helper.installYarnService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_alertHelper.populateData(m_cluster); Predicate clusterPredicate = null; Predicate hdfsPredicate = null; Predicate yarnPredicate = null; Predicate adminPredicate = null; Predicate adminOrOperatorPredicate = null; Predicate pendingPredicate = null; Predicate noticeIdPredicate = null; clusterPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_CLUSTER_NAME).equals("c1").toPredicate(); hdfsPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_SERVICE_NAME).equals("HDFS").toPredicate(); yarnPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_SERVICE_NAME).equals("YARN").toPredicate(); adminPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_TARGET_NAME).equals( "Administrators").toPredicate(); adminOrOperatorPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_TARGET_NAME).equals( "Administrators").or().property( AlertNoticeResourceProvider.ALERT_NOTICE_TARGET_NAME).equals( "Operators").toPredicate(); pendingPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_STATE).equals( NotificationState.PENDING.name()).toPredicate(); AlertNoticeRequest request = new AlertNoticeRequest(); request.Predicate = clusterPredicate; List<AlertNoticeEntity> notices = m_dao.findAllNotices(request); assertEquals(3, notices.size()); request.Predicate = hdfsPredicate; notices = m_dao.findAllNotices(request); assertEquals(2, notices.size()); request.Predicate = yarnPredicate; notices = m_dao.findAllNotices(request); assertEquals(1, notices.size()); request.Predicate = adminPredicate; notices = m_dao.findAllNotices(request); assertEquals(2, notices.size()); request.Predicate = adminOrOperatorPredicate; notices = m_dao.findAllNotices(request); assertEquals(3, notices.size()); request.Predicate = pendingPredicate; notices = m_dao.findAllNotices(request); assertEquals(1, notices.size()); noticeIdPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_ID).equals( notices.get(0).getNotificationId()).toPredicate(); request.Predicate = noticeIdPredicate; notices = m_dao.findAllNotices(request); assertEquals(1, notices.size()); }
@Test public void testAlertNoticePagination() throws Exception { m_helper.addHost(m_clusters, m_cluster, HOSTNAME); m_helper.installHdfsService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_helper.installYarnService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_alertHelper.populateData(m_cluster); AlertNoticeRequest request = new AlertNoticeRequest(); request.Pagination = null; List<AlertNoticeEntity> notices = m_dao.findAllNotices(request); assertEquals(3, notices.size()); request.Pagination = new PageRequestImpl(StartingPoint.Beginning, 2, 0, null, null); notices = m_dao.findAllNotices(request); assertEquals(2, notices.size()); request.Pagination = new PageRequestImpl(StartingPoint.Beginning, 1, 2, null, null); notices = m_dao.findAllNotices(request); assertEquals(1, notices.size()); request.Pagination = new PageRequestImpl(StartingPoint.Beginning, 1, 3, null, null); notices = m_dao.findAllNotices(request); assertEquals(0, notices.size()); }
@Test public void testAlertNoticeSorting() throws Exception { m_helper.addHost(m_clusters, m_cluster, HOSTNAME); m_helper.installHdfsService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_helper.installYarnService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_alertHelper.populateData(m_cluster); List<SortRequestProperty> sortProperties = new ArrayList<>(); SortRequest sortRequest = new SortRequestImpl(sortProperties); AlertNoticeRequest request = new AlertNoticeRequest(); request.Sort = sortRequest; Predicate clusterPredicate = new PredicateBuilder().property( AlertNoticeResourceProvider.ALERT_NOTICE_CLUSTER_NAME).equals("c1").toPredicate(); request.Predicate = clusterPredicate; sortProperties.add(new SortRequestProperty( AlertNoticeResourceProvider.ALERT_NOTICE_ID, Order.ASC)); List<AlertNoticeEntity> notices = m_dao.findAllNotices(request); assertEquals(3, notices.size()); long lastId = 0L; for (AlertNoticeEntity notice : notices) { if (lastId == 0L) { lastId = notice.getNotificationId(); continue; } long currentId = notice.getNotificationId(); assertTrue(lastId < currentId); lastId = currentId; } sortProperties.clear(); sortProperties.add(new SortRequestProperty( AlertNoticeResourceProvider.ALERT_NOTICE_ID, Order.DESC)); notices = m_dao.findAllNotices(request); assertEquals(3, notices.size()); lastId = 0L; for (AlertNoticeEntity notice : notices) { if (lastId == 0L) { lastId = notice.getNotificationId(); continue; } long currentId = notice.getNotificationId(); assertTrue(lastId > currentId); lastId = currentId; } }