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

dependabot[bot]
05.57.2025 65655a0d4a2a8b053c5c8e02a1b8af9676e25319
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
/*
 * 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 2008 Sun Microsystems, Inc.
 * Portions copyright 2015-2016 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 java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.opendj.config.AdminTestCase;
import org.forgerock.opendj.config.TestCfg;
import org.forgerock.opendj.config.TestChildCfg;
import org.forgerock.opendj.config.TestParentCfg;
import org.forgerock.opendj.config.server.spi.ConfigAddListener;
import org.forgerock.opendj.config.server.spi.ConfigChangeListener;
import org.forgerock.opendj.config.server.spi.ConfigurationRepository;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.schema.Schema;
import org.mockito.ArgumentCaptor;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
@SuppressWarnings("javadoc")
/** Test default behavior on the server side, by checking values of configuration objects. */
public final class DefaultBehaviorTest extends AdminTestCase {
 
    private static class TestConfigurationAddListener implements ConfigurationAddListener<TestChildCfg> {
 
        private TestChildCfg childCfg;
 
        @Override
        public ConfigChangeResult applyConfigurationAdd(TestChildCfg configuration) {
            return new ConfigChangeResult();
        }
 
        /** Gets the child configuration checking that it has the expected name. */
        public TestChildCfg getChildCfg(String expectedName) {
            Assert.assertNotNull(childCfg);
            Assert.assertEquals(childCfg.name(), expectedName);
            return childCfg;
        }
 
