Skip to content

Commit

Permalink
Merge pull request #433 from eclipse/fix-custom-repository
Browse files Browse the repository at this point in the history
Fix custom repository
  • Loading branch information
otaviojava authored Aug 29, 2023
2 parents 5979635 + 057b2f1 commit 8d0c149
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public abstract class AbstractColumnRepositoryProxy<T, K> extends BaseColumnRepo

protected abstract Converters getConverters();

protected abstract Class<?> repositoryType();

@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {
RepositoryType type = RepositoryType.of(method);
RepositoryType type = RepositoryType.of(method, repositoryType());
Class<?> typeClass = getEntityMetadata().type();

switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class ColumnRepositoryProxy<T, K> extends AbstractColumnRepositoryProxy {

private final Converters converters;

private final Class<?> repositoryType;


ColumnRepositoryProxy(JNoSQLColumnTemplate template, EntitiesMetadata entities, Class<?> repositoryType,
Converters converters) {
Expand All @@ -49,6 +51,7 @@ class ColumnRepositoryProxy<T, K> extends AbstractColumnRepositoryProxy {
this.entityMetadata = entities.get(typeClass);
this.repository = new ColumnRepository(template, entityMetadata);
this.converters = converters;
this.repositoryType = repositoryType;
}

@Override
Expand All @@ -71,6 +74,11 @@ protected Converters getConverters() {
return converters;
}

@Override
protected Class<?> repositoryType() {
return repositoryType;
}


static class ColumnRepository extends AbstractColumnRepository implements PageableRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public enum RepositoryType {
* @param method the method
* @return a repository type
*/
public static RepositoryType of(Method method) {
public static RepositoryType of(Method method, Class<?> repositoryType) {
Objects.requireNonNull(method, "method is required");
Class<?> declaringClass = method.getDeclaringClass();
if (method.isDefault()) {
Expand All @@ -112,7 +112,7 @@ public static RepositoryType of(Method method) {
if (Objects.nonNull(method.getAnnotation(Query.class))) {
return QUERY;
}
if (isCustomRepository(declaringClass)) {
if (!repositoryType.equals(declaringClass) && isCustomRepository(declaringClass)) {
return CUSTOM_REPOSITORY;
}
String methodName = method.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,70 +35,70 @@ class RepositoryTypeTest {

@Test
public void shouldReturnDefault() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "save")));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "deleteById")));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "findById")));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "existsById")));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "count")));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(PageableRepository.class, "findAll")));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "save"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "deleteById"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "findById"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "existsById"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(CrudRepository.class, "count"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.DEFAULT, RepositoryType.of(getMethod(PageableRepository.class, "findAll"), CrudRepository.class));
}


@Test
public void shouldReturnObjectMethod() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.OBJECT_METHOD, RepositoryType.of(getMethod(Object.class, "equals")));
Assertions.assertEquals(RepositoryType.OBJECT_METHOD, RepositoryType.of(getMethod(Object.class, "hashCode")));
Assertions.assertEquals(RepositoryType.OBJECT_METHOD, RepositoryType.of(getMethod(Object.class, "equals"), CrudRepository.class));
Assertions.assertEquals(RepositoryType.OBJECT_METHOD, RepositoryType.of(getMethod(Object.class, "hashCode"), CrudRepository.class));
}


@Test
public void shouldReturnFindBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.FIND_BY, RepositoryType.of(getMethod(DevRepository.class, "findByName")));
Assertions.assertEquals(RepositoryType.FIND_BY, RepositoryType.of(getMethod(DevRepository.class, "findByName"), CrudRepository.class));
}

@Test
public void shouldReturnDeleteBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.DELETE_BY, RepositoryType.of(getMethod(DevRepository.class, "deleteByName")));
Assertions.assertEquals(RepositoryType.DELETE_BY, RepositoryType.of(getMethod(DevRepository.class, "deleteByName"), CrudRepository.class));
}

