Search in sources :

Example 26 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class ArangoDBDocumentCollectionManagerTest method shouldSaveSubDocument2.

@Test
public void shouldSaveSubDocument2() {
    DocumentEntity entity = getEntity();
    entity.add(Document.of("phones", Arrays.asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))));
    DocumentEntity entitySaved = entityManager.insert(entity);
    Document id = entitySaved.find(KEY_NAME).get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentEntity entityFound = entityManager.select(query).get(0);
    Document subDocument = entityFound.find("phones").get();
    List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
    });
    assertThat(documents, containsInAnyOrder(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231")));
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 27 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class EntityConverter method convertIterable.

private static void convertIterable(JsonObject jsonObject, Document document, Object value) {
    JsonObject map = JsonObject.create();
    JsonArray array = JsonArray.create();
    Iterable.class.cast(value).forEach(element -> {
        if (Document.class.isInstance(element)) {
            Document subdocument = Document.class.cast(element);
            map.put(subdocument.getName(), subdocument.get());
        } else if (isSudDocument(element)) {
            JsonObject subJson = JsonObject.create();
            stream(Iterable.class.cast(element).spliterator(), false).forEach(getSubdocument(subJson));
            array.add(subJson);
        } else {
            array.add(element);
        }
    });
    if (array.isEmpty()) {
        jsonObject.put(document.getName(), map);
    } else {
        jsonObject.put(document.getName(), array);
    }
}
Also used : JsonArray(com.couchbase.client.java.document.json.JsonArray) JsonObject(com.couchbase.client.java.document.json.JsonObject) Document(org.jnosql.diana.api.document.Document)

Example 28 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class EntityConverter method convertDocument.

private static void convertDocument(JsonObject jsonObject, Document d, Object value) {
    Document document = Document.class.cast(value);
    jsonObject.put(d.getName(), Collections.singletonMap(document.getName(), document.get()));
}
Also used : Document(org.jnosql.diana.api.document.Document)

Example 29 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class CouchbaseDocumentCollectionManagerAsyncTest method setUp.

@BeforeEach
public void setUp() {
    CouchbaseDocumentConfiguration configuration = new CouchbaseDocumentConfiguration();
    CouhbaseDocumentCollectionManagerFactory managerFactory = configuration.get();
    entityManagerAsync = managerFactory.getAsync(CouchbaseUtil.BUCKET_NAME);
    entityManager = managerFactory.get(CouchbaseUtil.BUCKET_NAME);
    DocumentEntity documentEntity = getEntity();
    Document id = documentEntity.find("name").get();
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    entityManagerAsync.delete(deleteQuery);
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class CouchbaseDocumentCollectionManagerAsyncTest method shouldInserAsyncTTL.

@Test
public void shouldInserAsyncTTL() throws InterruptedException {
    DocumentEntity entity = getEntity();
    entityManagerAsync.insert(entity, Duration.ofSeconds(1L));
    TimeUnit.SECONDS.sleep(2L);
    Document id = entity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    List<DocumentEntity> entities = entityManager.select(query);
    assertTrue(entities.isEmpty());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.jnosql.diana.api.document.Document)126 Test (org.junit.jupiter.api.Test)102 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)94 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)71 List (java.util.List)41 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)37 ArrayList (java.util.ArrayList)25 DocumentCondition (org.jnosql.diana.api.document.DocumentCondition)25 Arrays.asList (java.util.Arrays.asList)23 HashMap (java.util.HashMap)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Map (java.util.Map)11 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)11 TypeReference (org.jnosql.diana.api.TypeReference)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Matchers.contains (org.hamcrest.Matchers.contains)10 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)10 Arrays (java.util.Arrays)8 Optional (java.util.Optional)8 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)8