PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/integration-tests/src/test/java/org/springframework/ldap/itest/LdapTemplateSearchResultITest.java

https://gitlab.com/lucky.sutanto/spring-ldap
Java | 424 lines | 344 code | 58 blank | 22 comment | 0 complexity | 9e3196463c8dc376693f0ed39fff94d3 MD5 | raw file
  1. /*
  2. * Copyright 2005-2013 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.ldap.itest;
  17. import org.junit.After;
  18. import org.junit.Before;
  19. import org.junit.Test;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.dao.EmptyResultDataAccessException;
  22. import org.springframework.dao.IncorrectResultSizeDataAccessException;
  23. import org.springframework.ldap.NameNotFoundException;
  24. import org.springframework.ldap.SizeLimitExceededException;
  25. import org.springframework.ldap.core.ContextMapper;
  26. import org.springframework.ldap.core.DirContextAdapter;
  27. import org.springframework.ldap.core.DirContextOperations;
  28. import org.springframework.ldap.core.LdapTemplate;
  29. import org.springframework.ldap.core.support.AbstractContextMapper;
  30. import org.springframework.ldap.query.SearchScope;
  31. import org.springframework.ldap.support.LdapUtils;
  32. import org.springframework.ldap.test.AttributeCheckAttributesMapper;
  33. import org.springframework.ldap.test.AttributeCheckContextMapper;
  34. import org.springframework.test.annotation.DirtiesContext;
  35. import org.springframework.test.annotation.DirtiesContext.ClassMode;
  36. import org.springframework.test.context.ContextConfiguration;
  37. import javax.naming.Name;
  38. import javax.naming.NamingException;
  39. import javax.naming.directory.SearchControls;
  40. import java.util.List;
  41. import static junit.framework.Assert.assertEquals;
  42. import static junit.framework.Assert.assertNotNull;
  43. import static junit.framework.Assert.assertTrue;
  44. import static junit.framework.Assert.fail;
  45. import static org.springframework.ldap.query.LdapQueryBuilder.query;
  46. /**
  47. * Tests for LdapTemplate's search methods. This test class tests all the
  48. * different versions of the search methods except the generic ones covered in
  49. * other tests.
  50. *
  51. * @author Mattias Hellborg Arthursson
  52. */
  53. @ContextConfiguration(locations = {"/conf/ldapTemplateTestContext.xml"})
  54. @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
  55. public class LdapTemplateSearchResultITest extends AbstractLdapTemplateIntegrationTest {
  56. @Autowired
  57. private LdapTemplate tested;
  58. private AttributeCheckAttributesMapper attributesMapper;
  59. private AttributeCheckContextMapper contextMapper;
  60. private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", "telephoneNumber" };
  61. private static final String[] CN_SN_ATTRS = { "cn", "sn" };
  62. private static final String[] ABSENT_ATTRIBUTES = { "description", "telephoneNumber" };
  63. private static final String[] CN_SN_VALUES = { "Some Person2", "Person2" };
  64. private static final String[] ALL_VALUES = { "Some Person2", "Person2", "Sweden, Company1, Some Person2",
  65. "+46 555-654321" };
  66. private static final String BASE_STRING = "";
  67. private static final String FILTER_STRING = "(&(objectclass=person)(sn=Person2))";
  68. private static final Name BASE_NAME = LdapUtils.newLdapName(BASE_STRING);
  69. @Before
  70. public void prepareTestedInstance() throws Exception {
  71. attributesMapper = new AttributeCheckAttributesMapper();
  72. contextMapper = new AttributeCheckContextMapper();
  73. }
  74. @After
  75. public void cleanup() throws Exception {
  76. attributesMapper = null;
  77. contextMapper = null;
  78. }
  79. @Test
  80. public void testSearch_AttributesMapper() {
  81. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  82. attributesMapper.setExpectedValues(ALL_VALUES);
  83. List list = tested.search(BASE_STRING, FILTER_STRING, attributesMapper);
  84. assertEquals(1, list.size());
  85. }
  86. @Test
  87. public void testSearch_LdapQuery_AttributesMapper() {
  88. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  89. attributesMapper.setExpectedValues(ALL_VALUES);
  90. List<Object> list = tested.search(query()
  91. .base(BASE_STRING)
  92. .where("objectclass").is("person").and("sn").is("Person2"),
  93. attributesMapper);
  94. assertEquals(1, list.size());
  95. }
  96. @Test
  97. public void testSearch_LdapQuery_AttributesMapper_FewerAttributes() {
  98. attributesMapper.setExpectedAttributes(new String[] {"cn"});
  99. attributesMapper.setExpectedValues(new String[]{"Some Person2"});
  100. List<Object> list = tested.search(query()
  101. .base(BASE_STRING)
  102. .attributes("cn")
  103. .where("objectclass").is("person").and("sn").is("Person2"),
  104. attributesMapper);
  105. assertEquals(1, list.size());
  106. }
  107. @Test
  108. public void testSearch_LdapQuery_AttributesMapper_SearchScope() {
  109. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  110. attributesMapper.setExpectedValues(ALL_VALUES);
  111. List<Object> list = tested.search(query()
  112. .base(BASE_STRING)
  113. .searchScope(SearchScope.ONELEVEL)
  114. .where("objectclass").is("person").and("sn").is("Person2"),
  115. attributesMapper);
  116. assertEquals(0, list.size());
  117. }
  118. @Test
  119. public void testSearch_LdapQuery_AttributesMapper_SearchScope_CorrectBase() {
  120. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  121. attributesMapper.setExpectedValues(ALL_VALUES);
  122. List<Object> list = tested.search(query()
  123. .base("ou=company1,ou=Sweden")
  124. .searchScope(SearchScope.ONELEVEL)
  125. .where("objectclass").is("person").and("sn").is("Person2"),
  126. attributesMapper);
  127. assertEquals(1, list.size());
  128. }
  129. @Test
  130. public void testSearch_LdapQuery_AttributesMapper_NoBase() {
  131. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  132. attributesMapper.setExpectedValues(ALL_VALUES);
  133. List<Object> list = tested.search(query()
  134. .where("objectclass").is("person").and("sn").is("Person2"),
  135. attributesMapper);
  136. assertEquals(1, list.size());
  137. }
  138. @Test
  139. public void testSearch_LdapQuery_AttributesMapper_DifferentBase() {
  140. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  141. attributesMapper.setExpectedValues(ALL_VALUES);
  142. List<Object> list = tested.search(query()
  143. .base("ou=Norway")
  144. .where("objectclass").is("person").and("sn").is("Person2"),
  145. attributesMapper);
  146. assertEquals(0, list.size());
  147. }
  148. @Test
  149. public void testSearch_SearchScope_AttributesMapper() {
  150. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  151. attributesMapper.setExpectedValues(ALL_VALUES);
  152. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper);
  153. assertEquals(1, list.size());
  154. }
  155. @Test
  156. public void testSearch_SearchScope_LimitedAttrs_AttributesMapper() {
  157. attributesMapper.setExpectedAttributes(CN_SN_ATTRS);
  158. attributesMapper.setExpectedValues(CN_SN_VALUES);
  159. attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  160. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  161. attributesMapper);
  162. assertEquals(1, list.size());
  163. }
  164. @Test
  165. public void testSearch_AttributesMapper_Name() {
  166. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  167. attributesMapper.setExpectedValues(ALL_VALUES);
  168. List list = tested.search(BASE_NAME, FILTER_STRING, attributesMapper);
  169. assertEquals(1, list.size());
  170. }
  171. @Test
  172. public void testSearch_SearchScope_AttributesMapper_Name() {
  173. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  174. attributesMapper.setExpectedValues(ALL_VALUES);
  175. List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper);
  176. assertEquals(1, list.size());
  177. }
  178. @Test
  179. public void testSearch_SearchScope_LimitedAttrs_AttributesMapper_Name() {
  180. attributesMapper.setExpectedAttributes(CN_SN_ATTRS);
  181. attributesMapper.setExpectedValues(CN_SN_VALUES);
  182. attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  183. List list = tested
  184. .search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper);
  185. assertEquals(1, list.size());
  186. }
  187. @Test
  188. public void testSearch_ContextMapper() {
  189. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  190. contextMapper.setExpectedValues(ALL_VALUES);
  191. List list = tested.search(BASE_STRING, FILTER_STRING, contextMapper);
  192. assertEquals(1, list.size());
  193. }
  194. @Test
  195. public void testSearchForObject() {
  196. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  197. contextMapper.setExpectedValues(ALL_VALUES);
  198. DirContextAdapter result = (DirContextAdapter) tested
  199. .searchForObject(BASE_STRING, FILTER_STRING, contextMapper);
  200. assertNotNull(result);
  201. }
  202. @Test(expected = IncorrectResultSizeDataAccessException.class)
  203. public void testSearchForObjectWithMultipleHits() {
  204. tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=*))", new AbstractContextMapper() {
  205. @Override
  206. protected Object doMapFromContext(DirContextOperations ctx) {
  207. return ctx;
  208. }
  209. });
  210. }
  211. @Test(expected = EmptyResultDataAccessException.class)
  212. public void testSearchForObjectNoHits() {
  213. tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=Person does not exist))", new AbstractContextMapper() {
  214. @Override
  215. protected Object doMapFromContext(DirContextOperations ctx) {
  216. return ctx;
  217. }
  218. });
  219. }
  220. @Test
  221. public void testSearch_SearchScope_ContextMapper() {
  222. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  223. contextMapper.setExpectedValues(ALL_VALUES);
  224. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper);
  225. assertEquals(1, list.size());
  226. }
  227. @Test
  228. public void testSearch_SearchScope_LimitedAttrs_ContextMapper() {
  229. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  230. contextMapper.setExpectedValues(CN_SN_VALUES);
  231. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  232. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
  233. assertEquals(1, list.size());
  234. }
  235. @Test
  236. public void testSearch_ContextMapper_Name() {
  237. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  238. contextMapper.setExpectedValues(ALL_VALUES);
  239. List list = tested.search(BASE_NAME, FILTER_STRING, contextMapper);
  240. assertEquals(1, list.size());
  241. }
  242. @Test
  243. public void testSearch_ContextMapper_LdapQuery() {
  244. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  245. contextMapper.setExpectedValues(ALL_VALUES);
  246. List<DirContextAdapter> list = tested.search(query()
  247. .base(BASE_NAME)
  248. .where("objectclass").is("person").and("sn").is("Person2"),
  249. contextMapper);
  250. assertEquals(1, list.size());
  251. }
  252. @Test
  253. public void testSearch_ContextMapper_LdapQuery_NoBase() {
  254. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  255. contextMapper.setExpectedValues(ALL_VALUES);
  256. List<DirContextAdapter> list = tested.search(query()
  257. .where("objectclass").is("person").and("sn").is("Person2"),
  258. contextMapper);
  259. assertEquals(1, list.size());
  260. }
  261. @Test
  262. public void testSearch_ContextMapper_LdapQuery_SearchScope() {
  263. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  264. contextMapper.setExpectedValues(ALL_VALUES);
  265. List<DirContextAdapter> list = tested.search(query()
  266. .base(BASE_NAME)
  267. .searchScope(SearchScope.ONELEVEL)
  268. .where("objectclass").is("person").and("sn").is("Person2"),
  269. contextMapper);
  270. assertEquals(0, list.size());
  271. }
  272. @Test
  273. public void testSearch_ContextMapper_LdapQuery_SearchScope_CorrectBase() {
  274. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  275. contextMapper.setExpectedValues(ALL_VALUES);
  276. List<DirContextAdapter> list = tested.search(query()
  277. .base("ou=company1,ou=Sweden")
  278. .searchScope(SearchScope.ONELEVEL)
  279. .where("objectclass").is("person").and("sn").is("Person2"),
  280. contextMapper);
  281. assertEquals(1, list.size());
  282. }
  283. @Test
  284. public void testSearchForContext_LdapQuery() {
  285. DirContextOperations result = tested.searchForContext(query()
  286. .where("objectclass").is("person").and("sn").is("Person2"));
  287. assertNotNull(result);
  288. assertEquals("Person2", result.getStringAttribute("sn"));
  289. }
  290. @Test(expected = EmptyResultDataAccessException.class)
  291. public void testSearchForContext_LdapQuery_SearchScopeNotFound() {
  292. tested.searchForContext(query()
  293. .searchScope(SearchScope.ONELEVEL)
  294. .where("objectclass").is("person").and("sn").is("Person2"));
  295. }
  296. @Test
  297. public void testSearchForContext_LdapQuery_SearchScope_CorrectBase() {
  298. DirContextOperations result =
  299. tested.searchForContext(query()
  300. .searchScope(SearchScope.ONELEVEL)
  301. .base("ou=company1,ou=Sweden")
  302. .where("objectclass").is("person").and("sn").is("Person2"));
  303. assertNotNull(result);
  304. assertEquals("Person2", result.getStringAttribute("sn"));
  305. }
  306. @Test
  307. public void testSearch_SearchScope_ContextMapper_Name() {
  308. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  309. contextMapper.setExpectedValues(ALL_VALUES);
  310. List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper);
  311. assertEquals(1, list.size());
  312. }
  313. @Test
  314. public void testSearch_SearchScope_LimitedAttrs_ContextMapper_Name() {
  315. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  316. contextMapper.setExpectedValues(CN_SN_VALUES);
  317. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  318. List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
  319. assertEquals(1, list.size());
  320. }
  321. @Test
  322. public void testSearchWithInvalidSearchBaseShouldByDefaultThrowException() {
  323. try {
  324. tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  325. contextMapper);
  326. fail("NameNotFoundException expected");
  327. }
  328. catch (NameNotFoundException expected) {
  329. assertTrue(true);
  330. }
  331. }
  332. @Test
  333. public void testSearchWithInvalidSearchBaseCanBeConfiguredToSwallowException() {
  334. tested.setIgnoreNameNotFoundException(true);
  335. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  336. contextMapper.setExpectedValues(CN_SN_VALUES);
  337. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  338. List list = tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  339. contextMapper);
  340. assertEquals(0, list.size());
  341. }
  342. @Test
  343. public void verifyThatSearchWithCountLimitReturnsTheEntriesFoundSoFar() {
  344. List<Object> result = tested.search(query()
  345. .countLimit(3)
  346. .where("objectclass").is("person"), new ContextMapper<Object>() {
  347. @Override
  348. public Object mapFromContext(Object ctx) throws NamingException {
  349. return new Object();
  350. }
  351. });
  352. assertEquals(3, result.size());
  353. }
  354. @Test(expected = SizeLimitExceededException.class)
  355. public void verifyThatSearchWithCountLimitWithFlagToFalseThrowsException() {
  356. tested.setIgnoreSizeLimitExceededException(false);
  357. tested.search(query()
  358. .countLimit(3)
  359. .where("objectclass").is("person"), new ContextMapper<Object>() {
  360. @Override
  361. public Object mapFromContext(Object ctx) throws NamingException {
  362. return new Object();
  363. }
  364. });
  365. }
  366. }