BAEL-1216: Introduction to the OrientDB Java APIs (#3300)
* BAEL-1216: Introduction to the Oriented Java APIs * OrientDB Java Graph and Document APIs * some cleans * BAEL-1216: Object API + code formating * Prevent the build to failed * Improve code and readme * Update Readme * Simulate failed test in jenkins pipeline
This commit is contained in:
@@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.orientdb;
|
||||
|
||||
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
|
||||
import com.orientechnologies.orient.core.record.impl.ODocument;
|
||||
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class OrientDBDocumentAPITest {
|
||||
private static ODatabaseDocumentTx db = null;
|
||||
|
||||
// @BeforeClass
|
||||
public static void setup() {
|
||||
String orientDBFolder = System.getenv("ORIENTDB_HOME");
|
||||
db = new ODatabaseDocumentTx("plocal:" + orientDBFolder + "/databases/BaeldungDBTwo").open("admin", "admin");
|
||||
}
|
||||
|
||||
// @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.save();
|
||||
|
||||
assertEquals("Author", author.getSchemaClass().getName());
|
||||
}
|
||||
|
||||
// @Test
|
||||
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.save();
|
||||
|
||||
ODocument authorTwo = new ODocument("Author");
|
||||
authorTwo.field("firstName", "Lucien");
|
||||
authorTwo.field("level", 9);
|
||||
authorTwo.save();
|
||||
|
||||
List<ODocument> result = db.query(
|
||||
new OSQLSynchQuery<ODocument>("select * from Author where level = 7"));
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
// @AfterClass
|
||||
public static void closeDB() {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.baeldung.orientdb;
|
||||
|
||||
import com.orientechnologies.orient.core.metadata.schema.OType;
|
||||
import com.tinkerpop.blueprints.Vertex;
|
||||
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
|
||||
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class OrientDBGraphAPITest {
|
||||
private static OrientGraphNoTx graph = null;
|
||||
|
||||
// @BeforeClass
|
||||
public static void setup() {
|
||||
String orientDBFolder = System.getenv("ORIENTDB_HOME");
|
||||
graph = new OrientGraphNoTx("plocal:" + orientDBFolder + "/databases/BaeldungDB", "admin", "admin");
|
||||
}
|
||||
|
||||
// @BeforeClass
|
||||
public static void init() {
|
||||
boolean articleExists = graph.getVertexType("Article") != null;
|
||||
boolean writerExists = graph.getVertexType("Writer") != null;
|
||||
boolean authorExists = graph.getVertexType("Author") != null;
|
||||
boolean editorExists = graph.getVertexType("Editor") != null;
|
||||
|
||||
boolean classesExist = articleExists && writerExists && authorExists && editorExists;
|
||||
|
||||
if(!classesExist) {
|
||||
graph.createVertexType("Article");
|
||||
|
||||
OrientVertexType writerType = graph.createVertexType("Writer");
|
||||
writerType.setStrictMode(true);
|
||||
writerType.createProperty("firstName", OType.STRING);
|
||||
writerType.createProperty("lastName", OType.STRING);
|
||||
writerType.createProperty("country", OType.STRING);
|
||||
|
||||
OrientVertexType authorType = graph.createVertexType("Author", "Writer");
|
||||
authorType.createProperty("level", OType.INTEGER).setMax("3");
|
||||
|
||||
OrientVertexType editorType = graph.createVertexType("Editor", "Writer");
|
||||
editorType.createProperty("level", OType.INTEGER).setMin("3");
|
||||
|
||||
Vertex vEditor = graph.addVertex("class:Editor");
|
||||
vEditor.setProperty("firstName", "Maxim");
|
||||
vEditor.setProperty("lastName", "Mink's");
|
||||
vEditor.setProperty("country", "Cameroon");
|
||||
vEditor.setProperty("publicProfile", true);
|
||||
vEditor.setProperty("level", "7");
|
||||
|
||||
Vertex vAuthor = graph.addVertex("class:Author");
|
||||
vAuthor.setProperty("firstName", "Jerome");
|
||||
vAuthor.setProperty("country", "Romania");
|
||||
vAuthor.setProperty("publicProfile", false);
|
||||
vAuthor.setProperty("level", "3");
|
||||
|
||||
Vertex vArticle = graph.addVertex("class:Article");
|
||||
vArticle.setProperty("title", "Introduction to the OrientDB Java APIs.");
|
||||
vArticle.setProperty("priority", "High");
|
||||
vArticle.setProperty("type", "Article");
|
||||
vArticle.setProperty("level", "+L4");
|
||||
|
||||
graph.addEdge(null, vAuthor, vEditor, "has");
|
||||
graph.addEdge(null, vAuthor, vArticle, "wrote");
|
||||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void givenBaeldungDB_checkWeHaveThreeRecords() {
|
||||
long size = graph.countVertices();
|
||||
|
||||
assertEquals(3, size);
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void givenBaeldungDB_checkWeHaveTwoWriters() {
|
||||
long size = graph.countVertices("Writer");
|
||||
|
||||
assertEquals(2, size);
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void givenBaeldungDB_getEditorWithLevelSeven() {
|
||||
String onlyEditor = "";
|
||||
for(Vertex v : graph.getVertices("Editor.level", 7)) {
|
||||
onlyEditor = v.getProperty("firstName").toString();
|
||||
}
|
||||
|
||||
assertEquals("Maxim", onlyEditor);
|
||||
}
|
||||
|
||||
// @AfterClass
|
||||
public static void closeDB() {
|
||||
graph.getRawGraph().getStorage().close(true, false);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user