Skip to content

Commit

Permalink
test: create scenarios at Repository proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 31, 2024
1 parent bf40314 commit eebe202
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
Expand Up @@ -14,9 +14,13 @@
*/
package org.eclipse.jnosql.mapping.semistructured.query;

import jakarta.data.page.PageRequest;
import org.eclipse.jnosql.communication.semistructured.DeleteQuery;
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
import org.eclipse.jnosql.mapping.core.repository.DynamicQueryMethodReturn;
import org.eclipse.jnosql.mapping.core.repository.DynamicReturn;
import org.eclipse.jnosql.mapping.core.repository.RepositoryReflectionUtils;
import org.eclipse.jnosql.mapping.core.repository.SpecialParameters;

import java.lang.reflect.Method;
import java.util.Map;
Expand All @@ -41,6 +45,16 @@ protected Object executeQuery(Object instance, Method method, Object[] params) {
return methodReturn.execute();
}

@Override
protected Object executeCursorPagination(Object instance, Method method, Object[] params) {
SelectQuery query = query(method, params);
SpecialParameters special = DynamicReturn.findSpecialParameters(params);
PageRequest<?> pageRequest = special.pageRequest()
.orElseThrow(() -> new IllegalArgumentException("Pageable is required in the method signature as parameter at " + method));
return this.template().selectCursor(query, pageRequest);
}


@Override
protected Object executeDeleteByAll(Object instance, Method method, Object[] params) {
DeleteQuery deleteQuery = deleteQuery(method, params);
Expand Down
Expand Up @@ -20,27 +20,25 @@
import jakarta.data.page.Page;
import jakarta.data.page.PageRequest;
import org.eclipse.jnosql.communication.Params;
import org.eclipse.jnosql.communication.query.DeleteQuery;
import org.eclipse.jnosql.communication.query.SelectQuery;
import org.eclipse.jnosql.communication.query.method.DeleteMethodProvider;
import org.eclipse.jnosql.communication.query.method.SelectMethodProvider;
import org.eclipse.jnosql.communication.semistructured.CommunicationObserverParser;
import org.eclipse.jnosql.communication.semistructured.CriteriaCondition;
import org.eclipse.jnosql.communication.semistructured.DeleteQueryParams;
import org.eclipse.jnosql.communication.semistructured.DeleteQueryParser;
import org.eclipse.jnosql.communication.semistructured.Element;
import org.eclipse.jnosql.communication.semistructured.QueryParams;
import org.eclipse.jnosql.communication.semistructured.SelectQueryParser;
import org.eclipse.jnosql.mapping.semistructured.MappingQuery;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.NoSQLPage;
import org.eclipse.jnosql.mapping.semistructured.SemistructuredTemplate;
import org.eclipse.jnosql.mapping.core.query.AbstractRepositoryProxy;
import org.eclipse.jnosql.mapping.core.repository.SpecialParameters;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.core.repository.DynamicReturn;
import org.eclipse.jnosql.mapping.core.repository.SpecialParameters;
import org.eclipse.jnosql.mapping.core.util.ParamsBinder;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata;
import org.eclipse.jnosql.mapping.semistructured.MappingQuery;
import org.eclipse.jnosql.mapping.semistructured.SemistructuredTemplate;

import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down
Expand Up @@ -26,7 +26,7 @@


/**
* Proxy handler to generate {@link org.eclipse.jnosql.mapping.NoSQLRepository} for column-based repositories.
* Proxy handler to generate {@link org.eclipse.jnosql.mapping.NoSQLRepository} for Semistructure database repositories.
*
* @param <T> The entity type managed by the repository.
* @param <K> The key type used for column-based operations.
Expand Down
Expand Up @@ -20,7 +20,11 @@
import jakarta.data.page.PageRequest;
import jakarta.data.repository.BasicRepository;
import jakarta.data.Sort;
import jakarta.data.repository.By;
import jakarta.data.repository.Find;
import jakarta.data.repository.Param;
import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.communication.Condition;
import org.eclipse.jnosql.communication.TypeReference;
import org.eclipse.jnosql.communication.Value;
Expand Down Expand Up @@ -615,18 +619,41 @@ public void shouldFindByNameOrderByName() {
CursoredPage<Person> page = personRepository.findByNameOrderByName("name",
PageRequest.<Person>ofSize(10).afterKey("Ada"));

SoftAssertions.assertSoftly(s -> {
s.assertThat(page).isEqualTo(mock);
});

}

@Test
public void shouldMachParameter() {
when(template.singleResult(any(SelectQuery.class))).thenReturn(Optional
.of(Person.builder().build()));

personRepository.parameter("name", 10);
ArgumentCaptor<SelectQuery> captor = ArgumentCaptor.forClass(SelectQuery.class);
verify(template).select(captor.capture());
SelectQuery query = captor.getValue();
CriteriaCondition condition = query.condition().get();
assertEquals("Person", query.name());
assertEquals(EQUALS, condition.condition());
assertThat(query.sorts()).hasSize(1)
.containsExactly(Sort.asc("name"));
assertEquals(Element.of("name", "name"), condition.element());

SoftAssertions.assertSoftly(soft ->{
soft.assertThat(query.name()).isEqualTo("Person");
soft.assertThat(query.skip()).isEqualTo(0);
soft.assertThat(query.limit()).isEqualTo(0);
soft.assertThat(query.condition().isPresent()).isTrue();
soft.assertThat(query.sorts()).hasSize(0);
CriteriaCondition condition = query.condition().orElseThrow();
soft.assertThat(condition.condition()).isEqualTo(AND);
List<CriteriaCondition> conditions = condition.element().get(new TypeReference<>() {
});
soft.assertThat(conditions).hasSize(2);
soft.assertThat(conditions.get(0)).isEqualTo(CriteriaCondition.eq(Element.of("name", "name")));
soft.assertThat(conditions.get(1)).isEqualTo(CriteriaCondition.eq(Element.of("age", 10)));

});
}



private PageRequest getPageRequest() {
return PageRequest.ofPage(2).size(6);
}
Expand All @@ -635,6 +662,9 @@ interface PersonRepository extends BasicRepository<Person, Long> {

Person findByName(String name, PageRequest<Person> pageRequest);

@Find
List<Person> parameter(@By("name") String name, @By("age") Integer age);

CursoredPage<Person> findByNameOrderByName(String name, PageRequest<Person> pageRequest);

List<Person> findByName(String name, Sort<Person> sort);
Expand Down
Expand Up @@ -17,6 +17,7 @@
import jakarta.data.exceptions.MappingException;
import jakarta.data.repository.By;
import jakarta.data.repository.Delete;
import jakarta.data.repository.Find;
import jakarta.data.repository.Insert;
import jakarta.data.repository.OrderBy;
import jakarta.data.repository.BasicRepository;
Expand Down Expand Up @@ -957,6 +958,7 @@ default Map<Boolean, List<Person>> partcionate(String name) {
@OrderBy("age")
List<Person> findByException();

@Find
List<Person> find(@By("name") String name);
}

Expand Down

0 comments on commit eebe202

Please sign in to comment.