AmbariLdapDataPopulator { protected boolean isMemberAttributeBaseDn(String memberAttributeValue) { Pattern pattern = Pattern.compile(String.format(IS_MEMBER_DN_REGEXP, ldapServerProperties.getUsernameAttribute(), ldapServerProperties.getGroupNamingAttr())); return pattern.matcher(memberAttributeValue).find(); } @Inject  AmbariLdapDataPopulator(Configuration configuration, Users users); boolean isLdapEnabled(); LdapSyncDto getLdapSyncInfo(); LdapBatchDto synchronizeAllLdapGroups(LdapBatchDto batchInfo); LdapBatchDto synchronizeAllLdapUsers(LdapBatchDto batchInfo); LdapBatchDto synchronizeLdapGroups(Set<String> groups, LdapBatchDto batchInfo); LdapBatchDto synchronizeLdapUsers(Set<String> users, LdapBatchDto batchInfo); LdapBatchDto synchronizeExistingLdapGroups(LdapBatchDto batchInfo); LdapBatchDto synchronizeExistingLdapUsers(LdapBatchDto batchInfo);  }
@Test public void testIsMemberAttributeBaseDn() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn("UID"); expect(ldapServerProperties.getGroupNamingAttr()).andReturn("CN"); replay(configuration, users, ldapServerProperties); AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); boolean result = populator.isMemberAttributeBaseDn("CN=mygroupname,OU=myOrganizationUnit,DC=apache,DC=org"); assertTrue(result); }
@Test public void testIsMemberAttributeBaseDn_withUidMatch() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn("UID"); expect(ldapServerProperties.getGroupNamingAttr()).andReturn("CN"); replay(configuration, users, ldapServerProperties); AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); boolean result = populator.isMemberAttributeBaseDn("uid=myuid,OU=myOrganizationUnit,DC=apache,DC=org"); assertTrue(result); }
@Test public void testIsMemberAttributeBaseDn_withLowerAndUpperCaseValue() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn("uid"); expect(ldapServerProperties.getGroupNamingAttr()).andReturn("CN"); replay(configuration, users, ldapServerProperties); AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); boolean result = populator.isMemberAttributeBaseDn("cn=mygroupname,OU=myOrganizationUnit,DC=apache,DC=org"); assertTrue(result); }
@Test public void testIsMemberAttributeBaseDn_withWrongAttribute() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn("uid"); expect(ldapServerProperties.getGroupNamingAttr()).andReturn("CN"); replay(configuration, users, ldapServerProperties); AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); boolean result = populator.isMemberAttributeBaseDn("cnn=mygroupname,OU=myOrganizationUnit,DC=apache,DC=org"); assertFalse(result); }
@Test public void testIsMemberAttributeBaseDn_withEmptyValues() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn(""); expect(ldapServerProperties.getGroupNamingAttr()).andReturn(null); replay(configuration, users, ldapServerProperties); AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); boolean result = populator.isMemberAttributeBaseDn("cnn=mygroupname,OU=myOrganizationUnit,DC=apache,DC=org"); assertFalse(result); }
@Test public void testIsMemberAttributeBaseDn_withDifferentUserAndGroupNameAttribute() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn("sAMAccountName"); expect(ldapServerProperties.getGroupNamingAttr()).andReturn("groupOfNames"); replay(configuration, users, ldapServerProperties); AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); boolean result = populator.isMemberAttributeBaseDn("cn=mygroupname,OU=myOrganizationUnit,DC=apache,DC=org"); assertTrue(result); }