RoleMemberReviewNotificationTask implements NotificationTask { @Override public List<Notification> getNotifications() { Map<String, DomainRoleMember> reviewMembers = dbService.getRoleReviewMembers(1); if (reviewMembers == null || reviewMembers.isEmpty()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No members require review reminders"); } return new ArrayList<>(); } return roleMemberNotificationCommon.getNotificationDetails( reviewMembers, roleReviewPrincipalNotificationToEmailConverter, roleReviewDomainNotificationToEmailConverter, new ReviewRoleMemberDetailStringer(), NOTIFICATION_TYPE); } RoleMemberReviewNotificationTask(DBService dbService, String userDomainPrefix); @Override List<Notification> getNotifications(); @Override String getDescription();  }
@Test public void testSendRoleMemberRemindersException() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; Mockito.when(dbsvc.getRoleReviewMembers(1)).thenThrow(new IllegalArgumentException()); NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); RoleMemberReviewNotificationTask roleMemberReviewNotificationTask = new RoleMemberReviewNotificationTask(dbsvc, USER_DOMAIN_PREFIX); Mockito.when(mockNotificationService.notify(any())).thenThrow(new IllegalArgumentException()); try { roleMemberReviewNotificationTask.getNotifications(); fail(); } catch (Exception ex) { assertTrue(ex instanceof IllegalArgumentException); } notificationManager.shutdown(); }
@Test public void testSendRoleMemberReviewRemindersEmptySet() { 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()); RoleMemberReviewNotificationTask roleMemberReviewNotificationTask = new RoleMemberReviewNotificationTask(dbsvc, USER_DOMAIN_PREFIX); assertEquals(roleMemberReviewNotificationTask.getNotifications(), new ArrayList<>()); notificationManager.shutdown(); }
@Test public void testSendRoleMemberReviewReminders() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; List<MemberRole> memberRoles = new ArrayList<>(); memberRoles.add(new MemberRole().setRoleName("role1") .setDomainName("athenz1") .setMemberName("user.joe") .setReviewReminder(Timestamp.fromMillis(100))); DomainRoleMember domainRoleMember = new DomainRoleMember() .setMemberName("user.joe") .setMemberRoles(memberRoles); Map<String, DomainRoleMember> reviewMembers = new HashMap<>(); reviewMembers.put("user.joe", domainRoleMember); Mockito.when(dbsvc.getRoleReviewMembers(1)) .thenReturn(null) .thenReturn(reviewMembers); 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 RoleMemberReviewNotificationTask(dbsvc, USER_DOMAIN_PREFIX).getNotifications(); assertEquals(notifications.size(), 2); Notification expectedFirstNotification = new Notification(); expectedFirstNotification.addRecipient("user.joe"); expectedFirstNotification.addDetails(NOTIFICATION_DETAILS_ROLES_LIST, "athenz1;role1;1970-01-01T00:00:00.100Z"); expectedFirstNotification.addDetails("member", "user.joe"); expectedFirstNotification.setNotificationToEmailConverter(new RoleMemberReviewNotificationTask.RoleReviewPrincipalNotificationToEmailConverter()); expectedFirstNotification.setType("role_membership_review"); Notification expectedSecondNotification = new Notification(); expectedSecondNotification.addRecipient("user.jane"); expectedSecondNotification.addDetails(NOTIFICATION_DETAILS_MEMBERS_LIST, "user.joe;role1;1970-01-01T00:00:00.100Z"); expectedSecondNotification.addDetails("domain", "athenz1"); expectedSecondNotification.setNotificationToEmailConverter(new RoleMemberReviewNotificationTask.RoleReviewDomainNotificationToEmailConverter()); expectedSecondNotification.setType("role_membership_review"); assertEquals(notifications.get(0), expectedFirstNotification); assertEquals(notifications.get(1), expectedSecondNotification); notificationManager.shutdown(); }
@Test public void testSendRoleMemberReviewRemindersNoValidDomain() { DBService dbsvc = Mockito.mock(DBService.class); List<MemberRole> memberRoles = new ArrayList<>(); memberRoles.add(new MemberRole().setRoleName("role1") .setDomainName("athenz1") .setMemberName("user.joe") .setExpiration(Timestamp.fromMillis(100))); DomainRoleMember domainRoleMember = new DomainRoleMember() .setMemberRoles(memberRoles); Map<String, DomainRoleMember> reviewMembers = new HashMap<>(); reviewMembers.put("user.joe", domainRoleMember); Mockito.when(dbsvc.getRoleReviewMembers(1)) .thenReturn(null) .thenReturn(reviewMembers); ZMSTestUtils.sleep(1000); Mockito.when(dbsvc.getAthenzDomain("athenz1", false)).thenReturn(null); List<Notification> notifications = new RoleMemberReviewNotificationTask(dbsvc, USER_DOMAIN_PREFIX).getNotifications(); assertEquals(notifications, new ArrayList<>()); }