Search in sources :

Example 1 with PrismContainerDefinitionImpl

use of com.evolveum.midpoint.prism.PrismContainerDefinitionImpl in project midpoint by Evolveum.

the class SchemaProcessorTest method testRoundTripGeneric.

@Test
public void testRoundTripGeneric() throws SchemaException {
    // GIVEN
    PrismSchemaImpl schema = new PrismSchemaImpl(SCHEMA_NS, PrismTestUtil.getPrismContext());
    // Ordinary property
    schema.createPropertyDefinition("number1", DOMUtil.XSD_INT);
    // Property container
    PrismContainerDefinitionImpl containerDefinition = schema.createPropertyContainerDefinition("ContainerType");
    // ... in it ordinary property
    containerDefinition.createPropertyDefinition("login", DOMUtil.XSD_STRING);
    // ... and local property with a type from another schema
    containerDefinition.createPropertyDefinition("password", ProtectedStringType.COMPLEX_TYPE);
    // ... property reference
    containerDefinition.createPropertyDefinition(SchemaConstants.C_CREDENTIALS, CredentialsType.COMPLEX_TYPE);
    // ... read-only int property 
    PrismPropertyDefinition counterProperty = containerDefinition.createPropertyDefinition("counter", DOMUtil.XSD_INT);
    ((PrismPropertyDefinitionImpl) counterProperty).setReadOnly();
    System.out.println("Generic schema before serializing to XSD: ");
    System.out.println(schema.debugDump());
    System.out.println();
    // WHEN
    Document xsd = schema.serializeToXsd();
    String stringXmlSchema = DOMUtil.serializeDOMToString(xsd);
    System.out.println("Generic schema after serializing to XSD: ");
    System.out.println(stringXmlSchema);
    System.out.println();
    Document parsedXsd = DOMUtil.parseDocument(stringXmlSchema);
    PrismSchema newSchema = PrismSchemaImpl.parse(DOMUtil.getFirstChildElement(parsedXsd), true, "serialized schema", PrismTestUtil.getPrismContext());
    System.out.println("Generic schema after parsing from XSD: ");
    System.out.println(newSchema.debugDump());
    System.out.println();
    // THEN
    PrismPropertyDefinition number1def = newSchema.findItemDefinitionByElementName(new QName(SCHEMA_NS, "number1"), PrismPropertyDefinition.class);
    assertEquals(new QName(SCHEMA_NS, "number1"), number1def.getName());
    assertEquals(DOMUtil.XSD_INT, number1def.getTypeName());
    PrismContainerDefinition newContainerDef = schema.findContainerDefinitionByType(new QName(SCHEMA_NS, "ContainerType"));
    assertEquals(new QName(SCHEMA_NS, "ContainerType"), newContainerDef.getTypeName());
    PrismPropertyDefinition loginDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "login"));
    assertEquals(new QName(SCHEMA_NS, "login"), loginDef.getName());
    assertEquals(DOMUtil.XSD_STRING, loginDef.getTypeName());
    assertTrue("Read flag is wrong", loginDef.canRead());
    assertTrue("Create flag is wrong", loginDef.canAdd());
    assertTrue("Update flag is wrong", loginDef.canModify());
    PrismPropertyDefinition passwdDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "password"));
    assertEquals(new QName(SCHEMA_NS, "password"), passwdDef.getName());
    assertEquals(ProtectedStringType.COMPLEX_TYPE, passwdDef.getTypeName());
    PrismPropertyDefinition credDef = newContainerDef.findPropertyDefinition(new QName(SchemaConstants.NS_C, "credentials"));
    assertEquals(new QName(SchemaConstants.NS_C, "credentials"), credDef.getName());
    assertEquals(new QName(SchemaConstants.NS_C, "CredentialsType"), credDef.getTypeName());
    PrismPropertyDefinition countDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "counter"));
    assertEquals(new QName(SCHEMA_NS, "counter"), countDef.getName());
    assertEquals(DOMUtil.XSD_INT, countDef.getTypeName());
    assertTrue("Read flag is wrong", countDef.canRead());
    assertFalse("Create flag is wrong", countDef.canAdd());
    assertFalse("Update flag is wrong", countDef.canModify());
}
Also used : PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) PrismSchemaImpl(com.evolveum.midpoint.prism.schema.PrismSchemaImpl) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) PrismContainerDefinitionImpl(com.evolveum.midpoint.prism.PrismContainerDefinitionImpl) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) Document(org.w3c.dom.Document) Test(org.testng.annotations.Test)

Aggregations

PrismContainerDefinition (com.evolveum.midpoint.prism.PrismContainerDefinition)1 PrismContainerDefinitionImpl (com.evolveum.midpoint.prism.PrismContainerDefinitionImpl)1 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)1 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)1 PrismSchema (com.evolveum.midpoint.prism.schema.PrismSchema)1 PrismSchemaImpl (com.evolveum.midpoint.prism.schema.PrismSchemaImpl)1 QName (javax.xml.namespace.QName)1 Test (org.testng.annotations.Test)1 Document (org.w3c.dom.Document)1