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

Nicolas Capponi
12.08.2015 530e312594f469609337996570cf0ea504554a68
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
/*
 * 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 2014 ForgeRock AS
 */
package org.opends.server.admin;
 
import org.forgerock.opendj.config.DurationUnit;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import org.opends.server.admin.std.meta.RootCfgDefn;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
import static org.testng.Assert.*;
 
/**
 * DurationPropertyDefinition Tester.
 */
public class DurationPropertyDefinitionTest extends DirectoryServerTestCase {
 
  /**
   * Sets up tests
   *
   * @throws Exception
   *           If the server could not be initialized.
   */
  @BeforeClass
  public void setUp() throws Exception {
    // This test suite depends on having the schema available, so
    // we'll start the server.
    TestCaseUtils.startServer();
  }
 
  /**
   * Tests creation of builder succeeds
   */
  @Test
  public void testCreateBuilder() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    assertNotNull(builder);
  }
 
  /**
   * Tests setting/getting of lower limit as long
   */
  @Test
  public void testLowerLimit1() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setLowerLimit(1);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    assertEquals(spd.getLowerLimit(), 1);
  }
 
  /**
   * Creates data for testing string-based limit values
   * @return data
   */
  @DataProvider(name = "longLimitData")
  public Object[][] createLongLimitData() {
    return new Object[][]{
            {1L, 1L},
            // { null, 0 }
    };
  }
 
  /**
   * Creates data for testing limit values
   * @return data
   */
  @DataProvider(name = "illegalLimitData")
  public Object[][] createIllegalLimitData() {
    return new Object[][]{
            {-1L, 0L, true}, // lower, upper, lower first
            {0L, -1L, false},
            {2L, 1L, true},
            {2L, 1L, false}
    };
  }
 
 
  /**
   * Tests setting/getting of lower limit as String
   * @param limit unit limit
   * @param expectedValue to compare
   */
  @Test(dataProvider = "longLimitData")
  public void testLowerLimit2(long limit, long expectedValue) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setLowerLimit(limit);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    assertEquals(spd.getLowerLimit(), expectedValue);
  }
 
  /**
   * Creates data for testing string-based limit values
   *
   * @return data
   */
  @DataProvider(name = "stringLimitData")
  public Object[][] createStringLimitData() {
    return new Object[][] {
        { "ms", "123", 123 },
        { "ms", "123s", 123000 },
        { "s", "123", 123000 },
        { "s", "123s", 123000 },
        { "m", "10", 600000 },
        { "m", "10s", 10000 }
    };
  }
 
  /**
   * Tests setting/getting of lower limit as String.
   *
   * @param unit
   *          The unit.
   * @param value
   *          The limit value.
   * @param expected
   *          The expected limit in ms.
   */
  @Test(dataProvider = "stringLimitData")
  public void testLowerLimit3(String unit, String value, long expected) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setBaseUnit(DurationUnit.getUnit(unit));
    builder.setLowerLimit(value);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    assertEquals(spd.getLowerLimit(), expected);
  }
 
  /**
   * Tests setting/getting of lower limit as long
   */
  @Test
  public void testUpperLimit1() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setLowerLimit(1);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    assertEquals(spd.getLowerLimit(), 1);
  }
 
  /**
   * Tests setting/getting of lower limit as String
   * @param limit upper limit
   * @param expectedValue to compare
   */
  @Test(dataProvider = "longLimitData")
  public void testUpperLimit2(long limit, long expectedValue) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setUpperLimit(limit);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    assertEquals((long) spd.getUpperLimit(), expectedValue);
  }
 
  /**
   * Tests setting/getting of lower limit as String
   * @param upper upper limit
   * @param lower lower limit
   * @param lowerFirst when true sets the lower limit property first
   */
  @Test(dataProvider = "illegalLimitData", expectedExceptions = IllegalArgumentException.class)
  public void testIllegalLimits(long lower, long upper, boolean lowerFirst) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    if (lowerFirst) {
      builder.setLowerLimit(lower);
      builder.setUpperLimit(upper);
    } else {
      builder.setUpperLimit(upper);
      builder.setLowerLimit(lower);
    }
  }
 
  /**
   * Tests the allowUnlimited property
   */
  @Test
  public void testIsAllowUnlimited1() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.decodeValue("unlimited");
  }
 
  /**
   * Tests the allowUnlimited property
   */
  @Test(expectedExceptions = PropertyException.class)
  public void testIsAllowUnlimited2() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(false);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.decodeValue("unlimited");
  }
 
  /**
   * Tests the allowUnlimited property
   */
  @Test(expectedExceptions = PropertyException.class)
  public void testIsAllowUnlimited3() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(false);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.validateValue(-1L);
  }
 
  /**
   * Creates illegal data for validate value
   * @return data
   */
  @DataProvider(name = "validateValueData")
  public Object[][] createValidateValueData() {
    return new Object[][]{
            {5000L, 10000L, false, 7L},
            {5000L, null, true, -1L},
            {5000L, 10000L, false, 5L},
            {5000L, 10000L, false, 10L},
            {5000L, null, false, 10000L}
    };
  }
 
  /**
   * Tests that validateValue works
   * @param allowUnlimited when true allows unlimited
   * @param high upper limit
   * @param low lower limit
   * @param value to validate
   */
  @Test(dataProvider = "validateValueData")
  public void testValidateValue1(Long low, Long high, boolean allowUnlimited, Long value) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setLowerLimit(low);
    builder.setUpperLimit(high);
    builder.setAllowUnlimited(allowUnlimited);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.validateValue(value);
  }
 
  /**
   * Creates illegal data for validate value
   * @return data
   */
  @DataProvider(name = "illegalValidateValueData")
  public Object[][] createIllegalValidateValueData() {
    return new Object[][]{
            {5000L, 10000L, false, null},
            {5000L, 10000L, false, 1L},
            {5000L, 10000L, false, 11L},
            {5000L, 10000L, false, -1L}
    };
  }
 
  /**
   * Tests that validateValue throws exceptions
   * @param low lower limit
   * @param high upper limit
   * @param allowUnlimited when true allows unlimited
   * @param value to validate
   */
  @Test(dataProvider = "illegalValidateValueData",
          expectedExceptions = {AssertionError.class,NullPointerException.class,PropertyException.class})
  public void testValidateValue2(Long low, Long high, boolean allowUnlimited, Long value) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setLowerLimit(low);
    builder.setUpperLimit(high);
    builder.setAllowUnlimited(allowUnlimited);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.validateValue(value);
  }
 
  /**
   * Creates encode test data
   * @return data
   */
  @DataProvider(name = "encodeValueData")
  public Object[][] createEncodeValueData() {
    return new Object[][]{
            {-1L, "unlimited"},
            {0L, "0 s"},
            {1L, "1 s"},
            {2L, "2 s"},
            {999L, "999 s"},
            {1000L, "1000 s"},
            {1001L, "1001 s"},
            {1023L, "1023 s"},
            {1024L, "1024 s"},
            {1025L, "1025 s"},
            {1000L * 1000L, "1000000 s"},
    };
  }
 
  /**
   * Tests encode value
   * @param value to encode
   * @param expectedValue to compare
   */
  @Test(dataProvider = "encodeValueData")
  public void testEncodeValue(Long value, String expectedValue) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    assertEquals(spd.encodeValue(value), expectedValue);
  }
 
  /**
   * Test that accept doesn't throw and exception
   */
  @Test
  public void testAccept() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
 
    PropertyDefinitionVisitor<Boolean, Void> v = new PropertyDefinitionVisitor<Boolean, Void>() {
 
      @Override
      public Boolean visitDuration(DurationPropertyDefinition d,
          Void o) {
        return true;
      }
 
      @Override
      public Boolean visitUnknown(PropertyDefinition d, Void o)
          throws PropertyException {
        return false;
      }
 
    };
 
    assertEquals((boolean) spd.accept(v, null), true);
  }
 
  /**
   * Make sure toString doesn't barf
   */
  @Test
  public void testToString() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.toString();
  }
 
  /**
   * Make sure toString doesn't barf
   */
  @Test
  public void testToString2() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setUpperLimit(10L);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.toString();
  }
 
  /**
   * Test value comparisons.
   */
  @Test
  public void testCompare() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.compare(1L, 2L);
  }
 
  /**
   * Test setting a default behavior provider.
   */
  @Test
  public void testSetDefaultBehaviorProvider() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    builder.setDefaultBehaviorProvider(new DefaultBehaviorProvider<Long>() {
      @Override
      public <R, P> R accept(DefaultBehaviorProviderVisitor<Long, R, P> v, P p) {
        return null;
      }
    });
  }
 
  /**
   * Test setting a property option.
   */
  @Test
  public void testSetOption() {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setOption(PropertyOption.HIDDEN);
  }
 
  /**
   * Creates encode test data
   * @return data
   */
  @DataProvider(name = "decodeValueData")
  public Object[][] createDecodeValueData() {
    return new Object[][]{
            // syntax tests
            {"unlimited", -1L},
            {"0h", 0L},
            {"0.0h", 0L},
            {"0.00h", 0L},
            {"0 h", 0L},
            {"0.00 h", 0L},
            {"1h", 1L},
            {"1 h", 1L},
            { "0ms", 0L },
            { "1h60m", 2L },
            { "1d10h", 34L },
            { "4d600m", 106L },
 
            // conversion tests
            {"1 d", 24L},
            {"2 d", 48L},
            {"0.5 d", 12L}
    };
  }
 
  /**
   * Tests decodeValue()
   * @param value to decode
   * @param expectedValue for comparison
   */
  @Test(dataProvider = "decodeValueData")
  public void testDecodeValue(String value, Long expectedValue) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(true);
    builder.setBaseUnit(DurationUnit.HOURS);
    builder.setMaximumUnit(DurationUnit.DAYS);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
