@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 {
            // Yes, tag3/valueX can be repeated twice
            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);
}