AmbariLdapDataPopulator { protected String getUniqueIdByMemberPattern(String memberAttributeValue, String pattern) { if (StringUtils.isNotEmpty(memberAttributeValue) && StringUtils.isNotEmpty(pattern)) { try { Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(memberAttributeValue); LOG.debug("Apply replace pattern '{}' on '{}' membership attribbute value.", memberAttributeValue, pattern); if (m.matches()) { memberAttributeValue = m.replaceAll(MEMBER_ATTRIBUTE_REPLACE_STRING); LOG.debug("Membership attribute value after replace pattern applied: '{}'", memberAttributeValue); } else { LOG.warn("Membership attribute value pattern is not matched ({}) on '{}'", pattern, memberAttributeValue); } } catch (Exception e) { LOG.error("Error during replace memberAttribute '{}' with pattern '{}'", memberAttributeValue, pattern); } } return memberAttributeValue; } @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 testGetUniqueIdMemberPattern() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); String syncUserMemberPattern = "(?<sid>.*);(?<guid>.*);(?<member>.*)"; String memberAttribute = "<SID=...>;<GUID=...>;cn=member,dc=apache,dc=org"; AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); String result = populator.getUniqueIdByMemberPattern(memberAttribute, syncUserMemberPattern); assertEquals("cn=member,dc=apache,dc=org", result); }
@Test public void testGetUniqueIdByMemberPatternWhenPatternIsWrong() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); String syncUserMemberPattern = "(?<sid>.*);(?<guid>.*);(?<mem>.*)"; String memberAttribute = "<SID=...>;<GUID=...>;cn=member,dc=apache,dc=org"; AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); String result = populator.getUniqueIdByMemberPattern(memberAttribute, syncUserMemberPattern); assertEquals(memberAttribute, result); }
@Test public void testGetUniqueIdByMemberPatternWhenPatternIsEmpty() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); String memberAttribute = "<SID=...>;<GUID=...>;cn=member,dc=apache,dc=org"; AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); String result = populator.getUniqueIdByMemberPattern(memberAttribute, ""); assertEquals(memberAttribute, result); }
@Test public void testGetUniqueIdByMemberPatternWhenMembershipAttributeIsNull() { Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); String syncUserMemberPattern = "(?<sid>.*);(?<guid>.*);(?<member>.*)"; AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users); String result = populator.getUniqueIdByMemberPattern(null, syncUserMemberPattern); assertNull(result); }