use of com.orientechnologies.orient.core.db.document.ODatabaseDocument in project orientdb by orientechnologies.
the class OCommandCacheTest method testCommandCacheConfiguration.
@Test
public void testCommandCacheConfiguration() {
OGlobalConfiguration.COMMAND_CACHE_ENABLED.setValue(true);
OGlobalConfiguration.COMMAND_CACHE_MIN_EXECUTION_TIME.setValue(1);
OGlobalConfiguration.COMMAND_CACHE_MAX_RESULSET_SIZE.setValue(10);
OGlobalConfiguration.COMMAND_CACHE_EVICT_STRATEGY.setValue(OCommandCache.STRATEGY.INVALIDATE_ALL);
String buildDirectory = System.getProperty("buildDirectory");
if (buildDirectory == null)
buildDirectory = ".";
String dbPath = "plocal:" + buildDirectory + File.separator + OCommandCacheTest.class.getSimpleName();
ODatabaseDocument db = new ODatabaseDocumentTx(dbPath);
db.create();
try {
File commandCacheCfg = new File(buildDirectory + File.separator + OCommandCacheTest.class.getSimpleName() + "/command-cache.json");
final String configurationContent = OIOUtils.readFileAsString(commandCacheCfg);
ODocument cfg = new ODocument().fromJSON(configurationContent);
Boolean enabled = cfg.field("enabled");
String evict = cfg.field("evictStrategy");
OCommandCache.STRATEGY evictStrategy = OCommandCache.STRATEGY.valueOf(evict);
int minExecutionTime = cfg.field("minExecutionTime");
int maxResultsetSize = cfg.field("maxResultsetSize");
Assert.assertEquals(enabled, OGlobalConfiguration.COMMAND_CACHE_ENABLED.getValue());
Assert.assertEquals(evictStrategy.toString(), OGlobalConfiguration.COMMAND_CACHE_EVICT_STRATEGY.getValue().toString());
Assert.assertEquals(minExecutionTime, OGlobalConfiguration.COMMAND_CACHE_MIN_EXECUTION_TIME.getValue());
Assert.assertEquals(maxResultsetSize, OGlobalConfiguration.COMMAND_CACHE_MAX_RESULSET_SIZE.getValue());
} catch (IOException e) {
Assert.fail("Cannot find file configuration", e);
} finally {
db.drop();
}
File f = new File(buildDirectory + File.separator + OCommandCacheTest.class.getSimpleName());
Assert.assertEquals(f.exists(), false);
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument 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.db.document.ODatabaseDocument 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.db.document.ODatabaseDocument 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();
}
}
use of com.orientechnologies.orient.core.db.document.ODatabaseDocument 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();
}
}
Aggregations