Skip to content

Commit 255bfaa

Browse files
committed
Polish ReturnAttributes Override
Closes gh-465
1 parent df40b39 commit 255bfaa

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

Diff for: core/src/main/java/org/springframework/ldap/core/LdapTemplate.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.ldap.query.LdapQuery;
3131
import org.springframework.ldap.support.LdapUtils;
3232
import org.springframework.util.Assert;
33+
import org.springframework.util.CollectionUtils;
34+
import org.springframework.util.ObjectUtils;
3335

3436
import javax.naming.Binding;
3537
import javax.naming.Name;
@@ -43,6 +45,8 @@
4345
import javax.naming.directory.ModificationItem;
4446
import javax.naming.directory.SearchControls;
4547
import javax.naming.ldap.LdapName;
48+
49+
import java.util.Arrays;
4650
import java.util.List;
4751

4852
/**
@@ -1830,11 +1834,11 @@ public <T> List<T> find(Name base, Filter filter, SearchControls searchControls,
18301834
}
18311835

18321836
// extend search controls with the attributes to return
1833-
if (isNotCustomReturningAttributes(searchControls)) {
1837+
if (searchControls.getReturningAttributes() == null) {
18341838
String[] attributes = this.odm.manageClass(clazz);
18351839
searchControls.setReturningAttributes(attributes);
18361840
}
1837-
1841+
18381842
if (LOG.isDebugEnabled()) {
18391843
LOG.debug(String.format("Searching - base=%1$s, finalFilter=%2$s, scope=%3$s", base, finalFilter, searchControls));
18401844
}
@@ -1879,10 +1883,6 @@ else if (result.size() != 1) {
18791883

18801884
return result.get(0);
18811885
}
1882-
1883-
private boolean isNotCustomReturningAttributes(SearchControls searchControls) {
1884-
return searchControls.getReturningAttributes() == null || searchControls.getReturningAttributes().length == 0;
1885-
}
18861886

18871887
/**
18881888
* The status of an authentication attempt.

Diff for: core/src/test/java/org/springframework/ldap/core/LdapTemplateTest.java

+54-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.Before;
2222
import org.junit.Test;
2323
import org.mockito.ArgumentCaptor;
24+
import org.mockito.verification.VerificationMode;
25+
2426
import org.springframework.LdapDataEntry;
2527
import org.springframework.dao.EmptyResultDataAccessException;
2628
import org.springframework.dao.IncorrectResultSizeDataAccessException;
@@ -29,6 +31,7 @@
2931
import org.springframework.ldap.PartialResultException;
3032
import org.springframework.ldap.UncategorizedLdapException;
3133
import org.springframework.ldap.filter.EqualsFilter;
34+
import org.springframework.ldap.filter.Filter;
3235
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
3336
import org.springframework.ldap.query.LdapQuery;
3437
import org.springframework.ldap.query.LdapQueryBuilder;
@@ -683,6 +686,56 @@ public void verifyThatFindOneThrowsIncorrectResultSizeDataAccessExceptionWhenMor
683686
verify(dirContextMock).close();
684687
}
685688

689+
@Test
690+
public void findWhenSearchControlsReturningAttributesSpecifiedThenOverridesOdmReturningAttributes() throws Exception {
691+
Class<Object> expectedClass = Object.class;
692+
693+
Filter filter = new EqualsFilter("ou", "somevalue");
694+
when(contextSourceMock.getReadOnlyContext()).thenReturn(dirContextMock);
695+
when(odmMock.filterFor(any(Class.class), any(Filter.class))).thenReturn(filter);
696+
SearchControls controls = new SearchControls();
697+
controls.setReturningAttributes(new String[] { "attribute" });
698+
DirContextAdapter expectedObject = new DirContextAdapter();
699+
SearchResult searchResult = new SearchResult("", expectedObject, new BasicAttributes());
700+
setupSearchResults(controls, searchResult);
701+
Object expectedResult = expectedObject;
702+
when(odmMock.mapFromLdapDataEntry(expectedObject, expectedClass)).thenReturn(expectedResult, expectedResult);
703+
704+
List<Object> results = tested.find(nameMock, filter, controls, expectedClass);
705+
assertThat(results).hasSize(1);
706+
verify(odmMock, never()).manageClass(any(Class.class));
707+
708+
verify(namingEnumerationMock).close();
709+
verify(dirContextMock).close();
710+
}
711+
712+
@Test
713+
public void findWhenSearchControlsReturningAttributesUnspecifiedThenOdmReturningAttributesOverrides() throws Exception {
714+
Class<Object> expectedClass = Object.class;
715+
String[] expectedReturningAttributes = new String[] { "odmattribute" };
716+
SearchControls expectedControls = new SearchControls();
717+
expectedControls.setReturningObjFlag(true);
718+
expectedControls.setReturningAttributes(expectedReturningAttributes);
719+
720+
Filter filter = new EqualsFilter("ou", "somevalue");
721+
when(contextSourceMock.getReadOnlyContext()).thenReturn(dirContextMock);
722+
when(odmMock.filterFor(eq(expectedClass), any(Filter.class))).thenReturn(filter);
723+
when(odmMock.manageClass(eq(expectedClass))).thenReturn(expectedReturningAttributes);
724+
SearchControls controls = new SearchControls();
725+
DirContextAdapter expectedObject = new DirContextAdapter();
726+
SearchResult searchResult = new SearchResult("", expectedObject, new BasicAttributes());
727+
setupSearchResults(expectedControls, searchResult);
728+
Object expectedResult = expectedObject;
729+
when(odmMock.mapFromLdapDataEntry(expectedObject, expectedClass)).thenReturn(expectedResult, expectedResult);
730+
731+
List<Object> results = tested.find(nameMock, null, controls, expectedClass);
732+
assertThat(results).hasSize(1);
733+
verify(odmMock).manageClass(eq(expectedClass));
734+
735+
verify(namingEnumerationMock).close();
736+
verify(dirContextMock).close();
737+
}
738+
686739
@Test
687740
public void testSearch_ContextMapper_ReturningAttrs() throws Exception {
688741
expectGetReadOnlyContext();
@@ -1816,7 +1869,7 @@ private void singleSearchResult(SearchControls controls, SearchResult searchResu
18161869
setupSearchResults(controls, new SearchResult[] { searchResult });
18171870
}
18181871

1819-
private void setupSearchResults(SearchControls controls, SearchResult[] searchResults) throws Exception {
1872+
private void setupSearchResults(SearchControls controls, SearchResult... searchResults) throws Exception {
18201873
when(dirContextMock.search(
18211874
eq(nameMock),
18221875
eq("(ou=somevalue)"),

0 commit comments

Comments
 (0)