Search in sources :

Example 11 with OValidationException

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

the class GraphValidationTest method testNoTxGraphConstraints.

@Test
public void testNoTxGraphConstraints() {
    OrientGraphNoTx graphNoTx = new OrientGraphNoTx(URL);
    OrientVertexType testType = graphNoTx.createVertexType("Test");
    testType.createProperty("age", OType.INTEGER).setMax("3");
    OrientVertex vert1 = graphNoTx.addVertex("class:Test", "age", 2);
    try {
        vert1.setProperty("age", 4);
    } catch (OValidationException e) {
        //this fails
        Assert.assertEquals(vert1.getProperty("age"), 2);
    } finally {
        graphNoTx.shutdown();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) Test(org.junit.Test)

Example 12 with OValidationException

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

the class CRUDDocumentValidationTest method validationDisabledAdDatabaseLevel.

@Test(dependsOnMethods = "validationMandatoryNullableNoCloseDb")
public void validationDisabledAdDatabaseLevel() throws ParseException {
    database.getMetadata().reload();
    try {
        ODocument doc = new ODocument("MyTestClass");
        doc.save();
        Assert.fail();
    } catch (OValidationException e) {
    }
    database.command(new OCommandSQL("ALTER DATABASE " + ODatabase.ATTRIBUTES.VALIDATION.name() + " FALSE")).execute();
    database.setValidationEnabled(false);
    try {
        ODocument doc = new ODocument("MyTestClass");
        doc.save();
        doc.delete();
    } finally {
        database.setValidationEnabled(true);
        database.command(new OCommandSQL("ALTER DATABASE " + ODatabase.ATTRIBUTES.VALIDATION.name() + " TRUE")).execute();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 13 with OValidationException

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

the class OSchedulerTrigger method onRecordBeforeUpdate.

@Override
public RESULT onRecordBeforeUpdate(final ODocument iDocument) {
    try {
        final String schedulerName = iDocument.field(OScheduledEvent.PROP_NAME);
        OScheduledEvent event = database.getMetadata().getScheduler().getEvent(schedulerName);
        if (event != null) {
            // UPDATED EVENT
            final Set<String> dirtyFields = new HashSet<String>(Arrays.asList(iDocument.getDirtyFields()));
            if (dirtyFields.contains(OScheduledEvent.PROP_NAME))
                throw new OValidationException("Scheduled event cannot change name");
            if (dirtyFields.contains(OScheduledEvent.PROP_RULE)) {
                // RULE CHANGED, STOP CURRENT EVENT AND RESCHEDULE IT
                database.getMetadata().getScheduler().updateEvent(new OScheduledEvent(iDocument));
            } else {
                iDocument.field(OScheduledEvent.PROP_STATUS, STATUS.STOPPED.name());
                event.fromStream(iDocument);
            }
            return RESULT.RECORD_CHANGED;
        }
    } catch (Exception ex) {
        OLogManager.instance().error(this, "Error on updating scheduled event", ex);
    }
    return RESULT.RECORD_NOT_CHANGED;
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OValidationException(com.orientechnologies.orient.core.exception.OValidationException) HashSet(java.util.HashSet)

Example 14 with OValidationException

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

the class ORecordSerializerBinaryV0 method serializeValue.

@SuppressWarnings("unchecked")
public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
    int pointer = 0;
    switch(type) {
        case INTEGER:
        case LONG:
        case SHORT:
            pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            break;
        case STRING:
            pointer = writeString(bytes, value.toString());
            break;
        case DOUBLE:
            long dg = Double.doubleToLongBits(((Number) value).doubleValue());
            pointer = bytes.alloc(OLongSerializer.LONG_SIZE);
            OLongSerializer.INSTANCE.serializeLiteral(dg, bytes.bytes, pointer);
            break;
        case FLOAT:
            int fg = Float.floatToIntBits(((Number) value).floatValue());
            pointer = bytes.alloc(OIntegerSerializer.INT_SIZE);
            OIntegerSerializer.INSTANCE.serializeLiteral(fg, bytes.bytes, pointer);
            break;
        case BYTE:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Number) value).byteValue();
            break;
        case BOOLEAN:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Boolean) value) ? (byte) 1 : (byte) 0;
            break;
        case DATETIME:
            if (value instanceof Number) {
                pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            } else
                pointer = OVarIntSerializer.write(bytes, ((Date) value).getTime());
            break;
        case DATE:
            long dateValue;
            if (value instanceof Number) {
                dateValue = ((Number) value).longValue();
            } else
                dateValue = ((Date) value).getTime();
            dateValue = convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), dateValue);
            pointer = OVarIntSerializer.write(bytes, dateValue / MILLISEC_PER_DAY);
            break;
        case EMBEDDED:
            pointer = bytes.offset;
            if (value instanceof ODocumentSerializable) {
                ODocument cur = ((ODocumentSerializable) value).toDocument();
                cur.field(ODocumentSerializable.CLASS_NAME, value.getClass().getName());
                serialize(cur, bytes, false);
            } else {
                serialize((ODocument) value, bytes, false);
            }
            break;
        case EMBEDDEDSET:
        case EMBEDDEDLIST:
            if (value.getClass().isArray())
                pointer = writeEmbeddedCollection(bytes, Arrays.asList(OMultiValue.array(value)), linkedType);
            else
                pointer = writeEmbeddedCollection(bytes, (Collection<?>) value, linkedType);
            break;
        case DECIMAL:
            BigDecimal decimalValue = (BigDecimal) value;
            pointer = bytes.alloc(ODecimalSerializer.INSTANCE.getObjectSize(decimalValue));
            ODecimalSerializer.INSTANCE.serialize(decimalValue, bytes.bytes, pointer);
            break;
        case BINARY:
            pointer = writeBinary(bytes, (byte[]) (value));
            break;
        case LINKSET:
        case LINKLIST:
            Collection<OIdentifiable> ridCollection = (Collection<OIdentifiable>) value;
            pointer = writeLinkCollection(bytes, ridCollection);
            break;
        case LINK:
            if (!(value instanceof OIdentifiable))
                throw new OValidationException("Value '" + value + "' is not a OIdentifiable");
            pointer = writeOptimizedLink(bytes, (OIdentifiable) value);
            break;
        case LINKMAP:
            pointer = writeLinkMap(bytes, (Map<Object, OIdentifiable>) value);
            break;
        case EMBEDDEDMAP:
            pointer = writeEmbeddedMap(bytes, (Map<Object, Object>) value);
            break;
        case LINKBAG:
            pointer = ((ORidBag) value).toStream(bytes);
            break;
        case CUSTOM:
            if (!(value instanceof OSerializableStream))
                value = new OSerializableWrapper((Serializable) value);
            pointer = writeString(bytes, value.getClass().getName());
            writeBinary(bytes, ((OSerializableStream) value).toStream());
            break;
        case TRANSIENT:
            break;
        case ANY:
            break;
    }
    return pointer;
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) BigDecimal(java.math.BigDecimal) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 15 with OValidationException

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

the class HookChangeValidationTest method testHookUpdateChange.

@Test
public void testHookUpdateChange() {
    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 {
            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)

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