From 291c4d3535eb8a89c84dcadcbbcf5d6a0d01bfb2 Mon Sep 17 00:00:00 2001 From: danthe1st Date: Wed, 27 Nov 2024 11:57:29 +0100 Subject: [PATCH] tests for element IDs with Neo4J --- arebac-neo4j/Benchmarks.md | 2 + .../danthe1st/arebac/neo4j/graph/Neo4jDB.java | 8 +- .../neo4j/tests/airbnb/ElementIdTest.java | 96 +++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/ElementIdTest.java diff --git a/arebac-neo4j/Benchmarks.md b/arebac-neo4j/Benchmarks.md index b73555b..c080514 100644 --- a/arebac-neo4j/Benchmarks.md +++ b/arebac-neo4j/Benchmarks.md @@ -23,6 +23,8 @@ The following command can be used for checking whether all benchmarks run succes java -cp target/classes:target/test-classes:target/all-dependencies/*:target/arebac-neo4j-0.0.1-SNAPSHOT.jar:../arebac-core/target/classes/ org.openjdk.jmh.Main -f 1 -i 1 -wi 0 -r 100ms -foe true ``` +To store the output in a `jmh-result.csv` file, add `-rf csv` to the command. + ## profiling In order to profile a specific benchmark using [`async-profiler`](https://github.com/async-profiler/async-profiler), first download and extract `async-profiler` and then use the following command (replace `/PATH/TO/async-profiler` with the path you extracted `async-profiler` to): diff --git a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java index e25a345..a6962cb 100644 --- a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java +++ b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java @@ -13,6 +13,7 @@ import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.NotFoundException; import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.ResourceIterable; @@ -41,7 +42,12 @@ public Neo4jDB(Transaction tx) { @Override public Neo4jNode findNodeById(String id) { - return new Neo4jNode(tx.getNodeByElementId(id)); + try{ + Node dbNode = tx.getNodeByElementId(id); + return new Neo4jNode(dbNode); + }catch(NotFoundException | IllegalArgumentException e){ + return null; + } } @Override diff --git a/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/ElementIdTest.java b/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/ElementIdTest.java new file mode 100644 index 0000000..e5c14ec --- /dev/null +++ b/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/ElementIdTest.java @@ -0,0 +1,96 @@ +package io.github.danthe1st.arebac.neo4j.tests.airbnb; + +import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute; +import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY; +import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import io.github.danthe1st.arebac.data.graph_pattern.GPGraph; +import io.github.danthe1st.arebac.data.graph_pattern.GPNode; +import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern; +import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement; +import io.github.danthe1st.arebac.gpeval.GPEval; +import io.github.danthe1st.arebac.neo4j.graph.Neo4jDB; +import io.github.danthe1st.arebac.neo4j.graph.Neo4jNode; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.neo4j.dbms.archive.IncorrectFormat; +import org.neo4j.graphdb.GraphDatabaseService; +import org.neo4j.graphdb.Label; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.ResourceIterator; +import org.neo4j.graphdb.Transaction; + +class ElementIdTest { + + private GraphDatabaseService database; + + @BeforeEach + void setUp() throws IOException, IncorrectFormat { + database = AirbnbSetup.getDatabase(); + } + + @Test + void testLookupWithElementId() { + try(Transaction tx = database.beginTx()){ + Node someNode = findSomeNode(tx); + Neo4jNode retrieved = new Neo4jDB(tx).findNodeById(someNode.getElementId()); + assertNotNull(retrieved); + assertEquals(someNode.getElementId(), retrieved.getDBNode().getElementId()); + } + } + + @Test + void testLookupWithNonexistentElementId() { + try(Transaction tx = database.beginTx()){ + Neo4jNode retrieved = new Neo4jDB(tx).findNodeById("thisdoesnotexist"); + assertNull(retrieved); + } + } + + @Test + void testLookupWithNonexistentElementIdUsingGPEval() { + try(Transaction tx = database.beginTx()){ + GraphPattern pattern = createGraphPatternExpectingElementId("thisdoesnotexist"); + Set> result = GPEval.evaluate(new Neo4jDB(tx), pattern); + assertEquals(Set.of(), result); + } + } + + @Test + void testLookupWithElementIdUsingGPEval() { + try(Transaction tx = database.beginTx()){ + Node someNode = findSomeNode(tx); + String elementId = someNode.getElementId(); + GraphPattern pattern = createGraphPatternExpectingElementId(elementId); + Set> result = GPEval.evaluate(new Neo4jDB(tx), pattern); + assertEquals(1, result.size()); + assertEquals(result, Set.of(List.of(new Neo4jNode(someNode)))); + } + } + + private GraphPattern createGraphPatternExpectingElementId(String elementId) { + GPNode patternNode = new GPNode("node", AirbnbSetup.HOST); + return new GraphPattern( + new GPGraph(List.of(patternNode), List.of()), + List.of(), + Map.of(patternNode, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute(elementId)))), + Map.of(), + List.of(patternNode), + Map.of() + ); + } + + private Node findSomeNode(Transaction tx) { + try(ResourceIterator nodes = tx.findNodes(Label.label(AirbnbSetup.HOST))){ + return nodes.next(); + } + } +}