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
- 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 :)
$ mvn test
```

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;
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<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());
}

View File

@ -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();
}

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();
}
}