mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
13 hours ago ebb19c5816399b83c42b8f9194dbf3d9296975a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.admin.ads;
 
import static org.assertj.core.api.Assertions.*;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
import org.forgerock.opendj.ldap.DN;
import org.opends.server.DirectoryServerTestCase;
import org.testng.annotations.Test;
 
/** Tests the id-based {@code equals()}/{@code hashCode()} contract of {@link SuffixDescriptor}. */
@Test(groups = { "precommit", "admin" }, sequential = true)
@SuppressWarnings("javadoc")
public class SuffixDescriptorTest extends DirectoryServerTestCase
{
  private static final DN SUFFIX_DN = DN.valueOf("dc=example,dc=com");
  private static final DN OTHER_SUFFIX_DN = DN.valueOf("dc=example,dc=org");
 
  @Test
  public void suffixesWithTheSameIdAreEqual()
  {
    ServerDescriptor server1 = server("server1.example.com");
    ServerDescriptor server2 = server("server2.example.com");
 
    SuffixDescriptor suffix1 = suffix(SUFFIX_DN, server1, server2);
    SuffixDescriptor suffix2 = suffix(SUFFIX_DN, server1, server2);
 
    assertThat(suffix1.getId()).isEqualTo(suffix2.getId());
    assertThat(suffix1).isEqualTo(suffix2);
    assertThat(suffix1.hashCode()).isEqualTo(suffix2.hashCode());
    assertThat(suffix1.compareTo(suffix2)).isZero();
  }
 
  @Test
  public void suffixesWithTheSameIdCollapseInASet()
  {
    ServerDescriptor server = server("server1.example.com");
 
    Set<SuffixDescriptor> suffixes = new HashSet<>();
    suffixes.add(suffix(SUFFIX_DN, server));
    suffixes.add(suffix(SUFFIX_DN, server));
 
    assertThat(suffixes).hasSize(1);
  }
 
  @Test
  public void suffixesOnDifferentDnsAreNotEqual()
  {
    ServerDescriptor server = server("server1.example.com");
 
    assertThat(suffix(SUFFIX_DN, server)).isNotEqualTo(suffix(OTHER_SUFFIX_DN, server));
  }
 
  @Test
  public void suffixesOnDifferentServersAreNotEqual()
  {
    SuffixDescriptor suffix1 = suffix(SUFFIX_DN, server("server1.example.com"));
    SuffixDescriptor suffix2 = suffix(SUFFIX_DN, server("server2.example.com"));
 
    assertThat(suffix1).isNotEqualTo(suffix2);
  }
 
  @Test
  public void suffixIsNotEqualToItsId()
  {
    SuffixDescriptor suffix = suffix(SUFFIX_DN, server("server1.example.com"));
 
    assertThat(suffix.equals(suffix.getId())).isFalse();
    assertThat(suffix.equals(null)).isFalse();
  }
 
  /**
   * The id must not depend on the iteration order of the replica set: {@code getReplicas()}
   * hands out a {@code HashSet} of {@code ReplicaDescriptor}s, which do not override
   * {@code hashCode()}.
   */
  @Test
  public void idIsIndependentOfTheReplicaOrder()
  {
    ServerDescriptor server1 = server("server1.example.com");
    ServerDescriptor server2 = server("server2.example.com");
    assertThat(server1.getId()).isLessThan(server2.getId());
 
    String expectedId = SUFFIX_DN + "-" + server1.getId() + "-" + server2.getId();
 
    assertThat(suffix(SUFFIX_DN, server1, server2).getId()).isEqualTo(expectedId);
    assertThat(suffix(SUFFIX_DN, server2, server1).getId()).isEqualTo(expectedId);
  }
 
  private ServerDescriptor server(String hostName)
  {
    Map<ADSContext.ServerProperty, Object> adsProperties = new HashMap<>();
    adsProperties.put(ADSContext.ServerProperty.HOST_NAME, hostName);
    adsProperties.put(ADSContext.ServerProperty.LDAP_PORT, 1389);
    return ServerDescriptor.createStandalone(adsProperties);
  }
 
  private SuffixDescriptor suffix(DN suffixDN, ServerDescriptor... servers)
  {
    SuffixDescriptor suffix = new SuffixDescriptor(suffixDN, replica(servers[0]));
    for (int i = 1; i < servers.length; i++)
    {
      suffix.addReplica(replica(servers[i]));
    }
    return suffix;
  }
 
  private ReplicaDescriptor replica(ServerDescriptor server)
  {
    ReplicaDescriptor replica = new ReplicaDescriptor();
    replica.setServer(server);
    return replica;
  }
}