diff --git a/servers/zms/src/test/java/com/yahoo/athenz/zms/ZMSNotificationsTest.java b/servers/zms/src/test/java/com/yahoo/athenz/zms/ZMSNotificationsTest.java index 43b5ebdd517..83695b51cd9 100644 --- a/servers/zms/src/test/java/com/yahoo/athenz/zms/ZMSNotificationsTest.java +++ b/servers/zms/src/test/java/com/yahoo/athenz/zms/ZMSNotificationsTest.java @@ -18,17 +18,15 @@ package com.yahoo.athenz.zms; -import com.yahoo.athenz.common.server.db.PoolableDataSource; import com.yahoo.athenz.common.server.notification.Notification; import com.yahoo.athenz.zms.notification.GroupMemberExpiryNotificationTask; import com.yahoo.athenz.zms.notification.RoleMemberExpiryNotificationTask; import com.yahoo.rdl.Timestamp; import org.testng.annotations.*; -import java.sql.Connection; -import java.sql.SQLException; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static org.testng.Assert.*; @@ -62,46 +60,43 @@ public void testRoleExpiryNotification() { "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser()); zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1); - long currentTimeMillis = System.currentTimeMillis(); - Timestamp currentTimestamp = Timestamp.fromMillis(currentTimeMillis); - - // Create 29 members which will be expired in the next 29 days. Each day exactly one member will be expired. - List roleMembers = new ArrayList<>(); - for (int i = 0; i <= 28; ++i) { - Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); - roleMembers.add(new RoleMember().setMemberName("user.expireddays" + i).setExpiration(timestamp)); - } - - Role role1 = zmsTestInitializer.createRoleObject("test-domain1", "Role1", - null, roleMembers); - - zmsTestInitializer.getZms().putRole(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Role1", zmsTestInitializer.getAuditRef(), role1); - RoleMemberExpiryNotificationTask roleMemberExpiryNotificationTask = new RoleMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); - List notifications = roleMemberExpiryNotificationTask.getNotifications(); - - // Email notifications should be sent every 7 days while metrics should be recorded every day - Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ - "user.expireddays0", - "user.expireddays1", - "user.expireddays7", - "user.expireddays14", - "user.expireddays21", - "user.expireddays28"})); - for (Notification notification : notifications) { - String recipient = notification.getRecipients().stream().findFirst().get(); - if (recipient.equals("user.testadminuser")) { - verifyAdminNotifications(emailNotificationMembers, notification); - } else { - if (emailNotificationMembers.contains(recipient)) { - assertNotNull(notification.getNotificationAsEmail()); + try { + long currentTimeMillis = System.currentTimeMillis(); + Timestamp currentTimestamp = Timestamp.fromMillis(currentTimeMillis); + + List roleMembers = generateRoleMembers(currentTimeMillis); + + Role role1 = zmsTestInitializer.createRoleObject("test-domain1", "Role1", + null, roleMembers); + + zmsTestInitializer.getZms().putRole(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Role1", zmsTestInitializer.getAuditRef(), role1); + RoleMemberExpiryNotificationTask roleMemberExpiryNotificationTask = new RoleMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); + List notifications = roleMemberExpiryNotificationTask.getNotifications(); + + // Email notifications should be sent every 7 days while metrics should be recorded every day + Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ + "user.expireddays0", + "user.expireddays1", + "user.expireddays7", + "user.expireddays14", + "user.expireddays21", + "user.expireddays28"})); + for (Notification notification : notifications) { + String recipient = notification.getRecipients().stream().findFirst().get(); + if (recipient.equals("user.testadminuser")) { + verifyAdminNotifications(emailNotificationMembers, notification); } else { - assertNull(notification.getNotificationAsEmail()); + if (emailNotificationMembers.contains(recipient)) { + assertNotNull(notification.getNotificationAsEmail()); + } else { + assertNull(notification.getNotificationAsEmail()); + } + assertNotNull(notification.getNotificationAsMetrics(currentTimestamp)); } - assertNotNull(notification.getNotificationAsMetrics(currentTimestamp)); } + } finally { + zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } - - zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } private void verifyAdminNotifications(Set emailNotificationMembers, Notification notification) { @@ -109,12 +104,12 @@ private void verifyAdminNotifications(Set emailNotificationMembers, Noti if (notification.getNotificationAsEmail() != null) { // Email and metric notification for admin for (String member : emailNotificationMembers) { - assertTrue(membersList.contains(member + ";")); + assertTrue(membersList.contains(member + ";"), "memberList: " + membersList + " doesn't contain member: " + member); } } else { // Metric only notification for admin for (String member : emailNotificationMembers) { - assertFalse(membersList.contains(member + ";")); + assertFalse(membersList.contains(member + ";"), "memberList: " + membersList + " contains member: " + member); } } } @@ -125,46 +120,43 @@ public void testGroupExpiryNotification() { "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser()); zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1); - long currentTimeMillis = System.currentTimeMillis(); - Timestamp currentTimestamp = Timestamp.fromMillis(currentTimeMillis); - - // Create 29 group members which will be expired in the next 29 days. Each day exactly one member will be expired. - List groupMembers = new ArrayList<>(); - for (int i = 0; i <= 28; ++i) { - Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); - groupMembers.add(new GroupMember().setMemberName("user.expireddays" + i).setExpiration(timestamp)); - } - - Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); - - zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); - GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); - List notifications = groupMemberExpiryNotificationTask.getNotifications(); - - // Email notifications should be sent every 7 days - Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ - "user.expireddays0", - "user.expireddays1", - "user.expireddays7", - "user.expireddays14", - "user.expireddays21", - "user.expireddays28"})); - assertEquals(notifications.size(), 7); - for (Notification notification : notifications) { - String recipient = notification.getRecipients().stream().findFirst().get(); - if (recipient.equals("user.testadminuser")) { - verifyAdminNotifications(emailNotificationMembers, notification); - } else { - if (emailNotificationMembers.contains(recipient)) { - assertNotNull(notification.getNotificationAsEmail()); + try { + long currentTimeMillis = System.currentTimeMillis(); + Timestamp currentTimestamp = Timestamp.fromMillis(currentTimeMillis); + + List groupMembers = generateGroupMembers(currentTimeMillis); + + Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); + + zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); + GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); + List notifications = groupMemberExpiryNotificationTask.getNotifications(); + + // Email notifications should be sent every 7 days + Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ + "user.expireddays0", + "user.expireddays1", + "user.expireddays7", + "user.expireddays14", + "user.expireddays21", + "user.expireddays28"})); + assertEquals(notifications.size(), 7, "notificationRecipients: " + notificationsToRecipientString(notifications)); + for (Notification notification : notifications) { + String recipient = notification.getRecipients().stream().findFirst().get(); + if (recipient.equals("user.testadminuser")) { + verifyAdminNotifications(emailNotificationMembers, notification); } else { - assertNull(notification.getNotificationAsEmail()); + if (emailNotificationMembers.contains(recipient)) { + assertNotNull(notification.getNotificationAsEmail()); + } else { + assertNull(notification.getNotificationAsEmail()); + } + assertNotNull(notification.getNotificationAsMetrics(currentTimestamp)); } - assertNotNull(notification.getNotificationAsMetrics(currentTimestamp)); } + } finally { + zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } - - zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } @Test @@ -173,41 +165,42 @@ public void testDisableUserGroupExpiryNotification() { "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser()); zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1); - long currentTimeMillis = System.currentTimeMillis(); - - // Create 29 group members which will be expired in the next 29 days. Each day exactly one member will be expired. - List groupMembers = new ArrayList<>(); - for (int i = 0; i <= 28; ++i) { - Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); - groupMembers.add(new GroupMember().setMemberName("user.expireddays" + i).setExpiration(timestamp)); + try { + long currentTimeMillis = System.currentTimeMillis(); + + List groupMembers = generateGroupMembers(currentTimeMillis); + + Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); + // Now disable notification for users + Map disableUserTags = new HashMap<>(); + disableUserTags.put("zms.DisableReminderNotifications", new TagValueList().setList(Arrays.asList("1"))); + group1.setTags(disableUserTags); + + zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); + GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); + List notifications = groupMemberExpiryNotificationTask.getNotifications(); + + // Email notifications should be sent every 7 days + Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ + "user.expireddays0", + "user.expireddays1", + "user.expireddays7", + "user.expireddays14", + "user.expireddays21", + "user.expireddays28"})); + assertEquals(notifications.size(), 1, "notificationRecipients: " + notificationsToRecipientString(notifications)); + Notification notification = notifications.get(0); + assertEquals(notification.getRecipients().size(), 1, "notificationRecipients: " + notificationsToRecipientString(notifications)); + String recipient = notification.getRecipients().stream().findFirst().get(); + assertEquals(recipient, "user.testadminuser"); + verifyAdminNotifications(emailNotificationMembers, notification); + } finally { + zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } + } - Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); - // Now disable notification for users - Map disableUserTags = new HashMap<>(); - disableUserTags.put("zms.DisableReminderNotifications", new TagValueList().setList(Arrays.asList("1"))); - group1.setTags(disableUserTags); - - zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); - GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); - List notifications = groupMemberExpiryNotificationTask.getNotifications(); - - // Email notifications should be sent every 7 days - Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ - "user.expireddays0", - "user.expireddays1", - "user.expireddays7", - "user.expireddays14", - "user.expireddays21", - "user.expireddays28"})); - assertEquals(notifications.size(), 1); - Notification notification = notifications.get(0); - assertEquals(notification.getRecipients().size(), 1); - String recipient = notification.getRecipients().stream().findFirst().get(); - assertEquals(recipient, "user.testadminuser"); - verifyAdminNotifications(emailNotificationMembers, notification); - - zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); + private String notificationsToRecipientString(List notifications) { + return notifications.stream().map(notif -> notif.getRecipients().stream().findAny().get()).collect(Collectors.joining(", ")); } @Test @@ -216,43 +209,40 @@ public void testDisableAdminGroupExpiryNotification() { "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser()); zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1); - long currentTimeMillis = System.currentTimeMillis(); - - // Create 29 group members which will be expired in the next 29 days. Each day exactly one member will be expired. - List groupMembers = new ArrayList<>(); - for (int i = 0; i <= 28; ++i) { - Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); - groupMembers.add(new GroupMember().setMemberName("user.expireddays" + i).setExpiration(timestamp)); - } - - Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); - // Now disable notification for admins - Map disableUserTags = new HashMap<>(); - disableUserTags.put("zms.DisableReminderNotifications", new TagValueList().setList(Arrays.asList("2"))); - group1.setTags(disableUserTags); - - zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); - GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); - List notifications = groupMemberExpiryNotificationTask.getNotifications(); - - // Email notifications should be sent every 7 days - Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ - "user.expireddays0", - "user.expireddays1", - "user.expireddays7", - "user.expireddays14", - "user.expireddays21", - "user.expireddays28"})); - assertEquals(notifications.size(), 6); - for (Notification notification : notifications) { - assertEquals(notification.getRecipients().size(), 1); - String recipient = notification.getRecipients().stream().findFirst().get(); - assertTrue(emailNotificationMembers.contains(recipient)); - emailNotificationMembers.remove(recipient); + try { + long currentTimeMillis = System.currentTimeMillis(); + + List groupMembers = generateGroupMembers(currentTimeMillis); + + Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); + // Now disable notification for admins + Map disableUserTags = new HashMap<>(); + disableUserTags.put("zms.DisableReminderNotifications", new TagValueList().setList(Arrays.asList("2"))); + group1.setTags(disableUserTags); + + zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); + GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); + List notifications = groupMemberExpiryNotificationTask.getNotifications(); + + // Email notifications should be sent every 7 days + Set emailNotificationMembers = new HashSet<>(Arrays.asList(new String[]{ + "user.expireddays0", + "user.expireddays1", + "user.expireddays7", + "user.expireddays14", + "user.expireddays21", + "user.expireddays28"})); + assertEquals(notifications.size(), 6, "notificationRecipients: " + notificationsToRecipientString(notifications)); + for (Notification notification : notifications) { + assertEquals(notification.getRecipients().size(), 1, "notificationRecipients: " + notificationsToRecipientString(notifications)); + String recipient = notification.getRecipients().stream().findFirst().get(); + assertTrue(emailNotificationMembers.contains(recipient)); + emailNotificationMembers.remove(recipient); + } + assertTrue(emailNotificationMembers.isEmpty()); + } finally { + zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } - assertTrue(emailNotificationMembers.isEmpty()); - - zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); } @Test @@ -261,25 +251,45 @@ public void testDisableAllGroupExpiryNotification() { "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser()); zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1); - long currentTimeMillis = System.currentTimeMillis(); + try { + long currentTimeMillis = System.currentTimeMillis(); + List groupMembers = generateGroupMembers(currentTimeMillis); + + Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); + // Now disable all notifications + Map disableUserTags = new HashMap<>(); + disableUserTags.put("zms.DisableReminderNotifications", new TagValueList().setList(Arrays.asList("3"))); + group1.setTags(disableUserTags); + + zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); + GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); + List notifications = groupMemberExpiryNotificationTask.getNotifications(); + assertEquals(notifications.size(), 0, "notificationRecipients: " + notificationsToRecipientString(notifications)); + } finally { + zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); + } + } - // Create 29 group members which will be expired in the next 29 days. Each day exactly one member will be expired. + private List generateGroupMembers(long currentTimeMillis) { List groupMembers = new ArrayList<>(); - for (int i = 0; i <= 28; ++i) { - Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); + Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS)); + groupMembers.add(new GroupMember().setMemberName("user.expireddays0").setExpiration(timestamp)); + for (int i = 1; i <= 28; ++i) { + timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); groupMembers.add(new GroupMember().setMemberName("user.expireddays" + i).setExpiration(timestamp)); } + return groupMembers; + } - Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers); - // Now disable all notifications - Map disableUserTags = new HashMap<>(); - disableUserTags.put("zms.DisableReminderNotifications", new TagValueList().setList(Arrays.asList("3"))); - group1.setTags(disableUserTags); - - zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1); - GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon); - List notifications = groupMemberExpiryNotificationTask.getNotifications(); - assertEquals(notifications.size(), 0); - zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef()); + private List generateRoleMembers(long currentTimeMillis) { + // Create 29 members which will be expired in the next 29 days. Each day exactly one member will be expired. + List roleMembers = new ArrayList<>(); + Timestamp timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS)); + roleMembers.add(new RoleMember().setMemberName("user.expireddays0").setExpiration(timestamp)); + for (int i = 1; i <= 28; ++i) { + timestamp = Timestamp.fromMillis(currentTimeMillis + TimeUnit.MILLISECONDS.convert(i, TimeUnit.DAYS)); + roleMembers.add(new RoleMember().setMemberName("user.expireddays" + i).setExpiration(timestamp)); + } + return roleMembers; } }