PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/server/src/test/java/org/eurekastreams/server/persistence/mappers/ldap/LdapLookupTest.java

http://github.com/lmco/eurekastreams
Java | 150 lines | 67 code | 28 blank | 55 comment | 0 complexity | 42ff6905a04a5de309e59ad7507c247f MD5 | raw file
  1. /*
  2. * Copyright (c) 2010 Lockheed Martin Corporation
  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.eurekastreams.server.persistence.mappers.ldap;
  17. import static org.junit.Assert.assertEquals;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import javax.naming.directory.SearchControls;
  22. import org.eurekastreams.server.persistence.mappers.ldap.callback.CallbackHandlerFactory;
  23. import org.eurekastreams.server.persistence.mappers.ldap.filters.FilterCreator;
  24. import org.eurekastreams.server.persistence.mappers.ldap.templateretrievers.LdapTemplateRetriever;
  25. import org.eurekastreams.server.persistence.mappers.requests.LdapLookupRequest;
  26. import org.jmock.Expectations;
  27. import org.jmock.integration.junit4.JUnit4Mockery;
  28. import org.jmock.lib.legacy.ClassImposteriser;
  29. import org.junit.Test;
  30. import org.springframework.ldap.core.CollectingNameClassPairCallbackHandler;
  31. import org.springframework.ldap.core.LdapTemplate;
  32. import org.springframework.ldap.filter.AbstractFilter;
  33. /**
  34. * Test for LdapLookup.
  35. *
  36. */
  37. public class LdapLookupTest
  38. {
  39. /**
  40. * Mocking context.
  41. */
  42. private final JUnit4Mockery context = new JUnit4Mockery()
  43. {
  44. {
  45. setImposteriser(ClassImposteriser.INSTANCE);
  46. }
  47. };
  48. /**
  49. * {@link LdapTemplateRetriever}.
  50. */
  51. private LdapTemplateRetriever ldapTemplateRetriever = context.mock(LdapTemplateRetriever.class);
  52. /**
  53. * LDAP filter creator for finding group by name.
  54. */
  55. private FilterCreator filterCreator = context.mock(FilterCreator.class);
  56. /**
  57. * The {@link CallbackHandlerFactory}.
  58. */
  59. private CallbackHandlerFactory handlerFactory = context.mock(CallbackHandlerFactory.class);
  60. /**
  61. * Strategy for performing the search against ldap.
  62. */
  63. private LdapSearchStrategy ldapSearchStrategy = context.mock(LdapSearchStrategy.class);
  64. /**
  65. * {@link LdapLookupRequest}.
  66. */
  67. private LdapLookupRequest ldapLookupRequest = context.mock(LdapLookupRequest.class);
  68. /**
  69. * {@link LdapTemplate).
  70. */
  71. private LdapTemplate template = context.mock(LdapTemplate.class);
  72. /**
  73. * {@link AbstractFilter}.
  74. */
  75. private AbstractFilter filter = context.mock(AbstractFilter.class);
  76. /**
  77. * {@link CollectingNameClassPairCallbackHandler}.
  78. */
  79. private CollectingNameClassPairCallbackHandler handler = context.mock(CollectingNameClassPairCallbackHandler.class);
  80. /**
  81. * String to use in request for query.
  82. */
  83. private String queryString = "queryString";
  84. /**
  85. * System under test.
  86. */
  87. private LdapLookup<Object> sut = new LdapLookup<Object>(ldapTemplateRetriever, filterCreator, handlerFactory,
  88. ldapSearchStrategy);
  89. /**
  90. * Test.
  91. */
  92. @Test
  93. public void test()
  94. {
  95. final ArrayList<Object> rawResults = new ArrayList<Object>(Arrays.asList(new Object(), null, new Object()));
  96. context.checking(new Expectations()
  97. {
  98. {
  99. allowing(ldapLookupRequest).getSearchUpperBound();
  100. will(returnValue(Integer.MAX_VALUE));
  101. allowing(ldapLookupRequest).getQueryString();
  102. will(returnValue(queryString));
  103. allowing(ldapTemplateRetriever).getLdapTemplate(ldapLookupRequest);
  104. will(returnValue(template));
  105. allowing(filterCreator).getFilter(queryString);
  106. will(returnValue(filter));
  107. allowing(handlerFactory).getCallbackHandler();
  108. will(returnValue(handler));
  109. allowing(filter).encode();
  110. will(returnValue("filter"));
  111. allowing(ldapSearchStrategy).searchLdap(with(any(LdapTemplate.class)), with(any(String.class)),
  112. with(any(SearchControls.class)), with(any(CollectingNameClassPairCallbackHandler.class)));
  113. allowing(handler).getList();
  114. will(returnValue(rawResults));
  115. }
  116. });
  117. assertEquals(3, rawResults.size());
  118. List<Object> results = sut.execute(ldapLookupRequest);
  119. assertEquals(2, results.size());
  120. context.assertIsSatisfied();
  121. }
  122. }