Search in sources :

Example 1 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestModels method testODocumentModel.

@Test
public void testODocumentModel() {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    ORecordId recordId = new ORecordId("#6:0");
    ODocumentModel model = new ODocumentModel(recordId);
    assertModelObjectEquals(recordId.getRecord(), model);
    recordId = new ORecordId("#6:1");
    model.setObject((ODocument) recordId.getRecord());
    assertModelObjectEquals(recordId.getRecord(), model);
    model.setObject(null);
    assertModelObjectEquals(null, model);
    // Test persistence between calls
    db.begin();
    ODocument doc = new ODocument("ClassA");
    doc.field("name", "test1");
    model = new ODocumentModel(doc);
    assertEquals("test1", model.getObject().field("name"));
    interupt(db, model);
    assertEquals("test1", model.getObject().field("name"));
    model.getObject().save();
    interupt(db, model);
    // Test that document actually reloaded from DB after closing transaction
    assertEquals("test1", model.getObject().field("name"));
    model.getObject().field("name", "test2");
    assertEquals("test2", model.getObject().field("name"));
    interupt(db, model);
    assertEquals("test1", model.getObject().field("name"));
    // Test autoSave feature
    model.getObject().field("name", "test3");
    model.setAutoSave(true);
    interupt(db, model);
    assertEquals("test3", model.getObject().field("name"));
}
Also used : ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocumentModel(ru.ydn.wicket.wicketorientdb.model.ODocumentModel) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 2 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class OrientDBSettingsTest method testInstallingHooks.

@Test
public void testInstallingHooks() {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    OClass clazz = db.getMetadata().getSchema().getClass("TestHooks");
    assertNotNull(clazz);
    try (OResultSet result = db.query("select from TestHooks")) {
        assertTrue(result.hasNext());
        ODocument doc = (ODocument) result.next().getElement().orElse(null);
        assertNotNull(doc);
        assertEquals("HOOK", doc.field("name"));
    }
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 3 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestRegisterHooks method testRegisterHooks.

@Test
public void testRegisterHooks() {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    IOrientDbSettings settings = wicket.getTester().getApplication().getOrientDbSettings();
    settings.addORecordHooks(RegisterHook.class);
    assertRegisteredHook(db, RegisterHook.class);
    db = wicket.getTester().getDatabaseSession();
    assertRegisteredHook(db, RegisterHook.class);
}
Also used : ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) Test(org.junit.Test)

Example 4 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    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();
            try (OResultSet result = database.query(script)) {
                if (result.hasNext()) {
                    iDocument.field("c", (Object) result.next().getProperty("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();
    doc.reload();
    assertEquals(3, (Object) doc.field("a"));
    assertEquals(3, (Object) doc.field("b"));
    assertEquals(6, (Object) doc.field("c"));
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 5 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testOFunctions.

@Test
public void testOFunctions() throws Exception {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    ODocument doc = new ODocument(OFunction.CLASS_NAME);
    doc.field("name", "testResurection");
    doc.field("language", "JavaScript");
    doc.field("idempotent", true);
    doc.save();
    ORID orid = doc.getIdentity();
    for (int i = 0; i < 10; i++) {
        db = wicket.getTester().getDatabaseSession();
        String signature = "signature" + RANDOM.nextLong();
        boolean isGoodCall = (i + 1) % 3 != 0;
        db.begin();
        doc = orid.getRecord();
        String code = isGoodCall ? "return \"" + signature + "\";" : "return nosuchvar;";
        doc.field("code", code);
        doc.save();
        db.commit();
        db.close();
        if (isGoodCall) {
            String result;
            for (int j = 0; j < 3; j++) {
                result = wicket.getTester().executeUrl("orientdb/function/db/testResurection", "GET", null);
                assertContains(signature, result);
            }
        } else {
            try {
                wicket.getTester().executeUrl("orientdb/function/db/testResurection", "GET", null);
                assertFalse("We should be there, because function should have 400 response", true);
            } catch (Exception e) {
            // NOP
            }
        }
    }
}
Also used : ORID(com.orientechnologies.orient.core.id.ORID) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Aggregations

ODatabaseSession (com.orientechnologies.orient.core.db.ODatabaseSession)31 Test (org.junit.Test)23 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)21 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)19 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)17 Ignore (org.junit.Ignore)10 ORID (com.orientechnologies.orient.core.id.ORID)6 ODocumentHookAbstract (com.orientechnologies.orient.core.hook.ODocumentHookAbstract)5 OResultSet (com.orientechnologies.orient.core.sql.executor.OResultSet)4 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OSchemaHelper (ru.ydn.wicket.wicketorientdb.utils.OSchemaHelper)2 Function (com.google.common.base.Function)1 Maps (com.google.common.collect.Maps)1 OResettable (com.orientechnologies.common.util.OResettable)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseRecordThreadLocal (com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal)1 OrientDB (com.orientechnologies.orient.core.db.OrientDB)1