Skip to content

Include Test scenarion to Map support on entity #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String, Program> programs;

private Computer(String name, Map<String, Program> programs) {
this.name = name;
this.programs = programs;
}

@Deprecated
Computer() {
}

public String getName() {
return name;
}

public Map<String, Program> 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<String, Program> programs) {
return new Computer(name, programs);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, ProgramRecord> programs){

}
Original file line number Diff line number Diff line change
@@ -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<String, String> socialMedia;

public String getName() {
return name;
}

public Map<String, String> getSocialMedia() {
return socialMedia;
}

private Program(String name, Map<String, String> 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<String, String> socialMedia) {
return new Program(name, socialMedia);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> socialMedia) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
});
}
}