/** * Get's the name for an authorizable on what the list should be sorted. sakai:group-title for * Groups, lastName for Users. * * @param member The authorizable to get a name for. * @return The name. * @throws RepositoryException */ private String getName(Authorizable member) throws RepositoryException { String name = member.getID(); if (member.isGroup()) { Value[] values = member.getProperty("sakai:group-title"); if (values != null && values.length != 0) { name = values[0].getString(); } } else { Value[] values = member.getProperty("lastName"); if (values != null && values.length != 0) { name = values[0].getString(); } } // We need to add the ID to keep the keys unique. return name + member.getID(); }
@Test public void testGetEmailAddrs_Group() throws Exception { // mock group and users String groupPath = "/home/users/g/group"; List<Authorizable> groupMembers = new ArrayList<Authorizable>(); Authorizable user1 = mock(Authorizable.class); Authorizable user2 = mock(Authorizable.class); when(user1.hasProperty(PN_EMAIL)).thenReturn(true); when(user1.getProperty(PN_EMAIL)) .thenReturn(new MockValue[] {new MockValue("*****@*****.**")}); when(user2.hasProperty(PN_EMAIL)).thenReturn(true); when(user2.getProperty(PN_EMAIL)) .thenReturn(new MockValue[] {new MockValue("*****@*****.**")}); groupMembers.add(user1); groupMembers.add(user2); ResourceResolver resolver = mock(ResourceResolver.class); Resource groupRes = mock(Resource.class); Authorizable groupAuth = mock(Authorizable.class); Group userGroup = mock(Group.class); when(resolver.getResource(groupPath)).thenReturn(groupRes); when(groupRes.adaptTo(Authorizable.class)).thenReturn(groupAuth); when(groupAuth.isGroup()).thenReturn(true); when(groupRes.adaptTo(Group.class)).thenReturn(userGroup); when(userGroup.getMembers()).thenReturn(groupMembers.iterator()); String[] emails = SendTemplatedEmailUtils.getEmailAddrsFromUserPath(resolver, groupPath); assertEquals(2, emails.length); assertEquals("*****@*****.**", emails[0]); assertEquals("*****@*****.**", emails[1]); }
@Test public void testGetEmailAddrs_User() throws Exception { String userPath = "/home/users/a/admin"; MockValue[] emailVal = new MockValue[] {new MockValue("*****@*****.**")}; ResourceResolver resolver = mock(ResourceResolver.class); Resource userRes = mock(Resource.class); Authorizable adminUser = mock(Authorizable.class); when(resolver.getResource(userPath)).thenReturn(userRes); when(userRes.adaptTo(Authorizable.class)).thenReturn(adminUser); when(adminUser.isGroup()).thenReturn(false); when(adminUser.hasProperty(PN_EMAIL)).thenReturn(true); when(adminUser.getProperty(PN_EMAIL)).thenReturn(emailVal); String[] emails = SendTemplatedEmailUtils.getEmailAddrsFromUserPath(resolver, userPath); assertEquals(1, emails.length); assertEquals("*****@*****.**", emails[0]); }