@Test
public void testNotificationsByDomain() {

    Date currentDate = new Date();
    List<X509CertRecord> records = new ArrayList<>();
    System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider");
    // Create 6 records, each in it's own domain (domain0, domain1... domain5)
    for (int i = 0; i < 6; ++i) {
        X509CertRecord record = getMockX509CertRecord(currentDate, i);
        records.add(record);
        NotificationTestsCommon.mockDomainData(i, dataStore);
    }
    // Now add one record to domain 0 and one record to domain 5
    Mockito.when(hostnameResolver.isValidHostname(any())).thenReturn(true);
    X509CertRecord recordDomain0 = getMockX509CertRecord(currentDate, 0);
    recordDomain0.setHostName("secondHostName0");
    X509CertRecord recordDomain5 = getMockX509CertRecord(currentDate, 5);
    recordDomain5.setHostName("secondHostName5");
    records.add(recordDomain0);
    records.add(recordDomain5);
    Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records);
    CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask(
            instanceCertManager,
            dataStore,
            hostnameResolver,
            userDomainPrefix,
            serverName,
            httpsPort);
    List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications();
    assertEquals(6, notifications.size());
    // Assert 2 records for domain5 and domain0:
    String twoRecordsDomain5 = "service5;provider;instanceID5;" + new Timestamp(currentDate.getTime()) + ";;hostName5|" +
            "service5;provider;instanceID5;" + new Timestamp(currentDate.getTime()) + ";;secondHostName5";
    assertEquals(twoRecordsDomain5, notifications.get(1).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
    String twoRecordsDomain0 = "service0;provider;instanceID0;" + new Timestamp(currentDate.getTime()) + ";;hostName0|" +
            "service0;provider;instanceID0;" + new Timestamp(currentDate.getTime()) + ";;secondHostName0";
    assertEquals(twoRecordsDomain0, notifications.get(4).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
    // Assert other domains only have 1 record:
    String oneRecordDomain1 = "service1;provider;instanceID1;" + new Timestamp(currentDate.getTime()) + ";;hostName1";
    assertEquals(oneRecordDomain1, notifications.get(5).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
    System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST);
}