@Test
public void shouldReturnFindAllBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.FIND_ALL, RepositoryType.of(getMethod(DevRepository.class, "findAll")));
Assertions.assertEquals(RepositoryType.FIND_ALL, RepositoryType.of(getMethod(DevRepository.class, "findAll"), CrudRepository.class));
}

@Test
public void shouldReturnJNoSQLQuery() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.QUERY, RepositoryType.of(getMethod(DevRepository.class, "query")));
Assertions.assertEquals(RepositoryType.QUERY, RepositoryType.of(getMethod(DevRepository.class, "query"), CrudRepository.class));
}

@Test
public void shouldReturnUnknown() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.UNKNOWN, RepositoryType.of(getMethod(DevRepository.class, "nope")));
Assertions.assertEquals(RepositoryType.UNKNOWN, RepositoryType.of(getMethod(DevRepository.class, "nope"), CrudRepository.class));
}

@Test
public void shouldReturnCountBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.COUNT_BY, RepositoryType.of(getMethod(DevRepository.class, "countByName")));
Assertions.assertEquals(RepositoryType.COUNT_BY, RepositoryType.of(getMethod(DevRepository.class, "countByName"), CrudRepository.class));
}

@Test
public void shouldReturnExistsBy() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.EXISTS_BY, RepositoryType.of(getMethod(DevRepository.class, "existsByName")));
Assertions.assertEquals(RepositoryType.EXISTS_BY, RepositoryType.of(getMethod(DevRepository.class, "existsByName"), CrudRepository.class));
}

@Test
public void shouldReturnOrder() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.ORDER_BY, RepositoryType.of(getMethod(DevRepository.class,
"order")));
"order"), CrudRepository.class));

Assertions.assertEquals(RepositoryType.ORDER_BY, RepositoryType.of(getMethod(DevRepository.class,
"order2")));
"order2"), CrudRepository.class));
}

@Test
public void shouldDefaultMethod() throws NoSuchMethodException {
Assertions.assertEquals(RepositoryType.DEFAULT_METHOD, RepositoryType.of(getMethod(DevRepository.class,
"duplicate")));
"duplicate"), CrudRepository.class));
}

@Test
Expand All @@ -110,7 +110,7 @@ public void shouldReturnCustom() throws NoSuchMethodException {
cdi.when(CDI::current).thenReturn(current);
Mockito.when(current.select(Calculate.class)).thenReturn(instance);
Assertions.assertEquals(RepositoryType.CUSTOM_REPOSITORY, RepositoryType.of(getMethod(Calculate.class,
"sum")));
"sum"), CrudRepository.class));
}
}

Expand All @@ -123,10 +123,22 @@ public void shouldReturnFindByCustom() throws NoSuchMethodException {
cdi.when(CDI::current).thenReturn(current);
Mockito.when(current.select(Calculate.class)).thenReturn(instance);
Assertions.assertEquals(RepositoryType.CUSTOM_REPOSITORY, RepositoryType.of(getMethod(Calculate.class,
"findBySum")));
"findBySum"), CrudRepository.class));
}
}

