Search in sources :

Example 1 with OValidationException

use of com.orientechnologies.orient.core.exception.OValidationException in project orientdb by orientechnologies.

the class OSqlUpdateContentValidationTest method testReadOnlyValidation.

@Test
public void testReadOnlyValidation() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:" + OSqlUpdateContentValidationTest.class.getSimpleName());
    db.create();
    OClass clazz = db.getMetadata().getSchema().createClass("Test");
    clazz.createProperty("testNormal", OType.STRING);
    clazz.createProperty("test", OType.STRING).setReadonly(true);
    OIdentifiable res = db.command(new OCommandSQL("insert into Test content {\"testNormal\":\"hello\",\"test\":\"only read\"} ")).execute();
    try {
        db.command(new OCommandSQL("update " + res.getIdentity() + " CONTENT {\"testNormal\":\"by\"}")).execute();
        Assert.fail("Error on update of a record removing a readonly property");
    } catch (OValidationException val) {
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Test(org.testng.annotations.Test)

Example 2 with OValidationException

use of com.orientechnologies.orient.core.exception.OValidationException in project orientdb by orientechnologies.

the class HookChangeValidationTest method testHookUpdateChangeTx.

@Test
public void testHookUpdateChangeTx() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        classA.createProperty("property1", OType.STRING).setNotNull(true);
        classA.createProperty("property2", OType.STRING).setReadonly(true);
        classA.createProperty("property3", OType.STRING).setMandatory(true);
        db.registerHook(new ODocumentHookAbstract() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        doc.save();
        assertEquals("value1-create", doc.field("property1"));
        assertEquals("value2-create", doc.field("property2"));
        assertEquals("value3-create", doc.field("property3"));
        doc.field("property1", "value1-update");
        doc.field("property2", "value2-update");
        try {
            db.begin();
            doc.save();
            db.commit();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 3 with OValidationException

use of com.orientechnologies.orient.core.exception.OValidationException in project orientdb by orientechnologies.

the class HookChangeValidationTest method testHookCreateChangeTx.

@Test
public void testHookCreateChangeTx() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        classA.createProperty("property1", OType.STRING).setNotNull(true);
        classA.createProperty("property2", OType.STRING).setReadonly(true);
        classA.createProperty("property3", OType.STRING).setMandatory(true);
        db.registerHook(new ODocumentHookAbstract() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        try {
            db.begin();
            doc.save();
            db.commit();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 4 with OValidationException

use of com.orientechnologies.orient.core.exception.OValidationException in project orientdb by orientechnologies.

the class HookChangeValidationTest method testHookCreateChange.

@Test
public void testHookCreateChange() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        classA.createProperty("property1", OType.STRING).setNotNull(true);
        classA.createProperty("property2", OType.STRING).setReadonly(true);
        classA.createProperty("property3", OType.STRING).setMandatory(true);
        db.registerHook(new ODocumentHookAbstract() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        try {
            doc.save();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 5 with OValidationException

use of com.orientechnologies.orient.core.exception.OValidationException in project orientdb by orientechnologies.

the class ServerClusterSchemaTest method executeTest.

@Override
protected void executeTest() throws Exception {
    for (int s = 0; s < SERVERS; ++s) {
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            System.out.println("Creating vertex class Client" + s + " against server " + g + "...");
            OrientVertexType t = g.createVertexType("Client" + s);
            t.createProperty("name", OType.STRING).setMandatory(true);
            System.out.println("Creating vertex class Knows" + s + " against server " + g + "...");
            g.createEdgeType("Knows" + s);
        } finally {
            g.shutdown();
        }
    }
    for (int s = 0; s < SERVERS; ++s) {
        System.out.println("Checking vertices classes on server " + s + "...");
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            for (int i = 0; i < SERVERS; ++i) {
                Assert.assertNotNull(g.getVertexType("Client" + i));
                Assert.assertNotNull(g.getEdgeType("Knows" + i));
            }
        } finally {
            g.shutdown();
        }
    }
    for (int s = 0; s < SERVERS; ++s) {
        System.out.println("Add vertices on server " + s + "...");
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            for (int i = 0; i < SERVERS; ++i) {
                try {
                    final OrientVertex v = g.addVertex("class:" + "Client" + i);
                    Assert.assertTrue(false);
                } catch (OValidationException e) {
                // EXPECTED
                }
            }
        } finally {
            g.shutdown();
        }
    }
    for (int s = 0; s < SERVERS; ++s) {
        System.out.println("Add vertices in TX on server " + s + "...");
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraph g = factory.getTx();
        try {
            for (int i = 0; i < SERVERS; ++i) {
                try {
                    final OrientVertex v = g.addVertex("class:" + "Client" + i);
                    g.commit();
                    Assert.assertTrue(false);
                } catch (ONeedRetryException e) {
                // EXPECTED
                } catch (OValidationException e) {
                // EXPECTED
                }
            }
        } finally {
            g.shutdown();
        }
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex)

Aggregations

OValidationException (com.orientechnologies.orient.core.exception.OValidationException)17 Test (org.testng.annotations.Test)11 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)10 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)10 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)9 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 ODocumentHookAbstract (com.orientechnologies.orient.core.hook.ODocumentHookAbstract)4 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)2 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)2 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)2 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)2 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)2 OrientVertexType (com.tinkerpop.blueprints.impls.orient.OrientVertexType)2 BigDecimal (java.math.BigDecimal)2 Test (org.junit.Test)2 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)1