Search in sources :

Example 41 with Document

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

the class DocumentQueryTest method shouldSelectAll.

@Test
public void shouldSelectAll() {
    DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id"), Document.of("name", "name")));
    DocumentQuery query = select().from(COLLECTION_NAME).build();
    Optional<Document> name = entity.find("name");
    List<DocumentEntity> entities = entityManager.select(query);
    assertFalse(entities.isEmpty());
    assertTrue(entities.size() >= 4);
}
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)

Example 42 with Document

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

the class DocumentEntityGerator method getEntity.

static DocumentEntity getEntity() {
    DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
    Map<String, Object> map = new HashMap<>();
    map.put("name", "Poliana");
    map.put("city", "Salvador");
    map.put("_id", "id");
    List<Document> documents = Documents.of(map);
    documents.forEach(entity::add);
    return entity;
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) HashMap(java.util.HashMap) Document(org.jnosql.diana.api.document.Document)

Example 43 with Document

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

the class ElasticsearchDocumentCollectionManagerTest method shouldFindDocumentByName.

@Test
public void shouldFindDocumentByName() throws InterruptedException {
    DocumentEntity entity = entityManager.insert(getEntity());
    Document name = entity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(name.getName()).eq(name.get()).build();
    TimeUnit.SECONDS.sleep(1L);
    List<DocumentEntity> entities = entityManager.select(query);
    assertFalse(entities.isEmpty());
    assertThat(entities, contains(entity));
}
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)

Example 44 with Document

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

the class DocumentQueryConversor method convert.

public static Bson convert(DocumentCondition condition) {
    Document document = condition.getDocument();
    Object value = document.get();
    switch(condition.getCondition()) {
        case EQUALS:
            return Filters.eq(document.getName(), value);
        case GREATER_THAN:
            return Filters.gt(document.getName(), value);
        case GREATER_EQUALS_THAN:
            return Filters.gte(document.getName(), value);
        case LESSER_THAN:
            return Filters.lt(document.getName(), value);
        case LESSER_EQUALS_THAN:
            return Filters.lte(document.getName(), value);
        case IN:
            List<Object> inList = document.get(new TypeReference<List<Object>>() {
            });
            return Filters.in(document.getName(), inList.toArray());
        case LIKE:
            return Filters.regex(document.getName(), value.toString());
        case AND:
            List<DocumentCondition> andList = condition.getDocument().getValue().get(new TypeReference<List<DocumentCondition>>() {
            });
            return Filters.and(andList.stream().map(DocumentQueryConversor::convert).collect(Collectors.toList()));
        case OR:
            List<DocumentCondition> orList = condition.getDocument().getValue().get(new TypeReference<List<DocumentCondition>>() {
            });
            return Filters.or(orList.stream().map(DocumentQueryConversor::convert).collect(Collectors.toList()));
        default:
            throw new UnsupportedOperationException("The condition " + condition.getCondition() + " is not supported from mongoDB diana driver");
    }
}
Also used : List(java.util.List) Document(org.jnosql.diana.api.document.Document) DocumentCondition(org.jnosql.diana.api.document.DocumentCondition)

Example 45 with Document

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

the class OrientDBDocumentCollectionManagerAsyncTest method shouldRemoveEntityAsync.

@Test
public void shouldRemoveEntityAsync() throws InterruptedException {
    DocumentEntity documentEntity = entityManager.insert(getEntity());
    Document id = documentEntity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    AtomicBoolean condition = new AtomicBoolean(false);
    entityManagerAsync.delete(deleteQuery, c -> condition.set(true));
    await().untilTrue(condition);
    assertTrue(entityManager.select(query).isEmpty());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) 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