@Test
public void shouldReturnFindByCustom2() throws NoSuchMethodException {
try (MockedStatic<CDI> cdi = Mockito.mockStatic(CDI.class)) {
CDI<Object> current = Mockito.mock(CDI.class);
Instance<Calculate> instance = Mockito.mock(Instance.class);
Mockito.when(instance.isResolvable()).thenReturn(true);
cdi.when(CDI::current).thenReturn(current);
Mockito.when(current.select(Calculate.class)).thenReturn(instance);
Assertions.assertEquals(RepositoryType.FIND_BY, RepositoryType.of(getMethod(Calculate.class,
"findBySum"), Calculate.class));
}
}
private Method getMethod(Class<?> repository, String methodName) throws NoSuchMethodException {
return Stream.of(repository.getDeclaredMethods())
.filter(m -> m.getName().equals(methodName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public abstract class AbstractDocumentRepositoryProxy<T> extends BaseDocumentRep

protected abstract PageableRepository getRepository();

protected abstract Class<?> repositoryType();

@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {

RepositoryType type = RepositoryType.of(method);
RepositoryType type = RepositoryType.of(method, repositoryType());
Class<?> typeClass = getEntityMetadata().type();

switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class DocumentRepositoryProxy<T> extends AbstractDocumentRepositoryProxy<T> {

private final Converters converters;

private final Class<?> repositoryType;


DocumentRepositoryProxy(JNoSQLDocumentTemplate template, EntitiesMetadata entities,
Class<?> repositoryType, Converters converters) {
Expand All @@ -48,6 +50,7 @@ class DocumentRepositoryProxy<T> extends AbstractDocumentRepositoryProxy<T> {
this.entityMetadata = entities.get(typeClass);
this.repository = new DocumentRepository(template, entityMetadata);
this.converters = converters;
this.repositoryType = repositoryType;
}


Expand All @@ -56,6 +59,11 @@ protected PageableRepository getRepository() {
return repository;
}

@Override
protected Class<?> repositoryType() {
return repositoryType;
}

@Override
protected JNoSQLDocumentTemplate getTemplate() {
return template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ abstract class AbstractGraphRepositoryProxy<T, K> implements InvocationHandler {

protected abstract Converters getConverters();

protected abstract Class<?> repositoryType();


@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {

RepositoryType type = RepositoryType.of(method);
RepositoryType type = RepositoryType.of(method, repositoryType());
Class<?> typeClass = getEntityMetadata().type();

switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class GraphRepositoryProxy<T, K> extends AbstractGraphRepositoryProxy<T, K> {

private final Converters converters;

private final Class<?> repositoryType;


GraphRepositoryProxy(GraphTemplate template, EntitiesMetadata entities,
Class<?> repositoryType,
Expand All @@ -60,6 +62,7 @@ class GraphRepositoryProxy<T, K> extends AbstractGraphRepositoryProxy<T, K> {
this.repository = new GraphRepository(template, entityMetadata);
this.template = template;
this.converters = converters;
this.repositoryType = repositoryType;

}

Expand Down Expand Up @@ -93,6 +96,11 @@ protected Converters getConverters() {
return converters;
}

@Override
protected Class<?> repositoryType() {
return repositoryType;
}


static class GraphRepository extends AbstractGraphRepository implements PageableRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public abstract class AbstractKeyValueRepositoryProxy<T> implements InvocationHa

protected abstract Class<T> getType();

protected abstract Class<?> repositoryType();

@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {

RepositoryType type = RepositoryType.of(method);
RepositoryType type = RepositoryType.of(method, repositoryType());
switch (type) {
case DEFAULT -> {
return unwrapInvocationTargetException(() -> method.invoke(getRepository(), args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class KeyValueRepositoryProxy<T> extends AbstractKeyValueRepositoryProxy<T> {
private final KeyValueTemplate template;
private final Class<T> type;

private final Class<?> repositoryType;


KeyValueRepositoryProxy(Class<?> repositoryType, EntitiesMetadata entitiesMetadata, KeyValueTemplate template) {
Class<T> typeClass = (Class) ((ParameterizedType) repositoryType.getGenericInterfaces()[0])
Expand All @@ -37,6 +39,7 @@ class KeyValueRepositoryProxy<T> extends AbstractKeyValueRepositoryProxy<T> {
this.repository = new DefaultKeyValueRepository(typeClass, metadata, template);
this.template = template;
this.type = typeClass;
this.repositoryType = repositoryType;
}

@Override
Expand All @@ -54,5 +57,10 @@ protected Class getType() {
return type;
}

@Override
protected Class<?> repositoryType() {
return repositoryType;
}


}

0 comments on commit 8d0c149

Please sign in to comment.