Java Code Examples for com.arangodb.entity.BaseDocument#addAttribute()

The following examples show how to use com.arangodb.entity.BaseDocument#addAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomSerdeTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void getDocument() throws ExecutionException, InterruptedException {
    String key = "test-" + UUID.randomUUID().toString();

    BaseDocument doc = new BaseDocument(key);
    doc.addAttribute("arr", Collections.singletonList("hello"));
    doc.addAttribute("int", 10);

    collection.insertDocument(doc, null).get();

    final BaseDocument result = db.collection(COLLECTION_NAME).getDocument(
            key,
            BaseDocument.class,
            null).get();

    assertThat(result.getAttribute("arr"), instanceOf(String.class));
    assertThat(result.getAttribute("arr"), is("hello"));
    assertThat(result.getAttribute("int"), instanceOf(BigInteger.class));
    assertThat(result.getAttribute("int"), is(BigInteger.valueOf(10)));
}
 
Example 2
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void updateVertexKeepNullTrue() {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    final VertexEntity createResult = vertices
            .insertVertex(doc, null);
    doc.updateAttribute("a", null);
    final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(true);
    final VertexUpdateEntity updateResult = vertices
            .updateVertex(createResult.getKey(), doc, options);
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = vertices
            .getVertex(createResult.getKey(), BaseDocument.class, null);
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getProperties().keySet().size(), is(1));
    assertThat(readResult.getProperties().keySet(), hasItem("a"));
}
 
Example 3
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void updateVertexIfMatchFail() {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    doc.addAttribute("c", "test");
    final VertexEntity createResult = vertices
            .insertVertex(doc, null);
    doc.updateAttribute("a", "test1");
    doc.addAttribute("b", "test");
    doc.updateAttribute("c", null);
    try {
        final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch("no");
        vertices.updateVertex(createResult.getKey(), doc, options);
        fail();
    } catch (final ArangoDBException e) {
        if (isAtLeastVersion(3, 4)) {
            // FIXME: atm the server replies 409 for HTTP_JSON or HTTP_VPACK
            // assertThat(e.getResponseCode(), is(412));
            assertThat(e.getErrorNum(), is(1200));
        } else {
            assertThat(e.getResponseCode(), is(412));
            assertThat(e.getErrorNum(), is(1903));
        }
    }
}
 
Example 4
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void replaceVertexIfMatch() throws InterruptedException, ExecutionException {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null)
            .get();
    doc.getProperties().clear();
    doc.addAttribute("b", "test");
    final VertexReplaceOptions options = new VertexReplaceOptions().ifMatch(createResult.getRev());
    final VertexUpdateEntity replaceResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .replaceVertex(createResult.getKey(), doc, options).get();
    assertThat(replaceResult, is(notNullValue()));
    assertThat(replaceResult.getId(), is(createResult.getId()));
    assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev())));
    assertThat(replaceResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .getVertex(createResult.getKey(), BaseDocument.class, null).get();
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getRevision(), is(replaceResult.getRev()));
    assertThat(readResult.getProperties().keySet(), not(hasItem("a")));
    assertThat(readResult.getAttribute("b"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("b")), is("test"));
}
 
Example 5
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void updateVertexKeepNullFalse() {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    final VertexEntity createResult = vertices
            .insertVertex(doc, null);
    doc.updateAttribute("a", null);
    final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(false);
    final VertexUpdateEntity updateResult = vertices
            .updateVertex(createResult.getKey(), doc, options);
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = vertices
            .getVertex(createResult.getKey(), BaseDocument.class, null);
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getId(), is(createResult.getId()));
    assertThat(readResult.getRevision(), is(notNullValue()));
    assertThat(readResult.getProperties().keySet(), not(hasItem("a")));
}
 
Example 6
Source File: CustomSerdeTest.java    From arangodb-java-driver-async with Apache License 2.0 6 votes vote down vote up
@Test
public void getDocument() throws ExecutionException, InterruptedException {
    String key = "test-" + UUID.randomUUID().toString();

    BaseDocument doc = new BaseDocument(key);
    doc.addAttribute("arr", Collections.singletonList("hello"));
    doc.addAttribute("int", 10);

    collection.insertDocument(doc, null).get();

    final BaseDocument result = db.collection(COLLECTION_NAME).getDocument(
            key,
            BaseDocument.class,
            null).get();

    assertThat(result.getAttribute("arr"), instanceOf(String.class));
    assertThat(result.getAttribute("arr"), is("hello"));
    assertThat(result.getAttribute("int"), instanceOf(BigInteger.class));
    assertThat(result.getAttribute("int"), is(BigInteger.valueOf(10)));
}
 
