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

Matthew Swift
15.36.2016 2502ac715d947766bd3e2aa700bdfd21c6059b9c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * 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 2007-2008 Sun Microsystems, Inc.
 * Portions copyright 2014-2016 ForgeRock AS.
 */
package org.forgerock.opendj.config.client.ldap;
 
import static org.fest.assertions.Assertions.assertThat;
import static org.forgerock.opendj.ldap.Connections.newInternalConnection;
 
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
 
import org.forgerock.opendj.config.AdminTestCase;
import org.forgerock.opendj.config.LDAPProfile;
import org.forgerock.opendj.config.PropertyException;
import org.forgerock.opendj.config.TestCfg;
import org.forgerock.opendj.config.TestChildCfgClient;
import org.forgerock.opendj.config.TestChildCfgDefn;
import org.forgerock.opendj.config.TestParentCfgClient;
import org.forgerock.opendj.config.client.ManagedObject;
import org.forgerock.opendj.config.client.ManagedObjectDecodingException;
import org.forgerock.opendj.config.client.ManagementContext;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.LinkedHashMapEntry;
import org.forgerock.opendj.ldap.MemoryBackend;
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.forgerock.opendj.server.config.client.RootCfgClient;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
@SuppressWarnings("javadoc")
@Test(singleThreaded = true)
public class AggregationClientTest extends AdminTestCase {
 
    /** Test LDIF. */
    private static final String[] TEST_LDIF = new String[] {
        // @formatter:off
        // Base entries.
        "dn:",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "",
        "dn: cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: config",
        "",
        "dn: cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: test-parents",
        "",
        // Parent 1 - uses default values for
        // optional-multi-valued-dn-property.
        "dn: cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-parent-dummy",
        "cn: test parent 1",
        "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "",
        // Child base entry.
        "dn:cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: multiple children",
        "",
        // Child 1 has no references.
        "dn: cn=test child 1,cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy",
        "cn: test child 1",
        "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "",
        // Child 2 has a single valid reference.
        "dn: cn=test child 2,cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy",
        "cn: test child 2",
        "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "ds-cfg-rotation-policy: cn=LDAP Connection Handler, cn=connection handlers, cn=config",
        "",
        // Child 3 has a multiple valid references.
        "dn: cn=test child 3,cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy",
        "cn: test child 3",
        "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "ds-cfg-rotation-policy: cn=LDAP Connection Handler, cn=connection handlers, cn=config",
        "ds-cfg-rotation-policy: cn=LDAPS Connection Handler, cn=connection handlers, cn=config",
        "",
        // Child 4 has a single bad reference.
        "dn: cn=test child 4,cn=test children,cn=test parent 1,cn=test parents,cn=config", "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy", "cn: test child 4", "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "ds-cfg-rotation-policy: cn=LDAP Connection Handler, cn=bad rdn, cn=config",
        "",
        "dn: cn=Connection Handlers,cn=config",
        "objectClass: top",
        "objectClass: ds-cfg-branch",
        "cn: Connection Handlers",
        "",
        "dn: cn=LDAP Connection Handler,cn=Connection Handlers,cn=config",
        "objectClass: top",
        "objectClass: ds-cfg-connection-handler",
        "objectClass: ds-cfg-ldap-connection-handler",
        "cn: LDAP Connection Handler",
        "ds-cfg-java-class: org.opends.server.protocols.ldap.LDAPConnectionHandler",
        "ds-cfg-enabled: true",
        "ds-cfg-listen-address: 0.0.0.0", "ds-cfg-listen-port: 389",
        "",
        "dn: cn=HTTP Connection Handler,cn=Connection Handlers,cn=config",
        "objectClass: top",
        "objectClass: ds-cfg-connection-handler",
        "objectClass: ds-cfg-http-connection-handler",
        "cn: HTTP Connection Handler",
        "ds-cfg-java-class: org.opends.server.protocols.http.HTTPConnectionHandler",
        "ds-cfg-enabled: false",
        "ds-cfg-listen-address: 0.0.0.0",
        "ds-cfg-listen-port: 8080",
        "",
        "dn: cn=JMX Connection Handler,cn=Connection Handlers,cn=config",
        "objectClass: top",
        "objectClass: ds-cfg-connection-handler",
        "objectClass: ds-cfg-jmx-connection-handler",
        "cn: JMX Connection Handler",
        "ds-cfg-java-class: org.opends.server.protocols.jmx.JmxConnectionHandler",
        "ds-cfg-enabled: false",
        "ds-cfg-listen-port: 1689",
        "" };
    // @formatter:on
 
