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();
}
}
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();
}
}
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;
}
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;
}
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();
}
}
Aggregations