Skip to content

Commit dc18980

Browse files
author
Dassi Orleando
committed
BAEL-1216: Object API + code formating
1 parent ac890d6 commit dc18980

5 files changed

Lines changed: 130 additions & 35 deletions

File tree

‎orientdb/README.md‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,18 @@ This is a simple maven project that shows how to do OrientDB operations using th
99
- Java 7 or higher
1010
- OrientDB
1111

12-
### Running
12+
### Build
1313

1414
To build and start the server simply type
1515

1616
```bash
1717
$ mvn clean install
1818
```
1919

20-
### Test Db Operation
20+
### Tests
2121

22-
You can see what crud operation are available using curl:
22+
To run unit tests
2323

2424
```bash
25-
$ curl localhost:8080
26-
```
27-
You can view existing student objects with this command:
28-
29-
```bash
30-
$ curl localhost:8080/articles
31-
```
32-
33-
34-
Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080)
35-
36-
Enjoy it :)
25+
$ mvn test
26+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.orientdb;
2+
3+
import javax.persistence.Id;
4+
5+
public class Author {
6+
@Id
7+
private Object id;
8+
9+
private String firstName;
10+
private String lastName;
11+
private int level;
12+
13+
public Author() {
14+
}
15+
16+
public Author(String firstName, String lastName, int level) {
17+
this.firstName = firstName;
18+
this.lastName = lastName;
19+
this.level = level;
20+
}
21+
22+
public String getFirstName() {
23+
return firstName;
24+
}
25+
26+
public void setFirstName(String firstName) {
27+
this.firstName = firstName;
28+
}
29+
30+
public String getLastName() {
31+
return lastName;
32+
}
33+
34+
public void setLastName(String lastName) {
35+
this.lastName = lastName;
36+
}
37+
38+
public int getLevel() {
39+
return level;
40+
}
41+
42+
public void setLevel(int level) {
43+
this.level = level;
44+
}
45+
46+
@java.lang.Override
47+
public java.lang.String toString() {
48+
return "Author{" +
49+
"firstName='" + firstName + '\'' +
50+
", lastName='" + lastName + '\'' +
51+
", level=" + level +
52+
'}';
53+
}
54+
}

‎orientdb/src/test/java/com/baeldung/orientdb/OrientDBDocumentAPITest.java‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package com.baeldung.orientdb;
22

33
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
4-
import com.orientechnologies.orient.core.exception.OSchemaException;
5-
import com.orientechnologies.orient.core.metadata.schema.OType;
64
import com.orientechnologies.orient.core.record.impl.ODocument;
75
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
8-
import com.tinkerpop.blueprints.Vertex;
9-
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
10-
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
11-
import org.junit.*;
6+
import org.junit.AfterClass;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
129

1310
import java.util.List;
1411

1512
import static junit.framework.Assert.assertEquals;
16-
import static junit.framework.Assert.assertNotNull;
17-
import static org.junit.Assert.assertNotEquals;
1813

1914
public class OrientDBDocumentAPITest {
2015
private static ODatabaseDocumentTx db = null;
@@ -28,32 +23,32 @@ public static void setup() {
2823
@Test
2924
public void givenDB_whenSavingDocument_thenClassIsAutoCreated() {
3025
ODocument author = new ODocument("Author");
31-
author.field( "firstName", "Paul" );
32-
author.field( "lastName", "Smith" );
33-
author.field( "country", "USA" );
34-
author.field( "publicProfile", false );
35-
author.field( "level", 7 );
26+
author.field("firstName", "Paul");
27+
author.field("lastName", "Smith");
28+
author.field("country", "USA");
29+
author.field("publicProfile", false);
30+
author.field("level", 7);
3631
author.save();
3732

3833
assertEquals("Author", author.getSchemaClass().getName());
3934
}
4035

4136
@Test
42-
public void givenAnEmptyDB_afterSavingTwoAuthors_thenWeGetAuthorsWithLevelSeven() {
37+
public void givenDB_whenSavingAuthors_thenWeGetOnesWithLevelSeven() {
4338
for (ODocument author : db.browseClass("Author")) author.delete();
4439

4540
ODocument authorOne = new ODocument("Author");
46-
authorOne.field( "firstName", "Leo" );
47-
authorOne.field( "level", 7 );
41+
authorOne.field("firstName", "Leo");
42+
authorOne.field("level", 7);
4843
authorOne.save();
4944

5045
ODocument authorTwo = new ODocument("Author");
51-
authorTwo.field( "firstName", "Lucien" );
52-
authorTwo.field( "level", 9 );
46+
authorTwo.field("firstName", "Lucien");
47+
authorTwo.field("level", 9);
5348
authorTwo.save();
5449

5550
List<ODocument> result = db.query(
56-
new OSQLSynchQuery<ODocument>("select * from Author where level = 7"));
51+
new OSQLSynchQuery<ODocument>("select * from Author where level = 7"));
5752

5853
assertEquals(1, result.size());
5954
}

‎orientdb/src/test/java/com/baeldung/orientdb/OrientDBGraphAPITest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void givenBaeldungDB_checkWeHaveTwoWriters() {
8181
@Test
8282
public void givenBaeldungDB_getEditorWithLevelSeven() {
8383
String onlyEditor = "";
84-
for( Vertex v : graph.getVertices("Editor.level", 7) ) {
84+
for(Vertex v : graph.getVertices("Editor.level", 7)) {
8585
onlyEditor = v.getProperty("firstName").toString();
8686
}
8787

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.orientdb;
2+
3+
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
4+
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
5+
import org.junit.AfterClass;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.List;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
13+
public class OrientDBObjectAPITest {
14+
private static OObjectDatabaseTx db = null;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
String orientDBFolder = System.getenv("ORIENTDB_HOME");
19+
db = new OObjectDatabaseTx("plocal:" + orientDBFolder + "/databases/BaeldungDBThree").open("admin", "admin");
20+
db.setSaveOnlyDirty(true);
21+
db.getEntityManager().registerEntityClass(Author.class);
22+
}
23+
24+
@Test
25+
public void givenDB_whenSavingObject_thenHisIdExists() {
26+
Author author = db.newInstance(Author.class);
27+
author.setFirstName("Luke");
28+
author.setLastName("Sky");
29+
author.setLevel(9);
30+
31+
long authors = db.countClass(Author.class);
32+
33+
db.save(author);
34+
}
35+
36+
@Test
37+
public void givenDB_whenSavingAuthors_thenWeGetOnesWithLevelSeven() {
38+
for (Author author : db.browseClass(Author.class)) db.delete(author);
39+
40+
Author authorOne = db.newInstance(Author.class, "Leo", "Marta", 7);
41+
db.save(authorOne);
42+
43+
Author authorTwo = db.newInstance(Author.class, "Lucien", "Aurelien", 9);
44+
db.save(authorTwo);
45+
46+
List<Author> result = db.query(
47+
new OSQLSynchQuery<Author>("select * from Author where level = 7"));
48+
49+
assertEquals(1, result.size());
50+
}
51+
52+
@AfterClass
53+
public static void closeDB() {
54+
db.close();
55+
}
56+
}

0 commit comments

Comments
 (0)