    @BeforeClass
    public void setUp() throws Exception {
        TestCfg.setUp();
    }
 
    /**
     * Tears down test environment.
     */
    @AfterClass
    public void tearDown() {
        TestCfg.cleanup();
    }
 
    /**
     * Tests that aggregation contains no values when it contains does not
     * contain any DN attribute values.
     *
     * @throws Exception
     *             If the test unexpectedly fails.
     */
    @Test
    public void testAggregationEmpty() throws Exception {
        MemoryBackend backend = new MemoryBackend(new LDIFEntryReader(TEST_LDIF));
        Connection c = newInternalConnection(backend);
        ManagementContext ctx =
                LDAPManagementContext.newManagementContext(c, LDAPProfile.getInstance());
        TestParentCfgClient parent = getTestParent(ctx, "test parent 1");
        TestChildCfgClient child = parent.getTestChild("test child 1");
        assertSetEquals(child.getAggregationProperty(), new String[0]);
    }
 
    /**
     * Tests that aggregation contains single valid value when it contains a
     * single valid DN attribute values.
     *
     * @throws Exception
     *             If the test unexpectedly fails.
     */
    @Test
    public void testAggregationSingle() throws Exception {
        MemoryBackend backend = new MemoryBackend(new LDIFEntryReader(TEST_LDIF));
        Connection c = newInternalConnection(backend);
        ManagementContext ctx =
                LDAPManagementContext.newManagementContext(c, LDAPProfile.getInstance());
        TestParentCfgClient parent = getTestParent(ctx, "test parent 1");
        TestChildCfgClient child = parent.getTestChild("test child 2");
 
        // Test normalization.
        assertSetEquals(child.getAggregationProperty(), "LDAP Connection Handler");
        assertSetEquals(child.getAggregationProperty(), "  LDAP   Connection  Handler ");
        assertSetEquals(child.getAggregationProperty(), "  ldap connection HANDLER ");
    }
 
    /**
     * Tests that aggregation contains multiple valid values when it contains a
     * multiple valid DN attribute values.
     *
     * @throws Exception
     *             If the test unexpectedly fails.
     */
    @Test
    public void testAggregationMultiple() throws Exception {
        MemoryBackend backend = new MemoryBackend(new LDIFEntryReader(TEST_LDIF));
        Connection c = newInternalConnection(backend);
        ManagementContext ctx =
                LDAPManagementContext.newManagementContext(c, LDAPProfile.getInstance());
        TestParentCfgClient parent = getTestParent(ctx, "test parent 1");
        TestChildCfgClient child = parent.getTestChild("test child 3");
        assertSetEquals(child.getAggregationProperty(), "LDAPS Connection Handler",
                "LDAP Connection Handler");
    }
 
    /**
     * Tests that aggregation is rejected when the LDAP DN contains a valid RDN
     * but an invalid parent DN.
     *
     * @throws Exception
     *             If the test unexpectedly fails.
     */
    @Test
    public void testAggregationBadBaseDN() throws Exception {
        MemoryBackend backend = new MemoryBackend(new LDIFEntryReader(TEST_LDIF));
        Connection c = newInternalConnection(backend);
        ManagementContext ctx =
                LDAPManagementContext.newManagementContext(c, LDAPProfile.getInstance());
        TestParentCfgClient parent = getTestParent(ctx, "test parent 1");
 
        try {
            parent.getTestChild("test child 4");
            Assert.fail("Unexpectedly retrieved test child 4"
                    + " when it had a bad aggregation value");
        } catch (ManagedObjectDecodingException e) {
            Collection<PropertyException> causes = e.getCauses();
            Assert.assertEquals(causes.size(), 1);
 
            Throwable cause = causes.iterator().next();
            if (cause instanceof PropertyException) {
                PropertyException pe = (PropertyException) cause;
                Assert.assertEquals(pe.getPropertyDefinition(), TestChildCfgDefn.getInstance()
                        .getAggregationPropertyPropertyDefinition());
            } else {
                // Got an unexpected cause.
                throw e;
            }
        }
    }
 
