View Javadoc
1   /*
2    * Copyright 2008-2016 Brian Thomas Matthews
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  
17  package com.btmatthews.maven.plugins.ldap;
18  
19  import com.unboundid.ldap.sdk.*;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  /**
27   * Helper methods used by test cases.
28   *
29   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
30   * @since 1.2.0
31   */
32  public final class FormatTestUtils {
33      /**
34       * Create a LDAP directory entry from the distinguished name an a set of attributes passed as name value pairs.
35       *
36       * @param dn                  The distinguished name.
37       * @param attributeNameValues Thr attribute names and values.
38       * @return An {@link Entry} object.
39       */
40      public static Entry createEntry(final String dn,
41                                      final String... attributeNameValues) {
42          final List<Attribute> attributes = new LinkedList<Attribute>();
43          for (int i = 0; i < attributeNameValues.length; i += 2) {
44              attributes.add(new Attribute(attributeNameValues[i], attributeNameValues[i + 1]));
45          }
46          return new Entry(dn, attributes);
47      }
48  
49      /**
50       * Create a search result entry from the distinguished name an a set of attributes passed as name value pairs.
51       *
52       * @param dn                  The distinguished name.
53       * @param attributeNameValues Thr attribute names and values.
54       * @return An {@link SearchResultEntry} object.
55       */
56      public static SearchResultEntry createSearchResultEntry(final String dn, final String... attributeNameValues) {
57          final Entry entry = createEntry(dn, attributeNameValues);
58          return new SearchResultEntry(entry);
59      }
60  
61      /**
62       * Create an empty search result.
63       *
64       * @return A {@link SearchResult} object.
65       */
66      public static SearchResult createSearchResult() {
67          return createSearchResult(new ArrayList<SearchResultEntry>());
68      }
69  
70      /**
71       * Create a search result with the search result entries passed as a {@link List}.
72       *
73       * @param entries A list of individual search result entries.
74       * @return A {@link SearchResult} object.
75       */
76      public static SearchResult createSearchResult(final List<SearchResultEntry> entries) {
77          return new SearchResult(0, ResultCode.SUCCESS, null, null, null, entries, null, entries.size(), 0, null);
78      }
79  
80      /**
81       * Create a search result with the search result entries passed as a variant argument list.
82       *
83       * @param entries The individual search result entries.
84       * @return A {@link SearchResult} object.
85       */
86      public static SearchResult createSearchResult(final SearchResultEntry... entries) {
87          return createSearchResult(Arrays.asList(entries));
88      }
89  }