PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/lucky.sutanto/spring-ldap
Java | 394 lines | 317 code | 55 blank | 22 comment | 0 complexity | c6a619524c5d95579e02bb5bfa0a2fa2 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.core.DirContextAdapter;
  25. import org.springframework.ldap.core.DirContextOperations;
  26. import org.springframework.ldap.core.LdapTemplate;
  27. import org.springframework.ldap.core.support.AbstractContextMapper;
  28. import org.springframework.ldap.query.SearchScope;
  29. import org.springframework.ldap.support.LdapUtils;
  30. import org.springframework.ldap.test.AttributeCheckAttributesMapper;
  31. import org.springframework.ldap.test.AttributeCheckContextMapper;
  32. import org.springframework.test.annotation.DirtiesContext;
  33. import org.springframework.test.annotation.DirtiesContext.ClassMode;
  34. import org.springframework.test.context.ContextConfiguration;
  35. import javax.naming.Name;
  36. import javax.naming.directory.SearchControls;
  37. import java.util.List;
  38. import static junit.framework.Assert.assertEquals;
  39. import static junit.framework.Assert.assertNotNull;
  40. import static junit.framework.Assert.assertTrue;
  41. import static junit.framework.Assert.fail;
  42. import static org.springframework.ldap.query.LdapQueryBuilder.query;
  43. /**
  44. * Tests for LdapTemplate's search methods. This test class tests all the
  45. * different versions of the search methods except the generic ones covered in
  46. * other tests.
  47. *
  48. * @author Mattias Hellborg Arthursson
  49. */
  50. @ContextConfiguration(locations = {"/conf/ldapTemplateNamespaceTestContext.xml"})
  51. @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
  52. public class LdapTemplateSearchResultNamespaceConfigITest extends AbstractLdapTemplateIntegrationTest {
  53. @Autowired
  54. private LdapTemplate tested;
  55. private AttributeCheckAttributesMapper attributesMapper;
  56. private AttributeCheckContextMapper contextMapper;
  57. private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", "telephoneNumber" };
  58. private static final String[] CN_SN_ATTRS = { "cn", "sn" };
  59. private static final String[] ABSENT_ATTRIBUTES = { "description", "telephoneNumber" };
  60. private static final String[] CN_SN_VALUES = { "Some Person2", "Person2" };
  61. private static final String[] ALL_VALUES = { "Some Person2", "Person2", "Sweden, Company1, Some Person2",
  62. "+46 555-654321" };
  63. private static final String BASE_STRING = "";
  64. private static final String FILTER_STRING = "(&(objectclass=person)(sn=Person2))";
  65. private static final Name BASE_NAME = LdapUtils.newLdapName(BASE_STRING);
  66. @Before
  67. public void prepareTestedInstance() throws Exception {
  68. attributesMapper = new AttributeCheckAttributesMapper();
  69. contextMapper = new AttributeCheckContextMapper();
  70. }
  71. @After
  72. public void cleanup() throws Exception {
  73. attributesMapper = null;
  74. contextMapper = null;
  75. }
  76. @Test
  77. public void testSearch_AttributesMapper() {
  78. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  79. attributesMapper.setExpectedValues(ALL_VALUES);
  80. List list = tested.search(BASE_STRING, FILTER_STRING, attributesMapper);
  81. assertEquals(1, list.size());
  82. }
  83. @Test
  84. public void testSearch_LdapQuery_AttributesMapper() {
  85. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  86. attributesMapper.setExpectedValues(ALL_VALUES);
  87. List<Object> list = tested.search(query()
  88. .base(BASE_STRING)
  89. .where("objectclass").is("person").and("sn").is("Person2"),
  90. attributesMapper);
  91. assertEquals(1, list.size());
  92. }
  93. @Test
  94. public void testSearch_LdapQuery_AttributesMapper_FewerAttributes() {
  95. attributesMapper.setExpectedAttributes(new String[] {"cn"});
  96. attributesMapper.setExpectedValues(new String[]{"Some Person2"});
  97. List<Object> list = tested.search(query()
  98. .base(BASE_STRING)
  99. .attributes("cn")
  100. .where("objectclass").is("person").and("sn").is("Person2"),
  101. attributesMapper);
  102. assertEquals(1, list.size());
  103. }
  104. @Test
  105. public void testSearch_LdapQuery_AttributesMapper_SearchScope() {
  106. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  107. attributesMapper.setExpectedValues(ALL_VALUES);
  108. List<Object> list = tested.search(query()
  109. .base(BASE_STRING)
  110. .searchScope(SearchScope.ONELEVEL)
  111. .where("objectclass").is("person").and("sn").is("Person2"),
  112. attributesMapper);
  113. assertEquals(0, list.size());
  114. }
  115. @Test
  116. public void testSearch_LdapQuery_AttributesMapper_SearchScope_CorrectBase() {
  117. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  118. attributesMapper.setExpectedValues(ALL_VALUES);
  119. List<Object> list = tested.search(query()
  120. .base("ou=company1,ou=Sweden")
  121. .searchScope(SearchScope.ONELEVEL)
  122. .where("objectclass").is("person").and("sn").is("Person2"),
  123. attributesMapper);
  124. assertEquals(1, list.size());
  125. }
  126. @Test
  127. public void testSearch_LdapQuery_AttributesMapper_NoBase() {
  128. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  129. attributesMapper.setExpectedValues(ALL_VALUES);
  130. List<Object> list = tested.search(query()
  131. .where("objectclass").is("person").and("sn").is("Person2"),
  132. attributesMapper);
  133. assertEquals(1, list.size());
  134. }
  135. @Test
  136. public void testSearch_LdapQuery_AttributesMapper_DifferentBase() {
  137. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  138. attributesMapper.setExpectedValues(ALL_VALUES);
  139. List<Object> list = tested.search(query()
  140. .base("ou=Norway")
  141. .where("objectclass").is("person").and("sn").is("Person2"),
  142. attributesMapper);
  143. assertEquals(0, list.size());
  144. }
  145. @Test
  146. public void testSearch_SearchScope_AttributesMapper() {
  147. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  148. attributesMapper.setExpectedValues(ALL_VALUES);
  149. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper);
  150. assertEquals(1, list.size());
  151. }
  152. @Test
  153. public void testSearch_SearchScope_LimitedAttrs_AttributesMapper() {
  154. attributesMapper.setExpectedAttributes(CN_SN_ATTRS);
  155. attributesMapper.setExpectedValues(CN_SN_VALUES);
  156. attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  157. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  158. attributesMapper);
  159. assertEquals(1, list.size());
  160. }
  161. @Test
  162. public void testSearch_AttributesMapper_Name() {
  163. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  164. attributesMapper.setExpectedValues(ALL_VALUES);
  165. List list = tested.search(BASE_NAME, FILTER_STRING, attributesMapper);
  166. assertEquals(1, list.size());
  167. }
  168. @Test
  169. public void testSearch_SearchScope_AttributesMapper_Name() {
  170. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  171. attributesMapper.setExpectedValues(ALL_VALUES);
  172. List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper);
  173. assertEquals(1, list.size());
  174. }
  175. @Test
  176. public void testSearch_SearchScope_LimitedAttrs_AttributesMapper_Name() {
  177. attributesMapper.setExpectedAttributes(CN_SN_ATTRS);
  178. attributesMapper.setExpectedValues(CN_SN_VALUES);
  179. attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  180. List list = tested
  181. .search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper);
  182. assertEquals(1, list.size());
  183. }
  184. @Test
  185. public void testSearch_ContextMapper() {
  186. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  187. contextMapper.setExpectedValues(ALL_VALUES);
  188. List list = tested.search(BASE_STRING, FILTER_STRING, contextMapper);
  189. assertEquals(1, list.size());
  190. }
  191. @Test
  192. public void testSearchForObject() {
  193. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  194. contextMapper.setExpectedValues(ALL_VALUES);
  195. DirContextAdapter result = (DirContextAdapter) tested
  196. .searchForObject(BASE_STRING, FILTER_STRING, contextMapper);
  197. assertNotNull(result);
  198. }
  199. @Test(expected = IncorrectResultSizeDataAccessException.class)
  200. public void testSearchForObjectWithMultipleHits() {
  201. tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=*))", new AbstractContextMapper() {
  202. @Override
  203. protected Object doMapFromContext(DirContextOperations ctx) {
  204. return ctx;
  205. }
  206. });
  207. }
  208. @Test(expected = EmptyResultDataAccessException.class)
  209. public void testSearchForObjectNoHits() {
  210. tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=Person does not exist))", new AbstractContextMapper() {
  211. @Override
  212. protected Object doMapFromContext(DirContextOperations ctx) {
  213. return ctx;
  214. }
  215. });
  216. }
  217. @Test
  218. public void testSearch_SearchScope_ContextMapper() {
  219. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  220. contextMapper.setExpectedValues(ALL_VALUES);
  221. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper);
  222. assertEquals(1, list.size());
  223. }
  224. @Test
  225. public void testSearch_SearchScope_LimitedAttrs_ContextMapper() {
  226. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  227. contextMapper.setExpectedValues(CN_SN_VALUES);
  228. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  229. List list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
  230. assertEquals(1, list.size());
  231. }
  232. @Test
  233. public void testSearch_ContextMapper_Name() {
  234. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  235. contextMapper.setExpectedValues(ALL_VALUES);
  236. List list = tested.search(BASE_NAME, FILTER_STRING, contextMapper);
  237. assertEquals(1, list.size());
  238. }
  239. @Test
  240. public void testSearch_ContextMapper_LdapQuery() {
  241. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  242. contextMapper.setExpectedValues(ALL_VALUES);
  243. List<DirContextAdapter> list = tested.search(query()
  244. .base(BASE_NAME)
  245. .where("objectclass").is("person").and("sn").is("Person2"),
  246. contextMapper);
  247. assertEquals(1, list.size());
  248. }
  249. @Test
  250. public void testSearch_ContextMapper_LdapQuery_NoBase() {
  251. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  252. contextMapper.setExpectedValues(ALL_VALUES);
  253. List<DirContextAdapter> list = tested.search(query()
  254. .where("objectclass").is("person").and("sn").is("Person2"),
  255. contextMapper);
  256. assertEquals(1, list.size());
  257. }
  258. @Test
  259. public void testSearch_ContextMapper_LdapQuery_SearchScope() {
  260. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  261. contextMapper.setExpectedValues(ALL_VALUES);
  262. List<DirContextAdapter> list = tested.search(query()
  263. .base(BASE_NAME)
  264. .searchScope(SearchScope.ONELEVEL)
  265. .where("objectclass").is("person").and("sn").is("Person2"),
  266. contextMapper);
  267. assertEquals(0, list.size());
  268. }
  269. @Test
  270. public void testSearch_ContextMapper_LdapQuery_SearchScope_CorrectBase() {
  271. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  272. contextMapper.setExpectedValues(ALL_VALUES);
  273. List<DirContextAdapter> list = tested.search(query()
  274. .base("ou=company1,ou=Sweden")
  275. .searchScope(SearchScope.ONELEVEL)
  276. .where("objectclass").is("person").and("sn").is("Person2"),
  277. contextMapper);
  278. assertEquals(1, list.size());
  279. }
  280. @Test
  281. public void testSearchForContext_LdapQuery() {
  282. DirContextOperations result = tested.searchForContext(query()
  283. .where("objectclass").is("person").and("sn").is("Person2"));
  284. assertNotNull(result);
  285. assertEquals("Person2", result.getStringAttribute("sn"));
  286. }
  287. @Test(expected = EmptyResultDataAccessException.class)
  288. public void testSearchForContext_LdapQuery_SearchScopeNotFound() {
  289. tested.searchForContext(query()
  290. .searchScope(SearchScope.ONELEVEL)
  291. .where("objectclass").is("person").and("sn").is("Person2"));
  292. }
  293. @Test
  294. public void testSearchForContext_LdapQuery_SearchScope_CorrectBase() {
  295. DirContextOperations result =
  296. tested.searchForContext(query()
  297. .searchScope(SearchScope.ONELEVEL)
  298. .base("ou=company1,ou=Sweden")
  299. .where("objectclass").is("person").and("sn").is("Person2"));
  300. assertNotNull(result);
  301. assertEquals("Person2", result.getStringAttribute("sn"));
  302. }
  303. @Test
  304. public void testSearch_SearchScope_ContextMapper_Name() {
  305. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  306. contextMapper.setExpectedValues(ALL_VALUES);
  307. List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper);
  308. assertEquals(1, list.size());
  309. }
  310. @Test
  311. public void testSearch_SearchScope_LimitedAttrs_ContextMapper_Name() {
  312. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  313. contextMapper.setExpectedValues(CN_SN_VALUES);
  314. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  315. List list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
  316. assertEquals(1, list.size());
  317. }
  318. @Test
  319. public void testSearchWithInvalidSearchBaseShouldByDefaultThrowException() {
  320. try {
  321. tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  322. contextMapper);
  323. fail("NameNotFoundException expected");
  324. }
  325. catch (NameNotFoundException expected) {
  326. assertTrue(true);
  327. }
  328. }
  329. @Test
  330. public void testSearchWithInvalidSearchBaseCanBeConfiguredToSwallowException() {
  331. tested.setIgnoreNameNotFoundException(true);
  332. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  333. contextMapper.setExpectedValues(CN_SN_VALUES);
  334. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  335. List list = tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  336. contextMapper);
  337. assertEquals(0, list.size());
  338. }
  339. }