Search in sources :

Example 1 with PrismSchemaImpl

use of com.evolveum.midpoint.prism.impl.schema.PrismSchemaImpl in project midpoint by Evolveum.

the class SchemaProcessorTest method testRoundTripGeneric.

@Test
public void testRoundTripGeneric() throws SchemaException {
    // GIVEN
    PrismSchemaImpl schema = new PrismSchemaImpl(MidPointConstants.NS_RI);
    // Ordinary property
    schema.createPropertyDefinition("number1", DOMUtil.XSD_INT);
    // Property container
    MutablePrismContainerDefinition<?> containerDefinition = schema.createContainerDefinition("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);
    counterProperty.toMutable().toMutable().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 ItemName(MidPointConstants.NS_RI, "number1"), PrismPropertyDefinition.class);
    assertEquals(new ItemName(MidPointConstants.NS_RI, "number1"), number1def.getItemName());
    assertEquals(DOMUtil.XSD_INT, number1def.getTypeName());
    PrismContainerDefinition newContainerDef = schema.findContainerDefinitionByType(new ItemName(MidPointConstants.NS_RI, "ContainerType"));
    assertEquals(new ItemName(MidPointConstants.NS_RI, "ContainerType"), newContainerDef.getTypeName());
    PrismPropertyDefinition loginDef = newContainerDef.findPropertyDefinition(new ItemName(MidPointConstants.NS_RI, "login"));
    assertEquals(new ItemName(MidPointConstants.NS_RI, "login"), loginDef.getItemName());
    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 ItemName(MidPointConstants.NS_RI, "password"));
    assertEquals(new ItemName(MidPointConstants.NS_RI, "password"), passwdDef.getItemName());
    assertEquals(ProtectedStringType.COMPLEX_TYPE, passwdDef.getTypeName());
    PrismPropertyDefinition credDef = newContainerDef.findPropertyDefinition(new ItemName(SchemaConstants.NS_C, "credentials"));
    assertEquals(new ItemName(SchemaConstants.NS_C, "credentials"), credDef.getItemName());
    assertEquals(new ItemName(SchemaConstants.NS_C, "CredentialsType"), credDef.getTypeName());
    PrismPropertyDefinition countDef = newContainerDef.findPropertyDefinition(new ItemName(MidPointConstants.NS_RI, "counter"));
    assertEquals(new ItemName(MidPointConstants.NS_RI, "counter"), countDef.getItemName());
    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.impl.schema.PrismSchemaImpl) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ItemName(com.evolveum.midpoint.prism.path.ItemName) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) MutablePrismContainerDefinition(com.evolveum.midpoint.prism.MutablePrismContainerDefinition) Document(org.w3c.dom.Document) Test(org.testng.annotations.Test) AbstractSchemaTest(com.evolveum.midpoint.schema.AbstractSchemaTest)

Aggregations

MutablePrismContainerDefinition (com.evolveum.midpoint.prism.MutablePrismContainerDefinition)1 PrismContainerDefinition (com.evolveum.midpoint.prism.PrismContainerDefinition)1 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)1 PrismSchemaImpl (com.evolveum.midpoint.prism.impl.schema.PrismSchemaImpl)1 ItemName (com.evolveum.midpoint.prism.path.ItemName)1 PrismSchema (com.evolveum.midpoint.prism.schema.PrismSchema)1 AbstractSchemaTest (com.evolveum.midpoint.schema.AbstractSchemaTest)1 Test (org.testng.annotations.Test)1 Document (org.w3c.dom.Document)1