    /**
     * Tests creation of a child managed object with a single reference.
     *
     * @throws Exception
     *             If an unexpected error occurred.
     */
    @Test
    public void testCreateChildManagedObject() throws Exception {
        MemoryBackend backend = new MemoryBackend(new LDIFEntryReader(TEST_LDIF));
        Connection c = newInternalConnection(backend);
        ManagementContext ctx =
                LDAPManagementContext.newManagementContext(c, LDAPProfile.getInstance());
        TestParentCfgClient parent = getTestParent(ctx, "test parent 1");
        TestChildCfgClient child =
                parent.createTestChild(TestChildCfgDefn.getInstance(), "test child new", null);
        child.setMandatoryBooleanProperty(true);
        child.setMandatoryReadOnlyAttributeTypeProperty(Schema.getDefaultSchema().getAttributeType(
                "description"));
        child.setAggregationProperty(Collections.singleton("LDAP Connection Handler"));
        child.commit();
 
        String dn = "cn=test child new,cn=test children,cn=test parent 1,cn=test parents,cn=config";
        assertThat(backend.get(dn)).isEqualTo(new LinkedHashMapEntry(
                "dn: " + dn,
                "cn: test child new",
                "objectClass: top",
                "objectClass: ds-cfg-test-child-dummy",
                "ds-cfg-enabled: true",
                "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
                "ds-cfg-attribute-type: description",
                "ds-cfg-rotation-policy: cn=LDAP Connection Handler,cn=connection handlers,cn=config"));
    }
 
    /**
     * Tests modification of a child managed object so that it has a different
     * reference.
     *
     * @throws Exception
     *             If an unexpected error occurred.
     */
    @Test
    public void testModifyChildManagedObject() throws Exception {
        MemoryBackend backend = new MemoryBackend(new LDIFEntryReader(TEST_LDIF));
        Connection c = newInternalConnection(backend);
        ManagementContext ctx =
                LDAPManagementContext.newManagementContext(c, LDAPProfile.getInstance());
        TestParentCfgClient parent = getTestParent(ctx, "test parent 1");
        TestChildCfgClient child = parent.getTestChild("test child 2");
        child.setAggregationProperty(Arrays.asList("JMX Connection Handler",
                "HTTP Connection Handler"));
        child.commit();
 
        String dn = "cn=test child 2,cn=test children,cn=test parent 1,cn=test parents,cn=config";
        assertThat(backend.get(dn).parseAttribute("ds-cfg-rotation-policy").asSetOfString())
                .containsOnly("cn=HTTP Connection Handler,cn=connection handlers,cn=config",
                        "cn=JMX Connection Handler,cn=connection handlers,cn=config");
    }
 
    /** Retrieve the named test parent managed object. */
    private TestParentCfgClient getTestParent(ManagementContext context, String name)
            throws Exception {
        ManagedObject<RootCfgClient> root = context.getRootConfigurationManagedObject();
        return root.getChild(TestCfg.getTestOneToManyParentRelationDefinition(), name)
                .getConfiguration();
    }
 
    /** Asserts that the actual set of DNs contains the expected values. */
    private void assertSetEquals(SortedSet<String> actual, String... expected) {
        SortedSet<String> values = new TreeSet<>(
            TestChildCfgDefn.getInstance().getAggregationPropertyPropertyDefinition());
        if (expected != null) {
            for (String value : expected) {
                values.add(value);
            }
        }
        Assert.assertEquals((Object) actual, (Object) values);
    }
 
}