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

Jean-Noel Rouvignac
13.01.2015 f65e950f4432cdddd7513899ab52ebed3fc66a6b
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
/*
 * 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 2008 Sun Microsystems, Inc.
 *      Portions copyright 2015 ForgeRock AS
 */
package org.forgerock.opendj.config.server;
 
import static org.fest.assertions.Assertions.assertThat;
import static org.forgerock.opendj.ldap.TestCaseUtils.failWasExpected;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
 
import java.io.IOException;
import java.util.List;
 
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.TestChildCfgDefn;
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.ConfigDeleteListener;
import org.forgerock.opendj.config.server.spi.ConfigurationRepository;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldif.LDIF;
import org.mockito.ArgumentCaptor;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/**
 * Test cases for constraints on the server-side.
 */
@SuppressWarnings("javadoc")
public final class ConstraintTest extends AdminTestCase {
 
    private static class AddListener implements ConfigurationAddListener<TestChildCfg> {
 
        public ConfigChangeResult applyConfigurationAdd(TestChildCfg configuration) {
            return new ConfigChangeResult();
        }
 
        public boolean isConfigurationAddAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            return true;
        }
    }
 
    private static class DeleteListener implements ConfigurationDeleteListener<TestChildCfg> {
 
        public ConfigChangeResult applyConfigurationDelete(TestChildCfg configuration) {
            return new ConfigChangeResult();
        }
 
        public boolean isConfigurationDeleteAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            return true;
        }
 
    }
 
    private static class ChangeListener implements ConfigurationChangeListener<TestChildCfg> {
 
        public ConfigChangeResult applyConfigurationChange(TestChildCfg configuration) {
            return new ConfigChangeResult();
        }
 
        public boolean isConfigurationChangeAcceptable(TestChildCfg configuration,
            List<LocalizableMessage> unacceptableReasons) {
            return true;
        }
 
    }
 
    // @Checkstyle:off
    private static final Entry TEST_CHILD_1 = LDIF.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_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");
 
    /**
     * Parent 1 - uses default values for
     * optional-multi-valued-dn-property.
     */
    private static final Entry TEST_PARENT_1 = LDIF.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");
    // @Checkstyle:on
 
    @BeforeClass
    public void setUp() throws Exception {
        TestCfg.setUp();
    }
 
    @AfterClass
    public void tearDown() throws Exception {
        TestCfg.cleanup();
    }
 
    /** Success just ensure there is no exception raised. */
    @Test
    public void testGetManagedObjectSuccess() throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        MockConstraint constraint = new MockConstraint(true, false, configRepository);
        try {
            TestCfg.addConstraint(constraint);
            TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
 
            // act
            parentCfg.getTestChild(entryName(TEST_CHILD_1));
        } finally {
            TestCfg.removeConstraint(constraint);
        }
    }
 
    @Test
    public void testGetManagedObjectFail() throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        MockConstraint constraint = new MockConstraint(false, true, configRepository);
        try {
            TestCfg.addConstraint(constraint);
            TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
 
            // act
            parentCfg.getTestChild(entryName(TEST_CHILD_1));
 
            failWasExpected(ConfigException.class);
        } catch (ConfigException e) {
            // assert
            Throwable cause = e.getCause();
            assertThat(e.getCause()).isNotNull().isInstanceOf(ConstraintViolationException.class);
            ConstraintViolationException cve = (ConstraintViolationException) cause;
            assertThat(cve.getMessages().size()).isEqualTo(1);
            assertThat(cve.getManagedObject().getManagedObjectDefinition()).isSameAs(TestChildCfgDefn.getInstance());
        } finally {
            TestCfg.removeConstraint(constraint);
        }
    }
 
    @DataProvider
    Object[][] constraintValues() {
        return new Object[][] {
            // value of constraint used
            { true }, // success
            { false } // failure
        };
    }
 
    @Test(dataProvider = "constraintValues")
    public void testAddConstraint(boolean isUsableConstraint) throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        parentCfg.addTestChildAddListener(new AddListener());
        MockConstraint constraint = new MockConstraint(isUsableConstraint, false, configRepository);
        try {
            TestCfg.addConstraint(constraint);
 
            // act
            boolean isAcceptable = simulateEntryAdd(TEST_CHILD_1, configRepository);
 
            // assert : success depends on constraint used
            assertThat(isAcceptable).isEqualTo(isUsableConstraint);
        } finally {
            TestCfg.removeConstraint(constraint);
        }
    }
 
    @Test(dataProvider = "constraintValues")
    public void testDeleteConstraint(boolean isDeleteAllowedConstraint) throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        parentCfg.addTestChildDeleteListener(new DeleteListener());
        MockConstraint constraint = new MockConstraint(false, isDeleteAllowedConstraint, configRepository);
        try {
            TestCfg.addConstraint(constraint);
 
            // act
            boolean isAcceptable = simulateEntryDelete(TEST_CHILD_1, configRepository);
 
            // assert : success depends on constraint used
            assertThat(isAcceptable).isEqualTo(isDeleteAllowedConstraint);
        } finally {
            TestCfg.removeConstraint(constraint);
        }
    }
 
    @Test(dataProvider = "constraintValues")
    public void testChangeConstraint(boolean isUsableConstraint) throws Exception {
        // arrange
        ConfigurationRepository configRepository =
            createConfigRepositoryWithEntries(TEST_PARENT_1, TEST_BASE_CHILD, TEST_CHILD_1);
        ServerManagementContext context =
            new ServerManagementContext(configRepository);
        MockConstraint constraint = new MockConstraint(isUsableConstraint, false, configRepository);
        TestParentCfg parentCfg = getParentCfg(TEST_PARENT_1, context);
        TestChildCfg childCfg = parentCfg.getTestChild(entryName(TEST_CHILD_1));
 
        try {
            TestCfg.addConstraint(constraint);
            childCfg.addChangeListener(new ChangeListener());
 
            // act
            // It is not an issue to use the same child entry here
            // as we're only interested in constraint checking
            boolean isAcceptable = simulateEntryChange(TEST_CHILD_1, configRepository);
 
            // assert : success depends on constraint used
            assertThat(isAcceptable).isEqualTo(isUsableConstraint);
        } finally {
            TestCfg.removeConstraint(constraint);
        }
    }
 
    /**
     * Simulate an entry add by triggering configAddIsAcceptable method of last
     * registered add listener.
     *
     * @return true if add is acceptable, false otherwise.
     */
    private boolean 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());
 
        return registeredListener.getValue().configAddIsAcceptable(entry, new LocalizableMessageBuilder());
    }
 
    /**
     * Simulate an entry delete by triggering configDeleteIsAcceptable method of
     * last registered add listener.
     *
     * @return true if delete is acceptable, false otherwise.
     */
    private boolean simulateEntryDelete(Entry entry, ConfigurationRepository configRepository) throws IOException {
        // use argument capture to retrieve the actual listener
        ArgumentCaptor<ConfigDeleteListener> registeredListener = ArgumentCaptor.forClass(ConfigDeleteListener.class);
        verify(configRepository).registerDeleteListener(eq(entry.getName().parent()), registeredListener.capture());
 
        return registeredListener.getValue().configDeleteIsAcceptable(entry, new LocalizableMessageBuilder());
    }
 
    /**
     * Simulate an entry change by triggering configChangeIsAcceptable method on
     * last registered change listener.
     *
     * @return true if change is acceptable, false otherwise.
     */
    private boolean 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());
 
        return registeredListener.getValue().configChangeIsAcceptable(newEntry, new LocalizableMessageBuilder());
    }
 
}