/
TestRefinedSchema.java
688 lines (547 loc) · 38.1 KB
/
TestRefinedSchema.java
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.common.refinery;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertTrue;
import static com.evolveum.midpoint.prism.util.PrismTestUtil.*;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.ConsistencyCheckScope;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.schema.processor.*;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import com.evolveum.midpoint.common.ResourceObjectPattern;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Definition;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.schema.util.SchemaTestConstants;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LayerType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAttributesType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
/**
* @author semancik
*/
@Listeners({com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class})
public class TestRefinedSchema {
public static final String TEST_DIR_NAME = "src/test/resources/refinery";
private static final File RESOURCE_COMPLEX_FILE = new File(TEST_DIR_NAME, "resource-complex.xml");
private static final File RESOURCE_SIMPLE_FILE = new File(TEST_DIR_NAME, "resource-simple.xml");
private static final File RESOURCE_POSIX_FILE = new File(TEST_DIR_NAME, "resource-ldap-posix.xml");
private static final String ENTITLEMENT_GROUP_INTENT = "group";
private static final String ENTITLEMENT_LDAP_GROUP_INTENT = "ldapGroup";
private static final String ENTITLEMENT_UNIX_GROUP_INTENT = "unixGroup";
private static final QName OBJECT_CLASS_INETORGPERSON_QNAME = new QName(MidPointConstants.NS_RI, "inetOrgPerson");
@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(MidPointConstants.NS_MIDPOINT_PUBLIC_PREFIX);
resetPrismContext(MidPointPrismContextFactory.FACTORY);
}
@Test
public void test010ParseFromResourceComplex() throws Exception {
final String TEST_NAME = "test010ParseFromResourceComplex";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_COMPLEX_FILE);
ResourceType resourceType = resource.asObjectable();
// WHEN
TestUtil.displayWhen(TEST_NAME);
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resourceType, prismContext);
// THEN
TestUtil.displayThen(TEST_NAME);
assertNotNull("Refined schema is null", rSchema);
System.out.println("Refined schema");
System.out.println(rSchema.debugDump());
assertRefinedSchema(resourceType, rSchema, null, LayerType.MODEL, true, true);
assertLayerRefinedSchema(resourceType, rSchema, LayerType.SCHEMA, LayerType.SCHEMA, true, true);
assertLayerRefinedSchema(resourceType, rSchema, LayerType.MODEL, LayerType.MODEL, true, true);
assertLayerRefinedSchema(resourceType, rSchema, LayerType.PRESENTATION, LayerType.PRESENTATION, true, true);
RefinedObjectClassDefinition rAccount = rSchema.getRefinedDefinition(ShadowKindType.ACCOUNT, (String)null);
RefinedAttributeDefinition userPasswordAttribute = rAccount.findAttributeDefinition("userPassword");
assertNotNull("No userPassword attribute", userPasswordAttribute);
assertTrue("userPassword not ignored", userPasswordAttribute.isIgnored());
}
private void assertLayerRefinedSchema(ResourceType resourceType, RefinedResourceSchema rSchema, LayerType sourceLayer,
LayerType validationLayer, boolean assertEntitlements, boolean assertPasswordPolicy) {
System.out.println("Refined schema: layer="+sourceLayer);
LayerRefinedResourceSchema lrSchema = rSchema.forLayer(sourceLayer);
System.out.println(lrSchema.debugDump());
assertRefinedSchema(resourceType, lrSchema, sourceLayer, validationLayer, assertEntitlements, assertPasswordPolicy);
}
@Test
public void test020ParseFromResourceSimple() throws Exception {
final String TEST_NAME = "test020ParseFromResourceSimple";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_SIMPLE_FILE);
ResourceType resourceType = resource.asObjectable();
// WHEN
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resourceType, prismContext);
// THEN
assertNotNull("Refined schema is null", rSchema);
System.out.println("Refined schema");
System.out.println(rSchema.debugDump());
assertRefinedSchema(resourceType, rSchema, null, LayerType.SCHEMA, false, false);
}
private void assertRefinedSchema(ResourceType resourceType, RefinedResourceSchema rSchema,
LayerType sourceLayer, LayerType validationLayer, boolean assertEntitlements, boolean assertPasswordPolicy) {
assertEquals("Unexpected number of object classes in refined schema", 2, rSchema.getDefinitions().size());
for (Definition def: rSchema.getDefinitions()) {
if (!(def instanceof RefinedObjectClassDefinition)) {
AssertJUnit.fail("Non-refined definition sneaked into resource schema: "+def);
}
}
assertFalse("No account definitions", rSchema.getRefinedDefinitions(ShadowKindType.ACCOUNT).isEmpty());
RefinedObjectClassDefinition rAccountDef = rSchema.getRefinedDefinition(ShadowKindType.ACCOUNT, (String)null);
RefinedObjectClassDefinition accountDefByNullObjectclass = rSchema.findRefinedDefinitionByObjectClassQName(ShadowKindType.ACCOUNT, null);
assertEquals("findAccountDefinitionByObjectClass(null) returned wrong value", rAccountDef, accountDefByNullObjectclass);
RefinedObjectClassDefinition accountDefByIcfAccountObjectclass = rSchema.findRefinedDefinitionByObjectClassQName(ShadowKindType.ACCOUNT,
new QName(resourceType.getNamespace(), SchemaTestConstants.ICF_ACCOUNT_OBJECT_CLASS_LOCAL_NAME));
assertEquals("findAccountDefinitionByObjectClass(ICF account) returned wrong value", rAccountDef, accountDefByIcfAccountObjectclass);
assertRObjectClassDef(rAccountDef, resourceType, sourceLayer, validationLayer);
System.out.println("Refined account definitionn:");
System.out.println(rAccountDef.debugDump());
assertEquals("Wrong kind", ShadowKindType.ACCOUNT, rAccountDef.getKind());
Collection<? extends RefinedAttributeDefinition> accAttrsDef = rAccountDef.getAttributeDefinitions();
assertNotNull("Null attributeDefinitions", accAttrsDef);
assertFalse("Empty attributeDefinitions", accAttrsDef.isEmpty());
assertEquals("Unexpected number of attributeDefinitions", 55, accAttrsDef.size());
RefinedAttributeDefinition disabledAttribute = rAccountDef.findAttributeDefinition("ds-pwp-account-disabled");
assertNotNull("No ds-pwp-account-disabled attribute", disabledAttribute);
assertTrue("ds-pwp-account-disabled not ignored", disabledAttribute.isIgnored());
RefinedAttributeDefinition<?> displayNameAttributeDef = rAccountDef.getDisplayNameAttribute();
assertNotNull("No account displayNameAttribute", displayNameAttributeDef);
assertEquals("Wrong account displayNameAttribute", new QName(resourceType.getNamespace(), "uid"), displayNameAttributeDef.getName());
// This is compatibility with PrismContainerDefinition, it should work well
Collection<? extends ItemDefinition> propertyDefinitions = rAccountDef.getDefinitions();
assertNotNull("Null propertyDefinitions", propertyDefinitions);
assertFalse("Empty propertyDefinitions", propertyDefinitions.isEmpty());
assertEquals("Unexpected number of propertyDefinitions", 55, propertyDefinitions.size());
if (assertPasswordPolicy) {
ObjectReferenceType passwordPolicyRef = rAccountDef.getPasswordPolicy();
assertNotNull("Expected password policy for account definition not found", passwordPolicyRef);
}
if (assertEntitlements) {
assertFalse("No entitlement definitions", rSchema.getRefinedDefinitions(ShadowKindType.ENTITLEMENT).isEmpty());
RefinedObjectClassDefinition rEntDef = rSchema.getRefinedDefinition(ShadowKindType.ENTITLEMENT, (String)null);
assertNotNull("No entitlement definition for null intent", rEntDef);
RefinedObjectClassDefinition rEntDefGroup = rSchema.getRefinedDefinition(ShadowKindType.ENTITLEMENT, ENTITLEMENT_GROUP_INTENT);
assertNotNull("No entitlement for intent '"+ENTITLEMENT_GROUP_INTENT+"'", rEntDefGroup);
assertEquals("Wrong kind", ShadowKindType.ENTITLEMENT, rEntDef.getKind());
Collection<? extends RefinedAttributeDefinition> entAttrDefs = rEntDef.getAttributeDefinitions();
assertNotNull("Null attributeDefinitions", entAttrDefs);
assertFalse("Empty attributeDefinitions", entAttrDefs.isEmpty());
assertEquals("Unexpected number of attributeDefinitions", 11, entAttrDefs.size());
// TODO
RefinedAttributeDefinition<?> entDisplayNameAttributeDef = rEntDef.getDisplayNameAttribute();
assertNotNull("No entitlement displayNameAttribute", entDisplayNameAttributeDef);
assertEquals("Wrong entitlement displayNameAttribute", new QName(resourceType.getNamespace(), "cn"), entDisplayNameAttributeDef.getName());
assertEquals("Unexpected number of entitlement associations", 1, rAccountDef.getAssociationDefinitions().size());
}
assertRefinedToLayer(rAccountDef, sourceLayer);
ResourceAttributeContainerDefinition resAttrContainerDef = rAccountDef.toResourceAttributeContainerDefinition();
assertNotNull("No ResourceAttributeContainerDefinition", resAttrContainerDef);
System.out.println("\nResourceAttributeContainerDefinition ("+sourceLayer+")");
System.out.println(resAttrContainerDef.debugDump());
ObjectClassComplexTypeDefinition rComplexTypeDefinition = resAttrContainerDef.getComplexTypeDefinition();
System.out.println("\nResourceAttributeContainerDefinition ComplexTypeDefinition ("+sourceLayer+")");
System.out.println(rComplexTypeDefinition.debugDump());
assertRefinedToLayer(rComplexTypeDefinition, sourceLayer);
ResourceAttributeDefinition riUidAttrDef = resAttrContainerDef.findAttributeDefinition(new QName(resourceType.getNamespace(), "uid"));
assertNotNull("No ri:uid def in ResourceAttributeContainerDefinition", riUidAttrDef);
System.out.println("\nri:uid def "+riUidAttrDef.getClass()+" ("+sourceLayer+")");
System.out.println(riUidAttrDef.debugDump());
assertRefinedToLayer(riUidAttrDef, sourceLayer);
if (validationLayer == LayerType.PRESENTATION) {
assertFalse("Can update "+riUidAttrDef+" from ResourceAttributeContainerDefinition ("+sourceLayer+")",
riUidAttrDef.canModify());
} else {
assertTrue("Cannot update "+riUidAttrDef+" from ResourceAttributeContainerDefinition ("+sourceLayer+")",
riUidAttrDef.canModify());
}
Collection<? extends ResourceAttributeDefinition> definitionsFromResAttrContainerDef = resAttrContainerDef.getDefinitions();
for (ResourceAttributeDefinition definitionFromResAttrContainerDef: definitionsFromResAttrContainerDef) {
assertRefinedToLayer(definitionFromResAttrContainerDef, sourceLayer);
}
}
private void assertRefinedToLayer(
ObjectClassComplexTypeDefinition ocDef,
LayerType expectedLayer) {
if (expectedLayer == null) {
// This is OK, it may not be layer-bound.
return;
}
if (!(ocDef instanceof LayerRefinedObjectClassDefinition)) {
AssertJUnit.fail("Expected that definition of objectclass "+ocDef.getTypeName()+" in layer "+expectedLayer
+" will be LayerRefinedObjectClassDefinition, but it is "+ocDef.getClass()+": "+ocDef);
}
assertEquals("Wrong layer in "+ocDef, expectedLayer, ((LayerRefinedObjectClassDefinition)ocDef).getLayer());
}
private void assertRefinedToLayer(ResourceAttributeDefinition attrDef, LayerType expectedLayer) {
if (expectedLayer == null) {
// This is OK, it may not be layer-bound.
return;
}
if (!(attrDef instanceof LayerRefinedAttributeDefinition)) {
AssertJUnit.fail("Expected that definition of attribute "+attrDef.getName()+" in layer "+expectedLayer
+" will be LayerRefinedAttributeDefinition, but it is "+attrDef.getClass()+": "+attrDef);
}
assertEquals("Wrong layer in "+attrDef, expectedLayer, ((LayerRefinedAttributeDefinition)attrDef).getLayer());
}
@Test
public void test100ParseAccount() throws Exception {
final String TEST_NAME = "test100ParseAccount";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_COMPLEX_FILE);
ResourceType resourceType = resource.asObjectable();
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resourceType, prismContext);
RefinedObjectClassDefinition defaultAccountDefinition = rSchema.getDefaultRefinedDefinition(ShadowKindType.ACCOUNT);
assertNotNull("No refined default account definition in "+rSchema, defaultAccountDefinition);
PrismObject<ShadowType> accObject = prismContext.parseObject(new File(TEST_DIR_NAME, "account-jack.xml"));
// WHEN
PrismObjectDefinition<ShadowType> objectDefinition = defaultAccountDefinition.getObjectDefinition();
System.out.println("Refined account definition:");
System.out.println(objectDefinition.debugDump());
accObject.applyDefinition(objectDefinition);
// THEN
System.out.println("Parsed account:");
System.out.println(accObject.debugDump());
assertAccountShadow(accObject, resource, prismContext);
}
@Test
public void test110ApplyAttributeDefinition() throws Exception {
final String TEST_NAME = "test110ApplyAttributeDefinition";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_COMPLEX_FILE);
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resource, prismContext);
System.out.println("Refined schema:");
System.out.println(rSchema.debugDump(1));
RefinedObjectClassDefinition defaultAccountDefinition = rSchema.getDefaultRefinedDefinition(ShadowKindType.ACCOUNT);
assertNotNull("No refined default account definition in "+rSchema, defaultAccountDefinition);
System.out.println("Refined account definition:");
System.out.println(defaultAccountDefinition.debugDump(1));
PrismObject<ShadowType> accObject = prismContext.parseObject(new File(TEST_DIR_NAME, "account-jack.xml"));
PrismContainer<Containerable> attributesContainer = accObject.findContainer(ShadowType.F_ATTRIBUTES);
System.out.println("Attributes container:");
System.out.println(attributesContainer.debugDump(1));
// WHEN
attributesContainer.applyDefinition((PrismContainerDefinition)defaultAccountDefinition.toResourceAttributeContainerDefinition(), true);
// THEN
System.out.println("Parsed account:");
System.out.println(accObject.debugDump(1));
assertAccountShadow(accObject, resource, prismContext);
}
private void assertAccountShadow(PrismObject<ShadowType> accObject, PrismObject<ResourceType> resource, PrismContext prismContext) throws SchemaException, JAXBException {
ResourceType resourceType = resource.asObjectable();
QName objectClassQName = new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "AccountObjectClass");
PrismAsserts.assertPropertyValue(accObject, ShadowType.F_NAME, createPolyString("jack"));
PrismAsserts.assertPropertyValue(accObject, ShadowType.F_OBJECT_CLASS, objectClassQName);
PrismAsserts.assertPropertyValue(accObject, ShadowType.F_INTENT, SchemaConstants.INTENT_DEFAULT);
PrismContainer<?> attributes = accObject.findOrCreateContainer(SchemaConstants.C_ATTRIBUTES);
assertEquals("Wrong type of <attributes> definition in account", ResourceAttributeContainerDefinitionImpl.class, attributes.getDefinition().getClass());
ResourceAttributeContainerDefinition attrDef = (ResourceAttributeContainerDefinition)attributes.getDefinition();
assertAttributeDefs(attrDef, resourceType, null, LayerType.MODEL);
PrismAsserts.assertPropertyValue(attributes, SchemaTestConstants.ICFS_NAME, "uid=jack,ou=People,dc=example,dc=com");
PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "cn"), "Jack Sparrow");
PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "givenName"), "Jack");
PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "sn"), "Sparrow");
PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "uid"), "jack");
assertEquals("JAXB class name doesn't match (1)", ShadowType.class, accObject.getCompileTimeClass());
accObject.checkConsistence();
ShadowType accObjectType = accObject.asObjectable();
assertEquals("Wrong JAXB name", createPolyStringType("jack"), accObjectType.getName());
assertEquals("Wrong JAXB objectClass", objectClassQName, accObjectType.getObjectClass());
ShadowAttributesType attributesType = accObjectType.getAttributes();
assertNotNull("null ResourceObjectShadowAttributesType (JAXB)", attributesType);
List<Object> attributeElements = attributesType.getAny();
TestUtil.assertElement(attributeElements, SchemaTestConstants.ICFS_NAME, "uid=jack,ou=People,dc=example,dc=com");
TestUtil.assertElement(attributeElements, getAttrQName(resource, "cn"), "Jack Sparrow");
TestUtil.assertElement(attributeElements, getAttrQName(resource, "givenName"), "Jack");
TestUtil.assertElement(attributeElements, getAttrQName(resource, "sn"), "Sparrow");
TestUtil.assertElement(attributeElements, getAttrQName(resource, "uid"), "jack");
String accString = PrismTestUtil.serializeObjectToString(accObjectType.asPrismObject());
System.out.println("Result of JAXB marshalling:\n"+accString);
accObject.checkConsistence(true, true, ConsistencyCheckScope.THOROUGH);
}
private QName getAttrQName(PrismObject<ResourceType> resource, String localPart) {
return new QName(ResourceTypeUtil.getResourceNamespace(resource), localPart);
}
@Test
public void test120CreateShadow() throws Exception {
final String TEST_NAME = "test120CreateShadow";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_COMPLEX_FILE);
ResourceType resourceType = resource.asObjectable();
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resourceType, prismContext);
assertNotNull("Refined schema is null", rSchema);
assertFalse("No account definitions", rSchema.getRefinedDefinitions(ShadowKindType.ACCOUNT).isEmpty());
RefinedObjectClassDefinition rAccount = rSchema.getRefinedDefinition(ShadowKindType.ACCOUNT, (String)null);
// WHEN
PrismObject<ShadowType> blankShadow = rAccount.createBlankShadow();
// THEN
assertNotNull("No blank shadow", blankShadow);
assertNotNull("No prism context in blank shadow", blankShadow.getPrismContext());
PrismObjectDefinition<ShadowType> objectDef = blankShadow.getDefinition();
assertNotNull("Blank shadow has no definition", objectDef);
PrismContainerDefinition<?> attrDef = objectDef.findContainerDefinition(ShadowType.F_ATTRIBUTES);
assertNotNull("Blank shadow has no definition for attributes", attrDef);
assertTrue("Wrong class for attributes definition: "+attrDef.getClass(), attrDef instanceof ResourceAttributeContainerDefinition);
}
@Test
public void test130ProtectedAccount() throws Exception {
final String TEST_NAME = "test130ProtectedAccount";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_COMPLEX_FILE);
ResourceType resourceType = resource.asObjectable();
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resourceType, prismContext);
assertNotNull("Refined schema is null", rSchema);
assertFalse("No account definitions", rSchema.getRefinedDefinitions(ShadowKindType.ACCOUNT).isEmpty());
RefinedObjectClassDefinition rAccount = rSchema.getRefinedDefinition(ShadowKindType.ACCOUNT, (String)null);
// WHEN
Collection<ResourceObjectPattern> protectedAccounts = rAccount.getProtectedObjectPatterns();
// THEN
assertNotNull("Null protectedAccounts", protectedAccounts);
assertFalse("Empty protectedAccounts", protectedAccounts.isEmpty());
assertEquals("Unexpected number of protectedAccounts", 2, protectedAccounts.size());
Iterator<ResourceObjectPattern> iterator = protectedAccounts.iterator();
assertDeprecatedProtectedAccount("first protected account", iterator.next(), "uid=idm,ou=Administrators,dc=example,dc=com", rAccount);
assertDeprecatedProtectedAccount("second protected account", iterator.next(), "uid=root,ou=Administrators,dc=example,dc=com", rAccount);
}
private void assertAttributeDefs(ResourceAttributeContainerDefinition attrsDef, ResourceType resourceType, LayerType sourceLayer, LayerType validationLayer) {
assertNotNull("Null account definition", attrsDef);
assertEquals(SchemaConstants.INTENT_DEFAULT, attrsDef.getIntent());
assertEquals("AccountObjectClass", attrsDef.getComplexTypeDefinition().getTypeName().getLocalPart());
assertEquals("Wrong objectclass in the definition of <attributes> definition in account", RefinedObjectClassDefinitionImpl.class, attrsDef.getComplexTypeDefinition().getClass());
RefinedObjectClassDefinition rAccount = (RefinedObjectClassDefinition) attrsDef.getComplexTypeDefinition();
assertRObjectClassDef(rAccount, resourceType, sourceLayer, validationLayer);
}
private void assertRObjectClassDef(RefinedObjectClassDefinition rAccount, ResourceType resourceType, LayerType sourceLayer, LayerType validationLayer) {
assertTrue(rAccount.isDefault());
Collection<? extends RefinedAttributeDefinition> attrs = rAccount.getAttributeDefinitions();
assertFalse(attrs.isEmpty());
assertAttributeDef(attrs, SchemaTestConstants.ICFS_NAME, DOMUtil.XSD_STRING, 1, 1, "Distinguished Name", 110,
true, false,
true, true, validationLayer == LayerType.SCHEMA, // Access: create, read, update
sourceLayer, validationLayer);
assertAttributeDef(attrs, SchemaTestConstants.ICFS_UID, DOMUtil.XSD_STRING, 1, 1, "Entry UUID", 100,
false, false,
false, true, false, // Access: create, read, update
sourceLayer, validationLayer);
assertAttributeDef(attrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "cn"), DOMUtil.XSD_STRING,
1, (validationLayer == LayerType.MODEL || validationLayer == LayerType.PRESENTATION) ? 1 : -1, "Common Name", 1,
true, validationLayer == LayerType.PRESENTATION,
true, true, true, // Access: create, read, update
sourceLayer, validationLayer);
assertAttributeDef(attrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "uid"),
DOMUtil.XSD_STRING,
validationLayer == LayerType.SCHEMA ? 0 : 1 , // minOccurs
validationLayer == LayerType.SCHEMA ? -1 : 1, // maxOccurs
"Login Name", 2,
true, false,
true, true, validationLayer != LayerType.PRESENTATION, // Access: create, read, update
sourceLayer, validationLayer);
assertAttributeDef(attrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "employeeNumber"),
DOMUtil.XSD_STRING, 0, 1, null, null,
false, false,
true, true, true, // Access: create, read, update
sourceLayer, validationLayer);
}
private void assertAttributeDef(Collection<? extends RefinedAttributeDefinition> attrDefs, QName name,
QName typeName, int minOccurs, int maxOccurs, String displayName, Integer displayOrder,
boolean hasOutbound, boolean ignore, boolean canCreate, boolean canRead, boolean canUpdate,
LayerType sourceLayer, LayerType validationLayer) {
for (RefinedAttributeDefinition def : attrDefs) {
if (def.getName().equals(name)) {
assertEquals("Attribute " + name + " ("+sourceLayer+") type mismatch", typeName, def.getTypeName());
assertEquals("Attribute " + name + " ("+sourceLayer+") minOccurs mismatch", minOccurs, def.getMinOccurs());
assertEquals("Attribute " + name + " ("+sourceLayer+") maxOccurs mismatch", maxOccurs, def.getMaxOccurs());
if (validationLayer == LayerType.MODEL || validationLayer == LayerType.PRESENTATION) {
assertEquals("Attribute " + name + " ("+sourceLayer+") displayName mismatch", displayName, def.getDisplayName());
assertEquals("Attribute " + name + " ("+sourceLayer+") displayOrder mismatch", displayOrder, def.getDisplayOrder());
assertEquals("Attribute " + name + " ("+sourceLayer+") outbound mismatch", hasOutbound, def.getOutboundMappingType() != null);
}
assertEquals("Attribute " + name + " ("+sourceLayer+") ignored flag mismatch", ignore, def.isIgnored());
assertEquals("Attribute " + name + " ("+sourceLayer+") canCreate mismatch", canCreate, def.canAdd());
assertEquals("Attribute " + name + " ("+sourceLayer+") canRead mismatch", canRead, def.canRead());
assertEquals("Attribute " + name + " ("+sourceLayer+") canUpdate mismatch", canUpdate, def.canModify());
return;
}
}
Assert.fail("Attribute " + name + " not found");
}
private void assertDeprecatedProtectedAccount(String message, ResourceObjectPattern protectedPattern, String identifierValue, RefinedObjectClassDefinition rAccount) throws SchemaException {
Collection<ResourceAttribute<?>> identifiers = protectedPattern.getIdentifiers();
assertNotNull("Null identifiers in "+message, identifiers);
assertEquals("Wrong number identifiers in "+message, 1, identifiers.size());
ResourceAttribute<?> identifier = identifiers.iterator().next();
assertNotNull("Null identifier in "+message, identifier);
assertEquals("Wrong identifier value in "+message, identifier.getRealValue(), identifierValue);
// Try matching
PrismObject<ShadowType> shadow = rAccount.getObjectDefinition().instantiate();
ResourceAttributeContainer attributesContainer = ShadowUtil.getOrCreateAttributesContainer(shadow, rAccount);
ResourceAttribute<String> confusingAttr1 = createStringAttribute(new QName("http://whatever.com","confuseMe"), "HowMuchWoodWouldWoodchuckChuckIfWoodchuckCouldChudkWood");
attributesContainer.add(confusingAttr1);
ResourceAttribute<String> nameAttr = createStringAttribute(SchemaTestConstants.ICFS_NAME, identifierValue);
attributesContainer.add(nameAttr);
ResourceAttribute<String> confusingAttr2 = createStringAttribute(new QName("http://whatever.com","confuseMeAgain"), "WoodchuckWouldChuckNoWoodAsWoodchuckCannotChuckWood");
attributesContainer.add(confusingAttr2);
assertTrue("Test attr not matched in "+message, protectedPattern.matches(shadow, null));
nameAttr.setRealValue("huhulumululul");
assertFalse("Test attr nonsense was matched in "+message, protectedPattern.matches(shadow, null));
}
private ResourceAttribute<String> createStringAttribute(QName attrName, String value) {
ResourceAttributeDefinition testAttrDef = new ResourceAttributeDefinitionImpl(attrName, DOMUtil.XSD_STRING, getPrismContext());
ResourceAttribute<String> testAttr = testAttrDef.instantiate();
testAttr.setRealValue(value);
return testAttr;
}
@Test
public void test140ParseFromResourcePosix() throws Exception {
final String TEST_NAME = "test140ParseFromResourcePosix";
TestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = createInitializedPrismContext();
PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_POSIX_FILE);
ResourceType resourceType = resource.asObjectable();
// WHEN
TestUtil.displayWhen(TEST_NAME);
RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resourceType, prismContext);
// THEN
TestUtil.displayThen(TEST_NAME);
assertNotNull("Refined schema is null", rSchema);
System.out.println("Refined schema");
System.out.println(rSchema.debugDump());
assertFalse("No account definitions", rSchema.getRefinedDefinitions(ShadowKindType.ACCOUNT).isEmpty());
// ### default account objectType
RefinedObjectClassDefinition rAccountDef = rSchema.getRefinedDefinition(ShadowKindType.ACCOUNT, (String)null);
assertNotNull("No default account definition", rAccountDef);
RefinedObjectClassDefinition accountDefByIcfAccountObjectclass = rSchema.findRefinedDefinitionByObjectClassQName(ShadowKindType.ACCOUNT,
OBJECT_CLASS_INETORGPERSON_QNAME);
assertTrue("findAccountDefinitionByObjectClass("+OBJECT_CLASS_INETORGPERSON_QNAME+") returned wrong value", rAccountDef.equals(accountDefByIcfAccountObjectclass));
assertTrue(rAccountDef.isDefault());
Collection<? extends RefinedAttributeDefinition> rAccountAttrs = rAccountDef.getAttributeDefinitions();
assertFalse(rAccountAttrs.isEmpty());
assertAttributeDef(rAccountAttrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "dn"),
DOMUtil.XSD_STRING, 1, 1, "Distinguished Name", 110,
true, false,
true, true, true, // Access: create, read, update
LayerType.SCHEMA, LayerType.PRESENTATION);
assertAttributeDef(rAccountAttrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "entryUUID"),
DOMUtil.XSD_STRING, 0, 1, null, 100,
false, false,
false, true, false, // Access: create, read, update
LayerType.SCHEMA, LayerType.PRESENTATION);
assertAttributeDef(rAccountAttrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "cn"),
DOMUtil.XSD_STRING, 1, -1, "Common Name", 590,
true, false,
true, true, true, // Access: create, read, update
LayerType.SCHEMA, LayerType.PRESENTATION);
assertAttributeDef(rAccountAttrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "uid"),
DOMUtil.XSD_STRING, 0, -1, "Login Name", 300,
true, false,
true, true, true, // Access: create, read, update
LayerType.SCHEMA, LayerType.PRESENTATION);
assertAttributeDef(rAccountAttrs, new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "employeeNumber"),
DOMUtil.XSD_STRING, 0, 1, null, 140,
false, false,
true, true, true, // Access: create, read, update
LayerType.SCHEMA, LayerType.PRESENTATION);
System.out.println("Refined account definitionn:");
System.out.println(rAccountDef.debugDump());
assertEquals("Wrong kind", ShadowKindType.ACCOUNT, rAccountDef.getKind());
Collection<? extends RefinedAttributeDefinition> accAttrsDef = rAccountDef.getAttributeDefinitions();
assertNotNull("Null attributeDefinitions", accAttrsDef);
assertFalse("Empty attributeDefinitions", accAttrsDef.isEmpty());
assertEquals("Unexpected number of attributeDefinitions", 53, accAttrsDef.size());
RefinedAttributeDefinition disabledAttribute = rAccountDef.findAttributeDefinition("ds-pwp-account-disabled");
assertNotNull("No ds-pwp-account-disabled attribute", disabledAttribute);
assertTrue("ds-pwp-account-disabled not ignored", disabledAttribute.isIgnored());
RefinedAttributeDefinition<?> displayNameAttributeDef = rAccountDef.getDisplayNameAttribute();
assertNotNull("No account displayNameAttribute", displayNameAttributeDef);
assertEquals("Wrong account displayNameAttribute", new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "dn"),
displayNameAttributeDef.getName());
// This is compatibility with PrismContainerDefinition, it should work well
Collection<? extends ItemDefinition> propertyDefinitions = rAccountDef.getDefinitions();
assertNotNull("Null propertyDefinitions", propertyDefinitions);
assertFalse("Empty propertyDefinitions", propertyDefinitions.isEmpty());
assertEquals("Unexpected number of propertyDefinitions", 53, propertyDefinitions.size());
assertFalse("No entitlement definitions", rSchema.getRefinedDefinitions(ShadowKindType.ENTITLEMENT).isEmpty());
RefinedObjectClassDefinition rEntDef = rSchema.getRefinedDefinition(ShadowKindType.ENTITLEMENT, (String)null);
assertNotNull("No entitlement definition for null intent", rEntDef);
RefinedObjectClassDefinition rEntDefGroup = rSchema.getRefinedDefinition(ShadowKindType.ENTITLEMENT, ENTITLEMENT_LDAP_GROUP_INTENT);
assertNotNull("No entitlement for intent '"+ENTITLEMENT_LDAP_GROUP_INTENT+"'", rEntDefGroup);
assertEquals("Wrong kind", ShadowKindType.ENTITLEMENT, rEntDef.getKind());
Collection<? extends RefinedAttributeDefinition> entAttrDefs = rEntDef.getAttributeDefinitions();
assertNotNull("Null attributeDefinitions", entAttrDefs);
assertFalse("Empty attributeDefinitions", entAttrDefs.isEmpty());
assertEquals("Unexpected number of attributeDefinitions", 12, entAttrDefs.size());
RefinedAttributeDefinition<?> entDisplayNameAttributeDef = rEntDef.getDisplayNameAttribute();
assertNotNull("No entitlement displayNameAttribute", entDisplayNameAttributeDef);
assertEquals("Wrong entitlement displayNameAttribute", new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "dn"),
entDisplayNameAttributeDef.getName());
assertEquals("Unexpected number of entitlement associations", 1, rAccountDef.getAssociationDefinitions().size());
ResourceAttributeContainerDefinition resAttrContainerDef = rAccountDef.toResourceAttributeContainerDefinition();
assertNotNull("No ResourceAttributeContainerDefinition", resAttrContainerDef);
System.out.println("\nResourceAttributeContainerDefinition");
System.out.println(resAttrContainerDef.debugDump());
ObjectClassComplexTypeDefinition rComplexTypeDefinition = resAttrContainerDef.getComplexTypeDefinition();
System.out.println("\nResourceAttributeContainerDefinition ComplexTypeDefinition");
System.out.println(rComplexTypeDefinition.debugDump());
ResourceAttributeDefinition<String> riUidAttrDef = resAttrContainerDef.findAttributeDefinition(new QName(resourceType.getNamespace(), "uid"));
assertNotNull("No ri:uid def in ResourceAttributeContainerDefinition", riUidAttrDef);
System.out.println("\nri:uid def "+riUidAttrDef.getClass());
System.out.println(riUidAttrDef.debugDump());
}
}