Example 7
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void updateVertexKeepNullFalse() throws InterruptedException, ExecutionException {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null)
            .get();
    doc.updateAttribute("a", null);
    final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(false);
    final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .updateVertex(createResult.getKey(), doc, options).get();
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .getVertex(createResult.getKey(), BaseDocument.class, null).get();
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getId(), is(createResult.getId()));
    assertThat(readResult.getRevision(), is(notNullValue()));
    assertThat(readResult.getProperties().keySet(), not(hasItem("a")));
}
 
Example 8
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void replaceVertexIfMatchFail() throws InterruptedException, ExecutionException {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null)
            .get();
    doc.getProperties().clear();
    doc.addAttribute("b", "test");
    try {
        final VertexReplaceOptions options = new VertexReplaceOptions().ifMatch("no");
        db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).replaceVertex(createResult.getKey(), doc, options)
                .get();
        fail();
    } catch (final ExecutionException e) {
        assertThat(e.getCause(), instanceOf(ArangoDBException.class));
    }
}
 
Example 9
Source File: CustomSerdeTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void insertDocument() {
    String key = "test-" + UUID.randomUUID().toString();

    BaseDocument doc = new BaseDocument(key);
    doc.addAttribute("arr", Collections.singletonList("hello"));
    doc.addAttribute("int", 10);

    BaseDocument result = collection.insertDocument(
            doc,
            new DocumentCreateOptions().returnNew(true)
    ).getNew();

    assertThat(result.getAttribute("arr"), instanceOf(String.class));
    assertThat(result.getAttribute("arr"), is("hello"));
    assertThat(result.getAttribute("int"), instanceOf(BigInteger.class));
    assertThat(result.getAttribute("int"), is(BigInteger.valueOf(10)));
}
 
Example 10
Source File: CustomSerdeTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void aqlDeserialization() throws ExecutionException, InterruptedException {
    String key = "test-" + UUID.randomUUID().toString();

    BaseDocument doc = new BaseDocument(key);
    doc.addAttribute("arr", Collections.singletonList("hello"));
    doc.addAttribute("int", 10);

    collection.insertDocument(doc, null).get();

    final BaseDocument result = db.query(
            "RETURN DOCUMENT(@docId)",
            Collections.singletonMap("docId", COLLECTION_NAME + "/" + key),
            BaseDocument.class
    ).get().first();

    assertThat(result.getAttribute("arr"), instanceOf(String.class));
    assertThat(result.getAttribute("arr"), is("hello"));
    assertThat(result.getAttribute("int"), instanceOf(BigInteger.class));
    assertThat(result.getAttribute("int"), is(BigInteger.valueOf(10)));
}
 
Example 11
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void updateVertexIfMatch() {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    doc.addAttribute("c", "test");
    final VertexEntity createResult = vertices
            .insertVertex(doc, null);
    doc.updateAttribute("a", "test1");
    doc.addAttribute("b", "test");
    doc.updateAttribute("c", null);
    final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch(createResult.getRev());
    final VertexUpdateEntity updateResult = vertices
            .updateVertex(createResult.getKey(), doc, options);
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = vertices
            .getVertex(createResult.getKey(), BaseDocument.class, null);
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getAttribute("a"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("a")), is("test1"));
    assertThat(readResult.getAttribute("b"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("b")), is("test"));
    assertThat(readResult.getRevision(), is(updateResult.getRev()));
    assertThat(readResult.getProperties().keySet(), hasItem("c"));
}
 
Example 12
Source File: ArangoSerializationTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void skipSerializeNullValues() {
    final BaseDocument entity = new BaseDocument();
    entity.addAttribute("bar", null);
    final VPackSlice vpack = util.serialize(entity);
    assertThat(vpack.get("bar").isNone(), is(true));
}
 
Example 13
Source File: AQLActorsAndMoviesExample.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
private static DocumentCreateEntity<BaseDocument> saveActor(
        final ArangoCollectionAsync actors,
        final String key,
        final String name,
        final int born) throws InterruptedException, ExecutionException {
    final BaseDocument value = new BaseDocument();
    value.setKey(key);
    value.addAttribute("name", name);
    value.addAttribute("born", born);
    return actors.insertDocument(value).get();
}
 
