PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/lucky.sutanto/spring-ldap
Java | 194 lines | 143 code | 31 blank | 20 comment | 0 complexity | 8b562f84573c6e532309fcdbdc7f4a78 MD5 | raw file
  1. /*
  2. * Copyright 2005-2010 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.core;
  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.core.io.ClassPathResource;
  22. import org.springframework.ldap.core.ContextSource;
  23. import org.springframework.ldap.core.DirContextAdapter;
  24. import org.springframework.ldap.core.DistinguishedName;
  25. import org.springframework.ldap.core.LdapTemplate;
  26. import org.springframework.ldap.support.LdapUtils;
  27. import org.springframework.ldap.test.AttributeCheckAttributesMapper;
  28. import org.springframework.ldap.test.AttributeCheckContextMapper;
  29. import org.springframework.ldap.test.LdapTestUtils;
  30. import org.springframework.test.context.ContextConfiguration;
  31. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  32. import javax.naming.Name;
  33. import javax.naming.directory.SearchControls;
  34. import java.util.List;
  35. import static junit.framework.Assert.assertEquals;
  36. /**
  37. * Verifies that LdapTemplate search methods work against OpenLDAP with TLS.
  38. *
  39. * @author Mattias Hellborg Arthursson
  40. */
  41. @ContextConfiguration(locations = { "/conf/ldapTemplateTestContext-tls.xml" })
  42. public class LdapTemplateSearchResultITest extends AbstractJUnit4SpringContextTests {
  43. @Autowired
  44. private LdapTemplate tested;
  45. @Autowired
  46. private ContextSource contextSource;
  47. private AttributeCheckAttributesMapper attributesMapper;
  48. private AttributeCheckContextMapper contextMapper;
  49. private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", "telephoneNumber" };
  50. private static final String[] CN_SN_ATTRS = { "cn", "sn" };
  51. private static final String[] ABSENT_ATTRIBUTES = { "description", "telephoneNumber" };
  52. private static final String[] CN_SN_VALUES = { "Some Person2", "Person2" };
  53. private static final String[] ALL_VALUES = { "Some Person2", "Person2", "Sweden, Company1, Some Person2",
  54. "+46 555-123458" };
  55. private static final String BASE_STRING = "";
  56. private static final String FILTER_STRING = "(&(objectclass=person)(sn=Person2))";
  57. private static final Name BASE_NAME = new DistinguishedName(BASE_STRING);
  58. @Before
  59. public void prepareTestedInstance() throws Exception {
  60. LdapTestUtils.cleanAndSetup(
  61. contextSource,
  62. LdapUtils.newLdapName("ou=People"),
  63. new ClassPathResource("/setup_data.ldif"));
  64. attributesMapper = new AttributeCheckAttributesMapper();
  65. contextMapper = new AttributeCheckContextMapper();
  66. }
  67. @After
  68. public void cleanup() throws Exception {
  69. LdapTestUtils.clearSubContexts(contextSource, LdapUtils.newLdapName("ou=People"));
  70. attributesMapper = null;
  71. contextMapper = null;
  72. }
  73. @Test
  74. public void testSearch_AttributesMapper() {
  75. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  76. attributesMapper.setExpectedValues(ALL_VALUES);
  77. List<Object> list = tested.search(BASE_STRING, FILTER_STRING, attributesMapper);
  78. assertEquals(1, list.size());
  79. }
  80. @Test
  81. public void testSearch_SearchScope_AttributesMapper() {
  82. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  83. attributesMapper.setExpectedValues(ALL_VALUES);
  84. List<Object> list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper);
  85. assertEquals(1, list.size());
  86. }
  87. @Test
  88. public void testSearch_SearchScope_LimitedAttrs_AttributesMapper() {
  89. attributesMapper.setExpectedAttributes(CN_SN_ATTRS);
  90. attributesMapper.setExpectedValues(CN_SN_VALUES);
  91. attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  92. List<Object> list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
  93. attributesMapper);
  94. assertEquals(1, list.size());
  95. }
  96. @Test
  97. public void testSearch_AttributesMapper_Name() {
  98. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  99. attributesMapper.setExpectedValues(ALL_VALUES);
  100. List<Object> list = tested.search(BASE_NAME, FILTER_STRING, attributesMapper);
  101. assertEquals(1, list.size());
  102. }
  103. @Test
  104. public void testSearch_SearchScope_AttributesMapper_Name() {
  105. attributesMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  106. attributesMapper.setExpectedValues(ALL_VALUES);
  107. List<Object> list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, attributesMapper);
  108. assertEquals(1, list.size());
  109. }
  110. @Test
  111. public void testSearch_SearchScope_LimitedAttrs_AttributesMapper_Name() {
  112. attributesMapper.setExpectedAttributes(CN_SN_ATTRS);
  113. attributesMapper.setExpectedValues(CN_SN_VALUES);
  114. attributesMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  115. List<Object> list = tested
  116. .search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, attributesMapper);
  117. assertEquals(1, list.size());
  118. }
  119. @Test
  120. public void testSearch_ContextMapper() {
  121. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  122. contextMapper.setExpectedValues(ALL_VALUES);
  123. List<DirContextAdapter> list = tested.search(BASE_STRING, FILTER_STRING, contextMapper);
  124. assertEquals(1, list.size());
  125. }
  126. @Test
  127. public void testSearch_SearchScope_ContextMapper() {
  128. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  129. contextMapper.setExpectedValues(ALL_VALUES);
  130. List<DirContextAdapter> list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper);
  131. assertEquals(1, list.size());
  132. }
  133. @Test
  134. public void testSearch_SearchScope_LimitedAttrs_ContextMapper() {
  135. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  136. contextMapper.setExpectedValues(CN_SN_VALUES);
  137. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  138. List<DirContextAdapter> list = tested.search(BASE_STRING, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
  139. assertEquals(1, list.size());
  140. }
  141. @Test
  142. public void testSearch_ContextMapper_Name() {
  143. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  144. contextMapper.setExpectedValues(ALL_VALUES);
  145. List<DirContextAdapter> list = tested.search(BASE_NAME, FILTER_STRING, contextMapper);
  146. assertEquals(1, list.size());
  147. }
  148. @Test
  149. public void testSearch_SearchScope_ContextMapper_Name() {
  150. contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
  151. contextMapper.setExpectedValues(ALL_VALUES);
  152. List<DirContextAdapter> list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, contextMapper);
  153. assertEquals(1, list.size());
  154. }
  155. @Test
  156. public void testSearch_SearchScope_LimitedAttrs_ContextMapper_Name() {
  157. contextMapper.setExpectedAttributes(CN_SN_ATTRS);
  158. contextMapper.setExpectedValues(CN_SN_VALUES);
  159. contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
  160. List<DirContextAdapter> list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
  161. assertEquals(1, list.size());
  162. }
  163. }