Exemplo n.º 1
0
 @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());
 }
Exemplo n.º 2
0
 @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());
 }
Exemplo n.º 3
0
  @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"));
  }
Exemplo n.º 4
0
  @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));
  }
Exemplo n.º 5
0
  @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);
  }
Exemplo n.º 6
0
  @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));
  }
Exemplo n.º 7
0
  @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);
  }
Exemplo n.º 8
0
 @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)));
 }
Exemplo n.º 9
0
  @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();
    }
  }
Exemplo n.º 10
0
 @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");
 }
Exemplo n.º 11
0
 @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");
 }
Exemplo n.º 12
0
 @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
   }
 }
Exemplo n.º 13
0
 @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));
 }
Exemplo n.º 14
0
 @Test
 public void shouldBeAbleToTraverseWithDefaultParameters() {
   long startNode = createBasicTraversableGraph();
   List<Object> hits =
       serialize(
           actions.traverse(startNode, new HashMap<String, Object>(), TraverserReturnType.node));
   assertEquals(2, hits.size());
 }
Exemplo n.º 15
0
 @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());
 }
Exemplo n.º 16
0
 @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);
 }
Exemplo n.º 17
0
  @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()));
  }
Exemplo n.º 18
0
 @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));
 }
Exemplo n.º 19
0
 @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());
 }
Exemplo n.º 20
0
 @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());
 }
Exemplo n.º 21
0
 @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));
 }
Exemplo n.º 22
0
  @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();
    }
  }
Exemplo n.º 23
0
 @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);
   }
 }
Exemplo n.º 24
0
  @Test
  public void shouldBeAbleToDeleteARelationship() throws Exception {
    long relationshipId = graphdbHelper.createRelationship("LOVES");

    actions.deleteRelationship(relationshipId);
    try {
      graphdbHelper.getRelationship(relationshipId);
      fail();
    } catch (NotFoundException e) {
    }
  }
Exemplo n.º 25
0
  @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());
  }
Exemplo n.º 26
0
  @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);
  }
Exemplo n.º 27
0
 @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());
 }
Exemplo n.º 28
0
  @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);
  }
Exemplo n.º 29
0
  @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"));
    }
  }
Exemplo n.º 30
0
  @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);
  }