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

Gaetan Boismal
29.04.2014 600f6cb4356f2355a004604b9353505f0c2f7f49
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2007-2008 Sun Microsystems, Inc.
 *      Portions copyright 2013 ForgeRock AS
 */
package org.forgerock.opendj.config.server;
 
import static org.fest.assertions.Assertions.assertThat;
import static org.forgerock.opendj.ldif.LDIF.makeEntry;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
 
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.opendj.config.AdminTestCase;
import org.forgerock.opendj.config.AdministratorAction;
import org.forgerock.opendj.config.AggregationPropertyDefinition;
import org.forgerock.opendj.config.PropertyException;
import org.forgerock.opendj.config.PropertyOption;
import org.forgerock.opendj.config.TestCfg;
import org.forgerock.opendj.config.TestChildCfg;
import org.forgerock.opendj.config.TestChildCfgDefn;
import org.forgerock.opendj.config.TestParentCfg;
import org.forgerock.opendj.config.UndefinedDefaultBehaviorProvider;
import org.forgerock.opendj.config.conditions.Conditions;
import org.forgerock.opendj.config.server.spi.ConfigChangeListener;
import org.forgerock.opendj.config.server.spi.ConfigDeleteListener;
import org.forgerock.opendj.config.server.spi.ConfigurationRepository;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldif.LDIF;
import org.forgerock.opendj.server.config.client.ConnectionHandlerCfgClient;
import org.forgerock.opendj.server.config.server.ConnectionHandlerCfg;
import org.mockito.ArgumentCaptor;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
/**
 * Test cases for aggregations on the server-side.
 */
@Test(singleThreaded = true)
@SuppressWarnings("javadoc")
public final class AggregationServerTest extends AdminTestCase {
 
    /**
     * Dummy change listener for triggering change constraint call-backs.
     */
    private static final class DummyChangeListener implements ConfigurationChangeListener<TestChildCfg> {
 
        public ConfigChangeResult applyConfigurationChange(TestChildCfg configuration) {
            return new ConfigChangeResult(ResultCode.SUCCESS, false);
        }
 