        @Override
        public boolean isConfigurationAddAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            childCfg = configuration;
            return true;
        }
    }
 
    private static class TestConfigurationChangeListener implements ConfigurationChangeListener<TestChildCfg> {
 
        private TestChildCfg childCfg;
 
        @Override
        public ConfigChangeResult applyConfigurationChange(TestChildCfg configuration) {
            return new ConfigChangeResult();
        }
 
        /** Gets the child configuration checking that it has the expected name. */
        public TestChildCfg getChildCfg(String expectedName) {
            Assert.assertNotNull(childCfg);
            Assert.assertEquals(childCfg.name(), expectedName);
            return childCfg;
        }
 
        @Override
        public boolean isConfigurationChangeAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            childCfg = configuration;
            return true;
        }
    }
 
    // @Checkstyle:off
    static final Entry CONFIG = makeEntry(
        "dn: cn=config",
        "objectClass: top",
        "objectClass: ds-cfg-root-config",
        "cn: config");
 
    static final Entry TEST_PARENTS = makeEntry(
        "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. */
    static final List<String> LDIF_TEST_PARENT_1 = Arrays.asList(
        "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");
 
    static final Entry TEST_PARENT_1 = makeEntry(LDIF_TEST_PARENT_1);
 
    /** Parent 2 - overrides default values for optional-multi-valued-dn-property. */
    static final Entry TEST_PARENT_2 = makeEntry(
        "dn: cn=test parent 2,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-test-parent-dummy",
        "cn: test parent 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-base-dn: dc=default value p2v1,dc=com",
        "ds-cfg-base-dn: dc=default value p2v2,dc=com");
 
    static final Entry TEST_CHILD_BASE_1 = makeEntry(
        "dn:cn=test children,cn=test parent 1,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: test children");
 
    static final Entry TEST_CHILD_BASE_2 = makeEntry(
        "dn:cn=test children,cn=test parent 2,cn=test parents,cn=config",
        "objectclass: top",
        "objectclass: ds-cfg-branch",
        "cn: test children");
 
    private static final List<String> LDIF_TEST_CHILD_1 = Arrays.asList(
        "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_1 = makeEntry(LDIF_TEST_CHILD_1);
 
    private static final List<String> NEW_ATTRS_1 = Arrays.asList(
        "ds-cfg-base-dn: dc=new value 1,dc=com",
        "ds-cfg-base-dn: dc=new value 2,dc=com",
        "ds-cfg-group-dn: dc=new value 3,dc=com",
        "ds-cfg-group-dn: dc=new value 4,dc=com");
 
    private static final List<String> NEW_ATTRS_2 = Arrays.asList(
        "ds-cfg-base-dn: dc=new value 1,dc=com",
        "ds-cfg-base-dn: dc=new value 2,dc=com");
 
    private static final List<String> NEW_ATTRS_3 = Arrays.asList(
        "ds-cfg-group-dn: dc=new value 1,dc=com",
        "ds-cfg-group-dn: dc=new value 2,dc=com");
 
    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-base-dn: dc=default value c2v1,dc=com",
        "ds-cfg-base-dn: dc=default value c2v2,dc=com");
 
    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-base-dn: dc=default value c3v1,dc=com",
        "ds-cfg-base-dn: dc=default value c3v2,dc=com",
        "ds-cfg-group-dn: dc=default value c3v3,dc=com",
        "ds-cfg-group-dn: dc=default value c3v4,dc=com");
 
    private static final Entry TEST_CHILD_4 = makeEntry(
        "dn: cn=test child 4,cn=test children,cn=test parent 2,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");
    // @Checkstyle:on
 
    @BeforeClass
    public void setUp() throws Exception {
        TestCfg.setUp();
    }
 
    @AfterClass
    public void tearDown() throws Exception {
        TestCfg.cleanup();
    }
 
    @DataProvider
    Object[][] childConfigurationsValues() {
        return new Object[][] {
            // parent entry, child base entry, child entry,
            // expected first dn property values,
            // expected second dn property values
            { TEST_PARENT_1, TEST_CHILD_BASE_1, TEST_CHILD_1,
                Arrays.asList("dc=domain1,dc=com", "dc=domain2,dc=com", "dc=domain3,dc=com"),
                Arrays.asList("dc=domain1,dc=com", "dc=domain2,dc=com", "dc=domain3,dc=com") },
 
            { TEST_PARENT_1, TEST_CHILD_BASE_1, TEST_CHILD_2,
                Arrays.asList("dc=default value c2v1,dc=com", "dc=default value c2v2,dc=com"),
                Arrays.asList("dc=default value c2v1,dc=com", "dc=default value c2v2,dc=com") },
 
            { TEST_PARENT_1, TEST_CHILD_BASE_1, TEST_CHILD_3,
                Arrays.asList("dc=default value c3v1,dc=com", "dc=default value c3v2,dc=com"),
                Arrays.asList("dc=default value c3v3,dc=com", "dc=default value c3v4,dc=com") },
 
            { TEST_PARENT_2, TEST_CHILD_BASE_2, TEST_CHILD_4,
                Arrays.asList("dc=default value p2v1,dc=com", "dc=default value p2v2,dc=com"),
                Arrays.asList("dc=default value p2v1,dc=com", "dc=default value p2v2,dc=com") } };
    }
 
    /** Test that a child config have correct values when accessed from its parent config. */
    @Test(dataProvider = "childConfigurationsValues")
    public void testChildValues(Entry testParent, Entry testBaseChild, Entry testChild,
        List<String> valuesForOptionalDNProperty1, List<String> valuesForOptionalDNProperty2) throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(testParent, testBaseChild, testChild);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(testParent, context);
 
        // assert
        assertChildHasCorrectValues(parentCfg.getTestChild(entryName(testChild)), valuesForOptionalDNProperty1,
            valuesForOptionalDNProperty2);
    }
 
    /** Test that a child config have correct values when accessed through an add listener. */
    @Test(dataProvider = "childConfigurationsValues")
    public void testAddListenerChildValues(Entry testParent, Entry testBaseChild, Entry testChild,
        List<String> valuesForOptionalDNProperty1, List<String> valuesForOptionalDNProperty2) throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(testParent, testBaseChild, testChild);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(testParent, context);
        TestConfigurationAddListener addListener = new TestConfigurationAddListener();
        parentCfg.addTestChildAddListener(addListener);
 
        // act
        simulateEntryAdd(testChild, configRepository);
 
        // assert
        assertChildHasCorrectValues(addListener.getChildCfg(entryName(testChild)), valuesForOptionalDNProperty1,
            valuesForOptionalDNProperty2);
    }
 
    @DataProvider
    Object[][] childConfigurationsValuesForChangeListener() {
        return new Object[][] {
            // new entry after change, expected first dn property values,
            // expected second dn property values
            { makeEntryFrom(LDIF_TEST_CHILD_1, NEW_ATTRS_1),
                Arrays.asList("dc=new value 1,dc=com", "dc=new value 2,dc=com"),
                Arrays.asList("dc=new value 3,dc=com", "dc=new value 4,dc=com") },
 
            { makeEntryFrom(LDIF_TEST_CHILD_1, NEW_ATTRS_2),
                Arrays.asList("dc=new value 1,dc=com", "dc=new value 2,dc=com"),
                Arrays.asList("dc=new value 1,dc=com", "dc=new value 2,dc=com") },
 
            { makeEntryFrom(LDIF_TEST_CHILD_1, NEW_ATTRS_3),
                Arrays.asList("dc=domain1,dc=com", "dc=domain2,dc=com", "dc=domain3,dc=com"),
                Arrays.asList("dc=new value 1,dc=com", "dc=new value 2,dc=com") },
 
            { makeEntryFrom(LDIF_TEST_PARENT_1, NEW_ATTRS_2),
                Arrays.asList("dc=new value 1,dc=com", "dc=new value 2,dc=com"),
                Arrays.asList("dc=new value 1,dc=com", "dc=new value 2,dc=com") } };
    }
 
    /**
     * Tests that a child config have correct values when accessed through an
     * change listener. The defaulted properties are replaced with some real
     * values.
     */
    @Test(dataProvider = "childConfigurationsValuesForChangeListener")
    public void testChangeListenerChildValues(Entry newEntry, List<String> valuesForOptionalDNProperty1,
        List<String> valuesForOptionalDNProperty2) throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_CHILD_BASE_1, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        TestChildCfg childCfg = parentCfg.getTestChild(entryName(TEST_CHILD_1));
        TestConfigurationChangeListener changeListener = new TestConfigurationChangeListener();
        childCfg.addChangeListener(changeListener);
 
        // act
        simulateEntryChange(newEntry, configRepository);
 
        // assert
        assertChildHasCorrectValues(changeListener.getChildCfg(entryName(TEST_CHILD_1)),
            valuesForOptionalDNProperty1, valuesForOptionalDNProperty2);
    }
 
    @DataProvider
    Object[][] parentConfigurationsValues() {
        return new Object[][] {
            // parent entry, expected first dn property values, expected second
            // dn property values
            { TEST_PARENT_1, Arrays.asList("dc=domain1,dc=com", "dc=domain2,dc=com", "dc=domain3,dc=com") },
            { TEST_PARENT_2, Arrays.asList("dc=default value p2v1,dc=com", "dc=default value p2v2,dc=com") } };
    }
 
    /** Tests that parent configuration has correct values. */
    @Test(dataProvider = "parentConfigurationsValues")
    public void testParentValues(Entry parentEntry, List<String> valuesForOptionalDNProperty) throws Exception {
        ConfigurationRepository configRepository = createConfigRepositoryWithEntries(parentEntry);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parent = getParentCfg(parentEntry, context);
 
        assertThat(parent.getMandatoryClassProperty()).isEqualTo(
            "org.opends.server.extensions.UserDefinedVirtualAttributeProvider");
        assertThat(parent.getMandatoryReadOnlyAttributeTypeProperty()).isEqualTo(
            Schema.getDefaultSchema().getAttributeType("description"));
        assertDNSetEquals(parent.getOptionalMultiValuedDNProperty(), valuesForOptionalDNProperty);
    }
 
    /**
     * Simulate an entry add by triggering configAddIsAcceptable method of last
     * registered add listener.
     */
    private void simulateEntryAdd(Entry entry, ConfigurationRepository configRepository) throws IOException {
        // use argument capture to retrieve the actual listener
        ArgumentCaptor<ConfigAddListener> registeredListener = ArgumentCaptor.forClass(ConfigAddListener.class);
        verify(configRepository).registerAddListener(eq(entry.getName().parent()), registeredListener.capture());
 
        registeredListener.getValue().configAddIsAcceptable(entry, new LocalizableMessageBuilder());
    }
 
    /**
     * Simulate an entry change by triggering configChangeIsAcceptable method on
     * last registered change listener.
     */
    private void simulateEntryChange(Entry newEntry, ConfigurationRepository configRepository) {
        // use argument capture to retrieve the actual listener
        ArgumentCaptor<ConfigChangeListener> registeredListener = ArgumentCaptor.forClass(ConfigChangeListener.class);
        verify(configRepository).registerChangeListener(eq(newEntry.getName()), registeredListener.capture());
 
        registeredListener.getValue().configChangeIsAcceptable(newEntry, new LocalizableMessageBuilder());
    }
 
    private void assertChildHasCorrectValues(TestChildCfg child, List<String> dnProperty1, List<String> dnProperty2) {
        assertThat(child.getMandatoryClassProperty()).isEqualTo(
            "org.opends.server.extensions.UserDefinedVirtualAttributeProvider");
        assertThat(child.getMandatoryReadOnlyAttributeTypeProperty()).isEqualTo(
            Schema.getDefaultSchema().getAttributeType("description"));
        assertDNSetEquals(child.getOptionalMultiValuedDNProperty1(), dnProperty1);
        assertDNSetEquals(child.getOptionalMultiValuedDNProperty2(), dnProperty2);
    }
 
    /** Asserts that the actual set of DNs contains the expected values. */
    private void assertDNSetEquals(SortedSet<DN> actualDNs, List<String> expectedDNs) {
        String[] actualStrings = new String[actualDNs.size()];
        int i = 0;
        for (DN dn : actualDNs) {
            actualStrings[i] = dn.toString();
            i++;
        }
        assertThat(actualStrings).containsOnly(expectedDNs.toArray(new Object[expectedDNs.size()]));
    }
 
    /** Make an entry by combining two lists. */
    static Entry makeEntryFrom(List<String> base, List<String> attrs) {
        List<String> ldif = new ArrayList<>(base);
        ldif.addAll(attrs);
        return makeEntry(ldif.toArray(new String[0]));
    }
}