View Javadoc
1   /*
2    * Copyright 2013-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.AddRequest;
20  import com.unboundid.ldap.sdk.Entry;
21  import org.hamcrest.BaseMatcher;
22  import org.hamcrest.Description;
23  
24  import static com.btmatthews.maven.plugins.ldap.FormatTestUtils.createEntry;
25  
26  /**
27   * Used to match {@link AddRequest}.
28   *
29   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
30   * @since 1.2.0
31   */
32  public final class AddRequestMatcher extends BaseMatcher<AddRequest> {
33  
34      /**
35       * The expected LDAP entry.
36       */
37      private Entry expected;
38  
39      /**
40       * Initialise the matcher with the expected LDAP entry.
41       *
42       * @param entry The expected LDAP entry.
43       */
44      public AddRequestMatcher(final Entry entry) {
45          expected = entry;
46      }
47  
48      /**
49       * Initialise the matcher with the expected distinguished name and attribute name value pairs.
50       *
51       * @param dn         The distinguished name.
52       * @param nameValues The attribute name value pairs.
53       */
54      public AddRequestMatcher(final String dn, final String... nameValues) {
55          this(createEntry(dn, nameValues));
56      }
57  
58      /**
59       * Match {@code item} to verify that it is an {@link AddRequest} with a DN and attribute values matching
60       * those of the LDAP entry saved in the constructor.
61       *
62       * @param item The object being matched.
63       * @return {@code true} if the object matches.
64       */
65      public boolean matches(final Object item) {
66          if (item instanceof AddRequest) {
67              final AddRequest request = (AddRequest) item;
68              if (expected.getDN().equals(request.getDN())) {
69                  return true;
70              }
71          }
72          return false;
73      }
74  
75      /**
76       * Describe this matching rule.
77       *
78       * @param description Used to build the description.
79       */
80      public void describeTo(final Description description) {
81      }
82  }