use of com.orientechnologies.orient.core.hook.ODocumentHookAbstract in project wicket-orientdb by OrienteerBAP.
the class TestInAppOrientDBCompatibility method testInHook.
@Test
public void testInHook() throws Exception {
ODatabaseDocument db = wicket.getTester().getDatabase();
OSchema schema = db.getMetadata().getSchema();
OClass oClass = schema.createClass("TestInHook");
oClass.createProperty("a", OType.INTEGER);
oClass.createProperty("b", OType.INTEGER);
oClass.createProperty("c", OType.INTEGER);
ODocument doc = new ODocument(oClass);
doc.field("a", 2);
doc.field("b", 2);
doc.save();
doc.reload();
assertEquals(2, (Object) doc.field("a"));
assertEquals(2, (Object) doc.field("b"));
assertNull(doc.field("c"));
db.registerHook(new ODocumentHookAbstract(db) {
{
setIncludeClasses("TestInHook");
}
@Override
public void onRecordAfterCreate(ODocument iDocument) {
onRecordAfterRead(iDocument);
}
@Override
public void onRecordAfterRead(ODocument iDocument) {
String script = "select sum(a, b) as value from " + iDocument.getIdentity();
List<ODocument> calculated = database.query(new OSQLSynchQuery<Object>(script));
if (calculated != null && !calculated.isEmpty()) {
iDocument.field("c", (Object) calculated.get(0).field("value"));
}
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
}
});
doc.reload();
assertEquals(2, (Object) doc.field("a"));
assertEquals(2, (Object) doc.field("b"));
assertEquals(4, (Object) doc.field("c"));
doc = new ODocument(oClass);
doc.field("a", 3);
doc.field("b", 3);
doc.save();
assertEquals(3, (Object) doc.field("a"));
assertEquals(3, (Object) doc.field("b"));
assertEquals(6, (Object) doc.field("c"));
}
use of com.orientechnologies.orient.core.hook.ODocumentHookAbstract in project orientdb by orientechnologies.
the class CheckHookCallCountTest method testInHook.
@Test
public void testInHook() throws Exception {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + CheckHookCallCountTest.class.getSimpleName());
db.create();
try {
OSchema schema = db.getMetadata().getSchema();
OClass oClass = schema.createClass("TestInHook");
oClass.createProperty("a", OType.INTEGER);
oClass.createProperty("b", OType.INTEGER);
oClass.createProperty("c", OType.INTEGER);
ODocument doc = new ODocument(oClass);
doc.field("a", 2);
doc.field("b", 2);
doc.save();
doc.reload();
assertEquals(2, doc.field("a"));
assertEquals(2, doc.field("b"));
assertNull(doc.field("c"));
db.registerHook(new ODocumentHookAbstract(db) {
{
setIncludeClasses("TestInHook");
}
@Override
public void onRecordAfterCreate(ODocument iDocument) {
onRecordAfterRead(iDocument);
}
@Override
public void onRecordAfterRead(ODocument iDocument) {
String script = "select sum(a, b) as value from " + iDocument.getIdentity();
List<ODocument> calculated = database.query(new OSQLSynchQuery<Object>(script));
if (calculated != null && !calculated.isEmpty()) {
iDocument.field("c", calculated.get(0).field("value"));
}
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
}
});
doc.reload();
assertEquals(2, doc.field("a"));
assertEquals(2, doc.field("b"));
assertEquals(4, doc.field("c"));
doc = new ODocument(oClass);
doc.field("a", 3);
doc.field("b", 3);
// FAILING here: infinite recursion
doc.save();
assertEquals(3, doc.field("a"));
assertEquals(3, doc.field("b"));
assertEquals(6, doc.field("c"));
} finally {
db.drop();
}
}
use of com.orientechnologies.orient.core.hook.ODocumentHookAbstract 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();
}
}
use of com.orientechnologies.orient.core.hook.ODocumentHookAbstract 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();
}
}
use of com.orientechnologies.orient.core.hook.ODocumentHookAbstract 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();
}
}
Aggregations