### Question:
SendTemplatedEmailUtils { @SuppressWarnings({"squid:S3776"}) protected static final String[] getEmailAddrsFromUserPath(ResourceResolver resourceResolver, String principlePath) { List<String> emailList = new LinkedList<String>(); try { Resource authRes = resourceResolver.getResource(principlePath); if (authRes != null) { Authorizable authorizable = authRes.adaptTo(Authorizable.class); if (authorizable != null) { if (authorizable.isGroup()) { Group authGroup = authRes.adaptTo(Group.class); Iterator<Authorizable> memberIt = authGroup.getMembers(); while (memberIt.hasNext()) { String currEmail = getAuthorizableEmail(memberIt.next()); if (currEmail != null) { emailList.add(currEmail); } } } else { String authEmail = getAuthorizableEmail(authorizable); if (authEmail != null) { emailList.add(authEmail); } } } } } catch (RepositoryException e) { log.warn("Could not get list of email(s) for users. {}", e); } String[] emailReturn = new String[emailList.size()]; return emailList.toArray(emailReturn); } private  SendTemplatedEmailUtils();   }### Answer:
@Test public void testGetEmailAddrsNull() throws Exception { ResourceResolver resolver = mock(ResourceResolver.class); String userPath = "/doesnotexist"; when(resolver.getResource(userPath)).thenReturn(null); String[] emails = SendTemplatedEmailUtils.getEmailAddrsFromUserPath(resolver, userPath); assertEquals(0, emails.length); }
@Test public void testGetEmailAddrs_User() throws Exception { String userPath = "/home/users/a/admin"; MockValue[] emailVal = new MockValue[] { new MockValue("admin@adobe.com") }; 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("admin@adobe.com", emails[0]); }
@Test public void testGetEmailAddrs_Group() throws Exception { final String groupPath = "/home/users/g/group"; final 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("user1@adobe.com") }); when(user2.hasProperty(PN_EMAIL)).thenReturn(true); when(user2.getProperty(PN_EMAIL)).thenReturn(new MockValue[] { new MockValue("user2@adobe.com") }); 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("user1@adobe.com", emails[0]); assertEquals("user2@adobe.com", emails[1]); }