-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #13067: Spring Data Repository with nested camel-case properties …
…does not work with Quarkus
- Loading branch information
Showing
12 changed files
with
468 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
273 changes: 118 additions & 155 deletions
273
...data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/MethodNameParser.java
Large diffs are not rendered by default.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
...-jpa/deployment/src/test/java/io/quarkus/spring/data/deployment/MethodNameParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.quarkus.spring.data.deployment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.assertj.core.api.AbstractStringAssert; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Index; | ||
import org.jboss.jandex.IndexView; | ||
import org.jboss.jandex.Indexer; | ||
import org.jboss.jandex.MethodInfo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MethodNameParserTest { | ||
|
||
private final Class<?> repositoryClass = PersonRepository.class; | ||
private final Class<?> entityClass = Person.class; | ||
private final Class[] additionalClasses = new Class[] { Person.Address.class, Person.Country.class }; | ||
|
||
@Test | ||
public void testFindAllByAddressZipCode() throws Exception { | ||
MethodNameParser.Result result = parseMethod(repositoryClass, "findAllByAddressZipCode", entityClass, | ||
additionalClasses); | ||
assertThat(result).isNotNull(); | ||
assertSameClass(result.getEntityClass(), entityClass); | ||
assertThat(result.getQuery()).isEqualTo("FROM Person WHERE address.zipCode = ?1"); | ||
assertThat(result.getParamCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void testFindAllByAddressCountry() throws Exception { | ||
MethodNameParser.Result result = parseMethod(repositoryClass, "findAllByAddressCountry", entityClass, | ||
additionalClasses); | ||
assertThat(result).isNotNull(); | ||
assertSameClass(result.getEntityClass(), entityClass); | ||
assertThat(result.getQuery()).isEqualTo("FROM Person WHERE addressCountry = ?1"); | ||
assertThat(result.getParamCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void testFindAllByAddress_Country() throws Exception { | ||
MethodNameParser.Result result = parseMethod(repositoryClass, "findAllByAddress_Country", entityClass, | ||
additionalClasses); | ||
assertThat(result).isNotNull(); | ||
assertSameClass(result.getEntityClass(), entityClass); | ||
assertThat(result.getQuery()).isEqualTo("FROM Person WHERE address.country = ?1"); | ||
assertThat(result.getParamCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void testFindAllByAddressCountryIsoCode() throws Exception { | ||
UnableToParseMethodException exception = assertThrows(UnableToParseMethodException.class, | ||
() -> parseMethod(repositoryClass, "findAllByAddressCountryIsoCode", entityClass, additionalClasses)); | ||
assertThat(exception).hasMessageContaining("Person does not contain a field named: addressCountryIsoCode"); | ||
} | ||
|
||
@Test | ||
public void testFindAllByAddress_CountryIsoCode() throws Exception { | ||
MethodNameParser.Result result = parseMethod(repositoryClass, "findAllByAddress_CountryIsoCode", entityClass, | ||
additionalClasses); | ||
assertThat(result).isNotNull(); | ||
assertSameClass(result.getEntityClass(), entityClass); | ||
assertThat(result.getQuery()).isEqualTo("FROM Person WHERE address.country.isoCode = ?1"); | ||
assertThat(result.getParamCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void testFindAllByAddress_Country_IsoCode() throws Exception { | ||
MethodNameParser.Result result = parseMethod(repositoryClass, "findAllByAddress_Country_IsoCode", entityClass, | ||
additionalClasses); | ||
assertThat(result).isNotNull(); | ||
assertSameClass(result.getEntityClass(), entityClass); | ||
assertThat(result.getQuery()).isEqualTo("FROM Person WHERE address.country.isoCode = ?1"); | ||
assertThat(result.getParamCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void testFindAllByAddress_CountryInvalid() throws Exception { | ||
UnableToParseMethodException exception = assertThrows(UnableToParseMethodException.class, | ||
() -> parseMethod(repositoryClass, "findAllByAddress_CountryInvalid", entityClass, additionalClasses)); | ||
assertThat(exception).hasMessageContaining("Person does not contain a field named: address_CountryInvalid"); | ||
assertThat(exception).hasMessageContaining("Country.invalid"); | ||
} | ||
|
||
@Test | ||
public void testFindAllBy_() throws Exception { | ||
UnableToParseMethodException exception = assertThrows(UnableToParseMethodException.class, | ||
() -> parseMethod(repositoryClass, "findAllBy_", entityClass, additionalClasses)); | ||
assertThat(exception).hasMessageContaining("Person does not contain a field named: _"); | ||
} | ||
|
||
private AbstractStringAssert<?> assertSameClass(ClassInfo classInfo, Class<?> aClass) { | ||
return assertThat(classInfo.name().toString()).isEqualTo(aClass.getName()); | ||
} | ||
|
||
private MethodNameParser.Result parseMethod(Class<?> repositoryClass, String methodToParse, | ||
Class<?> entityClass, Class<?>... additionalClasses) throws IOException { | ||
IndexView indexView = index(ArrayUtils.addAll(additionalClasses, repositoryClass, entityClass)); | ||
DotName repository = DotName.createSimple(repositoryClass.getName()); | ||
DotName entity = DotName.createSimple(entityClass.getName()); | ||
ClassInfo entityClassInfo = indexView.getClassByName(entity); | ||
ClassInfo repositoryClassInfo = indexView.getClassByName(repository); | ||
MethodNameParser methodNameParser = new MethodNameParser(entityClassInfo, indexView); | ||
MethodInfo repositoryMethod = repositoryClassInfo.firstMethod(methodToParse); | ||
MethodNameParser.Result result = methodNameParser.parse(repositoryMethod); | ||
return result; | ||
} | ||
|
||
public static Index index(Class<?>... classes) throws IOException { | ||
Indexer indexer = new Indexer(); | ||
for (Class<?> clazz : classes) { | ||
try (InputStream stream = MethodNameParserTest.class.getClassLoader() | ||
.getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) { | ||
indexer.index(stream); | ||
} | ||
} | ||
return indexer.complete(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ns/spring-data-jpa/deployment/src/test/java/io/quarkus/spring/data/deployment/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.spring.data.deployment; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Person { | ||
@Id | ||
private Integer id; | ||
|
||
private Address address; | ||
|
||
private String addressCountry; | ||
|
||
@Entity | ||
public static class Address { | ||
@Id | ||
private Integer id; | ||
|
||
private String zipCode; | ||
|
||
private Country country; | ||
} | ||
|
||
@Entity | ||
public static class Country { | ||
@Id | ||
private Integer id; | ||
|
||
private String isoCode; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...data-jpa/deployment/src/test/java/io/quarkus/spring/data/deployment/PersonRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.spring.data.deployment; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.repository.Repository; | ||
|
||
// issue 13067: | ||
public interface PersonRepository extends Repository<Person, Integer> { | ||
|
||
List<Person> findAllByAddressZipCode(String zipCode); | ||
|
||
List<Person> findAllByAddressCountry(String zipCode); | ||
|
||
List<Person> findAllByAddress_Country(String zipCode); | ||
|
||
List<Person> findAllByAddressCountryIsoCode(String zipCode); | ||
|
||
List<Person> findAllByAddress_CountryIsoCode(String zipCode); | ||
|
||
List<Person> findAllByAddress_Country_IsoCode(String zipCode); | ||
|
||
List<Person> findAllByAddress_CountryInvalid(String zipCode); | ||
|
||
List<Person> findAllBy_(String zipCode); | ||
} |
68 changes: 68 additions & 0 deletions
68
integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.quarkus.it.spring.data.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "employee") | ||
public class Employee extends AbstractEntity { | ||
|
||
@Column(name = "user_id") | ||
private String userId; | ||
|
||
@Column(name = "first_name") | ||
private String firstName; | ||
|
||
@Column(name = "last_name") | ||
private String lastName; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "team_id", nullable = false) | ||
private Team belongsToTeam; | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public Team getBelongsToTeam() { | ||
return belongsToTeam; | ||
} | ||
|
||
@Entity | ||
@Table(name = "team") | ||
public static class Team extends AbstractEntity { | ||
|
||
private String name; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "unit_id", nullable = false) | ||
private OrgUnit organizationalUnit; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public OrgUnit getOrganizationalUnit() { | ||
return organizationalUnit; | ||
} | ||
} | ||
|
||
@Entity | ||
@Table(name = "unit") | ||
public static class OrgUnit extends AbstractEntity { | ||
|
||
private String name; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/EmployeeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.quarkus.it.spring.data.jpa; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface EmployeeRepository extends JpaRepository<Employee, Long> { | ||
|
||
List<Employee> findByBelongsToTeamOrganizationalUnitName(String orgUnitName); | ||
} |
36 changes: 36 additions & 0 deletions
36
...n-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/EmployeeResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.it.spring.data.jpa; | ||
|
||
import java.util.List; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
|
||
@Path("/employee") | ||
@Produces("application/json") | ||
public class EmployeeResource { | ||
|
||
private final EmployeeRepository employeeRepository; | ||
|
||
public EmployeeResource(EmployeeRepository employeeRepository) { | ||
this.employeeRepository = employeeRepository; | ||
} | ||
|
||
@GET | ||
public List<Employee> findAll() { | ||
return this.employeeRepository.findAll(); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public Employee findById(@PathParam("id") Long id) { | ||
return this.employeeRepository.findById(id).orElse(null); | ||
} | ||
|
||
@GET | ||
@Path("/unit/{orgUnitName}") | ||
public List<Employee> findByManagerOfManager(@PathParam("orgUnitName") String orgUnitName) { | ||
return this.employeeRepository.findByBelongsToTeamOrganizationalUnitName(orgUnitName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...tests/spring-data-jpa/src/test/java/io/quarkus/it/spring/data/jpa/EmployeeResourceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.it.spring.data.jpa; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
public class EmployeeResourceIT extends EmployeeResourceTest { | ||
} |
24 changes: 24 additions & 0 deletions
24
...sts/spring-data-jpa/src/test/java/io/quarkus/it/spring/data/jpa/EmployeeResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.it.spring.data.jpa; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class EmployeeResourceTest { | ||
|
||
@Test | ||
public void testFindEmployeesByOrganizationalUnit() { | ||
List<Employee> employees = when().get("/employee/unit/Delivery Unit").then() | ||
.statusCode(200) | ||
.extract().body().jsonPath().getList(".", Employee.class); | ||
|
||
assertThat(employees).extracting("userId").containsExactlyInAnyOrder("johdoe", "petdig"); | ||
} | ||
|
||
} |