Skip to content

Commit

Permalink
Merge pull request #548 from eclipse/feature/recursion-issue-custom-r…
Browse files Browse the repository at this point in the history
…epository

Feature/recursion issue custom repository
  • Loading branch information
dearrudam authored Aug 31, 2024
2 parents de7c8f8 + 68c7412 commit 5ac0745
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> 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() {

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Task> listActiveTasks();

}

0 comments on commit 5ac0745

Please sign in to comment.