Skip to content

Commit

Permalink
Refactor to extract implementor logic to an unique class
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Apr 8, 2024
1 parent 67ae977 commit d94a248
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package io.quarkus.spring.data.rest.deployment;

import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.CRUD_REPOSITORY_INTERFACE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.JPA_REPOSITORY_INTERFACE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_CRUD_REPOSITORY_INTERFACE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_PAGING_AND_SORTING_REPOSITORY_INTERFACE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.PAGING_AND_SORTING_REPOSITORY_INTERFACE;

import java.util.List;

import jakarta.persistence.Id;

import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
Expand Down Expand Up @@ -67,4 +75,40 @@ public MethodDescriptor getMethod(ClassInfo entityClass, String name, Type... pa
}
return null;
}

public boolean isRepositoryInstanceOf(DotName target, String repositoryName) {
ClassInfo classByName = index.getClassByName(repositoryName);
List<Type> types = classByName.interfaceTypes();
return types.stream().anyMatch(type -> type.name().equals(target));
}

public boolean isCrudRepository(String repositoryName) {
return isRepositoryInstanceOf(CRUD_REPOSITORY_INTERFACE, repositoryName)
|| isRepositoryInstanceOf(LIST_CRUD_REPOSITORY_INTERFACE, repositoryName)
|| isRepositoryInstanceOf(JPA_REPOSITORY_INTERFACE, repositoryName);
}

public boolean isListCrudRepository(String repositoryName) {
return isRepositoryInstanceOf(LIST_CRUD_REPOSITORY_INTERFACE, repositoryName)
|| isRepositoryInstanceOf(JPA_REPOSITORY_INTERFACE, repositoryName);
}

public boolean isJpaRepository(String repositoryName) {
return isRepositoryInstanceOf(JPA_REPOSITORY_INTERFACE, repositoryName);
}

public boolean isPagingAndSortingRepository(String repositoryName) {
return isRepositoryInstanceOf(PAGING_AND_SORTING_REPOSITORY_INTERFACE, repositoryName)
|| isRepositoryInstanceOf(LIST_PAGING_AND_SORTING_REPOSITORY_INTERFACE, repositoryName)
|| isRepositoryInstanceOf(JPA_REPOSITORY_INTERFACE, repositoryName);
}

public boolean isListPagingAndSortingRepository(String repositoryName) {
return isRepositoryInstanceOf(LIST_PAGING_AND_SORTING_REPOSITORY_INTERFACE, repositoryName)
|| isRepositoryInstanceOf(JPA_REPOSITORY_INTERFACE, repositoryName);
}

