Search in sources :

Example 6 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class ODocumentValidationTest method testMaxValidation.

@Test
public void testMaxValidation() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
    db.create();
    try {
        OClass clazz = db.getMetadata().getSchema().createClass("Validation");
        clazz.createProperty("int", OType.INTEGER).setMax("11");
        clazz.createProperty("long", OType.LONG).setMax("11");
        clazz.createProperty("float", OType.FLOAT).setMax("11");
        // clazz.createProperty("boolean", OType.BOOLEAN) no meaning
        clazz.createProperty("binary", OType.BINARY).setMax("11");
        clazz.createProperty("byte", OType.BYTE).setMax("11");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, cal.get(Calendar.HOUR) == 11 ? 0 : 1);
        SimpleDateFormat format = ((ODatabaseDocumentTx) db).getStorage().getConfiguration().getDateFormatInstance();
        clazz.createProperty("date", OType.DATE).setMax(format.format(cal.getTime()));
        cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, 1);
        format = ((ODatabaseDocumentTx) db).getStorage().getConfiguration().getDateTimeFormatInstance();
        clazz.createProperty("datetime", OType.DATETIME).setMax(format.format(cal.getTime()));
        clazz.createProperty("decimal", OType.DECIMAL).setMax("11");
        clazz.createProperty("double", OType.DOUBLE).setMax("11");
        clazz.createProperty("short", OType.SHORT).setMax("11");
        clazz.createProperty("string", OType.STRING).setMax("11");
        // clazz.createProperty("link", OType.LINK) no meaning
        // clazz.createProperty("embedded", OType.EMBEDDED) no meaning
        clazz.createProperty("embeddedList", OType.EMBEDDEDLIST).setMax("2");
        clazz.createProperty("embeddedSet", OType.EMBEDDEDSET).setMax("2");
        clazz.createProperty("embeddedMap", OType.EMBEDDEDMAP).setMax("2");
        clazz.createProperty("linkList", OType.LINKLIST).setMax("2");
        clazz.createProperty("linkSet", OType.LINKSET).setMax("2");
        clazz.createProperty("linkMap", OType.LINKMAP).setMax("2");
        ODocument d = new ODocument(clazz);
        d.field("int", 11);
        d.field("long", 11);
        d.field("float", 11);
        d.field("binary", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
        d.field("byte", 11);
        d.field("date", new Date());
        d.field("datetime", new Date());
        d.field("decimal", 10);
        d.field("double", 10);
        d.field("short", 10);
        d.field("string", "yeah");
        d.field("embeddedList", Arrays.asList("a", "b"));
        d.field("embeddedSet", new HashSet<String>(Arrays.asList("a", "b")));
        HashMap<String, String> cont = new HashMap<String, String>();
        cont.put("one", "one");
        cont.put("two", "one");
        d.field("embeddedMap", cont);
        d.field("linkList", Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 34)));
        d.field("linkSet", new HashSet<ORecordId>(Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 31))));
        HashMap<String, ORecordId> cont1 = new HashMap<String, ORecordId>();
        cont1.put("one", new ORecordId(30, 30));
        cont1.put("two", new ORecordId(30, 30));
        d.field("linkMap", cont1);
        d.validate();
        checkField(d, "int", 12);
        checkField(d, "long", 12);
        checkField(d, "float", 20);
        checkField(d, "binary", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 });
        checkField(d, "byte", 20);
        cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        checkField(d, "date", cal.getTime());
        checkField(d, "datetime", cal.getTime());
        checkField(d, "decimal", 20);
        checkField(d, "double", 20);
        checkField(d, "short", 20);
        checkField(d, "string", "0123456789101112");
        checkField(d, "embeddedList", Arrays.asList("a", "b", "d"));
        checkField(d, "embeddedSet", new HashSet<String>(Arrays.asList("a", "b", "d")));
        HashMap<String, String> con1 = new HashMap<String, String>();
        con1.put("one", "one");
        con1.put("two", "one");
        con1.put("three", "one");
        checkField(d, "embeddedMap", con1);
        checkField(d, "linkList", Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 33), new ORecordId(40, 31)));
        checkField(d, "linkSet", new HashSet<ORecordId>(Arrays.asList(new ORecordId(40, 30), new ORecordId(40, 33), new ORecordId(40, 31))));
        HashMap<String, ORecordId> cont3 = new HashMap<String, ORecordId>();
        cont3.put("one", new ORecordId(30, 30));
        cont3.put("two", new ORecordId(30, 30));
        cont3.put("three", new ORecordId(30, 30));
        checkField(d, "linkMap", cont3);
    } finally {
        db.drop();
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.testng.annotations.Test)

Example 7 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class ODocumentValidationTest method testValidationNotValidEmbeddedMap.

