BAEL-1216: Object API + code formating

This commit is contained in:
Dassi Orleando 2017-12-26 18:14:48 +01:00
parent ac890d6ce4
commit dc189801a6
5 changed files with 130 additions and 35 deletions

View File

@ -9,7 +9,7 @@ This is a simple maven project that shows how to do OrientDB operations using th
- Java 7 or higher - Java 7 or higher
- OrientDB - OrientDB
### Running ### Build
To build and start the server simply type To build and start the server simply type
@ -17,20 +17,10 @@ To build and start the server simply type
$ mvn clean install $ mvn clean install
``` ```
### Test Db Operation ### Tests
You can see what crud operation are available using curl: To run unit tests
```bash ```bash
$ curl localhost:8080 $ mvn test
``` ```
You can view existing student objects with this command:
```bash
$ curl localhost:8080/articles
```
Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080)
Enjoy it :)

View File

@ -0,0 +1,54 @@
package com.baeldung.orientdb;
import javax.persistence.Id;
public class Author {
@Id
private Object id;
private String firstName;
private String lastName;
private int level;
public Author() {
}
public Author(String firstName, String lastName, int level) {
this.firstName = firstName;
this.lastName = lastName;
this.level = level;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
@java.lang.Override
public java.lang.String toString() {
return "Author{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", level=" + level +
'}';
}
}

View File

@ -1,20 +1,15 @@
package com.baeldung.orientdb; package com.baeldung.orientdb;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Vertex; import org.junit.AfterClass;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import org.junit.BeforeClass;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType; import org.junit.Test;
import org.junit.*;
import java.util.List; import java.util.List;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertNotEquals;
public class OrientDBDocumentAPITest { public class OrientDBDocumentAPITest {
private static ODatabaseDocumentTx db = null; private static ODatabaseDocumentTx db = null;
@ -28,32 +23,32 @@ public class OrientDBDocumentAPITest {
@Test @Test
public void givenDB_whenSavingDocument_thenClassIsAutoCreated() { public void givenDB_whenSavingDocument_thenClassIsAutoCreated() {
ODocument author = new ODocument("Author"); ODocument author = new ODocument("Author");
author.field( "firstName", "Paul" ); author.field("firstName", "Paul");
author.field( "lastName", "Smith" ); author.field("lastName", "Smith");
author.field( "country", "USA" ); author.field("country", "USA");
author.field( "publicProfile", false ); author.field("publicProfile", false);
author.field( "level", 7 ); author.field("level", 7);
author.save(); author.save();
assertEquals("Author", author.getSchemaClass().getName()); assertEquals("Author", author.getSchemaClass().getName());
} }
@Test @Test
public void givenAnEmptyDB_afterSavingTwoAuthors_thenWeGetAuthorsWithLevelSeven() { public void givenDB_whenSavingAuthors_thenWeGetOnesWithLevelSeven() {
for (ODocument author : db.browseClass("Author")) author.delete(); for (ODocument author : db.browseClass("Author")) author.delete();
ODocument authorOne = new ODocument("Author"); ODocument authorOne = new ODocument("Author");
authorOne.field( "firstName", "Leo" ); authorOne.field("firstName", "Leo");
authorOne.field( "level", 7 ); authorOne.field("level", 7);
authorOne.save(); authorOne.save();
ODocument authorTwo = new ODocument("Author"); ODocument authorTwo = new ODocument("Author");
authorTwo.field( "firstName", "Lucien" ); authorTwo.field("firstName", "Lucien");
authorTwo.field( "level", 9 ); authorTwo.field("level", 9);
authorTwo.save(); authorTwo.save();
List<ODocument> result = db.query( List<ODocument> result = db.query(
new OSQLSynchQuery<ODocument>("select * from Author where level = 7")); new OSQLSynchQuery<ODocument>("select * from Author where level = 7"));
assertEquals(1, result.size()); assertEquals(1, result.size());
} }

View File

@ -81,7 +81,7 @@ public class OrientDBGraphAPITest {
@Test @Test
public void givenBaeldungDB_getEditorWithLevelSeven() { public void givenBaeldungDB_getEditorWithLevelSeven() {
String onlyEditor = ""; String onlyEditor = "";
for( Vertex v : graph.getVertices("Editor.level", 7) ) { for(Vertex v : graph.getVertices("Editor.level", 7)) {
onlyEditor = v.getProperty("firstName").toString(); onlyEditor = v.getProperty("firstName").toString();
} }

View File

@ -0,0 +1,56 @@
package com.baeldung.orientdb;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.List;
import static junit.framework.Assert.assertEquals;
public class OrientDBObjectAPITest {
private static OObjectDatabaseTx db = null;
@BeforeClass
public static void setup() {
String orientDBFolder = System.getenv("ORIENTDB_HOME");
db = new OObjectDatabaseTx("plocal:" + orientDBFolder + "/databases/BaeldungDBThree").open("admin", "admin");
db.setSaveOnlyDirty(true);
db.getEntityManager().registerEntityClass(Author.class);
}
@Test
public void givenDB_whenSavingObject_thenHisIdExists() {
Author author = db.newInstance(Author.class);
author.setFirstName("Luke");
author.setLastName("Sky");
author.setLevel(9);
long authors = db.countClass(Author.class);
db.save(author);
}
@Test
public void givenDB_whenSavingAuthors_thenWeGetOnesWithLevelSeven() {
for (Author author : db.browseClass(Author.class)) db.delete(author);
Author authorOne = db.newInstance(Author.class, "Leo", "Marta", 7);
db.save(authorOne);
Author authorTwo = db.newInstance(Author.class, "Lucien", "Aurelien", 9);
db.save(authorTwo);
List<Author> result = db.query(
new OSQLSynchQuery<Author>("select * from Author where level = 7"));
assertEquals(1, result.size());
}
@AfterClass
public static void closeDB() {
db.close();
}
}