diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f1fbcc2ce..fa2395370 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -12,6 +12,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version - Include between query support at MongoDB - Include Graph as Apache TinkerPop +- Include UUID support to MongoDB === Changed diff --git a/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueReader.java b/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueReader.java new file mode 100644 index 000000000..8c89a4629 --- /dev/null +++ b/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueReader.java @@ -0,0 +1,37 @@ +/* + * 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.ValueReader; + +import java.util.UUID; + +public class UUIDValueReader implements ValueReader { + + @Override + public boolean test(Class type) { + return UUID.class.equals(type); + } + + + @SuppressWarnings("unchecked") + @Override + public T read(Class type, Object value) { + if (value instanceof UUID) { + return (T) value; + } + return null; + } +} diff --git a/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueWriter.java b/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueWriter.java new file mode 100644 index 000000000..40cdbd1ab --- /dev/null +++ b/jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueWriter.java @@ -0,0 +1,38 @@ +/* + * 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 java.util.Objects; +import java.util.UUID; + +public class UUIDValueWriter implements ValueWriter { + + @Override + public boolean test(Class type) { + return UUID.class.equals(type); + } + + + @Override + public String write(UUID uuid) { + if(Objects.nonNull(uuid)) { + return uuid.toString(); + } + return null; + } + +} diff --git a/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueReader b/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueReader index 98dd20367..d76361057 100644 --- a/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueReader +++ b/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueReader @@ -1 +1,2 @@ -org.eclipse.jnosql.databases.mongodb.communication.BinaryValueReader \ No newline at end of file +org.eclipse.jnosql.databases.mongodb.communication.BinaryValueReader +org.eclipse.jnosql.databases.mongodb.communication.UUIDValueReader diff --git a/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueWriter b/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueWriter new file mode 100644 index 000000000..1c499b074 --- /dev/null +++ b/jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueWriter @@ -0,0 +1 @@ +org.eclipse.jnosql.databases.mongodb.communication.UUIDValueWriter diff --git a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManagerTest.java b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManagerTest.java index ec063a2b2..0d00df439 100644 --- a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManagerTest.java +++ b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManagerTest.java @@ -607,6 +607,21 @@ void shouldUpdateNull(){ }); } + @Test + void shouldInsertUUID() { + var entity = getEntity(); + entity.add("uuid", UUID.randomUUID()); + var documentEntity = entityManager.insert(entity); + Optional uuid = documentEntity.find("uuid"); + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(uuid).isPresent(); + Element element = uuid.orElseThrow(); + soft.assertThat(element.name()).isEqualTo("uuid"); + soft.assertThat(element.get(UUID.class)).isInstanceOf(UUID.class); + }); + + } + private CommunicationEntity createDocumentList() { CommunicationEntity entity = CommunicationEntity.of("AppointmentBook");