From 209d9cf316f32f96526c28732b548b9c121556ad Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 31 Aug 2024 09:05:20 -0300 Subject: [PATCH 1/4] test: added test classes for more CustomRepositoryHandler test scenarios Signed-off-by: Maximillian Arruda --- .../mapping/semistructured/entities/Task.java | 81 +++++++++++++++++++ .../semistructured/entities/TaskBuilder.java | 41 ++++++++++ .../mapping/semistructured/query/Tasks.java | 29 +++++++ 3 files changed, 151 insertions(+) create mode 100644 jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/Task.java create mode 100644 jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/TaskBuilder.java create mode 100644 jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/Tasks.java diff --git a/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/Task.java b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/Task.java new file mode 100644 index 000000000..ebe811a83 --- /dev/null +++ b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/Task.java @@ -0,0 +1,81 @@ +/* + * 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: + * + * Maximillian Arruda + */ +package org.eclipse.jnosql.mapping.semistructured.entities; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; + +import java.util.Objects; + +@Entity +public class Task { + + public static TaskBuilder builder() { + return new TaskBuilder(); + } + + @Id + private String id; + + @Column + private String description; + + @Column + private boolean active; + + Task(){ + } + + Task(String id, String description, boolean active) { + this.id = id; + this.description = description; + this.active = active; + } + + public String getId() { + return id; + } + + public String getDescription() { + return description; + } + + public boolean isActive() { + return active; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Task task = (Task) o; + return active == task.active && Objects.equals(id, task.id) && Objects.equals(description, task.description); + } + + @Override + public int hashCode() { + return Objects.hash(id, description, active); + } + + @Override + public String toString() { + return "Task{" + + "id='" + id + '\'' + + ", description='" + description + '\'' + + ", active=" + active + + '}'; + } +} diff --git a/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/TaskBuilder.java b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/TaskBuilder.java new file mode 100644 index 000000000..cffdbcd0a --- /dev/null +++ b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/entities/TaskBuilder.java @@ -0,0 +1,41 @@ +/* + * 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: + * + * Maximillian Arruda + */ +package org.eclipse.jnosql.mapping.semistructured.entities; + +public class TaskBuilder { + + private String id; + private String description; + private boolean active = true; + + public TaskBuilder id(String id) { + this.id = id; + return this; + } + + public TaskBuilder description(String description) { + this.description = description; + return this; + } + + public TaskBuilder active(boolean active) { + this.active = active; + return this; + } + + public Task build() { + return new Task(id, description, active); + } +} diff --git a/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/Tasks.java b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/Tasks.java new file mode 100644 index 000000000..f89b499d3 --- /dev/null +++ b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/Tasks.java @@ -0,0 +1,29 @@ +/* + * 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: + * + * Maximillian Arruda + */ +package org.eclipse.jnosql.mapping.semistructured.query; + +import jakarta.data.repository.Query; +import jakarta.data.repository.Repository; +import org.eclipse.jnosql.mapping.semistructured.entities.Task; + +import java.util.List; + +@Repository +public interface Tasks { + + @Query("from Task where active = true") + List listActiveTasks(); + +} From 83555d7acd55bd3ef7853a0ef8c1acef8cb9503f Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 31 Aug 2024 09:05:33 -0300 Subject: [PATCH 2/4] test: added for more CustomRepositoryHandler test scenarios Signed-off-by: Maximillian Arruda --- .../query/CustomRepositoryHandlerTest.java | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandlerTest.java b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandlerTest.java index 27cfae466..d50650017 100644 --- a/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandlerTest.java +++ b/jnosql-mapping/jnosql-mapping-semistructured/src/test/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandlerTest.java @@ -31,6 +31,7 @@ import org.eclipse.jnosql.mapping.semistructured.MockProducer; import org.eclipse.jnosql.mapping.semistructured.SemiStructuredTemplate; import org.eclipse.jnosql.mapping.semistructured.entities.Person; +import org.eclipse.jnosql.mapping.semistructured.entities.Task; import org.jboss.weld.junit5.auto.AddExtensions; import org.jboss.weld.junit5.auto.AddPackages; import org.jboss.weld.junit5.auto.EnableAutoWeld; @@ -62,15 +63,29 @@ class CustomRepositoryHandlerTest { private People people; + private Tasks tasks; + @BeforeEach void setUp() { template = Mockito.mock(SemiStructuredTemplate.class); - CustomRepositoryHandler customRepositoryHandler = CustomRepositoryHandler.builder() + CustomRepositoryHandler customRepositoryHandlerForPeople = CustomRepositoryHandler.builder() .entitiesMetadata(entitiesMetadata) - .template(template).customRepositoryType(People.class) + .template(template) + .customRepositoryType(People.class) .converters(converters).build(); + people = (People) Proxy.newProxyInstance(People.class.getClassLoader(), new Class[]{People.class}, - customRepositoryHandler); + customRepositoryHandlerForPeople); + + CustomRepositoryHandler customRepositoryHandlerForTasks = CustomRepositoryHandler.builder() + .entitiesMetadata(entitiesMetadata) + .template(template) + .customRepositoryType(Tasks.class) + .converters(converters).build(); + + tasks = (Tasks) Proxy.newProxyInstance(Tasks.class.getClassLoader(), new Class[]{Tasks.class}, + customRepositoryHandlerForTasks); + } @Test @@ -384,6 +399,27 @@ void shouldExecuteQueryWithVoid(){ Assertions.assertThat(query).isEqualTo("delete from Person where name = :name"); } + @Test + void shouldExecuteFixedQuery() { + + var preparedStatement = Mockito.mock(org.eclipse.jnosql.mapping.semistructured.PreparedStatement.class); + Mockito.when(template.prepare(Mockito.anyString(), Mockito.anyString())) + .thenReturn(preparedStatement); + Mockito.when(template.query(Mockito.anyString())) + .thenReturn(Stream.of(Task.builder().description("refactor project A").build())); + + var result = tasks.listActiveTasks(); + + Assertions.assertThat(result).isNotNull().isInstanceOf(List.class); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + Mockito.verify(template).prepare(captor.capture(), Mockito.eq("Task")); + Mockito.verifyNoMoreInteractions(template); + var query = captor.getValue(); + + Assertions.assertThat(query).isEqualTo("from Task where active = true"); + + } + @Test void shouldExecuteCountBy() { From 01d9624ea050fa37953c530d0523810fb751d167 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 31 Aug 2024 09:06:32 -0300 Subject: [PATCH 3/4] chore: fixed recursive calling to avoid stack overflow issue Signed-off-by: Maximillian Arruda --- .../mapping/semistructured/query/CustomRepositoryHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jnosql-mapping/jnosql-mapping-semistructured/src/main/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandler.java b/jnosql-mapping/jnosql-mapping-semistructured/src/main/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandler.java index 5b54ab260..870c864e5 100644 --- a/jnosql-mapping/jnosql-mapping-semistructured/src/main/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandler.java +++ b/jnosql-mapping/jnosql-mapping-semistructured/src/main/java/org/eclipse/jnosql/mapping/semistructured/query/CustomRepositoryHandler.java @@ -146,7 +146,7 @@ public Object invoke(Object instance, Method method, Object[] params) throws Thr return Void.class; } - return unwrapInvocationTargetException(() -> repository(method).invoke(instance, method, params)); + return unwrapInvocationTargetException(() -> repository(method).executeQuery(instance, method, params)); } case COUNT_BY, COUNT_ALL -> { From 68c7412516a5d1f77b42f94fec3131666e319f33 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 31 Aug 2024 09:09:34 -0300 Subject: [PATCH 4/4] changelog: added fixed item - Fix recursion calling to avoid stack overflow on the custom repository's query methods with @Query annotation with predefined queries Signed-off-by: Maximillian Arruda --- CHANGELOG.adoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 5df08fdfc..c17078fb7 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -25,6 +25,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version - Invalid deserialization of maps with generic values - Make sure at the serialization to the field, the API does not return any communication layer, but standard Java types - Fix the like query at the JDQL +- Fix recursion calling to avoid stack overflow on the custom repository's query methods with @Query annotation with predefined queries === Removed