Example 14
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void updateVertex() throws InterruptedException, ExecutionException {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    doc.addAttribute("c", "test");
    final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null)
            .get();
    doc.updateAttribute("a", "test1");
    doc.addAttribute("b", "test");
    doc.updateAttribute("c", null);
    final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .updateVertex(createResult.getKey(), doc, null).get();
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .getVertex(createResult.getKey(), BaseDocument.class, null).get();
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getAttribute("a"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("a")), is("test1"));
    assertThat(readResult.getAttribute("b"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("b")), is("test"));
    assertThat(readResult.getRevision(), is(updateResult.getRev()));
    assertThat(readResult.getProperties().keySet(), hasItem("c"));
}
 
Example 15
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
@Test
public void updateVertexIfMatch() throws InterruptedException, ExecutionException {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    doc.addAttribute("c", "test");
    final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null)
            .get();
    doc.updateAttribute("a", "test1");
    doc.addAttribute("b", "test");
    doc.updateAttribute("c", null);
    final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch(createResult.getRev());
    final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .updateVertex(createResult.getKey(), doc, options).get();
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
            .getVertex(createResult.getKey(), BaseDocument.class, null).get();
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getAttribute("a"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("a")), is("test1"));
    assertThat(readResult.getAttribute("b"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("b")), is("test"));
    assertThat(readResult.getRevision(), is(updateResult.getRev()));
    assertThat(readResult.getProperties().keySet(), hasItem("c"));
}
 
Example 16
Source File: AQLActorsAndMoviesExample.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
private static DocumentCreateEntity<BaseDocument> saveMovie(
        final ArangoCollectionAsync movies,
        final String key,
        final String title,
        final int released,
        final String tagline) throws InterruptedException, ExecutionException {
    final BaseDocument value = new BaseDocument();
    value.setKey(key);
    value.addAttribute("title", title);
    value.addAttribute("released", released);
    value.addAttribute("tagline", tagline);
    return movies.insertDocument(value).get();
}
 
Example 17
Source File: ArangoVertexCollectionTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void updateVertex() {
    final BaseDocument doc = new BaseDocument();
    doc.addAttribute("a", "test");
    doc.addAttribute("c", "test");
    final VertexEntity createResult = vertices
            .insertVertex(doc, null);
    doc.updateAttribute("a", "test1");
    doc.addAttribute("b", "test");
    doc.updateAttribute("c", null);
    final VertexUpdateEntity updateResult = vertices
            .updateVertex(createResult.getKey(), doc, null);
    assertThat(updateResult, is(notNullValue()));
    assertThat(updateResult.getId(), is(createResult.getId()));
    assertThat(updateResult.getRev(), is(not(updateResult.getOldRev())));
    assertThat(updateResult.getOldRev(), is(createResult.getRev()));

    final BaseDocument readResult = vertices
            .getVertex(createResult.getKey(), BaseDocument.class, null);
    assertThat(readResult.getKey(), is(createResult.getKey()));
    assertThat(readResult.getAttribute("a"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("a")), is("test1"));
    assertThat(readResult.getAttribute("b"), is(notNullValue()));
    assertThat(String.valueOf(readResult.getAttribute("b")), is("test"));
    assertThat(readResult.getRevision(), is(updateResult.getRev()));
    assertThat(readResult.getProperties().keySet(), hasItem("c"));
}
 
Example 18
Source File: AqlQueryWithSpecialReturnTypesExample.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
private static void createExamples() {
    for (int i = 0; i < 100; i++) {
        final BaseDocument value = new BaseDocument();
        value.addAttribute("name", "TestUser" + i);
        value.addAttribute("gender", (i % 2) == 0 ? Gender.MALE : Gender.FEMALE);
        value.addAttribute("age", i + 10);
        db.collection(COLLECTION_NAME).insertDocument(value);
    }
}
 
Example 19
Source File: GetDocumentExample.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void before() {
    final BaseDocument value = new BaseDocument();
    value.addAttribute("foo", "bar");
    final DocumentCreateEntity<BaseDocument> doc = collection.insertDocument(value);
    key = doc.getKey();
}
 
Example 20
Source File: GetDocumentExample.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void before() throws InterruptedException, ExecutionException {
    final BaseDocument value = new BaseDocument();
    value.addAttribute("foo", "bar");
    final DocumentCreateEntity<BaseDocument> doc = collection.insertDocument(value).get();
    key = doc.getKey();
}