Skip to content

Commit d26b528

Browse files
authored
Merge pull request #323 from eclipse-jnosql/BUG-584
Include Test scenarion to Map support on entity
2 parents d2f8cb8 + 2797417 commit d26b528

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.integration;
16+
17+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Entity;
19+
import jakarta.nosql.Id;
20+
21+
import java.util.Map;
22+
import java.util.Objects;
23+
24+
@Entity
25+
public class Computer {
26+
@Id
27+
private String name;
28+
29+
@Column
30+
private Map<String, Program> programs;
31+
32+
private Computer(String name, Map<String, Program> programs) {
33+
this.name = name;
34+
this.programs = programs;
35+
}
36+
37+
@Deprecated
38+
Computer() {
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public Map<String, Program> getPrograms() {
46+
return programs;
47+
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (o == null || getClass() != o.getClass()) return false;
52+
Computer computer = (Computer) o;
53+
return Objects.equals(name, computer.name);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hashCode(name);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "Computer{" +
64+
"name='" + name + '\'' +
65+
", programs=" + programs +
66+
'}';
67+
}
68+
69+
70+
public static Computer of(String name, Map<String, Program> programs) {
71+
return new Computer(name, programs);
72+
}
73+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.integration;
16+
17+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Entity;
19+
import jakarta.nosql.Id;
20+
21+
import java.util.Map;
22+
23+
@Entity
24+
public record ComputerRecord (@Id String name, @Column Map<String, ProgramRecord> programs){
25+
26+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.integration;
16+
17+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Embeddable;
19+
import jakarta.nosql.Id;
20+
21+
import java.util.Map;
22+
import java.util.Objects;
23+
24+
@Embeddable
25+
public class Program {
26+
@Id
27+
private String name;
28+
29+
@Column
30+
private Map<String, String> socialMedia;
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public Map<String, String> getSocialMedia() {
37+
return socialMedia;
38+
}
39+
40+
private Program(String name, Map<String, String> socialMedia) {
41+
this.name = name;
42+
this.socialMedia = socialMedia;
43+
}
44+
45+
46+
@Deprecated
47+
Program() {
48+
}
49+
50+
51+
@Override
52+
public boolean equals(Object o) {
53+
if (o == null || getClass() != o.getClass()) {
54+
return false;
55+
}
56+
Program program = (Program) o;
57+
return Objects.equals(name, program.name);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hashCode(name);
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "Program{" +
68+
"name='" + name + '\'' +
69+
", socialMedia=" + socialMedia +
70+
'}';
71+
}
72+
73+
public static Program of(String name, Map<String, String> socialMedia) {
74+
return new Program(name, socialMedia);
75+
}
76+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.integration;
16+
17+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Embeddable;
19+
import jakarta.nosql.Id;
20+
21+
import java.util.Map;
22+
23+
@Embeddable
24+
public record ProgramRecord(@Id String name, @Column Map<String, String> socialMedia) {
25+
26+
}

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/TemplateIntegrationTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3535

3636
import java.util.List;
37+
import java.util.Map;
3738
import java.util.Optional;
3839

3940
import static java.util.UUID.randomUUID;
@@ -159,4 +160,75 @@ void shouldUpdateEmbeddable() {
159160
soft.assertThat(result.availableTransitions().get(0).restrictedRoleGroups().get(0)).isEqualTo("ADMIN");
160161
});
161162
}
163+
164+
@Test
165+
void shouldInsertEntityWithMap() {
166+
var program = Program.of(
167+
"Renamer",
168+
Map.of("twitter", "x")
169+
);
170+
var id = "Computer" + randomUUID();
171+
var computer = Computer.of(id,Map.of("Renamer", program));
172+
173+
var result = this.template.insert(computer);
174+
175+
SoftAssertions.assertSoftly(soft ->{
176+
soft.assertThat(result).isNotNull();
177+
soft.assertThat(result.getName()).isEqualTo(id);
178+
soft.assertThat(result.getPrograms()).hasSize(1);
179+
soft.assertThat(result.getPrograms().get("Renamer")).isNotNull();
180+
soft.assertThat(result.getPrograms().get("Renamer").getName()).isEqualTo("Renamer");
181+
soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia()).hasSize(1);
182+
soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia().get("twitter")).isEqualTo("x");
183+
});
184+
}
185+
186+
@Test
187+
void shouldInsertEntityWithTwoMap() {
188+
var program = Program.of(
189+
"Renamer",
190+
Map.of("twitter", "x")
191+
);
192+
193+
var program2 = Program.of(
194+
"Apple",
195+
Map.of("instagram", "x")
196+
);
197+
var id = "Computer" + randomUUID();
198+
var computer = Computer.of(id,Map.of("Renamer", program,
199+
"Apple", program2));
200+
201+
var result = this.template.insert(computer);
202+
203+
SoftAssertions.assertSoftly(soft ->{
204+
soft.assertThat(result).isNotNull();
205+
soft.assertThat(result.getName()).isEqualTo(id);
206+
soft.assertThat(result.getPrograms()).hasSize(2);
207+
soft.assertThat(result.getPrograms().get("Renamer")).isNotNull();
208+
soft.assertThat(result.getPrograms().get("Renamer").getName()).isEqualTo("Renamer");
209+
soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia()).hasSize(1);
210+
soft.assertThat(result.getPrograms().get("Renamer").getSocialMedia().get("twitter")).isEqualTo("x");
211+
});
212+
}
213+
214+
@Test
215+
void shouldInsertEntityWithMapUsingRecord() {
216+
var program = new ProgramRecord(
217+
"Renamer",
218+
Map.of("twitter", "x")
219+
);
220+
var computer = new ComputerRecord("Computer",Map.of("Renamer", program));
221+
222+
var result = this.template.insert(computer);
223+
224+
SoftAssertions.assertSoftly(soft ->{
225+
soft.assertThat(result).isNotNull();
226+
soft.assertThat(result.name()).isEqualTo("Computer");
227+
soft.assertThat(result.programs()).hasSize(1);
228+
soft.assertThat(result.programs().get("Renamer")).isNotNull();
229+
soft.assertThat(result.programs().get("Renamer").name()).isEqualTo("Renamer");
230+
soft.assertThat(result.programs().get("Renamer").socialMedia()).hasSize(1);
231+
soft.assertThat(result.programs().get("Renamer").socialMedia().get("twitter")).isEqualTo("x");
232+
});
233+
}
162234
}

0 commit comments

Comments
 (0)