@Test
public void testValidationNotValidEmbeddedMap() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
    db.create();
    try {
        OClass embeddedClazz = db.getMetadata().getSchema().createClass("EmbeddedValidation");
        embeddedClazz.createProperty("int", OType.INTEGER).setMandatory(true);
        embeddedClazz.createProperty("long", OType.LONG).setMandatory(true);
        OClass clazz = db.getMetadata().getSchema().createClass("Validation");
        clazz.createProperty("int", OType.INTEGER).setMandatory(true);
        clazz.createProperty("long", OType.LONG).setMandatory(true);
        clazz.createProperty("embeddedMap", OType.EMBEDDEDMAP, embeddedClazz).setMandatory(true);
        ODocument d = new ODocument(clazz);
        d.field("int", 30);
        d.field("long", 30);
        final Map<String, ODocument> embeddedMap = new HashMap<String, ODocument>();
        d.field("embeddedMap", embeddedMap);
        ODocument embeddedInMap = new ODocument("EmbeddedValidation");
        embeddedInMap.field("int", 30);
        embeddedInMap.field("long", 30);
        embeddedMap.put("1", embeddedInMap);
        ODocument embeddedInMap2 = new ODocument("EmbeddedValidation");
        embeddedInMap2.field("int", 30);
        embeddedMap.put("2", embeddedInMap2);
        try {
            d.validate();
            Assert.fail("Validation doesn't throw exception");
        } catch (OValidationException e) {
            Assert.assertTrue(e.toString().contains("EmbeddedValidation.long"));
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Example 8 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class ODocumentValidationTest method testRegExpValidation.

@Test
public void testRegExpValidation() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
    db.create();
    try {
        OClass clazz = db.getMetadata().getSchema().createClass("Validation");
        clazz.createProperty("string", OType.STRING).setRegexp("[^Z]*");
        ODocument d = new ODocument(clazz);
        d.field("string", "yeah");
        d.validate();
        checkField(d, "string", "yaZah");
    } finally {
        db.drop();
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Test(org.testng.annotations.Test)

Example 9 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class ODocumentValidationTest method testValidLinkCollectionsUpdate.

@Test
public void testValidLinkCollectionsUpdate() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
    db.create();
    try {
        OClass clazz = db.getMetadata().getSchema().createClass("Validation");
        OClass clazz1 = db.getMetadata().getSchema().createClass("Validation1");
        clazz.createProperty("linkList", OType.LINKLIST).setLinkedClass(clazz1);
        clazz.createProperty("linkSet", OType.LINKSET).setLinkedClass(clazz1);
        clazz.createProperty("linkMap", OType.LINKMAP).setLinkedClass(clazz1);
        clazz.createProperty("linkBag", OType.LINKBAG).setLinkedClass(clazz1);
        ODocument d = new ODocument(clazz);
        d.field("link", new ODocument(clazz1));
        d.field("embedded", new ODocument(clazz1));
        List<ODocument> list = Arrays.asList(new ODocument(clazz1));
        d.field("linkList", list);
        Set<ODocument> set = new HashSet<ODocument>(list);
        d.field("linkSet", set);
        d.field("linkBag", new ORidBag());
        Map<String, ODocument> map = new HashMap<String, ODocument>();
        map.put("a", new ODocument(clazz1));
        d.field("linkMap", map);
        db.save(d);
        try {
            ODocument newD = d.copy();
            ((Collection) newD.field("linkList")).add(new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
        try {
            ODocument newD = d.copy();
            ((Collection) newD.field("linkSet")).add(new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
        try {
            ODocument newD = d.copy();
            ((ORidBag) newD.field("linkBag")).add(new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
        try {
            ODocument newD = d.copy();
            ((Map<String, ODocument>) newD.field("linkMap")).put("a", new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Test(org.testng.annotations.Test)

Example 10 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class ODocumentValidationTest method testRequiredValidation.

@Test
public void testRequiredValidation() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
    db.create();
    try {
        ODocument doc = new ODocument();
        OIdentifiable id = db.save(doc).getIdentity();
        OClass embeddedClazz = db.getMetadata().getSchema().createClass("EmbeddedValidation");
        embeddedClazz.createProperty("int", OType.INTEGER).setMandatory(true);
        OClass clazz = db.getMetadata().getSchema().createClass("Validation");
        clazz.createProperty("int", OType.INTEGER).setMandatory(true);
        clazz.createProperty("long", OType.LONG).setMandatory(true);
        clazz.createProperty("float", OType.FLOAT).setMandatory(true);
        clazz.createProperty("boolean", OType.BOOLEAN).setMandatory(true);
        clazz.createProperty("binary", OType.BINARY).setMandatory(true);
        clazz.createProperty("byte", OType.BYTE).setMandatory(true);
        clazz.createProperty("date", OType.DATE).setMandatory(true);
        clazz.createProperty("datetime", OType.DATETIME).setMandatory(true);
        clazz.createProperty("decimal", OType.DECIMAL).setMandatory(true);
        clazz.createProperty("double", OType.DOUBLE).setMandatory(true);
        clazz.createProperty("short", OType.SHORT).setMandatory(true);
        clazz.createProperty("string", OType.STRING).setMandatory(true);
        clazz.createProperty("link", OType.LINK).setMandatory(true);
        clazz.createProperty("embedded", OType.EMBEDDED, embeddedClazz).setMandatory(true);
        clazz.createProperty("embeddedListNoClass", OType.EMBEDDEDLIST).setMandatory(true);
        clazz.createProperty("embeddedSetNoClass", OType.EMBEDDEDSET).setMandatory(true);
        clazz.createProperty("embeddedMapNoClass", OType.EMBEDDEDMAP).setMandatory(true);
        clazz.createProperty("embeddedList", OType.EMBEDDEDLIST, embeddedClazz).setMandatory(true);
        clazz.createProperty("embeddedSet", OType.EMBEDDEDSET, embeddedClazz).setMandatory(true);
        clazz.createProperty("embeddedMap", OType.EMBEDDEDMAP, embeddedClazz).setMandatory(true);
        clazz.createProperty("linkList", OType.LINKLIST).setMandatory(true);
        clazz.createProperty("linkSet", OType.LINKSET).setMandatory(true);
        clazz.createProperty("linkMap", OType.LINKMAP).setMandatory(true);
        ODocument d = new ODocument(clazz);
        d.field("int", 10);
        d.field("long", 10);
        d.field("float", 10);
        d.field("boolean", 10);
        d.field("binary", new byte[] {});
        d.field("byte", 10);
        d.field("date", new Date());
        d.field("datetime", new Date());
        d.field("decimal", 10);
        d.field("double", 10);
        d.field("short", 10);
        d.field("string", "yeah");
        d.field("link", id);
        d.field("linkList", new ArrayList<ORecordId>());
        d.field("linkSet", new HashSet<ORecordId>());
        d.field("linkMap", new HashMap<String, ORecordId>());
        d.field("embeddedListNoClass", new ArrayList<ORecordId>());
        d.field("embeddedSetNoClass", new HashSet<ORecordId>());
        d.field("embeddedMapNoClass", new HashMap<String, ORecordId>());
        ODocument embedded = new ODocument("EmbeddedValidation");
        embedded.field("int", 20);
        embedded.field("long", 20);
        d.field("embedded", embedded);
        ODocument embeddedInList = new ODocument("EmbeddedValidation");
        embeddedInList.field("int", 30);
        embeddedInList.field("long", 30);
        final ArrayList<ODocument> embeddedList = new ArrayList<ODocument>();
        embeddedList.add(embeddedInList);
        d.field("embeddedList", embeddedList);
        ODocument embeddedInSet = new ODocument("EmbeddedValidation");
        embeddedInSet.field("int", 30);
        embeddedInSet.field("long", 30);
        final Set<ODocument> embeddedSet = new HashSet<ODocument>();
        embeddedSet.add(embeddedInSet);
        d.field("embeddedSet", embeddedSet);
        ODocument embeddedInMap = new ODocument("EmbeddedValidation");
        embeddedInMap.field("int", 30);
        embeddedInMap.field("long", 30);
        final Map<String, ODocument> embeddedMap = new HashMap<String, ODocument>();
        embeddedMap.put("testEmbedded", embeddedInMap);
        d.field("embeddedMap", embeddedMap);
        d.validate();
        checkRequireField(d, "int");
        checkRequireField(d, "long");
        checkRequireField(d, "float");
        checkRequireField(d, "boolean");
        checkRequireField(d, "binary");
        checkRequireField(d, "byte");
        checkRequireField(d, "date");
        checkRequireField(d, "datetime");
        checkRequireField(d, "decimal");
        checkRequireField(d, "double");
        checkRequireField(d, "short");
        checkRequireField(d, "string");
        checkRequireField(d, "link");
        checkRequireField(d, "embedded");
        checkRequireField(d, "embeddedList");
        checkRequireField(d, "embeddedSet");
        checkRequireField(d, "embeddedMap");
        checkRequireField(d, "linkList");
        checkRequireField(d, "linkSet");
        checkRequireField(d, "linkMap");
    } finally {
        db.drop();
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Test(org.testng.annotations.Test)

Aggregations

ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)590 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)257 Test (org.testng.annotations.Test)182 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)120 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)81 Test (org.junit.Test)79 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)61 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)47 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)46 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)41 ORID (com.orientechnologies.orient.core.id.ORID)39 ORecordId (com.orientechnologies.orient.core.id.ORecordId)34 Before (org.junit.Before)34 File (java.io.File)33 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)25 ArrayList (java.util.ArrayList)25 BeforeClass (org.testng.annotations.BeforeClass)23 BeforeMethod (org.testng.annotations.BeforeMethod)23 OStorage (com.orientechnologies.orient.core.storage.OStorage)21 List (java.util.List)20