Skip to content

Commit

Permalink
feat: fix document repository
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Dec 22, 2023
1 parent 4d34b5f commit fb7a471
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import jakarta.data.page.Page;
import jakarta.data.page.Pageable;
import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.PageableRepository;
import org.eclipse.jnosql.communication.document.DocumentQuery;
import org.eclipse.jnosql.mapping.core.NoSQLPage;
Expand All @@ -40,7 +41,7 @@
/**
* The {@link PageableRepository} template method
*/
public abstract class AbstractDocumentRepository<T, K> implements PageableRepository<T, K> {
public abstract class AbstractDocumentRepository<T, K> implements PageableRepository<T, K>, CrudRepository<T, K> {

protected abstract JNoSQLDocumentTemplate getTemplate();

Expand Down Expand Up @@ -86,7 +87,7 @@ public Optional<T> findById(K id) {
@Override
public Stream<T> findByIdIn(Iterable<K> ids) {
requireNonNull(ids, "ids is required");
return stream(ids.spliterator(), false)
return stream(ids.spliterator(), false)
.flatMap(optionalToStream());
}

Expand All @@ -112,7 +113,7 @@ public Page findAll(Pageable pageable) {
EntityMetadata metadata = getEntityMetadata();
DocumentQuery query = new MappingDocumentQuery(pageable.sorts(),
pageable.size(), NoSQLPage.skip(pageable)
, null ,metadata.name());
, null, metadata.name());

List<Object> entities = getTemplate().select(query).toList();
return NoSQLPage.of(entities, pageable);
Expand Down Expand Up @@ -143,6 +144,31 @@ public void deleteAll() {
getTemplate().deleteAll(getType());
}

@Override
public <S extends T> S insert(S entity) {
Objects.requireNonNull(entity, "entity is required");
return getTemplate().insert(entity);
}

@Override
public <S extends T> Iterable<S> insertAll(Iterable<S> entities) {
Objects.requireNonNull(entities, "entities is required");
return getTemplate().insert(entities);
}

@Override
public boolean update(T entity) {
Objects.requireNonNull(entity, "entity is required");
return getTemplate().update(entity) != null;
}

@Override
public int updateAll(Iterable<T> entities) {
Objects.requireNonNull(entities, "entities is required");
getTemplate().update(entities);
return (int) StreamSupport.stream(entities.spliterator(), false).count();
}

private Class<T> getType() {
return (Class<T>) getEntityMetadata().type();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
import static org.eclipse.jnosql.communication.document.DocumentQuery.select;

/**
* The template method to {@link PageableRepository} to Document
* Template method to Repository proxy on column
*
* @param <T> the class type
* @param <T> the entity type
* @param <K> the key type
*/
public abstract class AbstractDocumentRepositoryProxy<T> extends BaseDocumentRepository implements InvocationHandler {
public abstract class AbstractDocumentRepositoryProxy<T, K> extends BaseDocumentRepository<T> implements InvocationHandler {

protected abstract PageableRepository getRepository();
protected abstract PageableRepository<T, K> getRepository();

protected abstract Class<?> repositoryType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public <T, K, R extends PageableRepository<T, K>> R get(Class<R> repositoryClass
Objects.requireNonNull(repositoryClass, "repository class is required");
Objects.requireNonNull(template, "template class is required");

DocumentRepositoryProxy<R> handler = new DocumentRepositoryProxy<>(template,
DocumentRepositoryProxy<T, K> handler = new DocumentRepositoryProxy<>(template,
entities, repositoryClass, converters);
return (R) Proxy.newProxyInstance(repositoryClass.getClassLoader(),
new Class[]{repositoryClass},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.jnosql.mapping.document.query;


import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.PageableRepository;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.document.JNoSQLDocumentTemplate;
Expand All @@ -29,11 +30,11 @@
*
* @param <T> the type
*/
class DocumentRepositoryProxy<T> extends AbstractDocumentRepositoryProxy<T> {
class DocumentRepositoryProxy<T, K> extends AbstractDocumentRepositoryProxy<T, K> {

private final JNoSQLDocumentTemplate template;

private final DocumentRepository repository;
private final DocumentRepository<T, K> repository;

private final EntityMetadata entityMetadata;

Expand All @@ -45,7 +46,7 @@ class DocumentRepositoryProxy<T> extends AbstractDocumentRepositoryProxy<T> {
DocumentRepositoryProxy(JNoSQLDocumentTemplate template, EntitiesMetadata entities,
Class<?> repositoryType, Converters converters) {
this.template = template;
Class<T> typeClass = (Class) ((ParameterizedType) repositoryType.getGenericInterfaces()[0])
Class<T> typeClass = (Class<T>) ((ParameterizedType) repositoryType.getGenericInterfaces()[0])
.getActualTypeArguments()[0];
this.entityMetadata = entities.get(typeClass);
this.repository = new DocumentRepository(template, entityMetadata);
Expand All @@ -55,7 +56,7 @@ class DocumentRepositoryProxy<T> extends AbstractDocumentRepositoryProxy<T> {


@Override
protected PageableRepository getRepository() {
protected PageableRepository<T, K> getRepository() {
return repository;
}

Expand All @@ -80,7 +81,8 @@ protected Converters getConverters() {
}


static class DocumentRepository extends AbstractDocumentRepository implements PageableRepository {
static class DocumentRepository<T, K> extends AbstractDocumentRepository<T, K> implements PageableRepository<T, K>,
CrudRepository<T, K> {

private final JNoSQLDocumentTemplate template;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,63 @@ void shouldSaveIterable() {
assertEquals(person, personCapture);
}

@Test
void shouldInsert() {

ArgumentCaptor<Person> captor = ArgumentCaptor.forClass(Person.class);
Person person = Person.builder().withName("Ada")
.withId(10L)
.withPhones(singletonList("123123"))
.build();
assertNotNull(personRepository.insert(person));
verify(template).insert(captor.capture());
Person value = captor.getValue();
assertEquals(person, value);
}

@Test
void shouldUpdate() {

ArgumentCaptor<Person> captor = ArgumentCaptor.forClass(Person.class);
Person person = Person.builder().withName("Ada")
.withId(10L)
.withPhones(singletonList("123123"))
.build();
personRepository.update(person);
verify(template).update(captor.capture());
Person value = captor.getValue();
assertEquals(person, value);
}

@Test
void shouldInsertIterable() {

ArgumentCaptor<List<Person>> captor = ArgumentCaptor.forClass(List.class);
Person person = Person.builder().withName("Ada")
.withId(10L)
.withPhones(singletonList("123123"))
.build();
assertNotNull(personRepository.insertAll(List.of(person)));
verify(template).insert(captor.capture());
List<Person> value = captor.getValue();
assertThat(value).contains(person);
}

@Test
void shouldUpdateIterable() {

ArgumentCaptor<List<Person>> captor = ArgumentCaptor.forClass(List.class);
Person person = Person.builder().withName("Ada")
.withId(10L)
.withPhones(singletonList("123123"))
.build();
personRepository.updateAll(List.of(person));
verify(template).update(captor.capture());
List<Person> value = captor.getValue();
assertThat(value).contains(person);
}


@Test
void shouldFindByNameInstance() {

Expand Down

0 comments on commit fb7a471

Please sign in to comment.