From dc189801a61cff38601f17509f91c22f28653110 Mon Sep 17 00:00:00 2001 From: Dassi Orleando Date: Tue, 26 Dec 2017 18:14:48 +0100 Subject: [PATCH] BAEL-1216: Object API + code formating --- orientdb/README.md | 20 ++----- .../java/com/baeldung/orientdb/Author.java | 54 ++++++++++++++++++ .../orientdb/OrientDBDocumentAPITest.java | 33 +++++------ .../orientdb/OrientDBGraphAPITest.java | 2 +- .../orientdb/OrientDBObjectAPITest.java | 56 +++++++++++++++++++ 5 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 orientdb/src/main/java/com/baeldung/orientdb/Author.java create mode 100644 orientdb/src/test/java/com/baeldung/orientdb/OrientDBObjectAPITest.java diff --git a/orientdb/README.md b/orientdb/README.md index 97d217d13d..7827a33f76 100644 --- a/orientdb/README.md +++ b/orientdb/README.md @@ -9,7 +9,7 @@ This is a simple maven project that shows how to do OrientDB operations using th - Java 7 or higher - OrientDB -### Running +### Build To build and start the server simply type @@ -17,20 +17,10 @@ To build and start the server simply type $ mvn clean install ``` -### Test Db Operation +### Tests -You can see what crud operation are available using curl: +To run unit tests ```bash -$ curl localhost:8080 -``` -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 :) \ No newline at end of file +$ mvn test +``` \ No newline at end of file diff --git a/orientdb/src/main/java/com/baeldung/orientdb/Author.java b/orientdb/src/main/java/com/baeldung/orientdb/Author.java new file mode 100644 index 0000000000..8366fa301f --- /dev/null +++ b/orientdb/src/main/java/com/baeldung/orientdb/Author.java @@ -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 + + '}'; + } +} diff --git a/orientdb/src/test/java/com/baeldung/orientdb/OrientDBDocumentAPITest.java b/orientdb/src/test/java/com/baeldung/orientdb/OrientDBDocumentAPITest.java index 3a677955d1..e82f59b3e7 100644 --- a/orientdb/src/test/java/com/baeldung/orientdb/OrientDBDocumentAPITest.java +++ b/orientdb/src/test/java/com/baeldung/orientdb/OrientDBDocumentAPITest.java @@ -1,20 +1,15 @@ package com.baeldung.orientdb; 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.sql.query.OSQLSynchQuery; -import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; -import com.tinkerpop.blueprints.impls.orient.OrientVertexType; -import org.junit.*; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; import java.util.List; import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static org.junit.Assert.assertNotEquals; public class OrientDBDocumentAPITest { private static ODatabaseDocumentTx db = null; @@ -28,32 +23,32 @@ public class OrientDBDocumentAPITest { @Test public void givenDB_whenSavingDocument_thenClassIsAutoCreated() { ODocument author = new ODocument("Author"); - author.field( "firstName", "Paul" ); - author.field( "lastName", "Smith" ); - author.field( "country", "USA" ); - author.field( "publicProfile", false ); - author.field( "level", 7 ); + author.field("firstName", "Paul"); + author.field("lastName", "Smith"); + author.field("country", "USA"); + author.field("publicProfile", false); + author.field("level", 7); author.save(); assertEquals("Author", author.getSchemaClass().getName()); } @Test - public void givenAnEmptyDB_afterSavingTwoAuthors_thenWeGetAuthorsWithLevelSeven() { + public void givenDB_whenSavingAuthors_thenWeGetOnesWithLevelSeven() { for (ODocument author : db.browseClass("Author")) author.delete(); ODocument authorOne = new ODocument("Author"); - authorOne.field( "firstName", "Leo" ); - authorOne.field( "level", 7 ); + authorOne.field("firstName", "Leo"); + authorOne.field("level", 7); authorOne.save(); ODocument authorTwo = new ODocument("Author"); - authorTwo.field( "firstName", "Lucien" ); - authorTwo.field( "level", 9 ); + authorTwo.field("firstName", "Lucien"); + authorTwo.field("level", 9); authorTwo.save(); List result = db.query( - new OSQLSynchQuery("select * from Author where level = 7")); + new OSQLSynchQuery("select * from Author where level = 7")); assertEquals(1, result.size()); } diff --git a/orientdb/src/test/java/com/baeldung/orientdb/OrientDBGraphAPITest.java b/orientdb/src/test/java/com/baeldung/orientdb/OrientDBGraphAPITest.java index f03fa87b2c..7b1970d292 100644 --- a/orientdb/src/test/java/com/baeldung/orientdb/OrientDBGraphAPITest.java +++ b/orientdb/src/test/java/com/baeldung/orientdb/OrientDBGraphAPITest.java @@ -81,7 +81,7 @@ public class OrientDBGraphAPITest { @Test public void givenBaeldungDB_getEditorWithLevelSeven() { String onlyEditor = ""; - for( Vertex v : graph.getVertices("Editor.level", 7) ) { + for(Vertex v : graph.getVertices("Editor.level", 7)) { onlyEditor = v.getProperty("firstName").toString(); } diff --git a/orientdb/src/test/java/com/baeldung/orientdb/OrientDBObjectAPITest.java b/orientdb/src/test/java/com/baeldung/orientdb/OrientDBObjectAPITest.java new file mode 100644 index 0000000000..7051383d06 --- /dev/null +++ b/orientdb/src/test/java/com/baeldung/orientdb/OrientDBObjectAPITest.java @@ -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 result = db.query( + new OSQLSynchQuery("select * from Author where level = 7")); + + assertEquals(1, result.size()); + } + + @AfterClass + public static void closeDB() { + db.close(); + } +}