Skip to content

Commit

Permalink
feat: implement insert at Graph database manager
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 6, 2024
1 parent a977c93 commit d246263
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.jnosql.communication.graph;

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.eclipse.jnosql.communication.semistructured.DeleteQuery;
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
Expand All @@ -37,6 +38,7 @@
*/
public class DefaultGraphDatabaseManager implements GraphDatabaseManager {

static final String ID_PROPERTY = "_id";
private final Graph graph;

private DefaultGraphDatabaseManager(Graph graph) {
Expand All @@ -56,7 +58,10 @@ public String name() {
@Override
public CommunicationEntity insert(CommunicationEntity entity) {
Objects.requireNonNull(entity, "entity is required");
return null;
Vertex vertex = graph.addVertex(entity.name());
entity.toMap().forEach(vertex::property);
entity.add(ID_PROPERTY, vertex.id());
return entity;
}

@Override
Expand Down
@@ -0,0 +1,35 @@
package org.eclipse.jnosql.communication.graph;

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class DefaultGraphDatabaseManagerTest {

private GraphDatabaseManager databaseManager;

@BeforeEach
void setUp(){
Graph graph = GraphSupplier.INSTANCE.get();
this.databaseManager = DefaultGraphDatabaseManager.of(graph);
}

@Test
void shouldInsertEntity(){
var entity = CommunicationEntity.of("Person");
entity.add("name", "Ada Lovelace");
entity.add("age", 30);
var communicationEntity = databaseManager.insert(entity);
assertNotNull(communicationEntity);

SoftAssertions.assertSoftly(softly -> {
softly.assertThat(communicationEntity.find("name", String.class)).get().isEqualTo("Ada Lovelace");
softly.assertThat(communicationEntity.find("age", int.class)).get().isEqualTo(30);
softly.assertThat(communicationEntity.find(DefaultGraphDatabaseManager.ID_PROPERTY)).isPresent();
});
}
}
@@ -0,0 +1,23 @@
package org.eclipse.jnosql.communication.graph;

import org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph;
import org.apache.tinkerpop.gremlin.structure.Graph;

import java.io.File;
import java.util.function.Supplier;
import java.util.logging.Logger;

import static java.lang.System.currentTimeMillis;

public enum GraphSupplier implements Supplier<Graph> {
INSTANCE;

private static final Logger LOGGER = Logger.getLogger(GraphSupplier.class.getName());

@Override
public Graph get() {
var directory = new File("").getAbsolutePath() + "/target/jnosql-communication-graph/" + currentTimeMillis() + "/";
LOGGER.info("Starting Graph database at directory: " + directory);
return Neo4jGraph.open(directory);
}
}

0 comments on commit d246263

Please sign in to comment.