public boolean containsPagedRepository(List<ClassInfo> repositories) {
return repositories.stream().anyMatch(r -> isPagingAndSortingRepository(r.name().toString()));
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
package io.quarkus.spring.data.rest.deployment;

import static io.quarkus.spring.data.rest.deployment.crud.CrudMethodsImplementor.ADD;
import static io.quarkus.spring.data.rest.deployment.crud.CrudMethodsImplementor.DELETE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.ADD;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.DELETE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_ITERABLE;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_PAGED;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_SORTED;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.SAVE_LIST;
import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.UPDATE;
import static io.quarkus.spring.data.rest.deployment.crud.CrudMethodsImplementor.GET;
import static io.quarkus.spring.data.rest.deployment.crud.CrudMethodsImplementor.UPDATE;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;
import org.springframework.data.domain.Pageable;

public class RepositoryPropertiesProvider extends ResourcePropertiesProvider {

public RepositoryPropertiesProvider(IndexView index) {
super(index, false);
private static final DotName PAGEABLE = DotName.createSimple(Pageable.class.getName());

public RepositoryPropertiesProvider(IndexView index, boolean paged) {
super(index, paged);
}

protected Map<String, Predicate<MethodInfo>> getMethodPredicates() {
Map<String, Predicate<MethodInfo>> methodPredicates = new HashMap<>();
methodPredicates.put("list", methodInfo -> methodInfo.name().equals(LIST.getName()));
methodPredicates.put("listIterable", methodInfo -> methodInfo.name().equals(LIST_ITERABLE.getName()));
methodPredicates.put("listPaged", methodInfo -> methodInfo.name().equals(LIST_PAGED.getName())
&& methodInfo.parametersCount() == 1
&& methodInfo.parameterType(0).name().equals(PAGEABLE));
methodPredicates.put("listSorted",
methodInfo -> methodInfo.name().equals(LIST_SORTED.getName()) && methodInfo.parameterTypes().isEmpty());
methodPredicates.put("addAll", methodInfo -> methodInfo.name().equals(SAVE_LIST.getName()));
methodPredicates.put("get", methodInfo -> methodInfo.name().equals(GET.getName()));
methodPredicates.put("add", methodInfo -> methodInfo.name().equals(ADD.getName()));
methodPredicates.put("update", methodInfo -> methodInfo.name().equals(UPDATE.getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.spring.data.rest.deployment;

import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_PAGED;

import jakarta.enterprise.context.ApplicationScoped;

import org.jboss.logging.Logger;
Expand Down Expand Up @@ -35,8 +37,8 @@ public String implement(ClassOutput classOutput, String resourceType, String ent
classCreator.addAnnotation(ApplicationScoped.class);
methodsImplementor.implementListIterable(classCreator, resourceType);
methodsImplementor.implementList(classCreator, resourceType);
methodsImplementor.implementListPaged(classCreator, resourceType);
methodsImplementor.implementListSort(classCreator, resourceType);
methodsImplementor.implementListPaged(classCreator, resourceType);
methodsImplementor.implementAddList(classCreator, resourceType);
methodsImplementor.implementListById(classCreator, resourceType);
methodsImplementor.implementListPageCount(classCreator, resourceType);
Expand All @@ -45,7 +47,13 @@ public String implement(ClassOutput classOutput, String resourceType, String ent
methodsImplementor.implementUpdate(classCreator, resourceType, entityType);
methodsImplementor.implementDelete(classCreator, resourceType);

boolean contains = classCreator.getExistingMethods().contains(LIST_PAGED);
// String string = classCreator.getExistingMethods().stream()
// .filter(methodDescriptor -> methodDescriptor.getName().equals(LIST_PAGED.getName())).findAny().get()
// .getDescriptor().toString();
// LOGGER.infof("Method descriptor '%s'", string);
classCreator.close();

LOGGER.tracef("Completed generation of '%s'", className);
return className;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import io.quarkus.rest.data.panache.deployment.properties.ResourcePropertiesBuildItem;
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.reactive.spi.ExceptionMapperBuildItem;
import io.quarkus.spring.data.rest.deployment.crud.CrudMethodsImplementor;
import io.quarkus.spring.data.rest.deployment.crud.CrudPropertiesProvider;
import io.quarkus.spring.data.rest.runtime.RestDataPanacheExceptionMapper;
import io.quarkus.spring.data.rest.runtime.jta.TransactionalUpdateExecutor;

Expand Down Expand Up @@ -90,12 +88,20 @@ void registerRepositories(CombinedIndexBuildItem indexBuildItem, Capabilities ca
BuildProducer<ResourcePropertiesBuildItem> resourcePropertiesProducer,
BuildProducer<UnremovableBeanBuildItem> unremovableBeansProducer) {
IndexView index = indexBuildItem.getIndex();

EntityClassHelper entityClassHelper = new EntityClassHelper(index);
List<ClassInfo> repositoriesToImplement = getRepositoriesToImplement(index, CRUD_REPOSITORY_INTERFACE,
LIST_CRUD_REPOSITORY_INTERFACE,
PAGING_AND_SORTING_REPOSITORY_INTERFACE, LIST_PAGING_AND_SORTING_REPOSITORY_INTERFACE,
JPA_REPOSITORY_INTERFACE);

boolean paged = false;
if (entityClassHelper.containsPagedRepository(repositoriesToImplement)) {
paged = true;
}
implementResources(capabilities, implementationsProducer, restDataResourceProducer, resourcePropertiesProducer,
unremovableBeansProducer, new CrudMethodsImplementor(index), new CrudPropertiesProvider(index),
getRepositoriesToImplement(index, CRUD_REPOSITORY_INTERFACE, LIST_CRUD_REPOSITORY_INTERFACE,
PAGING_AND_SORTING_REPOSITORY_INTERFACE, LIST_PAGING_AND_SORTING_REPOSITORY_INTERFACE,
JPA_REPOSITORY_INTERFACE));
unremovableBeansProducer, new RepositoryMethodsImplementor(index, entityClassHelper),
new RepositoryPropertiesProvider(index, paged),
repositoriesToImplement);
}

// @BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void implementListById(ClassCreator classCreator, String repositoryInterf

}

@Override
// @Override
public void implementListSort(ClassCreator classCreator, String repositoryInterface) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void implementListById(ClassCreator classCreator, String repositoryInterf

}

@Override
// @Override
public void implementListSort(ClassCreator classCreator, String repositoryInterface) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ void shouldListLastPageHal() {
}

@Test
@Disabled
void shouldNotGetNonExistentPage() {
given().accept("application/json")
.and().queryParam("page", 100)
Expand All @@ -260,7 +259,6 @@ void shouldNotGetNonExistentPage() {
}

@Test
@Disabled
void shouldNotGetNegativePageOrSize() {
given().accept("application/json")
.and().queryParam("page", -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -23,7 +22,6 @@ public class ModifiedPagedResourceTest {
.addAsResource("import.sql"));

@Test
@Disabled
void shouldGet() {
given().accept("application/json")
.when().get("/secret/records/1")
Expand All @@ -33,7 +31,6 @@ void shouldGet() {
}

@Test
@Disabled
void shouldGetHal() {
given().accept("application/hal+json")
.when().get("/secret/records/1")
Expand All @@ -48,22 +45,19 @@ void shouldGetHal() {
}

@Test
// @Disabled
void shouldList() {
given().accept("application/json")
.when().get("/secret/records")
.then().statusCode(200).log().all()
.then().statusCode(200)
.and().body("id", hasItems(1, 2))
.and().body("name", hasItems("first", "second"));
}

@Test
// @Disabled
void shouldListHal() {
given().accept("application/hal+json")
.when().get("/secret/records")
.then().statusCode(200)
.log().all()
.and().body("_embedded.secret-records.id", hasItems(1, 2))
.and().body("_embedded.secret-records.name", hasItems("first", "second"))
.and()
Expand All @@ -83,7 +77,6 @@ void shouldListHal() {
}

@Test
@Disabled
void shouldNotCreate() {
given().accept("application/json")
.and().contentType("application/json")
Expand All @@ -93,7 +86,6 @@ void shouldNotCreate() {
}

@Test
@Disabled
void shouldNotUpdate() {
given().accept("application/json")
.and().contentType("application/json")
Expand All @@ -103,7 +95,6 @@ void shouldNotUpdate() {
}

@Test
@Disabled
void shouldNotDelete() {
given().accept("application/json")
.and().contentType("application/json")
Expand Down

0 comments on commit d94a248

Please sign in to comment.