//    if (spd.decodeValue(value) != expectedValue) {
//      System.out.println(spd.decodeValue(value) + "!=" + expectedValue);
//    }
    assertEquals(spd.decodeValue(value), expectedValue);
  }
 
  /**
   * Creates encode test data
   * @return data
   */
  @DataProvider(name = "decodeValueData2")
  public Object[][] createDecodeValueData2() {
    return new Object[][]{
            {""},
            {"0"}, // no unit
            {"123"}, // no unit
            {"a s"},
            {"1 x"},
            {"0.h"},
            {"0. h"},
            {"1.h"},
            {"1. h"},
            {"1.1 h"}, // too granular
            {"30 m"}, // unit too small violation
            {"60 m"}, // unit too small violation
            {"1 w"},  // unit too big violation
            {"7 w"},  // unit too big violation
            {"1 x"},
            {"1 d"}, // upper limit violation
            {"2 h"}, // lower limit violation
            {"-1 h"} // unlimited violation
    };
  }
 
  /**
   * Tests decodeValue()
   * @param value to decode
   */
  @Test(dataProvider = "decodeValueData2",
          expectedExceptions = {PropertyException.class})
  public void testDecodeValue(String value) {
    DurationPropertyDefinition.Builder builder = createTestBuilder();
    builder.setAllowUnlimited(false);
    builder.setBaseUnit(DurationUnit.HOURS);
    builder.setMaximumUnit(DurationUnit.DAYS);
    builder.setLowerLimit(5L);
    builder.setUpperLimit(10L);
    DurationPropertyDefinition spd = buildTestDefinition(builder);
    spd.decodeValue(value);
  }
 
  private DurationPropertyDefinition.Builder createTestBuilder() {
    return DurationPropertyDefinition.createBuilder(
        RootCfgDefn.getInstance(), "test-property-name");
  }
 
  private DurationPropertyDefinition buildTestDefinition(DurationPropertyDefinition.Builder builder) {
    builder.setDefaultBehaviorProvider(new DefinedDefaultBehaviorProvider<Long>("0"));
    return builder.getInstance();
  }
 
}