        public boolean isConfigurationChangeAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            return true;
        }
    }
 
    /**
     * Dummy delete listener for triggering delete constraint call-backs.
     */
    private static final class DummyDeleteListener implements ConfigurationDeleteListener<TestChildCfg> {
 
        public ConfigChangeResult applyConfigurationDelete(TestChildCfg configuration) {
            return new ConfigChangeResult(ResultCode.SUCCESS, false);
        }
 
        public boolean isConfigurationDeleteAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            return true;
        }
    }
 
    // @Checkstyle:off
    private static final Entry TEST_CHILD_1 = makeEntry(
        "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",
        "ds-cfg-conflict-behavior: virtual-overrides-real");
 
    private static final Entry TEST_CHILD_2 = makeEntry(
        "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-conflict-behavior: virtual-overrides-real",
        "ds-cfg-rotation-policy: cn=LDAP Connection Handler, cn=connection handlers, cn=config");
 
    /** Has an invalid handler reference. */
    private static final Entry TEST_CHILD_3 = makeEntry(
        "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-conflict-behavior: virtual-overrides-real",
        "ds-cfg-rotation-policy: cn=LDAP Connection Handler, cn=bad rdn, cn=config");
 
    private static final Entry TEST_CHILD_4 = makeEntry(
        "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-conflict-behavior: virtual-overrides-real",
        "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");
 
    private static final Entry TEST_CHILD_5 = makeEntry(
        "dn: cn=test child 5,cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy",
        "cn: test child 5", "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "ds-cfg-conflict-behavior: virtual-overrides-real",
        "ds-cfg-rotation-policy: cn=BAD Connection Handler 1, cn=connection handlers, cn=config",
        "ds-cfg-rotation-policy: cn=BAD Connection Handler 2, cn=connection handlers, cn=config",
        "ds-cfg-rotation-policy: cn=LDAP Connection Handler, cn=connection handlers, cn=config");
 
    private static final Entry TEST_CHILD_6 = makeEntry(
        "dn: cn=test child 6,cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy",
        "cn: test child 6", "ds-cfg-enabled: true",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "ds-cfg-conflict-behavior: virtual-overrides-real",
        "ds-cfg-rotation-policy: cn=Test Connection Handler, cn=connection handlers, cn=config");
 
    private static final Entry TEST_CHILD_7 = makeEntry(
        "dn: cn=test child 7,cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-child-dummy",
        "cn: test child 7", "ds-cfg-enabled: false",
        "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
        "ds-cfg-attribute-type: description",
        "ds-cfg-conflict-behavior: virtual-overrides-real",
        "ds-cfg-rotation-policy: cn=Test Connection Handler, cn=connection handlers, cn=config");
 
    static final Entry TEST_PARENTS = makeEntry(
        "dn: cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: test parents");
 
    static final Entry TEST_PARENT_1 = makeEntry(
        "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",
        "ds-cfg-conflict-behavior: virtual-overrides-real");
 
    private static final Entry TEST_BASE_CHILD = LDIF.makeEntry(
        "dn:cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: test children");
 
    /** this handler is disabled - see ds-cfg-enabled property */
    protected static final Entry TEST_CONNECTION_HANDLER_ENTRY_DISABLED = LDIF.makeEntry(
        "dn: cn=" + "Test 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: false",
        "ds-cfg-listen-address: 0.0.0.0", "ds-cfg-listen-port: 389");
 
    /** this handler is enabled - see ds-cfg-enabled property */
    protected static final Entry TEST_CONNECTION_HANDLER_ENTRY_ENABLED = LDIF.makeEntry(
        "dn: cn=" + "Test 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");
    // @Checkstyle:on
 
    // @Checkstyle:off
    /**
     * The default test child configuration "aggregation-property" property
     * definition.
     */
    private AggregationPropertyDefinition<ConnectionHandlerCfgClient, ConnectionHandlerCfg>
        aggregationPropertyDefinitionDefault = null;
 
    /**
     * An aggregation where the target must be enabled if the source is enabled.
     */
    private AggregationPropertyDefinition<ConnectionHandlerCfgClient, ConnectionHandlerCfg>
        aggregationPropertyDefinitionTargetAndSourceMustBeEnabled = null;
 
    /** An aggregation where the target must be enabled. */
    private AggregationPropertyDefinition<ConnectionHandlerCfgClient, ConnectionHandlerCfg>
        aggregationPropertyDefinitionTargetMustBeEnabled = null;
    // @Checkstyle:on
 
    @BeforeClass
    public void setUp() throws Exception {
        TestCfg.setUp();
 
        // Save the aggregation property definition so that it can be
        // replaced and restored later.
        aggregationPropertyDefinitionDefault =
            TestChildCfgDefn.getInstance().getAggregationPropertyPropertyDefinition();
 
        // Create the two test aggregation properties.
        AggregationPropertyDefinition.Builder<ConnectionHandlerCfgClient, ConnectionHandlerCfg> builder;
        TestChildCfgDefn d = TestChildCfgDefn.getInstance();
        builder = AggregationPropertyDefinition.createBuilder(d, "aggregation-property");
        builder.setOption(PropertyOption.MULTI_VALUED);
        builder.setAdministratorAction(new AdministratorAction(AdministratorAction.Type.NONE, d,
            "aggregation-property"));
        builder.setDefaultBehaviorProvider(new UndefinedDefaultBehaviorProvider<String>());
        builder.setParentPath("/");
        builder.setRelationDefinition("connection-handler");
        builder.setTargetIsEnabledCondition(Conditions.contains("enabled", "true"));
        aggregationPropertyDefinitionTargetMustBeEnabled = builder.getInstance();
        TestCfg.initializePropertyDefinition(aggregationPropertyDefinitionTargetMustBeEnabled);
 
        builder = AggregationPropertyDefinition.createBuilder(d, "aggregation-property");
        builder.setOption(PropertyOption.MULTI_VALUED);
        builder.setAdministratorAction(new AdministratorAction(AdministratorAction.Type.NONE, d,
            "aggregation-property"));
        builder.setDefaultBehaviorProvider(new UndefinedDefaultBehaviorProvider<String>());
        builder.setParentPath("/");
        builder.setRelationDefinition("connection-handler");
        builder.setTargetIsEnabledCondition(Conditions.contains("enabled", "true"));
        builder.setTargetNeedsEnablingCondition(Conditions.contains("mandatory-boolean-property", "true"));
        aggregationPropertyDefinitionTargetAndSourceMustBeEnabled = builder.getInstance();
        TestCfg.initializePropertyDefinition(aggregationPropertyDefinitionTargetAndSourceMustBeEnabled);
    }
 
    @AfterClass
    public void tearDown() throws Exception {
        TestCfg.cleanup();
 
        // Restore the test child aggregation definition.
        TestCfg.addPropertyDefinition(aggregationPropertyDefinitionDefault);
    }
 
    /**
     * Tests that aggregation is rejected when the LDAP DN contains a valid RDN
     * but an invalid parent DN.
     */
    @Test
    public void testAggregationBadBaseDN() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_3, LDAP_CONN_HANDLER_ENTRY);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        try {
            parentCfg.getTestChild(entryName(TEST_CHILD_3));
            fail("Unexpectedly added test child 3 when it had a bad aggregation value");
        } catch (ConfigException e) {
            assertThat(e.getCause()).isNotNull().isInstanceOf(ServerManagedObjectDecodingException.class);
            ServerManagedObjectDecodingException de = (ServerManagedObjectDecodingException) e.getCause();
            assertThat(de.getCauses()).hasSize(1);
            PropertyException propertyException = de.getCauses().iterator().next();
            assertThat(propertyException).isInstanceOf(PropertyException.class);
            assertEquals(propertyException.getPropertyDefinition(), TestChildCfgDefn.getInstance()
                .getAggregationPropertyPropertyDefinition());
        }
    }
 
    /**
     * Tests that aggregation is rejected by a constraint violation when the DN
     * values are dangling.
     */
    @Test
    public void testAggregationDanglingReference() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_5, LDAP_CONN_HANDLER_ENTRY);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
 
        try {
            parentCfg.getTestChild(entryName(TEST_CHILD_5));
            fail("Unexpectedly added test child 5 when it had a dangling reference");
        } catch (ConfigException e) {
            assertThat(e.getCause()).isNotNull().isInstanceOf(ConstraintViolationException.class);
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            assertThat(cve.getMessages()).isNotNull().hasSize(2);
        }
    }
 
    /**
     * Tests that aggregation is REJECTED by a constraint violation when an
     * enabled component references a disabled component and the referenced
     * component must always be enabled.
     */
    @Test
    public void testAggregationDisabledReference1() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_6, TEST_CONNECTION_HANDLER_ENTRY_DISABLED);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
 
        registerAggregationDefinitionWithTargetEnabled();
 
        try {
            TestParentCfg parent = getParentCfg(TEST_PARENT_1, context);
            parent.getTestChild(entryName(TEST_CHILD_6));
            fail("Unexpectedly added test child 6 when it had a disabled reference");
        } catch (ConfigException e) {
            assertThat(e.getCause()).isNotNull().isInstanceOf(ConstraintViolationException.class);
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            assertThat(cve.getMessages()).isNotNull().hasSize(1);
        } finally {
            putBackDefaultAggregationDefinitionFromTargetEnabled();
        }
    }
 
    /**
     * Tests that aggregation is REJECTED by a constraint violation when a
     * disabled component references a disabled component and the referenced
     * component must always be enabled.
     */
    @Test
    public void testAggregationDisabledReference2() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_7, TEST_CONNECTION_HANDLER_ENTRY_DISABLED);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
 
        registerAggregationDefinitionWithTargetEnabled();
 
        try {
            TestParentCfg parent = getParentCfg(TEST_PARENT_1, context);
            parent.getTestChild(entryName(TEST_CHILD_7));
            fail("Unexpectedly added test child 7 when it had a disabled reference");
        } catch (ConfigException e) {
            assertThat(e.getCause()).isNotNull().isInstanceOf(ConstraintViolationException.class);
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            assertThat(cve.getMessages()).isNotNull().hasSize(1);
        } finally {
            putBackDefaultAggregationDefinitionFromTargetEnabled();
        }
    }
 
    /**
     * Tests that aggregation is REJECTED by a constraint violation when an
     * enabled component references a disabled component and the referenced
     * component must always be enabled when the referencing component is
     * enabled.
     */
    @Test
    public void testAggregationDisabledReference3() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_6, TEST_CONNECTION_HANDLER_ENTRY_DISABLED);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
 
        registerAggregationDefinitionWithTargetAndSourceEnabled();
 
        try {
            TestParentCfg parent = getParentCfg(TEST_PARENT_1, context);
            parent.getTestChild(entryName(TEST_CHILD_6));
            fail("Unexpectedly added test child 6 when it had a disabled reference");
        } catch (ConfigException e) {
            assertThat(e.getCause()).isNotNull().isInstanceOf(ConstraintViolationException.class);
            ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
            assertThat(cve.getMessages()).isNotNull().hasSize(1);
        } finally {
            putBackDefaultAggregationDefinitionFromTargetAndSourceEnabled();
        }
    }
 
    /**
     * Tests that aggregation is ALLOWED when a disabled component references a
     * disabled component and the referenced component must always be enabled
     * when the referencing component is enabled.
     */
    @Test
    public void testAggregationDisabledReference4() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_7, TEST_CONNECTION_HANDLER_ENTRY_DISABLED);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
 
        registerAggregationDefinitionWithTargetAndSourceEnabled();
 
        try {
            TestParentCfg parent = getParentCfg(TEST_PARENT_1, context);
            parent.getTestChild(entryName(TEST_CHILD_7));
        } finally {
            putBackDefaultAggregationDefinitionFromTargetAndSourceEnabled();
        }
    }
 
    /**
     * Tests that aggregation contains no values when it contains does not
     * contain any DN attribute values.
     */
    @Test
    public void testAggregationEmpty() throws Exception {
        ConfigurationRepository configRepository = createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        TestChildCfg testChildCfg = parentCfg.getTestChild(entryName(TEST_CHILD_1));
 
        assertEquals(testChildCfg.getMandatoryClassProperty(),
            "org.opends.server.extensions.UserDefinedVirtualAttributeProvider");
        assertEquals(testChildCfg.getMandatoryReadOnlyAttributeTypeProperty(), Schema.getDefaultSchema()
            .getAttributeType("description"));
        assertSetEquals(testChildCfg.getAggregationProperty(), new String[0]);
    }
 
    /**
     * Tests that aggregation contains multiple valid values when it contains a
     * multiple valid DN attribute values.
     */
    @Test
    public void testAggregationMultipleValues() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_4, LDAP_CONN_HANDLER_ENTRY,
                LDAPS_CONN_HANDLER_ENTRY);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        TestChildCfg testChildCfg = parentCfg.getTestChild(entryName(TEST_CHILD_4));
 
        assertEquals(testChildCfg.getMandatoryClassProperty(),
            "org.opends.server.extensions.UserDefinedVirtualAttributeProvider");
        assertEquals(testChildCfg.getMandatoryReadOnlyAttributeTypeProperty(), Schema.getDefaultSchema()
            .getAttributeType("description"));
        assertSetEquals(testChildCfg.getAggregationProperty(), "LDAPS Connection Handler", "LDAP Connection Handler");
    }
 
    /**
     * Tests that aggregation contains single valid value when it contains a
     * single valid DN attribute values.
     */
    @Test
    public void testAggregationSingle() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_2, LDAP_CONN_HANDLER_ENTRY);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        TestChildCfg testChildCfg = parentCfg.getTestChild(entryName(TEST_CHILD_2));
 
        assertEquals(testChildCfg.getMandatoryClassProperty(),
            "org.opends.server.extensions.UserDefinedVirtualAttributeProvider");
        assertEquals(testChildCfg.getMandatoryReadOnlyAttributeTypeProperty(), Schema.getDefaultSchema()
            .getAttributeType("description"));
 
        // Test normalization.
        assertSetEquals(testChildCfg.getAggregationProperty(), "LDAP Connection Handler");
        assertSetEquals(testChildCfg.getAggregationProperty(), "  LDAP   Connection  Handler ");
        assertSetEquals(testChildCfg.getAggregationProperty(), "  ldap connection HANDLER ");
    }
 
    /**
     * Tests that it is impossible to delete a referenced component when the
     * referenced component must always exist regardless of whether the
     * referencing component is enabled or not.
     */
    @Test
    public void testCannotDeleteReferencedComponent() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENTS, TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_7,
                CONN_HANDLER_ENTRY, TEST_CONNECTION_HANDLER_ENTRY_ENABLED);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
 
        registerAggregationDefinitionWithTargetEnabled();
 
        ConfigurationDeleteListener<TestChildCfg> deleteListener = new DummyDeleteListener();
        try {
            // Retrieve the parent and child managed objects and register
            // delete and change listeners respectively in order to trigger
            // the constraint call-backs.
            TestParentCfg parentCfg = getParentCfg(entryName(TEST_PARENT_1), context);
            parentCfg.addTestChildDeleteListener(deleteListener);
 
            ArgumentCaptor<ConfigDeleteListener> registeredListener =
                ArgumentCaptor.forClass(ConfigDeleteListener.class);
            verify(configRepository).registerDeleteListener(eq(TEST_BASE_CHILD.getName()),
                registeredListener.capture());
 
            // Now simulate the delete ofthe referenced connection handler.
            assertThat(
                registeredListener.getValue().configDeleteIsAcceptable(TEST_CONNECTION_HANDLER_ENTRY_ENABLED,
                    new LocalizableMessageBuilder())).isFalse();
 
        } finally {
            putBackDefaultAggregationDefinitionFromTargetEnabled();
        }
    }
 
    /**
     * Tests that it is impossible to disable a referenced component when the
     * referenced component must always be enabled regardless of whether the
     * referencing component is enabled or not.
     */
    @Test
    public void testCannotDisableReferencedComponent() throws Exception {
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENTS, TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_7,
                CONN_HANDLER_ENTRY, TEST_CONNECTION_HANDLER_ENTRY_ENABLED);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
 
        registerAggregationDefinitionWithTargetEnabled();
 
        ConfigurationChangeListener<TestChildCfg> changeListener = new DummyChangeListener();
        try {
            // Retrieve the parent and child managed objects and register
            // delete and change listeners respectively in order to trigger
            // the constraint call-backs.
            TestParentCfg parentCfg = getParentCfg(entryName(TEST_PARENT_1), context);
            TestChildCfg testChildCfg = parentCfg.getTestChild(entryName(TEST_CHILD_7));
            testChildCfg.addChangeListener(changeListener);
 
            ArgumentCaptor<ConfigChangeListener> registeredListener =
                ArgumentCaptor.forClass(ConfigChangeListener.class);
            verify(configRepository).registerChangeListener(eq(TEST_CHILD_7.getName()), registeredListener.capture());
 
            // Now simulate the disabling ofthe referenced connection handler.
            assertThat(
                registeredListener.getValue().configChangeIsAcceptable(TEST_CONNECTION_HANDLER_ENTRY_DISABLED,
                    new LocalizableMessageBuilder())).isFalse();
 
        } finally {
            putBackDefaultAggregationDefinitionFromTargetEnabled();
        }
    }
 
    /**
     * Register the temporary aggregation definition to be used in test. You
     * must call
     * {@code putBackDefaultAggregationDefinitionFromTargetAndSourceEnabled}
     * method at end of test.
     */
    private void registerAggregationDefinitionWithTargetAndSourceEnabled() {
        TestCfg.removeConstraint(aggregationPropertyDefinitionDefault.getSourceConstraint());
        TestCfg.addPropertyDefinition(aggregationPropertyDefinitionTargetAndSourceMustBeEnabled);
        TestCfg.addConstraint(aggregationPropertyDefinitionTargetAndSourceMustBeEnabled.getSourceConstraint());
    }
 
    /**
     * Put back the default aggregation definition.
     */
    private void putBackDefaultAggregationDefinitionFromTargetAndSourceEnabled() {
        TestCfg.removeConstraint(aggregationPropertyDefinitionTargetAndSourceMustBeEnabled.getSourceConstraint());
        TestCfg.addPropertyDefinition(aggregationPropertyDefinitionDefault);
        TestCfg.addConstraint(aggregationPropertyDefinitionDefault.getSourceConstraint());
    }
 
    /**
     * Register the temporary aggregation definition to be used in test. You
     * must call {@code putBackDefaultAggregationDefinitionFromTargetEnabled}
     * method at end of test.
     */
    private void registerAggregationDefinitionWithTargetEnabled() {
        TestCfg.removeConstraint(aggregationPropertyDefinitionDefault.getSourceConstraint());
        TestCfg.addPropertyDefinition(aggregationPropertyDefinitionTargetMustBeEnabled);
        TestCfg.addConstraint(aggregationPropertyDefinitionTargetMustBeEnabled.getSourceConstraint());
    }
 
    /**
     * Put back the default aggregation definition.
     */
    private void putBackDefaultAggregationDefinitionFromTargetEnabled() {
        TestCfg.removeConstraint(aggregationPropertyDefinitionTargetMustBeEnabled.getSourceConstraint());
        TestCfg.addPropertyDefinition(aggregationPropertyDefinitionDefault);
        TestCfg.addConstraint(aggregationPropertyDefinitionDefault.getSourceConstraint());
    }
 
    /** Asserts that the actual set of DNs contains the expected values. */
    private void assertSetEquals(SortedSet<String> actual, String... expected) {
        SortedSet<String> values =
            new TreeSet<String>(TestChildCfgDefn.getInstance().getAggregationPropertyPropertyDefinition());
        if (expected != null) {
            for (String value : expected) {
                values.add(value);
            }
        }
        Assert.assertEquals((Object) actual, (Object) values);
    }
 
}