@Test public void shouldBeAbleToTraverseEverything() { long startNode = createBasicTraversableGraph(); List<Object> hits = serialize( actions.traverse( startNode, MapUtil.map( "return_filter", MapUtil.map("language", "javascript", "body", "true;"), "max_depth", 10), TraverserReturnType.node)); assertEquals(6, hits.size()); hits = serialize( actions.traverse( startNode, MapUtil.map( "return_filter", MapUtil.map("language", "builtin", "name", "all"), "max_depth", 10), TraverserReturnType.node)); assertEquals(6, hits.size()); }
@Test public void shouldBeAbleToTraverseWithMaxDepthAndPruneEvaluatorCombined() { long startNode = createBasicTraversableGraph(); List<Object> hits = serialize( actions.traverse( startNode, MapUtil.map( "max_depth", 2, "prune_evaluator", MapUtil.map( "language", "javascript", "body", "position.endNode().getProperty('name').equals('Emil')")), TraverserReturnType.node)); assertEquals(3, hits.size()); hits = serialize( actions.traverse( startNode, MapUtil.map( "max_depth", 1, "prune_evaluator", MapUtil.map( "language", "javascript", "body", "position.endNode().getProperty('name').equals('Emil')")), TraverserReturnType.node)); assertEquals(2, hits.size()); }
@Test public void shouldBeAbleToGetPathsUsingDijkstra() throws Exception { long[] nodes = createDijkstraGraph(true); // /paths List<Object> result = serialize( actions.findPaths( nodes[0], nodes[1], map( "algorithm", "dijkstra", "cost_property", "cost", "relationships", map("type", "to", "direction", "out")))); assertPaths(1, nodes, 6, result); // /path Map<String, Object> path = serialize( actions.findSinglePath( nodes[0], nodes[1], map( "algorithm", "dijkstra", "cost_property", "cost", "relationships", map("type", "to", "direction", "out")))); assertPaths(1, nodes, 6, Arrays.<Object>asList(path)); assertEquals(6.0d, path.get("weight")); }
@Test public void shouldBeAbleToIndexNode() { String key = "mykey"; String value = "myvalue"; long nodeId = graphdbHelper.createNode(); String indexName = "node"; actions.createNodeIndex(MapUtil.map("name", indexName)); assertFalse(serialize(actions.getIndexedNodes(indexName, key, value)).iterator().hasNext()); actions.addToNodeIndex(indexName, key, value, nodeId); assertEquals(Arrays.asList(nodeId), graphdbHelper.getIndexedNodes(indexName, key, value)); }
@Test public void settingAnEmptyArrayShouldWorkIfOriginalEntityHasAnEmptyArrayAsWell() throws Exception { // Given long nodeId = createNode(map("emptyArray", new int[] {})); // When actions.setNodeProperty(nodeId, "emptyArray", new ArrayList<Object>()); // Then Representation val = actions.getNodeProperty(nodeId, "emptyArray"); System.out.println(val); }
@Test public void shouldBeAbleToGetShortestPaths() throws Exception { long[] nodes = createMoreComplexGraph(); // /paths List<Object> result = serialize( actions.findPaths( nodes[0], nodes[1], MapUtil.map( "max_depth", 2, "algorithm", "shortestPath", "relationships", MapUtil.map("type", "to", "direction", "out")))); assertPaths(2, nodes, 2, result); // /path Map<String, Object> path = serialize( actions.findSinglePath( nodes[0], nodes[1], MapUtil.map( "max_depth", 2, "algorithm", "shortestPath", "relationships", MapUtil.map("type", "to", "direction", "out")))); assertPaths(1, nodes, 2, Arrays.<Object>asList(path)); // /path {single: false} (has no effect) path = serialize( actions.findSinglePath( nodes[0], nodes[1], MapUtil.map( "max_depth", 2, "algorithm", "shortestPath", "relationships", MapUtil.map("type", "to", "direction", "out"), "single", false))); assertPaths(1, nodes, 2, Arrays.<Object>asList(path)); }
@Test public void shouldBeAbleToGetPropertiesOnRelationship() throws Exception { long relationshipId; Map<String, Object> properties = new HashMap<String, Object>(); properties.put("foo", "bar"); properties.put("neo", "Thomas A. Anderson"); properties.put("number", 15L); Transaction tx = database.getGraph().beginTx(); try { Node startNode = database.getGraph().createNode(); Node endNode = database.getGraph().createNode(); Relationship relationship = startNode.createRelationshipTo(endNode, DynamicRelationshipType.withName("knows")); for (Map.Entry<String, Object> entry : properties.entrySet()) { relationship.setProperty(entry.getKey(), entry.getValue()); } relationshipId = relationship.getId(); tx.success(); } finally { tx.finish(); } Map<String, Object> readProperties = serialize(actions.getAllRelationshipProperties(relationshipId)); assertEquals(properties, readProperties); }
@Test public void shouldBeAbleToGetPropertyOnNode() throws Exception { String key = "foo"; Object value = "bar"; long nodeId = createNode(Collections.singletonMap(key, value)); assertEquals(value, serialize(actions.getNodeProperty(nodeId, key))); }
@Test public void shouldOverwriteExistingProperties() throws PropertyValueException, NodeNotFoundException { long nodeId; Transaction tx = database.getGraph().beginTx(); try { Node node = database.getGraph().createNode(); node.setProperty("remove me", "trash"); nodeId = node.getId(); tx.success(); } finally { tx.finish(); } Map<String, Object> properties = new HashMap<String, Object>(); properties.put("foo", "bar"); properties.put("baz", 17); actions.setAllNodeProperties(nodeId, properties); tx = database.getGraph().beginTx(); try { Node node = database.getGraph().getNodeById(nodeId); assertHasProperties(node, properties); assertNull(node.getProperty("remove me", null)); } finally { tx.finish(); } }
@Test(expected = NoSuchPropertyException.class) public void shouldReturnFalseIfNodePropertyNotRemoved() throws Exception { Map<String, Object> properties = new HashMap<String, Object>(); properties.put("foo", "bar"); properties.put("number", 15); long nodeId = createNode(properties); actions.removeNodeProperty(nodeId, "baz"); }
@Test public void shouldReturnTrueIfNodePropertyRemoved() throws Exception { Map<String, Object> properties = new HashMap<String, Object>(); properties.put("foo", "bar"); properties.put("number", 15); long nodeId = createNode(properties); actions.removeNodeProperty(nodeId, "foo"); }
@Test public void shouldNotCreateRelationshipBetweenNonExistentNodes() throws Exception { long nodeId = graphdbHelper.createNode(); Map<String, Object> properties = Collections.<String, Object>emptyMap(); try { actions.createRelationship(nodeId, nodeId * 1000, "Loves", properties); fail(); } catch (EndNodeNotFoundException e) { // ok } try { actions.createRelationship(nodeId * 1000, nodeId, "Loves", properties); fail(); } catch (StartNodeNotFoundException e) { // ok } }
@Test public void shouldBeAbleToSetPropertyOnNode() throws Exception { long nodeId = createNode(Collections.<String, Object>emptyMap()); String key = "foo"; Object value = "bar"; actions.setNodeProperty(nodeId, key, value); assertEquals(Collections.singletonMap(key, value), graphdbHelper.getNodeProperties(nodeId)); }
@Test public void shouldBeAbleToTraverseWithDefaultParameters() { long startNode = createBasicTraversableGraph(); List<Object> hits = serialize( actions.traverse(startNode, new HashMap<String, Object>(), TraverserReturnType.node)); assertEquals(2, hits.size()); }
@Test public void shouldBeAbleToTraverseDepthTwo() { long startNode = createBasicTraversableGraph(); List<Object> hits = serialize( actions.traverse(startNode, MapUtil.map("max_depth", 2), TraverserReturnType.node)); assertEquals(3, hits.size()); }
@Test public void shouldAllowCreateRelationshipWithSameStartAsEndNode() throws Exception { long nodeId = graphdbHelper.createNode(); Map<String, Object> properties = Collections.<String, Object>emptyMap(); RelationshipRepresentation rel = actions.createRelationship(nodeId, nodeId, "Loves", properties); assertNotNull(rel); }
@Test public void shouldNotGetAnyRelationshipsWhenRetrievingFromNodeWithoutRelationships() throws Exception { long nodeId = graphdbHelper.createNode(); verifyRelReps( 0, actions.getNodeRelationships( nodeId, RelationshipDirection.all, Collections.<String>emptyList())); verifyRelReps( 0, actions.getNodeRelationships( nodeId, RelationshipDirection.in, Collections.<String>emptyList())); verifyRelReps( 0, actions.getNodeRelationships( nodeId, RelationshipDirection.out, Collections.<String>emptyList())); }
@Test public void shouldBeAbleToSetRelationshipProperties() throws Exception { long relationshipId = graphdbHelper.createRelationship("KNOWS"); Map<String, Object> properties = new HashMap<String, Object>(); properties.put("foo", "bar"); properties.put("number", 10); actions.setAllRelationshipProperties(relationshipId, properties); assertEquals(properties, graphdbHelper.getRelationshipProperties(relationshipId)); }
@Test public void shouldBeAbleToRemoveNodeFromIndex() { String key = "mykey2"; String value = "myvalue"; String value2 = "myvalue2"; String indexName = "node"; long nodeId = graphdbHelper.createNode(); actions.addToNodeIndex(indexName, key, value, nodeId); actions.addToNodeIndex(indexName, key, value2, nodeId); assertEquals(1, graphdbHelper.getIndexedNodes(indexName, key, value).size()); assertEquals(1, graphdbHelper.getIndexedNodes(indexName, key, value2).size()); actions.removeFromNodeIndex(indexName, key, value, nodeId); assertEquals(0, graphdbHelper.getIndexedNodes(indexName, key, value).size()); assertEquals(1, graphdbHelper.getIndexedNodes(indexName, key, value2).size()); actions.removeFromNodeIndex(indexName, key, value2, nodeId); assertEquals(0, graphdbHelper.getIndexedNodes(indexName, key, value).size()); assertEquals(0, graphdbHelper.getIndexedNodes(indexName, key, value2).size()); }
@Test public void shouldStoreRelationshipsBetweenTwoExistingNodes() throws Exception { int relationshipCount = graphdbHelper.getNumberOfRelationships(); actions.createRelationship( graphdbHelper.createNode(), graphdbHelper.createNode(), "LOVES", Collections.<String, Object>emptyMap()); assertEquals(relationshipCount + 1, graphdbHelper.getNumberOfRelationships()); }
@Test public void shouldBeAbleToSetRelationshipProperty() throws Exception { long relationshipId = graphdbHelper.createRelationship("KNOWS"); String key = "foo"; Object value = "bar"; actions.setRelationshipProperty(relationshipId, key, value); assertEquals( Collections.singletonMap(key, value), graphdbHelper.getRelationshipProperties(relationshipId)); }
@Test public void createdNodeShouldBeInDatabase() throws Exception { NodeRepresentation noderep = actions.createNode(Collections.<String, Object>emptyMap()); Transaction tx = database.getGraph().beginTx(); try { assertNotNull(database.getGraph().getNodeById(noderep.getId())); } finally { tx.finish(); } }
@Test public void shouldBeAbleToGetRelationshipsIfSpecified() { long startNode = createBasicTraversableGraph(); ListRepresentation traverse = actions.traverse( startNode, new HashMap<String, Object>(), TraverserReturnType.relationship); List<Object> hits = serialize(traverse); for (Object hit : hits) { RelationshipRepresentationTest.verifySerialisation((Map<String, Object>) hit); } }
@Test public void shouldBeAbleToDeleteARelationship() throws Exception { long relationshipId = graphdbHelper.createRelationship("LOVES"); actions.deleteRelationship(relationshipId); try { graphdbHelper.getRelationship(relationshipId); fail(); } catch (NotFoundException e) { } }
@Test public void shouldRemoveRelationshipProperty() throws Exception { long relId = graphdbHelper.createRelationship("PAIR-PROGRAMS_WITH"); Map<String, Object> map = new HashMap<String, Object>(); map.put("foo", "bar"); map.put("baz", 22); graphdbHelper.setRelationshipProperties(relId, map); actions.removeRelationshipProperty(relId, "foo"); assertEquals(1, graphdbHelper.getRelationshipProperties(relId).size()); }
@Test(expected = PropertyValueException.class) public void shouldFailOnTryingToStoreMixedArraysAsAProperty() throws Exception { long nodeId = graphdbHelper.createNode(); Map<String, Object> properties = new HashMap<String, Object>(); Object[] dodgyArray = new Object[3]; dodgyArray[0] = 0; dodgyArray[1] = 1; dodgyArray[2] = "two"; properties.put("foo", dodgyArray); actions.setAllNodeProperties(nodeId, properties); }
@Test public void shouldBeAbleToFulltextIndex() { String key = "key"; String value = "the value with spaces"; long nodeId = graphdbHelper.createNode(); String indexName = "fulltext-node"; graphdbHelper.createNodeFullTextIndex(indexName); assertFalse(serialize(actions.getIndexedNodes(indexName, key, value)).iterator().hasNext()); actions.addToNodeIndex(indexName, key, value, nodeId); assertEquals(Arrays.asList(nodeId), graphdbHelper.getIndexedNodes(indexName, key, value)); assertEquals( Arrays.asList(nodeId), graphdbHelper.getIndexedNodes(indexName, key, "the value with spaces")); assertEquals(Arrays.asList(nodeId), graphdbHelper.queryIndexedNodes(indexName, key, "the")); assertEquals(Arrays.asList(nodeId), graphdbHelper.queryIndexedNodes(indexName, key, "value")); assertEquals(Arrays.asList(nodeId), graphdbHelper.queryIndexedNodes(indexName, key, "with")); assertEquals(Arrays.asList(nodeId), graphdbHelper.queryIndexedNodes(indexName, key, "spaces")); assertEquals( Arrays.asList(nodeId), graphdbHelper.queryIndexedNodes(indexName, key, "*spaces*")); assertTrue(graphdbHelper.getIndexedNodes(indexName, key, "nohit").isEmpty()); }
@Test public void shouldBeAbleToRetrieveASinglePropertyFromARelationship() throws Exception { Map<String, Object> properties = new HashMap<String, Object>(); properties.put("foo", "bar"); properties.put("neo", "Thomas A. Anderson"); properties.put("number", 15L); long relationshipId = graphdbHelper.createRelationship("LOVES"); graphdbHelper.setRelationshipProperties(relationshipId, properties); Object relationshipProperty = serialize(actions.getRelationshipProperty(relationshipId, "foo")); assertEquals("bar", relationshipProperty); }
@Test public void shouldBeAbleToGetPathsIfSpecified() { long startNode = createBasicTraversableGraph(); List<Object> hits = serialize( actions.traverse(startNode, new HashMap<String, Object>(), TraverserReturnType.path)); for (Object hit : hits) { Map<String, Object> map = (Map<String, Object>) hit; assertThat(map, hasKey("start")); assertThat(map, hasKey("end")); assertThat(map, hasKey("length")); } }
@Test public void shouldGetExtendedNodeRepresentationsWhenGettingFromIndex() { String key = "mykey3"; String value = "value"; long nodeId = graphdbHelper.createNode(); String indexName = "node"; graphdbHelper.addNodeToIndex(indexName, key, value, nodeId); int counter = 0; for (Object rep : serialize(actions.getIndexedNodes(indexName, key, value))) { Map<String, Object> serialized = (Map<String, Object>) rep; NodeRepresentationTest.verifySerialisation(serialized); assertNotNull(serialized.get("indexed")); counter++; } assertEquals(1, counter); }