IspnAlertsServiceImpl implements AlertsService { @Override public void addAlertTags(String tenantId, Collection<String> alertIds, Map<String, String> tags) throws Exception { if (isEmpty(tenantId)) { throw new IllegalArgumentException("TenantId must be not null"); } if (isEmpty(alertIds)) { throw new IllegalArgumentException("AlertIds must be not null"); } if (isEmpty(tags)) { throw new IllegalArgumentException("Tags must be not null"); } AlertsCriteria criteria = new AlertsCriteria(); criteria.setAlertIds(alertIds); Page<Alert> existingAlerts = getAlerts(tenantId, criteria, null); for (Alert alert : existingAlerts) { tags.entrySet().stream().forEach(tag -> alert.addTag(tag.getKey(), tag.getValue())); backend.put(pk(alert), new IspnEvent(alert)); } }  void init(); void setAlertsEngine(AlertsEngine alertsEngine); void setDefinitionsService(DefinitionsService definitionsService); void setActionsService(ActionsService actionsService); void setIncomingDataManager(IncomingDataManager incomingDataManager); void setProperties(PropertiesService properties); @Override void ackAlerts(String tenantId, Collection<String> alertIds, String ackBy, String ackNotes); @Override void addAlerts(Collection<Alert> alerts); @Override void addAlertTags(String tenantId, Collection<String> alertIds, Map<String, String> tags); @Override void addEvents(Collection<Event> events); @Override void addEventTags(String tenantId, Collection<String> eventIds, Map<String, String> tags); @Override void persistEvents(Collection<Event> events); @Override void addNote(String tenantId, String alertId, String user, String text); @Override int deleteAlerts(String tenantId, AlertsCriteria criteria); @Override int deleteEvents(String tenantId, EventsCriteria criteria); @Override Alert getAlert(String tenantId, String alertId, boolean thin); @Override Page<Alert> getAlerts(String tenantId, AlertsCriteria criteria, Pager pager); @Override Page<Alert> getAlerts(Set<String> tenantIds, AlertsCriteria criteria, Pager pager); @Override Event getEvent(String tenantId, String eventId, boolean thin); @Override Page<Event> getEvents(String tenantId, EventsCriteria criteria, Pager pager); @Override Page<Event> getEvents(Set<String> tenantIds, EventsCriteria criteria, Pager pager); @Override void removeAlertTags(String tenantId, Collection<String> alertIds, Collection<String> tags); @Override void removeEventTags(String tenantId, Collection<String> eventIds, Collection<String> tags); @Override void resolveAlerts(String tenantId, Collection<String> alertIds, String resolvedBy, String resolvedNotes, List<Set<ConditionEval>> resolvedEvalSets); @Override void resolveAlertsForTrigger(String tenantId, String triggerId, String resolvedBy, String resolvedNotes, List<Set<ConditionEval>> resolvedEvalSets); @Override void sendData(Collection<Data> data); @Override void sendData(Collection<Data> data, boolean ignoreFiltering); @Override void sendEvents(Collection<Event> events); @Override void sendEvents(Collection<Event> events, boolean ignoreFiltering);  }
@Test public void addAlertTagsTest() throws Exception { int numTenants = 2; int numTriggers = 5; int numAlerts = 2; createTestAlerts(numTenants, numTriggers, numAlerts); Set<String> tenantIds = new HashSet<>(); tenantIds.add("tenant0"); tenantIds.add("tenant1"); List<Alert> nonTaggedAlerts = alerts.getAlerts(tenantIds, null, null); assertEquals(2 * 5 * 2, nonTaggedAlerts.size()); int count = 0; for (Alert alert : nonTaggedAlerts) { Map<String, String> tags = new HashMap<>(); if (count < 5) { tags.put("xyztag1", "value" + (count % 5)); } else if (count >= 5 && count < 10) { tags.put("xyztag2", "value" + (count % 5)); } else { tags.put("xyztag3", "value" + (count % 5)); } alerts.addAlertTags(alert.getTenantId(), Arrays.asList(alert.getAlertId()), tags); count++; } AlertsCriteria criteria = new AlertsCriteria(); criteria.setTagQuery("xyztag1"); List<Alert> tag1Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(5, tag1Alerts.size()); criteria.setTagQuery("xyztag2"); List<Alert> tag2Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(5, tag2Alerts.size()); criteria.setTagQuery("xyztag3"); List<Alert> tag3Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(10, tag3Alerts.size()); criteria.setTagQuery("xyztag1 = 'value1'"); List<Alert> tag1Value1Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(1, tag1Value1Alerts.size()); criteria.setTagQuery("xyztag2 = 'value1'"); List<Alert> tag2Value1Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(1, tag2Value1Alerts.size()); criteria.setTagQuery("xyztag3 = 'value2'"); List<Alert> tag3Value2Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(2, tag3Value2Alerts.size()); criteria.setTagQuery("xyztag1 = 'value10'"); List<Alert> tag1Value10Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(0, tag1Value10Alerts.size()); criteria.setTagQuery("xyztag1 or xyztag2"); List<Alert> tag1OrTag2Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(10, tag1OrTag2Alerts.size()); criteria.setTagQuery("xyztag1 = 'value.*'"); List<Alert> tag1ValueAlerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(5, tag1ValueAlerts.size()); criteria.setTagQuery("xyztag1 != 'value0'"); List<Alert> tag1NotValue0Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(4, tag1NotValue0Alerts.size()); criteria.setTagQuery("xyztag1 != 'value0' or xyztag2 != 'value0'"); List<Alert> tag1NotValue0Tag2NotValue0Alerts = alerts.getAlerts(tenantIds, criteria, null); assertEquals(8, tag1NotValue0Tag2NotValue0Alerts.size()); deleteTestAlerts(numTenants); }