LdapAutodiscovery { public List<LdapSrvRecord> getLdapServers(String domain) { try { return getLdapServers(new InitialDirContext(), domain); } catch (NamingException e) { LOG.error("Unable to determine LDAP server(s) from DNS", e); return Collections.emptyList(); } }  static String getDnsDomainName(); static String getDnsDomainName(String fqdn); static String getDnsDomainDn(String domain); List<LdapSrvRecord> getLdapServers(String domain);  }
@Test public void testGetLdapServer() throws NamingException { DirContext context = mock(DirContext.class); Attributes attributes = mock(Attributes.class); Attribute attribute = mock(Attribute.class); NamingEnumeration namingEnumeration = mock(NamingEnumeration.class); when(context.getAttributes(Mockito.anyString(), Mockito.<String[]>anyObject())).thenReturn(attributes); when(attributes.get(Mockito.eq("srv"))).thenReturn(attribute); when(attribute.getAll()).thenReturn(namingEnumeration); when(namingEnumeration.hasMore()).thenReturn(true, true, true, true, true, false); when(namingEnumeration.next()) .thenReturn("10 40 389 ldap5.example.org.") .thenReturn("0 10 389 ldap3.example.org") .thenReturn("0 60 389 ldap1.example.org") .thenReturn("0 30 389 ldap2.example.org") .thenReturn("10 60 389 ldap4.example.org"); assertThat(new LdapAutodiscovery().getLdapServers(context, "example.org.")).extracting("serverUrl") .isEqualTo( Arrays.asList("ldap: "ldap: }