GroupMemberExpiryNotificationTask implements NotificationTask { @Override public List<Notification> getNotifications() { Map<String, DomainGroupMember> expiryMembers = dbService.getGroupExpiryMembers(1); if (expiryMembers == null || expiryMembers.isEmpty()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No expiry group members available to send notifications"); } return new ArrayList<>(); } return getNotificationDetails(expiryMembers, groupExpiryPrincipalNotificationToEmailConverter, groupExpiryDomainNotificationToEmailConverter); } GroupMemberExpiryNotificationTask(DBService dbService, String userDomainPrefix); @Override List<Notification> getNotifications(); StringBuilder getDetailString(GroupMember memberGroup); @Override String getDescription();  }
@Test public void testSendGroupMemberExpiryRemindersException() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; Mockito.when(dbsvc.getGroupExpiryMembers(1)).thenThrow(new IllegalArgumentException()); NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(dbsvc, USER_DOMAIN_PREFIX); Mockito.when(mockNotificationService.notify(any())).thenThrow(new IllegalArgumentException()); try { groupMemberExpiryNotificationTask.getNotifications(); fail(); } catch (Exception ex) { assertTrue(ex instanceof IllegalArgumentException); } notificationManager.shutdown(); }
@Test public void testSendGroupMemberExpiryRemindersEmptySet() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); Mockito.when(mockNotificationService.notify(any())).thenThrow(new IllegalArgumentException()); GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(dbsvc, USER_DOMAIN_PREFIX); assertEquals(groupMemberExpiryNotificationTask.getNotifications(), new ArrayList<>()); notificationManager.shutdown(); }
@Test public void testSendGroupMemberExpiryReminders() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; List<GroupMember> memberGroups = new ArrayList<>(); memberGroups.add(new GroupMember().setGroupName("group1") .setDomainName("athenz1") .setMemberName("user.joe") .setExpiration(Timestamp.fromMillis(100))); DomainGroupMember domainGroupMember = new DomainGroupMember() .setMemberName("user.joe") .setMemberGroups(memberGroups); Map<String, DomainGroupMember> expiryMembers = new HashMap<>(); expiryMembers.put("user.joe", domainGroupMember); Mockito.when(dbsvc.getGroupExpiryMembers(1)) .thenReturn(null) .thenReturn(expiryMembers); NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); ZMSTestUtils.sleep(1000); AthenzDomain domain = new AthenzDomain("athenz1"); List<RoleMember> roleMembers = new ArrayList<>(); roleMembers.add(new RoleMember().setMemberName("user.jane")); Role adminRole = new Role().setName("athenz1:role.admin").setRoleMembers(roleMembers); List<Role> roles = new ArrayList<>(); roles.add(adminRole); domain.setRoles(roles); Mockito.when(dbsvc.getRolesByDomain("athenz1")).thenReturn(domain.getRoles()); List<Notification> notifications = new GroupMemberExpiryNotificationTask(dbsvc, USER_DOMAIN_PREFIX) .getNotifications(); assertEquals(notifications.size(), 2); Notification expectedFirstNotification = new Notification(); expectedFirstNotification.addRecipient("user.joe"); expectedFirstNotification.addDetails(NOTIFICATION_DETAILS_ROLES_LIST, "athenz1;group1;1970-01-01T00:00:00.100Z"); expectedFirstNotification.addDetails("member", "user.joe"); expectedFirstNotification.setNotificationToEmailConverter(new GroupMemberExpiryNotificationTask.GroupExpiryPrincipalNotificationToEmailConverter()); expectedFirstNotification.setType("group_membership_expiry"); Notification expectedSecondNotification = new Notification(); expectedSecondNotification.addRecipient("user.jane"); expectedSecondNotification.addDetails(NOTIFICATION_DETAILS_MEMBERS_LIST, "user.joe;group1;1970-01-01T00:00:00.100Z"); expectedSecondNotification.addDetails("domain", "athenz1"); expectedSecondNotification.setNotificationToEmailConverter(new GroupMemberExpiryNotificationTask.GroupExpiryDomainNotificationToEmailConverter()); expectedSecondNotification.setType("group_membership_expiry"); assertEquals(notifications.get(0), expectedFirstNotification); assertEquals(notifications.get(1), expectedSecondNotification); notificationManager.shutdown(); }
@Test public void testSendGroupMemberExpiryRemindersNoValidDomain() { DBService dbsvc = Mockito.mock(DBService.class); List<GroupMember> memberGroups = new ArrayList<>(); memberGroups.add(new GroupMember().setGroupName("group1") .setDomainName("athenz1") .setMemberName("user.joe") .setExpiration(Timestamp.fromMillis(100))); DomainGroupMember domainGroupMember = new DomainGroupMember() .setMemberGroups(memberGroups); Map<String, DomainGroupMember> expiryMembers = new HashMap<>(); expiryMembers.put("user.joe", domainGroupMember); Mockito.when(dbsvc.getGroupExpiryMembers(1)) .thenReturn(null) .thenReturn(expiryMembers); ZMSTestUtils.sleep(1000); Mockito.when(dbsvc.getAthenzDomain("athenz1", false)).thenReturn(null); List<Notification> notifications = new GroupMemberExpiryNotificationTask(dbsvc, USER_DOMAIN_PREFIX).getNotifications(); assertEquals(notifications, new ArrayList<>()); }