RoleMemberNotificationCommon { public List<Notification> getNotificationDetails(Map<String, DomainRoleMember> members, NotificationToEmailConverter principalNotificationToEmailConverter, NotificationToEmailConverter domainAdminNotificationToEmailConverter, RoleMemberDetailStringer roleMemberDetailStringer, String notificationType) { List<Notification> notificationList = new ArrayList<>(); Map<String, List<MemberRole>> domainAdminMap = new HashMap<>(); for (DomainRoleMember roleMember : members.values()) { Map<String, String> details = processRoleReminder(domainAdminMap, roleMember, roleMemberDetailStringer); Notification notification = notificationCommon.createNotification( roleMember.getMemberName(), details, principalNotificationToEmailConverter, notificationType); if (notification != null) { notificationList.add(notification); } } for (Map.Entry<String, List<MemberRole>> domainAdmin : domainAdminMap.entrySet()) { Map<String, String> details = processMemberReminder(domainAdmin.getKey(), domainAdmin.getValue(), roleMemberDetailStringer); Notification notification = notificationCommon.createNotification( ZMSUtils.roleResourceName(domainAdmin.getKey(), ADMIN_ROLE_NAME), details, domainAdminNotificationToEmailConverter, notificationType); if (notification != null) { notificationList.add(notification); } } return notificationList; } RoleMemberNotificationCommon(DBService dbService, String userDomainPrefix); List<Notification> getNotificationDetails(Map<String, DomainRoleMember> members,
                                                     NotificationToEmailConverter principalNotificationToEmailConverter,
                                                     NotificationToEmailConverter domainAdminNotificationToEmailConverter,
                                                     RoleMemberDetailStringer roleMemberDetailStringer,
                                                     String notificationType);  }
@Test public void testExpiryPrincipalGetNotificationDetails() { DBService dbsvc = Mockito.mock(DBService.class); RoleMemberNotificationCommon roleMemberNotificationCommon = new RoleMemberNotificationCommon(dbsvc, USER_DOMAIN_PREFIX); DomainRoleMember roleMember = new DomainRoleMember(); roleMember.setMemberName("user.joe"); Map<String, DomainRoleMember> members = new HashMap<>(); members.put("user.joe", roleMember); List<Notification> notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), "testNotification"); assertEquals(1, notification.size()); assertTrue(notification.get(0).getDetails().isEmpty()); assertEquals(notification.get(0).getType(), "testNotification"); roleMember.setMemberRoles(Collections.emptyList()); notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), "testNotification"); assertEquals(1, notification.size()); assertTrue(notification.get(0).getDetails().isEmpty()); assertEquals(notification.get(0).getType(), "testNotification"); final Timestamp expirationTs = Timestamp.fromMillis(100); final Timestamp reviewTs = Timestamp.fromMillis(50); List<MemberRole> memberRoles = new ArrayList<>(); memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz1").setMemberName("user.joe") .setExpiration(expirationTs).setReviewReminder(reviewTs)); roleMember.setMemberRoles(memberRoles); notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), "testNotification"); assertEquals(1, notification.size()); assertEquals(2, notification.get(0).getDetails().size()); assertEquals(notification.get(0).getType(), "testNotification"); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe"); memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz2").setMemberName("user.joe") .setExpiration(expirationTs).setReviewReminder(reviewTs)); memberRoles.add(new MemberRole().setRoleName("role2").setDomainName("athenz2").setMemberName("user.joe") .setExpiration(expirationTs).setReviewReminder(reviewTs)); notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), "testNotification"); assertEquals(notification.get(0).getType(), "testNotification"); assertEquals(1, notification.size()); assertEquals(2, notification.get(0).getDetails().size()); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs + "|athenz2;role1;" + expirationTs + "|athenz2;role2;" + expirationTs); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe"); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs + "|athenz2;role1;" + expirationTs + "|athenz2;role2;" + expirationTs); }
@Test public void testReviewPrincipalGetNotificationDetails() { DBService dbsvc = Mockito.mock(DBService.class); RoleMemberNotificationCommon roleMemberNotificationCommon = new RoleMemberNotificationCommon(dbsvc, USER_DOMAIN_PREFIX); DomainRoleMember roleMember = new DomainRoleMember(); roleMember.setMemberName("user.joe"); Map<String, DomainRoleMember> members = new HashMap<>(); members.put("user.joe", roleMember); List<Notification> notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberReviewNotificationTask.RoleReviewPrincipalNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.RoleReviewDomainNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.ReviewRoleMemberDetailStringer(), "testNotification"); assertEquals(1, notification.size()); assertTrue(notification.get(0).getDetails().isEmpty()); assertEquals(notification.get(0).getType(), "testNotification"); roleMember.setMemberRoles(Collections.emptyList()); notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberReviewNotificationTask.RoleReviewPrincipalNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.RoleReviewDomainNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.ReviewRoleMemberDetailStringer(), "testNotification"); assertEquals(1, notification.size()); assertTrue(notification.get(0).getDetails().isEmpty()); assertEquals(notification.get(0).getType(), "testNotification"); final Timestamp expirationTs = Timestamp.fromMillis(100); final Timestamp reviewTs = Timestamp.fromMillis(50); List<MemberRole> memberRoles = new ArrayList<>(); memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz1").setMemberName("user.joe") .setExpiration(expirationTs).setReviewReminder(reviewTs)); roleMember.setMemberRoles(memberRoles); notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberReviewNotificationTask.RoleReviewPrincipalNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.RoleReviewDomainNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.ReviewRoleMemberDetailStringer(), "testNotification"); assertEquals(notification.get(0).getType(), "testNotification"); assertEquals(1, notification.size()); assertEquals(2, notification.get(0).getDetails().size()); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + reviewTs); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe"); memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz2").setMemberName("user.joe") .setExpiration(expirationTs).setReviewReminder(reviewTs)); memberRoles.add(new MemberRole().setRoleName("role2").setDomainName("athenz2").setMemberName("user.joe") .setExpiration(expirationTs).setReviewReminder(reviewTs)); notification = roleMemberNotificationCommon.getNotificationDetails( members, new RoleMemberReviewNotificationTask.RoleReviewPrincipalNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.RoleReviewDomainNotificationToEmailConverter(), new RoleMemberReviewNotificationTask.ReviewRoleMemberDetailStringer(), "testNotification"); assertEquals(notification.get(0).getType(), "testNotification"); assertEquals(1, notification.size()); assertEquals(2, notification.get(0).getDetails().size()); String expectedRolesList = "athenz1;role1;" + reviewTs + "|athenz2;role1;" + reviewTs + "|athenz2;role2;" + reviewTs; assertEquals(expectedRolesList, notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST)); assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe"); assertEquals(expectedRolesList, notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST)); }