@Test public void testUpdateAndRenameWithException() { String dn = "cn=Some Person2,ou=company1,ou=Sweden"; String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; try { // Perform test dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } // Verify that entry was not moved. try { ldapTemplate.lookup(newDn); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertTrue(true); } // Verify that original entry was not updated. Object object = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Sweden, Company1, Some Person2", attributes.get("description").get()); return new Object(); } }); assertNotNull(object); }
@Test public void testUpdateWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; try { dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } log.debug("Verifying result"); Object ldapResult = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); assertNotNull(ldapResult); }
@Test public void testModifyAttributesWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; try { // Perform test dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } // Verify result - check that the operation was properly rolled back Object result = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); assertNotNull(result); }
@Override public void update(Person person) { Name dn = buildDn(person); DirContextAdapter context = (DirContextAdapter) ldapTemplate.lookup(dn); mapToContext(person, context); ldapTemplate.modifyAttributes(dn, context.getModificationItems()); }
public Attributes getAllUsersAttributes(User user) { Object o = ldapTemplate.lookup( getUserDN(String.valueOf(user.getId())), new UserAttributesContextMapper()); Attributes attrs = null; if (o != null) attrs = (Attributes) o; return attrs; }
@Test public void testCreate() { dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); log.debug("Verifying result"); String expectedDn = "cn=some testperson, ou=company1, ou=Sweden"; Object ldapResult = ldapTemplate.lookup(expectedDn); assertNotNull(ldapResult); ldapTemplate.unbind(expectedDn); }
public boolean userExist(User user) { Object o = null; try { o = ldapTemplate.lookup( getUserDN(String.valueOf(user.getId())), new UserPerunUserIdContextMapper()); } catch (NameNotFoundException ex) { return false; } return true; }
/** * This method depends on a DirObjectFactory ({@link * org.springframework.ldap.core.support.DefaultDirObjectFactory}) being set in the ContextSource. */ @Test public void testLookup_Plain() { String expectedDn = "cn=Some Person2, ou=company1, ou=Sweden," + base; DirContextAdapter result = (DirContextAdapter) tested.lookup(expectedDn); assertEquals("Some Person2", result.getStringAttribute("cn")); assertEquals("Person2", result.getStringAttribute("sn")); assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description")); LdapName expectedName = LdapUtils.newLdapName(expectedDn); assertEquals(expectedName, result.getDn()); assertEquals(expectedDn, result.getNameInNamespace()); }
private void populateMembers(DirContextAdapter contextAdapter, List<User> memberList) throws NamingException { String[] memberDNs = contextAdapter.getStringAttributes("member"); for (String memberDn : memberDNs) { DirContextAdapter memAdapter = (DirContextAdapter) ldapTemplate.lookup(memberDn); try { populateMembers(memAdapter, memberList); } catch (Exception e) { User person = getPerson(memAdapter); memberList.add(person); } } }
public boolean userPasswordExists(User user) { Object o = ldapTemplate.lookup( getUserDN(String.valueOf(user.getId())), new UserAttributesContextMapper()); Attributes attrs = null; if (o != null) attrs = (Attributes) o; if (attrs != null) { Attribute a = attrs.get("userPassword"); if (a != null) return true; } return false; }
@Test public void testUnbind() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; // Perform test dummyDao.unbind(dn, "Some Person"); try { // Verify result - check that the operation was not rolled back ldapTemplate.lookup(dn); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertTrue(true); } }
@Test public void testBindAndUnbind_Plain() { DirContextAdapter adapter = new DirContextAdapter(); adapter.setAttributeValues("objectclass", new String[] {"top", "person"}); adapter.setAttributeValue("cn", "Some Person4"); adapter.setAttributeValue("sn", "Person4"); tested.bind("cn=Some Person4, ou=company1, ou=Sweden," + base, adapter, null); DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person4, ou=company1, ou=Sweden," + base); assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person4"); assertThat(result.getStringAttribute("sn")).isEqualTo("Person4"); assertThat(result.getDn()) .isEqualTo(LdapUtils.newLdapName("cn=Some Person4,ou=company1,ou=Sweden," + base)); tested.unbind("cn=Some Person4,ou=company1,ou=Sweden," + base); try { tested.lookup("cn=Some Person4, ou=company1, ou=Sweden," + base); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertThat(true).isTrue(); } }
public List<String> getAllUniqueMembersInGroup(int groupId, int voId) { List<String> uniqueMembers = new ArrayList<String>(); Object o = ldapTemplate.lookup( getGroupDN(String.valueOf(voId), String.valueOf(groupId)), new GroupUniqueMemberOfContextMapper()); String[] uniqueGroupInformation = (String[]) o; if (uniqueGroupInformation != null) { for (String s : uniqueGroupInformation) { Matcher userIdMatcher = userIdPattern.matcher(s); if (userIdMatcher.find()) uniqueMembers.add(s.substring(userIdMatcher.start(), userIdMatcher.end())); } } return uniqueMembers; }
public String getVoShortName(int voId) throws InternalErrorException { Object o = ldapTemplate.lookup(getVoDNByVoId(String.valueOf(voId)), new VoShortNameContextMapper()); String[] voShortNameInformation = (String[]) o; String voShortName = null; if (voShortNameInformation == null || voShortNameInformation[0] == null) throw new InternalErrorException("There is no shortName in ldap for vo with id=" + voId); if (voShortNameInformation.length != 1) throw new InternalErrorException( "There is not exactly one short name of vo with id=" + voId + " in ldap. Count of shortnames is " + voShortNameInformation.length); voShortName = voShortNameInformation[0]; return voShortName; }
public boolean userAttributeExist(User user, String ldapAttributeName) throws InternalErrorException { if (ldapAttributeName == null) throw new InternalErrorException("ldapAttributeName can't be null."); Object o = null; try { setLdapAttributeName(ldapAttributeName); o = ldapTemplate.lookup( getUserDN(String.valueOf(user.getId())), new UserPerunUserAttributeContextMapper()); } catch (NameNotFoundException ex) { return false; } if (o == null) return false; return true; }
public boolean resourceAttributeExist(Resource resource, String ldapAttributeName) throws InternalErrorException { if (ldapAttributeName == null) throw new InternalErrorException("ldapAttributeName can't be null."); Object o = null; try { setLdapAttributeName(ldapAttributeName); o = ldapTemplate.lookup( getResourceDN(String.valueOf(resource.getVoId()), String.valueOf(resource.getId())), new ResourcePerunResourceAttributeContextMapper()); } catch (NameNotFoundException ex) { return false; } if (o == null) return false; return true; }
public boolean isAlreadyMember(Member member, Group group) { Object o = ldapTemplate.lookup( getUserDN(String.valueOf(member.getUserId())), new UserMemberOfContextMapper()); String[] memberOfInformation = (String[]) o; if (memberOfInformation != null) { for (String s : memberOfInformation) { if (s.equals( "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase())) return true; } } return false; }
@Test public void testCreateWithException() { try { dummyDao.createWithException( "Sweden", "company1", "some testperson", "testperson", "some description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } log.debug("Verifying result"); // Verify that no entry was created try { ldapTemplate.lookup("cn=some testperson, ou=company1, ou=Sweden"); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertTrue(true); } }
@Test public void testModifyAttributes() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; // Perform test dummyDao.modifyAttributes(dn, "Updated lastname", "Updated description"); // Verify result - check that the operation was not rolled back Object result = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Updated lastname", attributes.get("sn").get()); assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); assertNotNull(result); }
@Test public void testUpdateAndRename() { String dn = "cn=Some Person2,ou=company1,ou=Sweden"; String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; // Perform test dummyDao.updateAndRename(dn, newDn, "Updated description"); // Verify that entry was moved and updated. Object object = ldapTemplate.lookup( newDn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Updated description", attributes.get("description").get()); return new Object(); } }); assertNotNull(object); dummyDao.updateAndRename(newDn, dn, "Sweden, Company1, Some Person2"); }
@Test public void testUnbindWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; try { // Perform test dummyDao.unbindWithException(dn, "Some Person"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } // Verify result - check that the operation was properly rolled back Object ldapResult = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { // Just verify that the entry still exists. return new Object(); } }); assertNotNull(ldapResult); }
/** Outil de recherche d'un contact */ private Operator_Ldap findByPrimaryKey(String sn) { Name dn = buildDn(sn); return (Operator_Ldap) ldapTemplate.lookup(dn, new OperatorAttributMapper()); }
@Override public Person findByPrimaryKey(String country, String company, String fullname) { LdapName dn = buildDn(country, company, fullname); return ldapTemplate.lookup(dn, PERSON_CONTEXT_MAPPER); }