diff --git a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/Computer.java b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/Computer.java new file mode 100644 index 000000000..0ed6c9eb0 --- /dev/null +++ b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/Computer.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 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.integration; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +import java.util.Map; +import java.util.Objects; + +@Entity +public class Computer { + @Id + private String name; + + @Column + private Map programs; + + private Computer(String name, Map programs) { + this.name = name; + this.programs = programs; + } + + @Deprecated + Computer() { + } + + public String getName() { + return name; + } + + public Map getPrograms() { + return programs; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + Computer computer = (Computer) o; + return Objects.equals(name, computer.name); + } + + @Override + public int hashCode() { + return Objects.hashCode(name); + } + + @Override + public String toString() { + return "Computer{" + + "name='" + name + '\'' + + ", programs=" + programs + + '}'; + } + + + public static Computer of(String name, Map programs) { + return new Computer(name, programs); + } +} diff --git a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/ComputerRecord.java b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/ComputerRecord.java new file mode 100644 index 000000000..939329690 --- /dev/null +++ b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/ComputerRecord.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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.integration; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +import java.util.Map; + +@Entity +public record ComputerRecord (@Id String name, @Column Map programs){ + +} diff --git a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/Program.java b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/Program.java new file mode 100644 index 000000000..3b580fd4c --- /dev/null +++ b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/Program.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025 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.integration; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; +import jakarta.nosql.Id; + +import java.util.Map; +import java.util.Objects; + +@Embeddable +public class Program { + @Id + private String name; + + @Column + private Map socialMedia; + + public String getName() { + return name; + } + + public Map getSocialMedia() { + return socialMedia; + } + + private Program(String name, Map socialMedia) { + this.name = name; + this.socialMedia = socialMedia; + } + + + @Deprecated + Program() { + } + + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) { + return false; + } + Program program = (Program) o; + return Objects.equals(name, program.name); + } + + @Override + public int hashCode() { + return Objects.hashCode(name); + } + + @Override + public String toString() { + return "Program{" + + "name='" + name + '\'' + + ", socialMedia=" + socialMedia + + '}'; + } + + public static Program of(String name, Map socialMedia) { + return new Program(name, socialMedia); + } +} \ No newline at end of file diff --git a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/ProgramRecord.java b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/ProgramRecord.java new file mode 100644 index 000000000..cd1b1d215 --- /dev/null +++ b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/ProgramRecord.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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.integration; + +import jakarta.nosql.Column; +import jakarta.nosql.Embeddable; +import jakarta.nosql.Id; + +import java.util.Map; + +@Embeddable +public record ProgramRecord(@Id String name, @Column Map socialMedia) { + +} \ No newline at end of file diff --git a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/TemplateIntegrationTest.java b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/TemplateIntegrationTest.java index 3f4df7e7f..cbe4026f8 100644 --- a/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/TemplateIntegrationTest.java +++ b/jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/TemplateIntegrationTest.java @@ -34,6 +34,7 @@ import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import java.util.List; +import java.util.Map; import java.util.Optional; import static java.util.UUID.randomUUID; @@ -159,4 +160,75 @@ void shouldUpdateEmbeddable() { soft.assertThat(result.availableTransitions().get(0).restrictedRoleGroups().get(0)).isEqualTo("ADMIN"); }); } + + @Test + void shouldInsertEntityWithMap() { + var program = Program.of( + "Renamer", + Map.of("twitter", "x") + ); + var id = "Computer" + randomUUID(); + var computer = Computer.of(id,Map.of("Renamer", program)); + + var result = this.template.insert(computer); + + SoftAssertions.assertSoftly(soft ->{ + soft.assertThat(result).isNotNull(); + soft.assertThat(result.getName()).isEqualTo(id); + soft.assertThat(result.getPrograms()).hasSize(1); + soft.assertThat(result.getPrograms().get("Renamer")).isNotNull(); + soft.assertThat(result.getPrograms().get("Renamer").getName()).isEqualTo("Renamer"); + soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia()).hasSize(1); + soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia().get("twitter")).isEqualTo("x"); + }); + } + + @Test + void shouldInsertEntityWithTwoMap() { + var program = Program.of( + "Renamer", + Map.of("twitter", "x") + ); + + var program2 = Program.of( + "Apple", + Map.of("instagram", "x") + ); + var id = "Computer" + randomUUID(); + var computer = Computer.of(id,Map.of("Renamer", program, + "Apple", program2)); + + var result = this.template.insert(computer); + + SoftAssertions.assertSoftly(soft ->{ + soft.assertThat(result).isNotNull(); + soft.assertThat(result.getName()).isEqualTo(id); + soft.assertThat(result.getPrograms()).hasSize(2); + soft.assertThat(result.getPrograms().get("Renamer")).isNotNull(); + soft.assertThat(result.getPrograms().get("Renamer").getName()).isEqualTo("Renamer"); + soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia()).hasSize(1); + soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia().get("twitter")).isEqualTo("x"); + }); + } + + @Test + void shouldInsertEntityWithMapUsingRecord() { + var program = new ProgramRecord( + "Renamer", + Map.of("twitter", "x") + ); + var computer = new ComputerRecord("Computer",Map.of("Renamer", program)); + + var result = this.template.insert(computer); + + SoftAssertions.assertSoftly(soft ->{ + soft.assertThat(result).isNotNull(); + soft.assertThat(result.name()).isEqualTo("Computer"); + soft.assertThat(result.programs()).hasSize(1); + soft.assertThat(result.programs().get("Renamer")).isNotNull(); + soft.assertThat(result.programs().get("Renamer").name()).isEqualTo("Renamer"); + soft.assertThat(result.programs().get("Renamer").socialMedia()).hasSize(1); + soft.assertThat(result.programs().get("Renamer").socialMedia().get("twitter")).isEqualTo("x"); + }); + } }