use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.
the class DocumentRepositoryProxyTest method shouldFindByNameLike.
@Test
public void shouldFindByNameLike() {
Person ada = Person.builder().withAge(20).withName("Ada").build();
when(template.select(any(DocumentQuery.class))).thenReturn(Stream.of(ada));
personRepository.findByNameLike("Ada");
ArgumentCaptor<DocumentQuery> captor = ArgumentCaptor.forClass(DocumentQuery.class);
verify(template).select(captor.capture());
DocumentQuery query = captor.getValue();
DocumentCondition condition = query.getCondition().get();
assertEquals("Person", query.getDocumentCollection());
assertEquals(LIKE, condition.getCondition());
assertEquals(Document.of("name", "Ada"), condition.getDocument());
}
use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.
the class DefaultDocumentTemplateTest method shouldDeleteEntity.
@Test
public void shouldDeleteEntity() {
subject.delete(Person.class, "10");
ArgumentCaptor<DocumentDeleteQuery> queryCaptor = ArgumentCaptor.forClass(DocumentDeleteQuery.class);
verify(managerMock).delete(queryCaptor.capture());
DocumentDeleteQuery query = queryCaptor.getValue();
DocumentCondition condition = query.getCondition().get();
assertEquals("Person", query.getDocumentCollection());
assertEquals(DocumentCondition.eq(Document.of("_id", 10L)), condition);
}
use of jakarta.nosql.document.DocumentCondition in project jnosql-diana by eclipse.
the class ParamsBinderTest method shouldConvert2.
@Test
public void shouldConvert2() {
Method method = Stream.of(PersonRepository.class.getMethods()).filter(m -> m.getName().equals("findByAgeAndName")).findFirst().get();
ClassMapping classMapping = mappings.get(Person.class);
RepositoryDocumentObserverParser parser = new RepositoryDocumentObserverParser(classMapping);
paramsBinder = new ParamsBinder(classMapping, converters);
SelectMethodProvider selectMethodFactory = SelectMethodProvider.get();
SelectQuery selectQuery = selectMethodFactory.apply(method, classMapping.getName());
SelectQueryConverter converter = ServiceLoaderProvider.get(SelectQueryConverter.class);
DocumentQueryParams queryParams = converter.apply(selectQuery, parser);
Params params = queryParams.getParams();
paramsBinder.bind(params, new Object[] { 10L, "Ada" }, method);
DocumentQuery query = queryParams.getQuery();
DocumentCondition columnCondition = query.getCondition().get();
List<DocumentCondition> conditions = columnCondition.getDocument().get(new TypeReference<List<DocumentCondition>>() {
});
List<Object> values = conditions.stream().map(DocumentCondition::getDocument).map(Document::getValue).map(Value::get).collect(Collectors.toList());
assertEquals(10, values.get(0));
assertEquals("Ada", values.get(1));
}
use of jakarta.nosql.document.DocumentCondition in project jnosql-diana-driver by eclipse.
the class QueryAQLConverter method definesCondition.
private static void definesCondition(DocumentCondition condition, StringBuilder aql, Map<String, Object> params, char entity, int counter) {
Document document = condition.getDocument();
switch(condition.getCondition()) {
case IN:
appendCondition(aql, params, entity, document, IN);
return;
case EQUALS:
appendCondition(aql, params, entity, document, EQUALS);
return;
case GREATER_EQUALS_THAN:
appendCondition(aql, params, entity, document, GREATER_EQUALS_THAN);
return;
case GREATER_THAN:
appendCondition(aql, params, entity, document, GREATER_THAN);
return;
case LESSER_THAN:
appendCondition(aql, params, entity, document, LESSER_THAN);
return;
case LESSER_EQUALS_THAN:
appendCondition(aql, params, entity, document, LESSER_EQUALS_THAN);
return;
case LIKE:
appendCondition(aql, params, entity, document, LIKE);
return;
case AND:
for (DocumentCondition dc : document.get(new TypeReference<List<DocumentCondition>>() {
})) {
if (isFirstCondition(aql, counter)) {
aql.append(AND);
}
definesCondition(dc, aql, params, entity, ++counter);
}
return;
case OR:
for (DocumentCondition dc : document.get(new TypeReference<List<DocumentCondition>>() {
})) {
if (isFirstCondition(aql, counter)) {
aql.append(OR);
}
definesCondition(dc, aql, params, entity, ++counter);
}
return;
case NOT:
DocumentCondition documentCondition = document.get(DocumentCondition.class);
aql.append(NOT);
definesCondition(documentCondition, aql, params, entity, ++counter);
return;
default:
throw new IllegalArgumentException("The condition does not support in AQL: " + condition.getCondition());
}
}
use of jakarta.nosql.document.DocumentCondition 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");
}
}
Aggregations