Skip to content

Commit

Permalink
Merge pull request #301 from eclipse/isolate-mongodb
Browse files Browse the repository at this point in the history
[BUG] MongoDB conversions applied also to other databases
  • Loading branch information
otaviojava authored Dec 15, 2024
2 parents b6bbcf4 + 32bc587 commit cd11dea
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
- Update API using Apache Tinkerpop
- Update package name of Graph to Tinkerpop

== Fixed

- MongoDB conversions applied also to other databases

== [1.1.3] - 2024-10-24

=== Added
Expand Down
2 changes: 1 addition & 1 deletion jnosql-arangodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>The Eclipse JNoSQL layer to ArangoDB</description>

<properties>
<arango.driver>7.13.0</arango.driver>
<arango.driver>7.15.0</arango.driver>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private static void definesCondition(CriteriaCondition condition,
char entity, int counter) {

Element document = condition.element();
int localCounter = counter;
switch (condition.condition()) {
case IN:
appendCondition(aql, params, entity, document, IN);
Expand Down Expand Up @@ -157,27 +158,26 @@ private static void definesCondition(CriteriaCondition condition,
for (CriteriaCondition dc : document.get(new TypeReference<List<CriteriaCondition>>() {
})) {

if (isFirstCondition(aql, counter)) {
if (isFirstCondition(aql, localCounter)) {
aql.append(AND);
}
definesCondition(dc, aql, params, entity, ++counter);
definesCondition(dc, aql, params, entity, ++localCounter);
}
return;
case OR:

for (CriteriaCondition dc : document.get(new TypeReference<List<CriteriaCondition>>() {
})) {
if (isFirstCondition(aql, counter)) {
if (isFirstCondition(aql, localCounter)) {
aql.append(OR);
}
definesCondition(dc, aql, params, entity, ++counter);
definesCondition(dc, aql, params, entity, ++localCounter);
}
return;
case NOT:
CriteriaCondition documentCondition = document.get(CriteriaCondition.class);
aql.append(NOT);
aql.append(START_EXPRESSION);
definesCondition(documentCondition, aql, params, entity, ++counter);
aql.append(NOT).append(START_EXPRESSION);
definesCondition(documentCondition, aql, params, entity, ++localCounter);
aql.append(END_EXPRESSION);
return;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ private DocumentQueryConversor() {

public static Bson convert(CriteriaCondition condition) {
Element document = condition.element();
Object value = ValueUtil.convert(document.value());
Object value = ValueUtil.convert(document.value(), MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
return switch (condition.condition()) {
case EQUALS -> Filters.eq(document.name(), value);
case GREATER_THAN -> Filters.gt(document.name(), value);
case GREATER_EQUALS_THAN -> Filters.gte(document.name(), value);
case LESSER_THAN -> Filters.lt(document.name(), value);
case LESSER_EQUALS_THAN -> Filters.lte(document.name(), value);
case IN -> {
List<Object> inList = ValueUtil.convertToList(document.value());
List<Object> inList = ValueUtil.convertToList(document.value(), MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
yield Filters.in(document.name(), inList.toArray());
}
case NOT -> {
Expand All @@ -69,7 +69,7 @@ public static Bson convert(CriteriaCondition condition) {
.map(DocumentQueryConversor::convert).toList());
}
case BETWEEN -> {
List<Object> betweenList = ValueUtil.convertToList(document.value());
List<Object> betweenList = ValueUtil.convertToList(document.value(), MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
yield Filters.and(Filters.gte(document.name(), betweenList.get(0)),
Filters.lte(document.name(), betweenList.get(1)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.StreamSupport;

import static java.util.stream.StreamSupport.stream;

final class MongoDBUtils {
static final String ID_FIELD = "_id";

private static final Function<Object, String> KEY_DOCUMENT = d -> cast(d).name();
private static final UnaryOperator<Object> VALUE_DOCUMENT = d -> MongoDBUtils.convert(cast(d).value());

private MongoDBUtils() {
}
Expand All @@ -48,7 +45,7 @@ static Document getDocument(CommunicationEntity entity) {
}

private static Object convert(Value value) {
Object val = ValueUtil.convert(value);
Object val = ValueUtil.convert(value, MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
if (val instanceof Element subDocument) {
Object converted = convert(subDocument.value());
return new Document(subDocument.name(), converted);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.databases.mongodb.communication;

import org.eclipse.jnosql.communication.ValueWriter;
import org.eclipse.jnosql.communication.ValueWriterDecorator;

import java.util.UUID;

final class MongoDBValueWriteDecorator<T, S> implements ValueWriter<T, S> {

@SuppressWarnings("rawtypes")
static final ValueWriter MONGO_DB_VALUE_WRITER = new MongoDBValueWriteDecorator();

@SuppressWarnings("rawtypes")
private static final ValueWriter DEFAULT = ValueWriterDecorator.getInstance();

private static final UUIDValueWriter UUID_VALUE_WRITER = new UUIDValueWriter();


@Override
public boolean test(Class<?> type) {
return UUID_VALUE_WRITER.test(type) || DEFAULT.test(type);
}

@SuppressWarnings("unchecked")
@Override
public S write(T type) {
if(type != null && UUID_VALUE_WRITER.test(type.getClass())) {
return (S) UUID_VALUE_WRITER.write((UUID) type);
} else {
return (S) DEFAULT.write(type);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
org.eclipse.jnosql.databases.mongodb.communication.BinaryValueReader
org.eclipse.jnosql.databases.mongodb.communication.UUIDValueReader

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@
*/
package org.eclipse.jnosql.databases.mongodb.communication;

import org.eclipse.jnosql.communication.ValueReader;
import org.junit.jupiter.api.Test;

import java.util.UUID;

public class UUIDValueReader implements ValueReader {
import static org.junit.jupiter.api.Assertions.*;

@Override
public boolean test(Class<?> type) {
return UUID.class.equals(type);
}
@SuppressWarnings("rawtypes")
class MongoDBValueWriteDecoratorTest {

private final MongoDBValueWriteDecorator<Object, String> valueWriter = new MongoDBValueWriteDecorator<>();

@SuppressWarnings("unchecked")
@Override
public <T> T read(Class<T> type, Object value) {
if (value instanceof UUID) {
return (T) value;
}
return null;
@Test
void shouldTestUUIDType() {
assertTrue(valueWriter.test(UUID.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
* Alessandro Moscatelli
*/
package org.eclipse.jnosql.databases.mongodb.integration;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.UUID;

@Entity
public record MongoDBBook(@Id UUID id, @Column String title, @Column String author) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ void shouldUpdateNullValues(){
});
}

@Test
void shouldCreateMongoDBBook() {
var id = randomUUID();
var title = "Persistence with MongoDB";
var author = "Otavio Santana";
var book = template.insert(new MongoDBBook(id, title, author));

SoftAssertions.assertSoftly(softly -> {
softly.assertThat(book).isNotNull();
softly.assertThat(book.id()).isEqualTo(id);
softly.assertThat(book.title()).isEqualTo(title);
softly.assertThat(book.author()).isEqualTo(author);
});
}

@Test
void shouldFindByUUID() {
var id = randomUUID();
var title = "Persistence with MongoDB";
var author = "Otavio Santana";
var book = template.insert(new MongoDBBook(id, title, author));

var optional = template.find(MongoDBBook.class, id);
assertThat(optional).isPresent();
assertThat(optional.get().id()).isEqualTo(id);
assertThat(optional.get().title()).isEqualTo(title);
assertThat(optional.get().author()).isEqualTo(author);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import org.eclipse.jnosql.communication.semistructured.Element;
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentManager;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension;
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.reflection.Reflections;
import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension;
import org.eclipse.jnosql.mapping.semistructured.EntityConverter;
import org.eclipse.jnosql.mapping.semistructured.EventPersistManager;
import org.jboss.weld.junit5.auto.AddExtensions;
Expand Down Expand Up @@ -243,5 +243,4 @@ void shouldReturnErrorOnCountByFilterMethod() {
assertThrows(NullPointerException.class, () -> template.count((Class<Person>) null, filter));
assertThrows(NullPointerException.class, () -> template.count(Person.class, null));
}

}

0 comments on commit cd11dea

Please sign in to comment.