|
21 | 21 | import org.junit.Before;
|
22 | 22 | import org.junit.Test;
|
23 | 23 | import org.mockito.ArgumentCaptor;
|
| 24 | +import org.mockito.verification.VerificationMode; |
| 25 | + |
24 | 26 | import org.springframework.LdapDataEntry;
|
25 | 27 | import org.springframework.dao.EmptyResultDataAccessException;
|
26 | 28 | import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
|
29 | 31 | import org.springframework.ldap.PartialResultException;
|
30 | 32 | import org.springframework.ldap.UncategorizedLdapException;
|
31 | 33 | import org.springframework.ldap.filter.EqualsFilter;
|
| 34 | +import org.springframework.ldap.filter.Filter; |
32 | 35 | import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
33 | 36 | import org.springframework.ldap.query.LdapQuery;
|
34 | 37 | import org.springframework.ldap.query.LdapQueryBuilder;
|
@@ -683,6 +686,56 @@ public void verifyThatFindOneThrowsIncorrectResultSizeDataAccessExceptionWhenMor
|
683 | 686 | verify(dirContextMock).close();
|
684 | 687 | }
|
685 | 688 |
|
| 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 | + |
686 | 739 | @Test
|
687 | 740 | public void testSearch_ContextMapper_ReturningAttrs() throws Exception {
|
688 | 741 | expectGetReadOnlyContext();
|
@@ -1816,7 +1869,7 @@ private void singleSearchResult(SearchControls controls, SearchResult searchResu
|
1816 | 1869 | setupSearchResults(controls, new SearchResult[] { searchResult });
|
1817 | 1870 | }
|
1818 | 1871 |
|
1819 |
| - private void setupSearchResults(SearchControls controls, SearchResult[] searchResults) throws Exception { |
| 1872 | + private void setupSearchResults(SearchControls controls, SearchResult... searchResults) throws Exception { |
1820 | 1873 | when(dirContextMock.search(
|
1821 | 1874 | eq(nameMock),
|
1822 | 1875 | eq("(ou=somevalue)"),
|
|
0 commit comments