Search in sources :

Example 71 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerTest method shouldRetrieveListSubdocumentList.

@Test
public void shouldRetrieveListSubdocumentList() {
    DocumentEntity entity = entityManager.insert(createSubdocumentList());
    Document key = entity.find("_id").get();
    DocumentQuery query = select().from(DocumentEntityGerator.COLLECTION_NAME).where(key.getName()).eq(key.get()).build();
    DocumentEntity documentEntity = entityManager.singleResult(query).get();
    assertNotNull(documentEntity);
    List<List<Document>> contacts = (List<List<Document>>) documentEntity.find("contacts").get().get();
    assertEquals(3, contacts.size());
    assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) DocumentQuery.select(jakarta.nosql.document.DocumentQuery.select) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) ArrayList(java.util.ArrayList) TypeReference(jakarta.nosql.TypeReference) DocumentQuery(jakarta.nosql.document.DocumentQuery) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Documents(org.eclipse.jnosql.communication.document.Documents) Document(jakarta.nosql.document.Document) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) List(java.util.List) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) DocumentDeleteQuery.delete(jakarta.nosql.document.DocumentDeleteQuery.delete) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SECONDS(java.util.concurrent.TimeUnit.SECONDS) DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Example 72 with Document

use of jakarta.nosql.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 = ValueUtil.convert(document.getValue());
    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 = ValueUtil.convertToList(document.getValue());
            return Filters.in(document.getName(), inList.toArray());
        case NOT:
            return Filters.not(convert(document.get(DocumentCondition.class)));
        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(jakarta.nosql.document.Document) DocumentCondition(jakarta.nosql.document.DocumentCondition)

Example 73 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method shouldFindDocument2.

@Test
public void shouldFindDocument2() {
    DocumentEntity entity = entityManager.insert(getEntity());
    Optional<Document> id = entity.find("_id");
    DocumentQuery query = select().from(COLLECTION_NAME).where("name").eq("Poliana").and("city").eq("Salvador").and("_id").eq(id.get().get()).build();
    List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
    assertFalse(entities.isEmpty());
    assertThat(entities, contains(entity));
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Example 74 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method shouldFindDocument3.

@Test
public void shouldFindDocument3() {
    DocumentEntity entity = entityManager.insert(getEntity());
    Optional<Document> id = entity.find("_id");
    DocumentQuery query = select().from(COLLECTION_NAME).where("name").eq("Poliana").or("city").eq("Salvador").and(id.get().getName()).eq(id.get().get()).build();
    List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
    assertFalse(entities.isEmpty());
    assertThat(entities, contains(entity));
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Example 75 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method shouldFindDocument.

@Test
public void shouldFindDocument() {
    DocumentEntity entity = entityManager.insert(getEntity());
    Optional<Document> id = entity.find("_id");
    DocumentQuery query = select().from(COLLECTION_NAME).where("_id").eq(id.get().get()).build();
    List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
    assertFalse(entities.isEmpty());
    assertThat(entities, contains(entity));
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (jakarta.nosql.document.Document)196 Test (org.junit.jupiter.api.Test)140 DocumentEntity (jakarta.nosql.document.DocumentEntity)115 DocumentQuery (jakarta.nosql.document.DocumentQuery)93 DocumentCondition (jakarta.nosql.document.DocumentCondition)60 List (java.util.List)60 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)44 Arrays.asList (java.util.Arrays.asList)33 ArrayList (java.util.ArrayList)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 ValueSource (org.junit.jupiter.params.provider.ValueSource)26 TypeReference (jakarta.nosql.TypeReference)21 HashMap (java.util.HashMap)20 Map (java.util.Map)17 Collectors (java.util.stream.Collectors)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)16 DocumentQuery.select (jakarta.nosql.document.DocumentQuery.select)14 Matchers.contains (org.hamcrest.Matchers.contains)14 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14